хехе

 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
# -*- coding: utf-8
from django import newforms as forms
from code.models import Code
import pygments.lexers
BaseCodeAddForm = forms.form_for_model(Code,
fields=['code','comment'])
class CodeAddForm(BaseCodeAddForm):
def __init__(self,*args,**kwargs):
super(CodeAddForm,self).__init__(*args,**kwargs)
self.lexer_choices = self.build_lexers()
self.fields['lexer'] = forms.ChoiceField(
choices=self.lexer_choices,
label=u'Язык')
def build_lexers(self):
lexer_choices = [(x[1][0],x[0]) for x in\
pygments.lexers.get_all_lexers()]
lexer_choices = sorted(
lexer_choices,lambda x,y: cmp(x[0].lower(),y[0].lower()))
lexer_choices.insert(0,('guess','--- автоопределение ---'))
return lexer_choices
def save(self):
code = Code()
return forms.save_instance(self,code)