source: osmose-frontend/byuser.py

Last change on this file was 6f1633e, checked in by Frédéric Rodrigo <fred.rodrigo@…>, 6 years ago

Display errors number of logger user

  • Property mode set to 100644
File size: 4.2 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, template, redirect, response
24from tools import utils
25from tools import query
26from tools.OrderedDict import OrderedDict
27
28
29@route('/byuser')
30def byUser():
31    redirect("byuser/")
32
33
34@route('/byuser/')
35@route('/byuser/<username>')
36@route('/byuser/<username>.<format:ext>')
37@route('/api/0.2/user/<username>')
38def user(db, lang, username=None, format=None):
39    params = query._params()
40    if username:
41        params.users = utils.pg_escape(username.decode("utf-8")).split(",")
42    params.limit = 500
43    params.full = True
44    username = ",".join(params.users)
45
46    if not params.users:
47        return template('byuser/index')
48
49    errors = query._gets(db, params)
50    count = len(errors)
51    if request.path.startswith("/api") or format == "json":
52        out = OrderedDict()
53        out["description"] = ["id", "item", "lat", "lon", "source", "class", "elems", "subclass", "subtitle", "comment", "title", "level", "timestamp", "menu", "username", "date"]
54        for res in errors:
55            res["timestamp"] = str(res["timestamp"])
56            res["date"] = str(res["date"])
57        out["byusers"] = errors
58        return out
59
60    elif format == 'rss':
61        response.content_type = "application/rss+xml"
62        return template('byuser/byuser.rss', username=username, users=params.users, count=count, errors=errors, translate=utils.translator(lang), website=utils.website)
63
64    else:
65        return template('byuser/byuser', username=username, users=params.users, count=count, errors=errors, translate=utils.translator(lang), website=utils.website)
66
67
68def _user_count(db, username=None):
69    params = query._params()
70    if username:
71        params.users = utils.pg_escape(username.decode("utf-8")).split(",")
72
73    if not params.users:
74        return
75
76    res = query._count(db, params, ['dynpoi_class.level'], ['dynpoi_class.level'])
77    ret = {1:0, 2:0, 3:0}
78    for (l, c) in res:
79        ret[l] = c
80
81    return ret
82
83
84@route('/byuser_count/<username>')
85@route('/byuser_count/<username>.<format:ext>')
86@route('/api/0.2/user_count/<username>')
87def user_count(db, lang, username=None, format=None):
88    count = _user_count(db, username)
89    if request.path.startswith("/api") or format == "json":
90        return count
91
92    elif format == 'rss':
93        response.content_type = "application/rss+xml"
94        return template('byuser/byuser_count.rss', username=username, count=count, translate=utils.translator(lang), website=utils.website)
95
96    else:
97        return count
98
99
100def _users(db):
101    params = query._params()
102    return query._count(db, params, ["marker_elem.username"])
103
104
105@route('/byuser-stats')
106def byuser_stats(db):
107    return template('byuser/byuser-stats', results=_users(db))
108
109
110@route('/api/0.2/users')
111def users(db):
112    out = OrderedDict()
113    out["description"] = ["username", "count"]
114    out["users"] = _users(db)
115    return out
Note: See TracBrowser for help on using the repository browser.