# Path: api/user_utils.py
# def _user(db, lang, username):
#     params = Params()
#     if username:
#         params.users = username.split(",")
#     params.limit = 500
#     params.full = True
#     username = ",".join(params.users)
# 
#     errors = query._gets(db, params)
#     return [params, username, errors]
# 
# def _user_count(db, username=None):
#     params = Params()
#     if username:
#         params.users = username.split(",")
# 
#     if not params.users:
#         return
# 
#     res = query._count(db, params, ['class.level'], ['class.level'])
#     ret = {1:0, 2:0, 3:0}
#     for (l, c) in res:
#         ret[l] = c
# 
#     return ret
# 
# Path: web_api/issues.py
# def rss(website, lang, params, query, main_website, remote_url_read, issues, title = None):
#     content = map(lambda issue: rss_issue(issue, website, lang, query, main_website, remote_url_read), issues)
# 
#     lastBuildDate = []
#     if len(issues) > 0:
#         time = issues[0]['timestamp']
#         ctime = time.ctime()
#         rfc822 = '{0}, {1:02d} {2}'.format(ctime[0:3], time.day, ctime[4:7]) + time.strftime(' %Y %H:%M:%S %z')
#         lastBuildDate = [E.lastBuildDate(rfc822)]
# 
#     title, description, url = xml_header(params, title, website, lang, query)
#     E_atom = ElementMaker(namespace='http://www.w3.org/2005/Atom', nsmap={'atom':'http://www.w3.org/2005/Atom'})
#     return E.rss(
#         E.channel(
#             E_atom.link(
#                 href='http://{}/{}/issues/open.rss?{}'.format(website, lang, query),
#                 rel='self',
#                 type='application/rss+xml',
#             ),
#             E.title(title),
#             E.description(description or query),
#             *lastBuildDate,
#             E.link(url),
#             *content,
#         ),
#         version = '2.0',
#     )
# 
# def gpx(website, lang, params, query, main_website, remote_url_read, issues, title = None):
#     content = []
#     if len(issues) > 0:
#         content.append(E.time(issues[0]['timestamp'].strftime('%Y-%m-%dT%H:%M:%SZ')))
#     content += list(map(lambda issue: gpx_issue(issue, website, lang, query, main_website, remote_url_read), issues))
# 
#     title, _, url = xml_header(params, title, website, lang, query)
#     return E.gpx(
#         E.name(title),
#         E.url(url),
#         *content,
#         version = '1.0',
#         creator = 'http://osmose.openstreetmap.fr',
#         xmlns = 'http://www.topografix.com/GPX/1/0',
#         # xmlns:xsi = 'http://www.w3.org/2001/XMLSchema-instance',
#         # xsi:schemaLocation = 'http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd',
#     )
# 
# def kml(website, lang, params, query, main_website, remote_url_read, issues, title = None):
#     content = map(lambda issue: kml_issue(issue, website, lang, query, main_website, remote_url_read), issues)
# 
#     title, _, url = xml_header(params, title, website, lang, query)
#     if len(issues) > 0:
#         title += ' (' + issues[0]['timestamp'].strftime('%Y-%m-%dT%H:%M:%SZ') + ')'
#     return E.kml(
#         E.Document(
#             E.Style(
#                 E.IconStyle(
#                     E.Icon(
#                         E.href('http://maps.me/placemarks/placemark-purple.png')
#                     ),
#                 ),
#                 id = 'placemark-purple'
#             ),
#             E.name(title),
#             E.url(url),
#         ),
#         *content,
#         xmlns = 'http://www.opengis.net/kml/2.2',
#     )


from bottle import route, redirect, response, html_escape
from modules import utils
from modules.utils import i10n_select_auto
from modules import query
from lxml import etree
from lxml.builder import E, ElementMaker
from api.user_utils import _user, _user_count
from .issues import rss, gpx, kml
        return dict(username=username, users=params.users, count=count, errors=list(map(dict, errors)), website=utils.website + '/' + lang[0], main_website=utils.main_website, remote_url_read=utils.remote_url_read)


@route('/byuser_count/<username>.rss')
def user_count(db, lang, username=None):
    count = _user_count(db, username)
    print(count)
    response.content_type = "application/rss+xml"
    xml = E.rss(
        E.channel(
            E.title('Osmose - ' + username),
            E.description(_("Statistics for user {0}").format(username)),
            E.link('http://{}/byuser/{}'.format(utils.website, username)),
            E.item(
                E.title(_("Number of level {level} issues: {count}").format(level=1, count=count[1]))
            ),
            E.item(
                E.title(_("Number of level {level} issues: {count}").format(level=2, count=count[2]))
            ),
            E.item(
                E.title(_("Number of level {level} issues: {count}").format(level=3, count=count[3]))
            ),
        ),
        version = '2.0',
    )
    return etree.tostring(xml, pretty_print=True)


@route('/byuser_count/<username>')
def user_count(db, lang, username=None):

