view apps/snippet/forms.py @ 151:c7be7def8b57

Bundles! (basic functionality) Changes made in this commit: * Added new dependencies (see pip-requirements) * Added new dependency and setup information to README * Deleted the included mptt app (in apps/mptt) in favour of just adding the dependency to pip-requirements (makes it easier to update, etc) * Changed the import convention to use `from apps.bundle.models import Bundle` rather than `from agora.apps.bundle.models import Bundle` because Celery was having problems with the latter style. Everything should still work. * Moved the syntax-highlighting and related code for snippets into separate HTML files so that they can be used by the bundle app And, of course, the ability to upload bundles. But wait! There's more! Changes still to come, for only $19.95 a month: * Bundle versioning * Automatic license integration (i.e. adding headers to files) * The ability to download bundles (zip, tar, etc) * Rating bundles * And much, much more! Batteries not included.
author dellsystem <ilostwaldo@gmail.com>
date Mon, 15 Oct 2012 00:52:00 -0400
parents f299232c82e8
children 499e5f47f278
line wrap: on
line source

import datetime

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from apps.snippet.models import Snippet
from apps.snippet.highlight import LEXER_LIST_ALL, LEXER_LIST, LEXER_DEFAULT

#===============================================================================
# Snippet Form and Handling
#===============================================================================

EXPIRE_CHOICES = (
    (3600, _(u'In one hour')),
    (3600*24*7, _(u'In one week')),
    (3600*24*30, _(u'In one month')),
    (3600*24*30*12*100, _(u'Save forever')), # 100 years, I call it forever ;)
)

EXPIRE_DEFAULT = 3600*24*30

class SnippetForm(forms.ModelForm):
    file = forms.FileField(help_text=_("If the snippet you want to post is \
        saved as a file on your computer, you can upload it directly rather \
        than having to copy and paste it into the box above. If a file \
        is specified, the text in the content field above will be \
        ignored."),
        required=False)

    expire_options = forms.ChoiceField(
        choices=EXPIRE_CHOICES,
        initial=EXPIRE_DEFAULT,
        label=_(u'Expires'),
    )

    def __init__(self, *args, **kwargs):
        request = kwargs.pop('request')
        super(SnippetForm, self).__init__(*args, **kwargs)
        self.request = request

        try:
            if self.request.session['userprefs'].get('display_all_lexer',
                                                     False):
                self.fields['lexer'].choices = LEXER_LIST_ALL
        except KeyError:
            pass

        try:
            self.fields['author'].initial = \
                    self.request.session['userprefs'].get('default_name', '')
        except KeyError:
            pass

        # Make the content field not required (validated in clean())
        self.fields['content'].required = False
        self.fields['title'].required = True

    def clean(self):
        cleaned_data = super(SnippetForm, self).clean()
        file_data = cleaned_data.get('file')
        content = cleaned_data.get('content')

        if file_data:
            file_data.open()
            content_type = file_data.content_type

            # Do some very basic checking of types. NOT SECURE.
            if (content_type.startswith('text/') or
                content_type.startswith('application')):
                cleaned_data['content'] = file_data.read()
            else:
                raise forms.ValidationError(_("Please ensure that you upload \
                    a text file."))
        elif not content:
            # No snippet data specified
            raise forms.ValidationError(_("Please specify some content for \
                the snippet, either in the content field or by uploading \
                a file."))

        return cleaned_data

    def save(self, parent=None, *args, **kwargs):

        # Set parent snippet
        if parent:
            self.instance.parent = parent

        # Add expire datestamp
        self.instance.expires = datetime.datetime.now() + \
            datetime.timedelta(seconds=int(self.cleaned_data['expire_options']))

        # Save snippet in the db
        super(SnippetForm, self).save(*args, **kwargs)

        # Add the snippet to the user session list
        if self.request.session.get('snippet_list', False):
            if len(self.request.session['snippet_list']) >= \
                   getattr(settings, 'MAX_SNIPPETS_PER_USER', 10):
                self.request.session['snippet_list'].pop(0)
            self.request.session['snippet_list'] += [self.instance.pk]
        else:
            self.request.session['snippet_list'] = [self.instance.pk]

        return self.request, self.instance

    class Meta:
        model = Snippet
        fields = (
            'title',
            'content',
            'lexer',
        )


#===============================================================================
# User Settings
#===============================================================================

USERPREFS_FONT_CHOICES = [(None, _(u'Default'))] + [
    (i, i) for i in sorted((
        'Monaco',
        'Bitstream Vera Sans Mono',
        'Courier New',
        'Consolas',
    ))
]

USERPREFS_SIZES = [(None, _(u'Default'))] + [(i, '%dpx' % i) for i in range(5, 25)]

class UserSettingsForm(forms.Form):

    default_name = forms.CharField(label=_(u'Default Name'), required=False)
    display_all_lexer = forms.BooleanField(
        label=_(u'Display all lexer'),
        required=False,
        widget=forms.CheckboxInput,
        help_text=_(u'This also enables the super secret ' \
                     '\'guess lexer\' function.'),
    )
    font_family = forms.ChoiceField(label=_(u'Font Family'),
                                    required=False,
                                    choices=USERPREFS_FONT_CHOICES)
    font_size = forms.ChoiceField(label=_(u'Font Size'),
                                  required=False,
                                  choices=USERPREFS_SIZES)
    line_height = forms.ChoiceField(label=_(u'Line Height'),
                                    required=False,
                                    choices=USERPREFS_SIZES)