File: views.py

package info (click to toggle)
dico 2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,908 kB
  • sloc: ansic: 85,886; sh: 50,207; lex: 2,624; yacc: 1,437; tcl: 1,434; python: 1,281; makefile: 1,266; perl: 881; lisp: 489; awk: 157; pascal: 125; cpp: 44; fortran: 25; asm: 19; sed: 16; xml: 8
file content (287 lines) | stat: -rw-r--r-- 10,517 bytes parent folder | download | duplicates (2)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#  This file is part of GNU Dico.
#  Copyright (C) 2008-2010, 2012-2015 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
try:
    from django.core.urlresolvers import reverse
except ImportError:
    from django.urls import reverse
from django.core.cache import cache
from django.shortcuts import render_to_response
from django.utils.encoding import force_bytes
from django.utils.translation import ugettext as _

import dicoclient
try:
    from wikitrans import wiki2html
except ImportError:
    wiki2html = None
    print('WARNING: wikitrans is not installed.')

def onerror(err, dfl=None):
    try:
        act = settings.ONERROR[err]
    except:
        act = dfl
    return act

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(force_bytes(
        '%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(force_bytes(
            '%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)
        if not mtc:
            mtc = result

        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 i, df in enumerate(result['definitions']):
            if 'content-type' in df:
                if (df['content-type'].startswith('text/x-wiki')
                    and wiki2html):
                    lang = df['x-wiki-language'] \
                          if 'x-wiki-language' in df else 'en'
                    wikiparser = wiki2html.HtmlWiktionaryMarkup(
                                          text=df['desc'],
                                          html_base='?q=',
                                          lang=lang)
                    wikiparser.parse()
                    df['desc'] = str(wikiparser)
                    df['format_html'] = True
                elif df['content-type'].startswith('text/html'):
                    df['format_html'] = True
                elif df['content-type'].startswith('text/'):
                    df['format_html'] = False
                else:
                    act = onerror('UNSUPPORTED_CONTENT_TYPE',
                                  { 'action': 'replace',
                                    'message': 'Article cannot be displayed due to unsupported content type' })
                    if act['action'] == 'delete':
                        del(result['definitions'][i])
                        result['count'] -= 1
                    elif act['action'] == 'replace':
                        df['desc'] = act['message']
                        df['format_html'] = (
                            act['format_html'] if 'format_html' in act
                            else False)
                        result['definitions'][i] = df
                    elif act['action'] == 'display':
                        df['format_html'] = (
                            act['format_html'] if 'format_html' in act
                            else False)
                        result['definitions'][i] = df
                    elif settings.DEBUG:
                        raise AssertionError("""
Article cannot be displayed due to unsupported content type (%s).
Additionally, ONERROR['UNSUPPORTED_CONTENT_TYPE'] has unsupported value (%s).
""" % (df['content-type'], act))
                    else:
                        del(result['definitions'][i])
                        result['count'] -= 1
            else:
                df['desc'] = re.sub('_(.*?)_', '<b>\\1</b>', df['desc'])
                df['desc'] = re.sub(rx1, __subs1, df['desc'])
        if result['count'] == 0:
            result = { 'error': 552, 'msg': 'No match' }

    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(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},
                              content_type='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]
                try:
                    opt[1] = opt[1].decode('utf-8')
                except:
                    pass
                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)