File: views.py

package info (click to toggle)
dico 2.2-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,492 kB
  • ctags: 10,331
  • sloc: ansic: 42,330; sh: 40,809; python: 5,400; lex: 2,022; tcl: 1,434; makefile: 1,112; yacc: 672; lisp: 461; awk: 300; perl: 175; php: 59; cpp: 44; fortran: 25; asm: 19; sed: 16; xml: 8
file content (223 lines) | stat: -rw-r--r-- 8,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#  This file is part of GNU Dico.
#  Copyright (C) 2008-2010, 2012 Wojciech Polak
#
#  GNU Dico is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3, or (at your option)
#  any later version.
#
#  GNU Dico is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>.

import re
import hashlib
import socket
from django.conf import settings
from django.core import urlresolvers
from django.core.cache import cache
from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _

import dicoclient
from wit import wiki2html

def index (request):
    page = {
        'robots': 'index',
    }
    selects = {}
    mtc = {}
    result = {}
    markup_style = 'default'
    port = 2628

    sid = request.COOKIES.get ('dicoweb_sid', '')
    request.session.set_expiry (0)

    accept_lang = request.META.get ('HTTP_ACCEPT_LANGUAGE', '').split (',')
    for i, lang in enumerate (accept_lang):
        accept_lang[i] = lang.split (';')[0]

    if 'server' in request.session:
        server = request.session['server']
    else:
        server = settings.DICT_SERVERS[0]
    server = request.GET.get ('server', server)
    if not server in settings.DICT_SERVERS:
        server = settings.DICT_SERVERS[0]
    request.session['server'] = server;

    if len (settings.DICT_SERVERS) > 1:
        selects['sv'] = HtmlOptions (settings.DICT_SERVERS, server)

    key = hashlib.md5 ('%s/%s' % (sid, server.encode ('ascii',
                                                      'backslashreplace')))
    sid = key.hexdigest ()

    type = 'search'
    if 'define' in request.GET:
        type = 'define'
    else:
        type = 'search'

    database = request.GET.get ('db', '*')
    strategy = request.GET.get ('strategy', '.')

    key_databases = str ('dicoweb/databases/' + server)
    key_strategies = str ('dicoweb/strategies/' + server)

    databases = cache.get (key_databases)
    strategies = cache.get (key_strategies)

    if server.find (':') != -1:
        s = server.split (':', 1)
        server = s[0]
        port = int (s[1])

    if not databases or not strategies:
        dc = dicoclient.DicoClient ()
        try:
            dc.open (server, port)
            dc.timeout = settings.DICT_TIMEOUT
            databases = dc.show_databases ()['databases']
            strategies = dc.show_strategies ()['strategies']
            dc.close ()
        except (socket.timeout, socket.error, dicoclient.DicoNotConnectedError):
            return render_to_response ('index.html', {'selects': selects})

        cache.set (key_databases, databases, timeout=86400)
        cache.set (key_strategies, strategies, timeout=86400)

    for s in strategies:
        s[1] = _(s[1])
    databases.insert (0, ['!', _('First match')])
    databases.insert (0, ['*', _('All')])
    strategies.insert (0, ['.', _('Default')])

    selects['db'] = HtmlOptions (databases, database)
    selects['st'] = HtmlOptions (strategies, strategy)

    q = request.GET.get ('q', '')

    if 'q' in request.GET and q != '':
        langkey = '*';
        if database == '*': langkey = ','.join (accept_lang)

        key = hashlib.md5 ('%s:%d/%s/%s/%s/%s/%s' %
                           (server, port, langkey, type, database, strategy,
                            q.encode ('ascii', 'backslashreplace')))
        key = key.hexdigest ()
        result = cache.get ('dicoweb/' + key)

        if not result:
            try:
                dc = dicoclient.DicoClient ()
                dc.timeout = settings.DICT_TIMEOUT
                dc.open (server, port)
                dc.option ('MIME')

                if database == '*' and 'lang' in dc.server_capas:
                    dc.option ('LANG', ': ' + ' '.join (accept_lang))
                if 'markup-wiki' in dc.server_capas:
                    if dc.option ('MARKUP', 'wiki'):
                        markup_style = 'wiki'

                if database == 'dbinfo':
                    result = dc.show_info (q)
                elif type == 'define':
                    result = dc.define (database, q)
                else:
                    result = dc.match (database, strategy, q)
                dc.close ()

                result['markup_style'] = markup_style
                cache.set ('dicoweb/' + key, result, timeout=3600)

            except (socket.timeout, socket.error,
                    dicoclient.DicoNotConnectedError):
                return render_to_response ('index.html',
                                           {'selects': selects})

        # get last match results
        if sid and type == 'search':
            cache.set ('dicoweb/%s/last_match' % sid, key, timeout=3600)
        else:
            key = cache.get ('dicoweb/%s/last_match' % sid)
        if key != None:
            mtc = cache.get ('dicoweb/' + key)

        mtc['dbnames'] = {}
        if 'matches' in mtc:
            for m in mtc['matches']:
                for d in databases:
                    if d[0] == m:
                        mtc['dbnames'][m] = d[1]
                        break

        if database == 'dbinfo': q = ''
        if q != '':
            page['title'] = q + ' - '
            page['robots'] = 'noindex,nofollow'

    if 'definitions' in result:
        rx1 = re.compile ('{+(.*?)}+', re.DOTALL)
        for df in result['definitions']:
            if df.has_key ('content-type') \
                    and df['content-type'].startswith ('text/x-wiki'):
                lang = df['x-wiki-language'] \
                    if df.has_key ('x-wiki-language') else 'en'
                wikiparser = wiki2html.HtmlWiktionaryMarkup (text=df['desc'],
                                                             html_base='?q=',
                                                             lang=lang)
                wikiparser.parse ()
                df['desc'] = str (wikiparser)
                df['format_html'] = True
            else:
                df['desc'] = re.sub ('_(.*?)_', '<b>\\1</b>', df['desc'])
                df['desc'] = re.sub (rx1, __subs1, df['desc'])

    return render_to_response ('index.html', {'page': page,
                                              'q': q,
                                              'mtc': mtc,
                                              'result': result,
                                              'selects': selects,})

def opensearch (request):
    url_query = request.build_absolute_uri (urlresolvers.reverse ('index'))
    url_media = request.build_absolute_uri (settings.MEDIA_URL)
    return render_to_response ('opensearch.xml', {'url_query': url_query,
                                                  'url_media': url_media},
                               mimetype='application/xml')

def __subs1 (match):
    s = re.sub (r' +', ' ', match.group (1))
    return '<a href="?q=%s" title="Search for %s">%s</a>' \
        % (s.replace ('\n', ''), s.replace ('\n', ''), s)

class HtmlOptions:
    def __init__ (self, lst=[], value=''):
        self.lst = lst
        self.value = value

    def html (self):
        buf = []
        for opt in self.lst:
            if len (opt) == 2:
                if not opt[1]:
                    opt[1] = opt[0]
                if opt[0] == self.value:
                    buf.append ('<option value="%s" selected="selected">%s</option>' % (opt[0], opt[1]))
                else:
                    buf.append ('<option value="%s">%s</option>' % (opt[0],
                                                                    opt[1]))
            else:
                if opt == self.value:
                    buf.append ('<option value="%s" selected="selected">%s</option>' % (opt, opt))
                else:
                    buf.append ('<option value="%s">%s</option>' % (opt, opt))
        return '\n'.join (buf)