2 from django
import forms
3 from django
.conf
import settings
6 from datetime
import datetime
8 from worddb
.words
.models
import Word
, ChangeSheet
, LanguageChoices
, Etymology
9 from worddb
.words
.models
import Pronunciation
, POLChoices
, Meaning
, Short
10 from worddb
.utils
import no_new_lines
14 class BaseAjaxForm(forms
.Form
):
15 def __init__(self
, request
, *args
, **kw
):
16 self
.request
= request
17 print self
.request
.POST
18 super(BaseAjaxForm
, self
).__init
__(*args
, **kw
)
21 # utility validators # {{{
22 chars_not_allowed_in_word
= (
23 string
.whitespace
+ string
.punctuation
24 ).replace(",", "").replace(" ", "")
26 def validate_no_special_char(w
):
27 for c
in chars_not_allowed_in_word
:
29 raise forms
.ValidationError("%s not allowed" % c
)
31 def word_validator(name
):
33 word
= self
.cleaned_data
[name
]
35 word
= Word
.objects
.get(word
=word
)
36 except Word
.DoesNotExist
:
38 word
= Word
.objects
.get(pk
=word
)
39 except (Word
.DoesNotExist
, ValueError):
40 raise forms
.ValidationError("word does not exists")
41 except Word
.MultipleObjectsReturned
:
42 raise forms
.ValidationError("MultipleObjectsReturned")
47 # BaseWordAjaxForm # {{{
48 class BaseWordAjaxForm(BaseAjaxForm
):
49 word
= forms
.CharField(max_length
=100)
50 clean_word
= word_validator("word")
53 # TwoWordAjaxForm # {{{
54 class TwoWordAjaxForm(BaseWordAjaxForm
):
55 other_word
= forms
.CharField(max_length
=100)
56 clean_other_word
= word_validator("other_word")
59 # AlterM2MRelationForm # {{{
60 class AlterM2MRelationForm(TwoWordAjaxForm
):
61 url_re
= re
.compile("/w/ajax/word/(?P<operation>.*)-(?P<relation>.*)/")
63 d
= self
.cleaned_data
.get
64 if not d("word") or not d("other_word"):
65 return self
.cleaned_data
66 operation
, relation
= AlterM2MRelationForm
.url_re
.match(
69 relation_name
= relation
if relation
.endswith("s") else relation
+ "s"
70 changesheet_type
= "%s-added" if operation
== "add" else "%s-removed"
71 self
.changesheet_type
= changesheet_type
% relation
72 self
.operation
= operation
73 self
.relation_name
= relation_name
75 if relation
== "translation":
76 if d("word").language
== d("other_word").language
:
77 raise forms
.ValidationError(
78 "translation in same language not supported"
81 if d("word").language
!= d("other_word").language
:
82 raise forms
.ValidationError(
83 "both words are not in same language"
85 return self
.cleaned_data
88 d
= self
.cleaned_data
.get
90 getattr(d("word"), self
.relation_name
), self
.operation
92 ChangeSheet
.objects
.create(
94 user
= self
.request
.user
,
95 id_1
= d("other_word").id,
96 type = self
.changesheet_type
,
99 unicode(word
) for word
in getattr(
100 d("word"), self
.relation_name
105 # ChangeLanguageForm # {{{
106 class ChangeLanguageForm(BaseWordAjaxForm
):
107 language
= forms
.ChoiceField(choices
=LanguageChoices
)
109 d
= self
.cleaned_data
.get
110 ChangeSheet
.objects
.create(
112 user
= self
.request
.user
,
113 data
= d("word").language
, # old language gets logged.
114 type = "language-changed",
116 d("word").language
= d("language")
118 return d("word").language
121 # UpdateEtymologyForm # {{{
122 class UpdateEtymologyForm(BaseWordAjaxForm
):
124 Update Etymology for a word.
126 This will update Etymology of a word if it already exists for given
127 language. if not, it will add it. language of etymology can not be
128 changed after creation.
130 etymology
= forms
.IntegerField(required
=False)
131 text
= forms
.CharField()
132 language
= forms
.ChoiceField(choices
=LanguageChoices
)
134 def clean_etymology(self
):
135 d
= self
.cleaned_data
.get
136 etymology
= d("etymology")
138 etymology
= Etymology
.objects
.get(pk
=etymology
)
139 except Etymology
.DoesNotExist
:
140 raise forms
.ValidationError("etymology does not exists")
141 if d("word") and etymology
.word
!= d("word"):
142 raise form
.ValidationError("wrong word for this etymology")
145 def clean_language(self
):
146 d
= self
.cleaned_data
.get
148 if d("language") != d("etymology").language
:
149 raise forms
.ValidationError("language can not be changed")
151 if Etymology
.objects
.filter(
152 word
=d("word"), language
=d("language")
154 raise forms
.ValidationError(
155 "there is already a etymology for this "
156 "word in this language"
161 d
= self
.cleaned_data
.get
162 # check if etymology already exists, is so update it
163 etymology
= d("etymology")
165 ChangeSheet
.objects
.create(
167 user
= self
.request
.user
,
169 data
= etymology
.text
, # old etymology gets logged.
170 type = "etymology-updated",
172 etymology
.text
= d("text")
175 etymology
= Etymology
.objects
.create(
178 language
= d("language"),
180 ChangeSheet
.objects
.create(
182 user
= self
.request
.user
,
184 type = "etymology-create",
189 # AddPronunciation # {{{
190 class AddPronunciation(BaseWordAjaxForm
):
191 spelling
= forms
.CharField(max_length
=100)
192 key
= forms
.CharField(max_length
=100)
194 def clean_spelling(self
):
195 d
= self
.cleaned_data
.get
196 if d("spelling") not in d("word").forms
:
197 raise forms
.ValidationError(
198 "this spelling not allowed for this word"
203 d
= self
.cleaned_data
.get
204 p
, c
= Pronunciation
.objects
.get_or_create(
205 spelling
=d("spelling"), pronunciation_key
=d("key")
210 ChangeSheet
.objects
.create(
212 user
= self
.request
.user
,
214 type = "pronunciation-added",
218 # RemovePronunciation # {{{
219 class RemovePronunciation(BaseWordAjaxForm
):
220 pronunciation
= forms
.CharField(max_length
=100)
222 def clean_pronunciation(self
):
223 d
= self
.cleaned_data
.get
225 pronunciation
= Pronunciation
.objects
.get(pk
=d("pronunciation"))
226 except Pronunciation
.DoesNotExist
:
227 raise forms
.ValidationError("No such pronunciation")
228 if pronunciation
.spelling
not in d("word").forms
:
229 raise forms
.ValidationError(
230 "pronunciation not used for this word"
235 d
= self
.cleaned_data
.get
236 p
= d("pronunciation")
238 p
.deleted_on
= datetime
.now()
240 ChangeSheet
.objects
.create(
242 user
= self
.request
.user
,
244 type = "pronunciation-removed",
248 # EditPronunciation # {{{
249 class EditPronunciation(BaseWordAjaxForm
):
250 pronunciation
= forms
.CharField(max_length
=100)
251 key
= forms
.CharField(max_length
=100)
253 def clean_pronunciation(self
):
254 d
= self
.cleaned_data
.get
256 pronunciation
= Pronunciation
.objects
.get(pk
=d("pronunciation"))
257 except Pronunciation
.DoesNotExist
:
258 raise forms
.ValidationError("No such pronunciation")
259 if pronunciation
.spelling
not in d("word").forms
:
260 raise forms
.ValidationError(
261 "pronunciation not used for this word"
266 d
= self
.cleaned_data
.get
267 p
= d("pronunciation")
268 ChangeSheet
.objects
.create(
270 user
= self
.request
.user
,
272 data
= p
.pronunciation_key
, # old key
273 type = "pronunciation-edited",
275 p
.pronunciation_key
= d("key")
280 class AddMeaning(BaseWordAjaxForm
):
281 m
= forms
.CharField()
282 usage
= forms
.CharField(required
=False)
283 pol
= forms
.ChoiceField(choices
=POLChoices
)
284 language
= forms
.ChoiceField(choices
=LanguageChoices
)
286 clean_m
= no_new_lines("m")
287 clean_usage
= no_new_lines("usage")
290 d
= self
.cleaned_data
.get
291 meaning
= Meaning
.objects
.create(
292 word
= d("word"), meaning
= d("m"), usage
= d("usage"),
293 pol
= d("pol"), language
= d("language"),
295 ChangeSheet
.objects
.create(
297 user
= self
.request
.user
,
299 type = "meaning-added",
303 # RemoveMeaning # {{{
304 class RemoveMeaning(BaseWordAjaxForm
):
305 meaning
= forms
.CharField(max_length
=100)
307 def clean_meaning(self
):
308 d
= self
.cleaned_data
.get
310 meaning
= Meaning
.objects
.get(pk
=d("meaning"))
311 except Meaning
.DoesNotExist
:
312 raise forms
.ValidationError("No such meaning")
313 if meaning
.word
!= d("word"):
314 raise forms
.ValidationError(
315 "meaning not used for this word"
320 d
= self
.cleaned_data
.get
321 meaning
= d("meaning")
322 meaning
.is_deleted
= True
323 meaning
.deleted_on
= datetime
.now()
325 ChangeSheet
.objects
.create(
327 user
= self
.request
.user
,
329 type = "meaning-removed",
333 # UpdateMeaning # {{{
334 class UpdateMeaning(AddMeaning
, RemoveMeaning
):
336 d
= self
.cleaned_data
.get
337 meaning
= d("meaning")
340 "meaning: %s" % meaning
.meaning
,
341 "usage: %s" % meaning
.usage
,
342 "pol: %s" % meaning
.pol
,
343 "language: %s" % meaning
.language
,
346 meaning
.meaning
= d("m")
347 meaning
.usage
= d("usage")
348 meaning
.pol
= d("pol")
349 meaning
.language
= d("language")
351 ChangeSheet
.objects
.create(
353 user
= self
.request
.user
,
356 type = "meaning-updated",
361 class AddWordForm(BaseWordAjaxForm
):
362 form
= forms
.CharField()
364 def clean_form(self
):
365 # form same as word can not be added/deleted
366 d
= self
.cleaned_data
.get
367 validate_no_special_char(d("form"))
368 if d("form").lower() == d("word").word
.lower():
369 raise forms
.ValidationError("not allowed")
370 return d("form").lower()
373 d
= self
.cleaned_data
.get
374 ChangeSheet
.objects
.create(
376 user
= self
.request
.user
,
377 data
= d("word").forms
, # old forms
381 settings
.WORD_FORM_DELIMITER
, d("form"),
382 settings
.WORD_FORM_DELIMITER
383 ) not in d("word").forms
:
384 d("word").forms
+= d("form")+settings
.WORD_FORM_DELIMITER
386 return d("word").forms
.split(settings
.WORD_FORM_DELIMITER
)
389 # RemoveWordForm # {{{
390 class RemoveWordForm(BaseWordAjaxForm
):
391 form
= forms
.CharField()
393 def clean_form(self
):
394 # form same as word can not be added/deleted
395 d
= self
.cleaned_data
.get
396 if d("form").lower() == d("word").word
.lower():
397 raise forms
.ValidationError("not allowed")
399 settings
.WORD_FORM_DELIMITER
, d("form"),
400 settings
.WORD_FORM_DELIMITER
401 ) not in d("word").forms
:
402 raise forms
.ValidationError("form is not associated with word")
403 return d("form").lower()
406 d
= self
.cleaned_data
.get
407 ChangeSheet
.objects
.create(
409 user
= self
.request
.user
,
410 data
= d("form"), # form that is being removed
411 type = "form-removed",
413 forms
= d("word").forms
.split(settings
.WORD_FORM_DELIMITER
)
414 forms
.remove(d("form"))
415 d("word").forms
= settings
.WORD_FORM_DELIMITER
.join(forms
)
421 class AddWord(BaseAjaxForm
):
422 w
= forms
.CharField(max_length
=100)
423 language
= forms
.ChoiceField(choices
=LanguageChoices
)
426 d
= self
.cleaned_data
.get
427 validate_no_special_char(d("w"))
428 if settings
.WORD_FORM_DELIMITER
in d("w"):
429 raise forms
.ValidationError(
430 "%s not allowed" % settings
.WORD_FORM_DELIMITER
435 d
= self
.cleaned_data
.get
436 word
= Word
.objects
.create(
437 word
=d("w"), forms
="%s%s%s" % (
438 settings
.WORD_FORM_DELIMITER
, d("w"),
439 settings
.WORD_FORM_DELIMITER
440 ), language
=d("language"),
442 ChangeSheet
.objects
.create(
444 user
= self
.request
.user
,