source: osmose-frontend/control.py @ 6ee292d

Last change on this file since 6ee292d was 6ee292d, checked in by Frédéric Rodrigo <frodrigo@…>, 7 years ago

Simplify control list updates

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#! /usr/bin/env python
2#-*- coding: utf-8 -*-
3
4###########################################################################
5##                                                                       ##
6## Copyrights Etienne Chové <chove@crans.org> 2009                       ##
7##                                                                       ##
8## This program is free software: you can redistribute it and/or modify  ##
9## it under the terms of the GNU General Public License as published by  ##
10## the Free Software Foundation, either version 3 of the License, or     ##
11## (at your option) any later version.                                   ##
12##                                                                       ##
13## This program is distributed in the hope that it will be useful,       ##
14## but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
16## GNU General Public License for more details.                          ##
17##                                                                       ##
18## You should have received a copy of the GNU General Public License     ##
19## along with this program.  If not, see <http://www.gnu.org/licenses/>. ##
20##                                                                       ##
21###########################################################################
22
23from bottle import route, request, response, template, post
24from tools import utils
25import tools.update
26import os
27import sys
28
29@route('/control/update')
30def updates(db):
31    db.execute("""
32SELECT DISTINCT ON (dynpoi_source.source)
33    dynpoi_source.source,
34    EXTRACT(EPOCH FROM ((now())-dynpoi_update.timestamp)) AS age,
35    dynpoi_source.comment
36FROM
37    dynpoi_source
38    LEFT JOIN dynpoi_update ON
39        dynpoi_source.source = dynpoi_update.source
40ORDER BY
41    dynpoi_source.source ASC,
42    dynpoi_update.timestamp DESC
43""")
44    liste = []
45    for res in db.fetchall():
46        source, age, comment = res
47        if age:
48            if age >= 0:
49                # TRANSLATORS: days / hours / minutes since last source update, abbreviated to d / h / m
50                txt = _("{day}d, {hour}h, {minute}m ago").format(day=int(age/86400), hour=int(age/3600)%24, minute=int(age/60)%60)
51            else:
52                txt = _("in {day}d, {hour}h, {minute}m").format(day=int(-age/86400), hour=int(-age/3600)%24, minute=int(-age/60)%60)
53            liste.append((comment, age, txt, source))
54        else:
55            liste.append((comment, 1e10, _("never generated"), source))
56    liste.sort(lambda x, y: -cmp(x[1], y[1]))
57
58    return template('control/updates', liste=liste)
59
60
61@route('/control/update/<source:int>')
62def update(db, source=None):
63    sql = "SELECT source,timestamp,remote_url,remote_ip FROM dynpoi_update WHERE source=%d ORDER BY timestamp DESC;" % source
64    db.execute(sql)
65    return template('control/update', liste=db.fetchall())
66
67
68@route('/control/i18n')
69def update():
70    return os.popen("cd po && make statistics | sed -n '1h;2,$H;${g;s/\\n/<br>/g;p}'").read()
71
72
73@route('/control/lang')
74def update(lang):
75    out = request.headers['Accept-Language'] + "\n"
76    out += "\n".join(lang)
77    response.content_type = "text/plain; charset=utf-8"
78    return out
79
80
81@post('/control/send-update')
82@post('/cgi-bin/update.py') # Backward compatibility
83def send_update():
84    code = request.params.get('code')
85    url = request.params.get('url')
86
87    response.content_type = "text/plain; charset=utf-8"
88
89    if not code or not url:
90        return "FAIL"
91
92    sources = utils.get_sources()
93    for s in sources:
94        if sources[s].get("updatecode", 0) <> code:
95            continue
96        try:
97            tools.update.update(sources[s], url)
98        except:
99            import traceback
100            from cStringIO import StringIO
101            import smtplib
102            s = StringIO()
103            sys.stderr = s
104            traceback.print_exc()
105            sys.stderr = sys.__stderr__
106            traceback = s.getvalue()
107            return traceback.rstrip()
108
109        return "OK"
110
111    return "AUTH FAIL"
Note: See TracBrowser for help on using the repository browser.