# -*- coding: utf-8 -*-
"""Defines views."""

import calendar
from flask import redirect

from presence_analyzer.main import app
from presence_analyzer.utils import (
    jsonify,
    get_data,
    mean,
    group_by_weekday,
    group_by_start_end
)

import logging
log = logging.getLogger(__name__)  # pylint: disable-msg=C0103


@app.route('/')
def mainpage():
    """Redirects to front page."""
    return redirect('/static/presence_weekday.html')


@app.route('/api/v1/users', methods=['GET'])
@jsonify
def users_view():
    """Users listing for dropdown."""
    data = get_data()
    return [{'user_id': i, 'name': 'User {0}'.format(str(i))}
            for i in data.keys()]


@app.route('/api/v1/mean_time_weekday/<int:user_id>', methods=['GET'])
@jsonify
def mean_time_weekday_view(user_id):
    """Returns mean presence time of given user grouped by weekday."""
    data = get_data()
    if user_id not in data:
        log.debug('User {0} not found!'.format(user_id))
        return []

    weekdays = group_by_weekday(data[user_id])
    result = [(calendar.day_abbr[weekday], mean(intervals))
              for weekday, intervals in weekdays.items()]

    return result


@app.route('/api/v1/presence_weekday/<int:user_id>', methods=['GET'])
@jsonify
def presence_weekday_view(user_id):
    """Returns total presence time of given user grouped by weekday."""
    data = get_data()
    if user_id not in data:
        log.debug('User {0} not found!'.format(user_id))
        return []

    weekdays = group_by_weekday(data[user_id])
    result = [(calendar.day_abbr[weekday], sum(intervals))
              for weekday, intervals in weekdays.items()]

    result.insert(0, ('Weekday', 'Presence (s)'))
    return result


@app.route('/api/v1/presence_start_end/<int:user_id>', methods=['GET'])
@jsonify
def presence_start_end_view(user_id):
    """Returns mean start and end time of given user"""
    data = get_data()
    if user_id not in data:
        log.debug('User {0} not found!'.format(user_id))
        return []

    weekdays = group_by_start_end(data[user_id])
    result = [(calendar.day_abbr[weekday], mean(times['start']),
               mean(times['end'])) for weekday, times in weekdays.items()]
    return result

@@ -2081,32 +2081,41 @@
 )%0A    result = %5B
+%0A        
 (calendar.day_ab
@@ -2148,31 +2148,16 @@
 tart'%5D),
-%0A              
  mean(ti
@@ -2168,16 +2168,24 @@
 'end'%5D))
+%0A       
  for wee
@@ -2215,16 +2215,25 @@
 .items()
+%0A        
 %5D%0A    re

Correct code style