coding utf-8 2import pygments lexers 4from django import newforms as f

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8
2import pygments.lexers
3
4from django import newforms as forms
5from django.conf import settings
6from django.utils.translation import ugettext_lazy as _
7
8from dja.site.forms import CustomField
9from code.models import Code
10from tagging.forms import TagField
11
12class CodeField(CustomField(forms.CharField, Code, 'code')):
13 def __init__(self, *args, **kwargs):
14 super(CodeField, self).__init__(*args, **kwargs)
15 self.help_text += _('Maximum allowed size: %d') % settings.CODE_SIZE_LIMIT
16
17 def clean(self, value):
18 super(CodeField, self).clean(value)
19 if len(value) > settings.CODE_SIZE_LIMIT:
20 raise forms.ValidationError(_('Text size is bigger than %d' % settings.CODE_SIZE_LIMIT))
21 return value
22
23class LexerField(CustomField(forms.ChoiceField, Code, 'lexer')):
24 def __init__(self, *args, **kwargs):
25 super(LexerField, self).__init__(*args, **kwargs)
26 self.widget = forms.Select()
27 self.choices = self.build_lexers()
28 self.label = _('Formatting')
29 self.help_text = _('Choose proper code format')
30
31 def build_lexers(self):
32 lexer_choices = [(x[1][0], x[0]) for x in\
33 pygments.lexers.get_all_lexers()]
34 lexer_choices = sorted(
35 lexer_choices, lambda x, y: cmp(x[0].lower(), y[0].lower()))
36 #lexer_choices.insert(0, ('guess', _('--- autodetect ---')))
37 return lexer_choices
38
39class CodeAddForm(forms.Form):
40 code = CodeField()
41 lexer = LexerField()
42 comment = Code._meta.get_field('comment').formfield()
43 tags = TagField(required=False)
44 password = Code._meta.get_field('password').formfield()
45
46 def __init__(self, *args, **kwargs):
47 self.user = kwargs.pop('user')
48 kwargs.setdefault('initial', {})
49 if not 'lexer' in kwargs['initial']:
50 kwargs['initial']['lexer'] = 'text'
51 super(CodeAddForm, self).__init__(*args, **kwargs)
52
53 def save(self):
54 code = Code(user=self.user)
55 return forms.save_instance(self, code)
56
57class CodeEditForm(forms.Form):
58 code = CodeField()
59 lexer = LexerField()
60 comment = Code._meta.get_field('comment').formfield()
61 tags = TagField(required=False)
62 password = Code._meta.get_field('password').formfield()
63
64 def __init__(self, *args, **kwargs):
65 self.code = kwargs.pop('code')
66 super(CodeEditForm, self).__init__(*args, **kwargs)
67
68 def save(self):
69 return forms.save_instance(self, self.code)
70
71class PasswordForm(forms.Form):
72 password = Code._meta.get_field('password').formfield()
73
74 def __init__(self, *args, **kwargs):
75 self.request = kwargs.pop('request')
76 self.code = kwargs.pop('code')
77 self.base_fields['password'].help_text = ''
78 super(PasswordForm, self).__init__(*args, **kwargs)
79
80 def clean_password(self):
81 if 'password' in self.cleaned_data:
82 password = self.cleaned_data['password']
83 if not password == self.code.password:
84 raise forms.ValidationError(_('Incorrect password'))
85 return password
86
87 def save(self):
88 self.request.session['password_%d' % self.code.id] = self.cleaned_data['password']