PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/kai/model/forms.py

https://bitbucket.org/bbangert/kai/
Python | 170 lines | 159 code | 11 blank | 0 comment | 0 complexity | 2f1ec7ef4eca003457ee61c27bbdba0c MD5 | raw file
  1from formencode import All, Schema
  2from formencode.validators import FieldsMatch, UnicodeString, OneOf, Email
  3from tw2 import forms
  4import pytz
  5
  6from kai.lib.highlight import langdict
  7from kai.model.validators import ExistingSnippetTitle, ExistingEmail, UniqueDisplayname, UniqueEmail, ValidLogin, ValidPassword
  8
  9
 10class FilteringSchema(Schema):
 11    filter_extra_fields = False
 12    allow_extra_fields = True
 13    ignore_key_missing = False
 14
 15
 16class AutoComplete(forms.TextField):
 17    template = 'mako:kai.model.widgets.autocomplete'
 18
 19
 20class SecureToken(forms.HiddenField):
 21    template = 'mako:kai.model.widgets.secure'
 22
 23
 24class BotsAreLame(forms.HiddenField):
 25    template = 'mako:kai.model.widgets.notabot'
 26
 27
 28class CommentForm(forms.TableForm):
 29    comment = forms.TextArea(
 30        id = 'comment_form_comment',
 31        label='Comment',
 32        validator = UnicodeString(not_empty=True))
 33    preview = forms.Button(
 34        id='preview',
 35        label='',
 36        value='Preview')
 37comment_form = CommentForm()
 38
 39
 40class SnippetForm(forms.TableForm):
 41    title = forms.TextField(
 42        validator = ExistingSnippetTitle(not_empty=True))
 43    description = forms.TextArea(
 44        help_text = "ONE paragraph summarizing the snippet. NO FORMATTING IS APPLIED",
 45        validator = UnicodeString())
 46    content = forms.TextArea(
 47        id = 'snippet_form_content',
 48        name = 'content',
 49        help_text = "The full content of the snippet. Restructured Text formatting is used.",
 50        validator = UnicodeString(not_empty=True))
 51    tags = AutoComplete(
 52        validator = UnicodeString())
 53    preview = forms.Button(
 54        id='preview',
 55        label='',
 56        value='Preview')
 57snippet_form = SnippetForm()
 58
 59
 60class PastebinForm(forms.TableForm):
 61    title = forms.TextField(
 62        validator = UnicodeString(not_empty=True))
 63    language = forms.SingleSelectField(
 64        options = sorted(langdict.items(), cmp=lambda x,y: cmp(x[1], y[1])),
 65        validator = OneOf(langdict.keys(), not_empty=True))
 66    code = forms.TextArea(
 67        validator = UnicodeString(not_empty=True))
 68    tags = AutoComplete(
 69        validator = UnicodeString(not_empty=False))
 70    notabot = BotsAreLame(
 71        validator = UnicodeString(not_empty=True),
 72        value = 'most_likely')
 73pastebin_form = PastebinForm()
 74
 75
 76class NewArticleForm(forms.TableForm):
 77    css_class = 'new_article'
 78    title = forms.TextField(
 79        css_class = 'textfield',
 80        validator = UnicodeString(not_empty=True))
 81    summary = forms.TextField(
 82        css_class = 'textfield',
 83        validator = UnicodeString())
 84    body = forms.TextArea(
 85        rows = 15,
 86        validator = UnicodeString(not_empty=True))
 87    publish_date = forms.CalendarDateTimePicker()
 88    preview = forms.Button(id='Preview', label='', value='Preview')
 89new_article_form = NewArticleForm()
 90
 91
 92class ChangePasswordForm(forms.TableForm):
 93    password = forms.PasswordField(
 94        validator = ValidPassword(not_empty=True))
 95    confirm_password = forms.PasswordField(
 96        validator = ValidPassword(not_empty=True))
 97    authentication_token = SecureToken()
 98    
 99    class child(forms.TableLayout):    
100        validator = FilteringSchema(
101            chained_validators=[FieldsMatch('password', 'confirm_password')],
102            msgs=dict(childerror=''))
103change_password_form = ChangePasswordForm()
104
105
106class ForgotPasswordForm(forms.TableForm):
107    email_address = forms.TextField(
108        label_text = 'Email',
109        validator = ExistingEmail(not_empty=True))
110    authentication_token = SecureToken()
111forgot_password_form = ForgotPasswordForm()
112
113
114class LoginForm(forms.TableForm):
115    email_address = forms.TextField(
116        validator = Email(not_empty=True))
117    password = forms.PasswordField(
118        validator = UnicodeString(not_empty=True))
119    authentication_token = SecureToken()
120    class child(forms.TableLayout):
121        validator = FilteringSchema(
122            chained_validators=[ValidLogin(email='email_address', password='password')],
123            msgs=dict(childerror=''))
124    submit = forms.SubmitButton(css_class='submit', value='Login')
125login_form = LoginForm()
126
127class OpenIDLogin(forms.TableForm):
128    openid_identifier = forms.TextField(
129        label_text="OpenID Identifier",
130        validator = UnicodeString(not_empty=True))
131    authentication_token = SecureToken()
132    submit = forms.SubmitButton(css_class='submit', value='Login')
133openid_login_form = OpenIDLogin()
134
135
136class OpenIDRegistrationForm(forms.TableForm):
137    displayname = forms.TextField(
138        label_text = "Display Name",
139        help_text = "Name that will appear when posting/commenting",
140        validator = All(UnicodeString(not_empty=True), UniqueDisplayname()))
141    email_address = forms.TextField(
142        validator = All(Email(not_empty=True), UniqueEmail()))
143    timezone = forms.SingleSelectField(
144        options = pytz.common_timezones,
145        validator = OneOf(pytz.common_timezones, not_empty=True))
146openid_registration_form = OpenIDRegistrationForm()
147
148
149class RegistrationForm(forms.TableForm):
150    displayname = forms.TextField(
151        label_text = "Display Name",
152        help_text = "Name that will appear when posting/commenting",
153        validator = All(UnicodeString(not_empty=True), UniqueDisplayname()))
154    email_address = forms.TextField(
155        validator = All(Email(not_empty=True), UniqueEmail()))
156    timezone = forms.SingleSelectField(
157        options = pytz.common_timezones,
158        validator = OneOf(pytz.common_timezones, not_empty=True))
159    password = forms.PasswordField(
160        validator = ValidPassword(not_empty=True))
161    confirm_password = forms.PasswordField(
162        validator = ValidPassword(not_empty=True))
163    authentication_token = SecureToken()
164    
165    class child(forms.TableLayout):
166        validator = FilteringSchema(
167            chained_validators=[FieldsMatch('password', 'confirm_password')],
168            msgs=dict(childerror=''))
169registration_form = RegistrationForm()
170