1 from django
.shortcuts
import render_to_response
, get_object_or_404
2 from django
.http
import HttpResponseRedirect
, HttpResponse
3 from django
.template
import RequestContext
4 from django
.conf
import settings
6 from worddb
.words
.models
import *
7 from worddb
import utils
9 def view(request
, wordname
):
10 # import pdb; pdb.set_trace()
11 return render_to_response(
15 'word_list': Word
.objects
.filter(word
=wordname
),
16 'link_list': Word
.objects
.filter(
17 forms__contains
=utils
.wf(wordname
)
18 ).exclude(word
=wordname
),
20 context_instance
=RequestContext(request
),
23 def word_add(request
, wordname
):
24 newword
= Word(word
=wordname
, forms
=wordname
)
26 return HttpResponseRedirect('/%s/' % wordname
)
28 def search_ajax(request
):
29 # it supports the following parameters via GET:
30 # limit_language: only words with this language will be returned
31 # exclude_word: do not include this word in resultset
32 # exclude_language: only words other than ones in this language
33 qs
= Word
.objects
.filter(
34 word__startswith
=request
.REQUEST
.get("q", "")
36 if "exclude_word" in request
.REQUEST
:
37 qs
= qs
.exclude(word
=request
.REQUEST
["exclude_word"])
39 qs
= qs
.exclude(pk
=int(request
.REQUEST
["exclude_word"]))
40 except ValueError: print "error"
41 if "limit_language" in request
.REQUEST
:
42 qs
= qs
.filter(language
=request
.REQUEST
["limit_language"])
43 if "exclude_language" in request
.REQUEST
:
44 qs
= qs
.exclude(language
=request
.REQUEST
["exclude_language"])
47 "%s (%s)|%s" % ( w
.word
, w
.language
, w
.id ) for w
in qs
[:10]
51 def get_translations_ajax(request
):
52 #import pdb; pdb.set_trace()
53 word
= Word
.objects
.filter(pk
=request
.GET
['word_id'])[0]
54 translations
= ','.join(
55 "%s" % w
.word
for w
in
56 word
.translations
.filter(language
=request
.GET
['language'])
58 return HttpResponse(translations
)
60 def get_languages_ajax(request
):
61 #import pdb; pdb.set_trace()
62 word
= Word
.objects
.filter(pk
=request
.GET
['word_id'])[0]
64 for w
in word
.translations
.all():
65 language_choices
[w
.language
] = w
.get_language_display()
68 '<p id="%s__%d" class="show_translation">%s</p>'
69 % ( l
, word
.id, language_choices
[l
] ) for l
in
70 language_choices
.keys())