getting file size for all dict files to be downloaded. coming to be 400mb or so.
[worddb.git] / words / forms.py
blob8c4b41fb65400952204fd21b1878f23c899fdb4d
1 # imports # {{{
2 from django import forms
3 from django.conf import settings
5 import re, string
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
11 # }}}
13 # BaseAjaxForm # {{{
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)
19 # }}}
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:
28 if c in w:
29 raise forms.ValidationError("%s not allowed" % c)
31 def word_validator(name):
32 def clean_word(self):
33 word = self.cleaned_data[name]
34 try:
35 word = Word.objects.get(word=word)
36 except Word.DoesNotExist:
37 try:
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")
43 return word
44 return clean_word
45 # }}}
47 # BaseWordAjaxForm # {{{
48 class BaseWordAjaxForm(BaseAjaxForm):
49 word = forms.CharField(max_length=100)
50 clean_word = word_validator("word")
51 # }}}
53 # TwoWordAjaxForm # {{{
54 class TwoWordAjaxForm(BaseWordAjaxForm):
55 other_word = forms.CharField(max_length=100)
56 clean_other_word = word_validator("other_word")
57 # }}}
59 # AlterM2MRelationForm # {{{
60 class AlterM2MRelationForm(TwoWordAjaxForm):
61 url_re = re.compile("/w/ajax/word/(?P<operation>.*)-(?P<relation>.*)/")
62 def clean(self):
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(
67 self.request.path
68 ).groups()
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
74 # actual validation
75 if relation == "translation":
76 if d("word").language == d("other_word").language:
77 raise forms.ValidationError(
78 "translation in same language not supported"
80 else:
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
87 def save(self):
88 d = self.cleaned_data.get
89 getattr(
90 getattr(d("word"), self.relation_name), self.operation
91 )(d("other_word"))
92 ChangeSheet.objects.create(
93 word = d("word"),
94 user = self.request.user,
95 id_1 = d("other_word").id,
96 type = self.changesheet_type,
98 return [
99 unicode(word) for word in getattr(
100 d("word"), self.relation_name
101 ).all()
103 # }}}
105 # ChangeLanguageForm # {{{
106 class ChangeLanguageForm(BaseWordAjaxForm):
107 language = forms.ChoiceField(choices=LanguageChoices)
108 def save(self):
109 d = self.cleaned_data.get
110 ChangeSheet.objects.create(
111 word = d("word"),
112 user = self.request.user,
113 data = d("word").language, # old language gets logged.
114 type = "language-changed",
116 d("word").language = d("language")
117 d("word").save()
118 return d("word").language
119 # }}}
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")
137 try:
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")
143 return etymology
145 def clean_language(self):
146 d = self.cleaned_data.get
147 if d("etymology"):
148 if d("language") != d("etymology").language:
149 raise forms.ValidationError("language can not be changed")
150 else:
151 if Etymology.objects.filter(
152 word=d("word"), language=d("language")
153 ).count():
154 raise forms.ValidationError(
155 "there is already a etymology for this "
156 "word in this language"
158 return d("language")
160 def save(self):
161 d = self.cleaned_data.get
162 # check if etymology already exists, is so update it
163 etymology = d("etymology")
164 if etymology:
165 ChangeSheet.objects.create(
166 word = d("word"),
167 user = self.request.user,
168 id_1 = etymology.id,
169 data = etymology.text, # old etymology gets logged.
170 type = "etymology-updated",
172 etymology.text = d("text")
173 etymology.save()
174 else:
175 etymology = Etymology.objects.create(
176 word = d("word"),
177 text = d("text"),
178 language = d("language"),
180 ChangeSheet.objects.create(
181 word = d("word"),
182 user = self.request.user,
183 id_1 = etymology.id,
184 type = "etymology-create",
186 return etymology
187 # }}}
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"
200 return d("spelling")
202 def save(self):
203 d = self.cleaned_data.get
204 p, c = Pronunciation.objects.get_or_create(
205 spelling=d("spelling"), pronunciation_key=d("key")
207 p.is_deleted = False
208 p.deleted_on = None
209 p.save()
210 ChangeSheet.objects.create(
211 word = d("word"),
212 user = self.request.user,
213 id_1 = p.id,
214 type = "pronunciation-added",
216 # }}}
218 # RemovePronunciation # {{{
219 class RemovePronunciation(BaseWordAjaxForm):
220 pronunciation = forms.CharField(max_length=100)
222 def clean_pronunciation(self):
223 d = self.cleaned_data.get
224 try:
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"
232 return pronunciation
234 def save(self):
235 d = self.cleaned_data.get
236 p = d("pronunciation")
237 p.is_deleted = True
238 p.deleted_on = datetime.now()
239 p.save()
240 ChangeSheet.objects.create(
241 word = d("word"),
242 user = self.request.user,
243 id_1 = p.id,
244 type = "pronunciation-removed",
246 # }}}
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
255 try:
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"
263 return pronunciation
265 def save(self):
266 d = self.cleaned_data.get
267 p = d("pronunciation")
268 ChangeSheet.objects.create(
269 word = d("word"),
270 user = self.request.user,
271 id_1 = p.id,
272 data = p.pronunciation_key, # old key
273 type = "pronunciation-edited",
275 p.pronunciation_key = d("key")
276 p.save()
277 # }}}
279 # AddMeaning # {{{
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")
289 def save(self):
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(
296 word = d("word"),
297 user = self.request.user,
298 id_1 = meaning.id,
299 type = "meaning-added",
301 # }}}
303 # RemoveMeaning # {{{
304 class RemoveMeaning(BaseWordAjaxForm):
305 meaning = forms.CharField(max_length=100)
307 def clean_meaning(self):
308 d = self.cleaned_data.get
309 try:
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"
317 return meaning
319 def save(self):
320 d = self.cleaned_data.get
321 meaning = d("meaning")
322 meaning.is_deleted = True
323 meaning.deleted_on = datetime.now()
324 meaning.save()
325 ChangeSheet.objects.create(
326 word = d("word"),
327 user = self.request.user,
328 id_1 = meaning.id,
329 type = "meaning-removed",
331 # }}}
333 # UpdateMeaning # {{{
334 class UpdateMeaning(AddMeaning, RemoveMeaning):
335 def save(self):
336 d = self.cleaned_data.get
337 meaning = d("meaning")
338 data = "\n".join(
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")
350 meaning.save()
351 ChangeSheet.objects.create(
352 word = d("word"),
353 user = self.request.user,
354 id_1 = meaning.id,
355 data = data,
356 type = "meaning-updated",
358 # }}}
360 # AddWordForm # {{{
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()
372 def save(self):
373 d = self.cleaned_data.get
374 ChangeSheet.objects.create(
375 word = d("word"),
376 user = self.request.user,
377 data = d("word").forms, # old forms
378 type = "form-added",
380 if "%s%s%s" % (
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
385 d("word").save()
386 return d("word").forms.split(settings.WORD_FORM_DELIMITER)
387 # }}}
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")
398 if "%s%s%s" % (
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()
405 def save(self):
406 d = self.cleaned_data.get
407 ChangeSheet.objects.create(
408 word = d("word"),
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)
416 d("word").save()
417 return forms
418 # }}}
420 # AddWord # {{{
421 class AddWord(BaseAjaxForm):
422 w = forms.CharField(max_length=100)
423 language = forms.ChoiceField(choices=LanguageChoices)
425 def clean_w(self):
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
432 return d("w")
434 def save(self):
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(
443 word = word,
444 user = self.request.user,
445 type = "added-word",
447 return word.id
448 # }}}