# Path: api/user_utils.py
# def _user(db, lang, username):
#     params = Params()
#     if username:
#         params.users = username.split(",")
#     params.limit = 500
#     params.full = True
#     username = ",".join(params.users)
# 
#     errors = query._gets(db, params)
#     return [params, username, errors]
# 
# def _user_count(db, username=None):
#     params = Params()
#     if username:
#         params.users = username.split(",")
# 
#     if not params.users:
#         return
# 
#     res = query._count(db, params, ['class.level'], ['class.level'])
#     ret = {1:0, 2:0, 3:0}
#     for (l, c) in res:
#         ret[l] = c
# 
#     return ret
# 
# Path: web_api/issues.py
# def rss(website, lang, params, query, main_website, remote_url_read, issues, title = None):
#     content = map(lambda issue: rss_issue(issue, website, lang, query, main_website, remote_url_read), issues)
# 
#     lastBuildDate = []
#     if len(issues) > 0:
#         time = issues[0]['timestamp']
#         ctime = time.ctime()
#         rfc822 = '{0}, {1:02d} {2}'.format(ctime[0:3], time.day, ctime[4:7]) + time.strftime(' %Y %H:%M:%S %z')
#         lastBuildDate = [E.lastBuildDate(rfc822)]
# 
#     title, description, url = xml_header(params, title, website, lang, query)
#     E_atom = ElementMaker(namespace='http://www.w3.org/2005/Atom', nsmap={'atom':'http://www.w3.org/2005/Atom'})
#     return E.rss(
#         E.channel(
#             E_atom.link(
#                 href='http://{}/{}/issues/open.rss?{}'.format(website, lang, query),
#                 rel='self',
#                 type='application/rss+xml',
#             ),
#             E.title(title),
#             E.description(description or query),
#             *lastBuildDate,
#             E.link(url),
#             *content,
#         ),
#         version = '2.0',
#     )
# 
# def gpx(website, lang, params, query, main_website, remote_url_read, issues, title = None):
#     content = []
#     if len(issues) > 0:
#         content.append(E.time(issues[0]['timestamp'].strftime('%Y-%m-%dT%H:%M:%SZ')))
#     content += list(map(lambda issue: gpx_issue(issue, website, lang, query, main_website, remote_url_read), issues))
# 
#     title, _, url = xml_header(params, title, website, lang, query)
#     return E.gpx(
#         E.name(title),
#         E.url(url),
#         *content,
#         version = '1.0',
#         creator = 'http://osmose.openstreetmap.fr',
#         xmlns = 'http://www.topografix.com/GPX/1/0',
#         # xmlns:xsi = 'http://www.w3.org/2001/XMLSchema-instance',
#         # xsi:schemaLocation = 'http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd',
#     )
# 
# def kml(website, lang, params, query, main_website, remote_url_read, issues, title = None):
#     content = map(lambda issue: kml_issue(issue, website, lang, query, main_website, remote_url_read), issues)
# 
#     title, _, url = xml_header(params, title, website, lang, query)
#     if len(issues) > 0:
#         title += ' (' + issues[0]['timestamp'].strftime('%Y-%m-%dT%H:%M:%SZ') + ')'
#     return E.kml(
#         E.Document(
#             E.Style(
#                 E.IconStyle(
#                     E.Icon(
#                         E.href('http://maps.me/placemarks/placemark-purple.png')
#                     ),
#                 ),
#                 id = 'placemark-purple'
#             ),
#             E.name(title),
#             E.url(url),
#         ),
#         *content,
#         xmlns = 'http://www.opengis.net/kml/2.2',
#     )


# Path: web_api/byuser.py
from bottle import route, redirect, response, html_escape
from modules import utils
from modules.utils import i10n_select_auto
from modules import query
from lxml import etree
from lxml.builder import E, ElementMaker
from api.user_utils import _user, _user_count
from .issues import rss, gpx, kml

        return dict(username=username, users=params.users, count=count, errors=list(map(dict, errors)), website=utils.website + '/' + lang[0], main_website=utils.main_website, remote_url_read=utils.remote_url_read)


@route('/byuser_count/<username>.rss')
def user_count(db, lang, username=None):
    count = _user_count(db, username)
    print(count)
    response.content_type = "application/rss+xml"
    xml = E.rss(
        E.channel(
            E.title('Osmose - ' + username),
            E.description(_("Statistics for user {0}").format(username)),
            E.link('http://{}/byuser/{}'.format(utils.website, username)),
            E.item(
                E.title(_("Number of level {level} issues: {count}").format(level=1, count=count[1]))
            ),
            E.item(
                E.title(_("Number of level {level} issues: {count}").format(level=2, count=count[2]))
            ),
            E.item(
                E.title(_("Number of level {level} issues: {count}").format(level=3, count=count[3]))
            ),
        ),
        version = '2.0',
    )
    return etree.tostring(xml, pretty_print=True)


@route('/byuser_count/<username>')
def user_count(db, lang, username=None):

    return _user_count(db, username)