# 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


from bottle import default_app, route
from collections import OrderedDict
from .user_utils import _user, _user_count
##                                                                       ##
## This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. ##
##                                                                       ##
###########################################################################



app_0_2 = default_app.pop()


@app_0_2.route('/user/<username>')
@route('/user/<username>')
def user(db, lang, username):
    params, username, errors = _user(db, lang, username)

    out = OrderedDict()
    for res in errors:
        res["timestamp"] = str(res["timestamp"])
        res["lat"] = float(res["lat"])
        res["lon"] = float(res["lon"])
    out["issues"] = list(map(dict, errors))
    return out



# 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: api/user.py
from bottle import default_app, route
from collections import OrderedDict
from .user_utils import _user, _user_count

##                                                                       ##
## This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. ##
##                                                                       ##
###########################################################################



app_0_2 = default_app.pop()


@app_0_2.route('/user/<username>')
@route('/user/<username>')
def user(db, lang, username):
    params, username, errors = _user(db, lang, username)

    out = OrderedDict()
    for res in errors:
        res["timestamp"] = str(res["timestamp"])
        res["lat"] = float(res["lat"])
        res["lon"] = float(res["lon"])
    out["issues"] = list(map(dict, errors))
    return out



@app_0_2.route('/user_count/<username>')