2 from django
import forms
3 from django
.conf
import settings
6 from datetime
import datetime
8 from iwdb
.words
.models
import Word
, ChangeSheet
, LanguageChoices
9 from iwdb
.words
.models
import Pronunciation
, POLChoices
13 class BaseAjaxForm(forms
.Form
):
14 def __init__(self
, request
, *args
, **kw
):
15 self
.request
= request
16 super(BaseAjaxForm
, self
).__init
__(*args
, **kw
)
19 # BaseWordAjaxForm # {{{
20 class BaseWordAjaxForm(BaseAjaxForm
):
21 word
= forms
.CharField(max_length
=100)
24 word
= self
.cleaned_data
["word"]
26 word
= Word
.objects
.get(word
=word
)
27 except Word
.DoesNotExist
:
29 word
= Word
.objects
.get(pk
=word
)
30 except Word
.DoesNotExist
:
31 raise forms
.ValidationError("word does not exists")
32 except Word
.MultipleObjectsReturned
:
33 raise forms
.ValidationError("MultipleObjectsReturned")
37 # TwoWordAjaxForm # {{{
38 class TwoWordAjaxForm(BaseWordAjaxForm
):
39 other_word
= forms
.CharField(max_length
=100)
41 def clean_other_word(self
):
42 word
= self
.cleaned_data
["other_word"]
44 word
= Word
.objects
.get(word
=word
)
45 except Word
.DoesNotExist
:
47 word
= Word
.objects
.get(pk
=word
)
48 except Word
.DoesNotExist
:
49 raise forms
.ValidationError("word does not exists")
50 except Word
.MultipleObjectsReturned
:
51 raise forms
.ValidationError("MultipleObjectsReturned")
55 # AlterM2MRelationForm # {{{
56 class AlterM2MRelationForm(TwoWordAjaxForm
):
57 url_re
= re
.compile("/w/ajax/word/(?P<operation>.*)-(?P<relation>.*)/")
59 d
= self
.cleaned_data
.get
60 if not d("word") or not d("other_word"):
61 return self
.cleaned_data
62 operation
, relation
= AlterM2MRelationForm
.url_re
.match(
65 relation_name
= relation
if relation
.endswith("s") else relation
+ "s"
66 changesheet_type
= "%s-added" if operation
== "add" else "%s-removed"
67 self
.changesheet_type
= changesheet_type
% relation
68 self
.operation
= operation
69 self
.relation_name
= relation_name
71 if relation
== "translation":
72 if d("word").language
== d("other_word").language
:
73 raise forms
.ValidationError(
74 "translation in same language not supported"
77 if d("word").language
!= d("other_word").language
:
78 raise forms
.ValidationError(
79 "both words are not in same language"
81 return self
.cleaned_data
84 d
= self
.cleaned_data
.get
86 getattr(d("word"), self
.relation_name
), self
.operation
88 ChangeSheet
.objects
.create(
90 user
= self
.request
.user
,
91 id_1
= d("other_word").id,
92 type = self
.changesheet_type
,
95 unicode(word
) for word
in getattr(
96 d("word"), self
.relation_name
101 # ChangeLanguageForm # {{{
102 class ChangeLanguageForm(BaseWordAjaxForm
):
103 language
= forms
.ChoiceField(choices
=LanguageChoices
)
105 d
= self
.cleaned_data
.get
106 ChangeSheet
.objects
.create(
108 user
= self
.request
.user
,
109 data
= d("word").language
, # old language gets logged.
110 type = "language-changed",
112 d("word").language
= d("language")
114 return d("word").language
117 # UpdateEtymologyForm # {{{
118 class UpdateEtymologyForm(BaseWordAjaxForm
):
119 etymology
= forms
.CharField()
121 d
= self
.cleaned_data
.get
122 ChangeSheet
.objects
.create(
124 user
= self
.request
.user
,
125 data
= d("word").etymology
, # old etymology gets logged.
126 type = "etymology-updated",
128 d("word").etymology
= d("etymology")
130 return d("word").etymology
133 # AddPronunciation # {{{
134 class AddPronunciation(BaseWordAjaxForm
):
135 spelling
= forms
.CharField(max_length
=100)
136 key
= forms
.CharField(max_length
=100)
138 def clean_spelling(self
):
139 d
= self
.cleaned_data
.get
140 if d("spelling") not in d("word").forms
:
141 raise forms
.ValidationError(
142 "this spelling not allowed for this word"
147 d
= self
.cleaned_data
.get
148 p
, c
= Pronunciation
.objects
.get_or_create(
149 spelling
=d("spelling"), pronunciation_key
=d("key")
154 ChangeSheet
.objects
.create(
156 user
= self
.request
.user
,
158 type = "pronunciation-added",
162 # RemovePronunciation # {{{
163 class RemovePronunciation(BaseWordAjaxForm
):
164 pronunciation
= forms
.CharField(max_length
=100)
166 def clean_pronunciation(self
):
167 d
= self
.cleaned_data
.get
169 pronunciation
= Pronunciation
.objects
.get(pk
=d("pronunciation"))
170 except Pronunciation
.DoesNotExist
:
171 raise forms
.ValidationError("No such pronunciation")
172 if pronunciation
.spelling
not in d("word").forms
:
173 raise forms
.ValidationError(
174 "pronunciation not used for this word"
179 d
= self
.cleaned_data
.get
180 p
= d("pronunciation")
182 p
.deleted_on
= datetime
.now()
184 ChangeSheet
.objects
.create(
186 user
= self
.request
.user
,
188 type = "pronunciation-removed",
192 # EditPronunciation # {{{
193 class EditPronunciation(BaseWordAjaxForm
):
194 pronunciation
= forms
.CharField(max_length
=100)
195 key
= forms
.CharField(max_length
=100)
197 def clean_pronunciation(self
):
198 d
= self
.cleaned_data
.get
200 pronunciation
= Pronunciation
.objects
.get(pk
=d("pronunciation"))
201 except Pronunciation
.DoesNotExist
:
202 raise forms
.ValidationError("No such pronunciation")
203 if pronunciation
.spelling
not in d("word").forms
:
204 raise forms
.ValidationError(
205 "pronunciation not used for this word"
210 d
= self
.cleaned_data
.get
211 p
= d("pronunciation")
212 ChangeSheet
.objects
.create(
214 user
= self
.request
.user
,
216 data
= p
.pronunciation_key
, # old key
217 type = "pronunciation-edited",
219 p
.pronunciation_key
= d("key")
224 class AddMeaning(BaseWordAjaxForm
):
225 m
= forms
.CharField()
226 usage
= forms
.CharField(required
=False)
227 pol
= forms
.ChoiceField(choices
=POLChoices
)
230 d
= self
.cleaned_data
.get
231 meaning
= Meaning
.objects
.create(
232 word
= d("word"), meaning
= d("m"),
233 usage
= d("usage"), pol
= d("pol")
235 ChangeSheet
.objects
.create(
237 user
= self
.request
.user
,
239 type = "meaning-added",
243 # RemoveMeaning # {{{
244 class RemoveMeaning(BaseWordAjaxForm
):
245 meaning
= forms
.CharField(max_length
=100)
247 def clean_meaning(self
):
248 d
= self
.cleaned_data
.get
250 meaning
= Meaning
.objects
.get(pk
=d("meaning"))
251 except Meaning
.DoesNotExist
:
252 raise forms
.ValidationError("No such meaning")
253 if meaning
.word
!= d("word"):
254 raise forms
.ValidationError(
255 "meaning not used for this word"
260 d
= self
.cleaned_data
.get
261 meaning
= d("meaning")
262 meaning
.is_deleted
= True
263 meaning
.deleted_on
= datetime
.now()
265 ChangeSheet
.objects
.create(
267 user
= self
.request
.user
,
269 type = "meaning-removed",
273 # EditMeaningMeaning # {{{
274 class EditMeaningMeaning(BaseWordAjaxForm
):
275 meaning
= forms
.CharField()
276 m
= forms
.CharField()
278 def clean_meaning(self
):
279 d
= self
.cleaned_data
.get
281 meaning
= Meaning
.objects
.get(pk
=d("meaning"))
282 except Meaning
.DoesNotExist
:
283 raise forms
.ValidationError("No such meaning")
284 if meaning
.word
!= d("word"):
285 raise forms
.ValidationError(
286 "meaning not used for this word"
291 d
= self
.cleaned_data
.get
292 meaning
= d("meaning")
293 meaning
.meaning
= d("m")
295 ChangeSheet
.objects
.create(
297 user
= self
.request
.user
,
299 data
= meaning
.meaning
, # old meaning
300 type = "meaning-edited-meaning",
304 # EditMeaningPOL # {{{
305 class EditMeaningPOL(BaseWordAjaxForm
):
306 meaning
= forms
.CharField()
307 pol
= forms
.CharField()
309 def clean_meaning(self
):
310 d
= self
.cleaned_data
.get
312 meaning
= Meaning
.objects
.get(pk
=d("meaning"))
313 except Meaning
.DoesNotExist
:
314 raise forms
.ValidationError("No such meaning")
315 if meaning
.word
!= d("word"):
316 raise forms
.ValidationError(
317 "meaning not used for this word"
322 d
= self
.cleaned_data
.get
323 meaning
= d("meaning")
324 meaning
.pol
= d("pol")
326 ChangeSheet
.objects
.create(
328 user
= self
.request
.user
,
330 data
= meaning
.pol
, # old pol
331 type = "meaning-edited-meaning",
335 # EditMeaningUsage # {{{
336 class EditMeaningUsage(BaseWordAjaxForm
):
337 meaning
= forms
.CharField()
338 usage
= forms
.CharField()
340 def clean_meaning(self
):
341 d
= self
.cleaned_data
.get
343 meaning
= Meaning
.objects
.get(pk
=d("meaning"))
344 except Meaning
.DoesNotExist
:
345 raise forms
.ValidationError("No such meaning")
346 if meaning
.word
!= d("word"):
347 raise forms
.ValidationError(
348 "meaning not used for this word"
353 d
= self
.cleaned_data
.get
354 meaning
= d("meaning")
355 meaning
.usage
= d("usage")
357 ChangeSheet
.objects
.create(
359 user
= self
.request
.user
,
361 data
= meaning
.usage
, # old usage
362 type = "edit-meaning-usage",
367 class AddWordForm(BaseWordAjaxForm
):
368 form
= forms
.CharField()
370 def clean_form(self
):
371 # form same as word can not be added/deleted
372 d
= self
.cleaned_data
.get
373 if d("form").lower() == d("word").word
.lower():
374 raise forms
.ValidationError("not allowed")
375 return d("form").lower()
378 d
= self
.cleaned_data
.get
379 ChangeSheet
.objects
.create(
381 user
= self
.request
.user
,
382 data
= d("word").forms
, # old forms
386 settings
.WORD_FORM_DELIMITER
, d("form"),
387 settings
.WORD_FORM_DELIMITER
388 ) not in d("word").forms
:
389 d("word").forms
+= d("form")+settings
.WORD_FORM_DELIMITER
391 return d("word").forms
.split(settings
.WORD_FORM_DELIMITER
)
394 # RemoveWordForm # {{{
395 class RemoveWordForm(BaseWordAjaxForm
):
396 form
= forms
.CharField()
398 def clean_form(self
):
399 # form same as word can not be added/deleted
400 d
= self
.cleaned_data
.get
401 if d("form").lower() == d("word").word
.lower():
402 raise forms
.ValidationError("not allowed")
404 settings
.WORD_FORM_DELIMITER
, d("form"),
405 settings
.WORD_FORM_DELIMITER
406 ) not in d("word").forms
:
407 raise forms
.ValidationError("form is not associated with word")
408 return d("form").lower()
411 d
= self
.cleaned_data
.get
412 ChangeSheet
.objects
.create(
414 user
= self
.request
.user
,
415 data
= d("form"), # form that is being removed
416 type = "form-removed",
418 forms
= d("word").forms
.split(settings
.WORD_FORM_DELIMITER
)
419 forms
.remove(d("form"))
420 d("word").forms
= settings
.WORD_FORM_DELIMITER
.join(forms
)
426 class AddWord(BaseAjaxForm
):
427 word
= forms
.CharField(max_length
=100)
429 def clean_word(self
):
430 d
= self
.cleaned_data
.get
431 if settings
.WORD_FORM_DELIMITER
in d("word"):
432 raise forms
.ValidationError(
433 "%s not allowed" % settings
.WORD_FORM_DELIMITER
435 # what if someone tries to add a word that is already a form
436 # of another word? this cant be because they ll go to that forms
437 # page and they ll be redirected to the original word, they wud just
438 # not see the add button for that word.
442 d
= self
.cleaned_data
.get
443 word
= Word
.objects
.create(
444 word
=d("word"), forms
="%s%s%s" % (
445 settings
.WORD_FORM_DELIMITER
, wordname
,
446 settings
.WORD_FORM_DELIMITER
449 ChangeSheet
.objects
.create(
451 user
= self
.request
.user
,