# Path: app/reports/forms.py
# class AllIncidentsReportForm(Form):
# 
#     status_choices = (
#         (u'All', u'All'),
#         (u'Active', u'Active'),
#         (u'Acknowledged', u'Acknowledged'),
#         (u'Cleared', u'Cleared')
#     )
#     event_severity = (
#         (u'All', u'All'),
#         (u'S0', u'S0'),
#         (u'S1', u'S1'),
#         (u'S2', u'S2'),
#         (u'S3', u'S3'),
#     )
# 
#     team = ChoiceField(label="Team")
#     from_date = CharField(label="From")
#     to_date = CharField(label="To")
#     severity = ChoiceField(choices=event_severity, label='Severity')
#     csv_export = BooleanField(label="Export as CSV", required=False)
# 
#     def __init__(self, *args, **kwargs):
#         super(AllIncidentsReportForm, self).__init__(*args, **kwargs)
#         self.team_list = [(0, u'All')]
#         for team in Team.objects.all():
#             self.team_list.append((team.id, team.name))
#         self.fields['team'].choices = self.team_list
# 
#     def clean(self):
#         cleaned_data = super(AllIncidentsReportForm, self).clean()
#         from_date = cleaned_data.get('from_date')
#         to_date = cleaned_data.get('to_date')
#         try:
#             datetime.strptime(from_date, '%Y-%m-%d')
#         except Exception as e:
#             self._errors["from_date"] = self.error_class(['Invalid date'])
# 
#         try:
#             datetime.strptime(to_date, '%Y-%m-%d')
#         except Exception as e:
#             self._errors["to_date"] = self.error_class(['Invalid date'])
# 
#         return cleaned_data
# 
# class EventsPerTeam(Form):
# 
#     time_choices = (
#         ('1', 'Last 24hours'),
#         ('7', 'Last week'),
#         ('30', '4 weeks'),
#     )
#     result_limit_choices = (
#         ('10', '10'),
#         ('25', '25'),
#         ('50', '50'),
#         ('100', '100'),
#     )
# 
#     team = ChoiceField(label="Team")
#     timerange = ChoiceField(choices=time_choices, label="Range")
#     event_id = IntegerField(required=False, min_value=1, label="Event ID")
#     result_limit = ChoiceField(choices=result_limit_choices, label="Results")
# 
#     def __init__(self, *args, **kwargs):
#         super(EventsPerTeam, self).__init__(*args, **kwargs)
#         # change a widget attribute:
#         self.fields['event_id'].widget.attrs["placeholder"] = 'Search by EventID or leave blank'
#         self.team_list = [(0, u'All')]
#         for team in Team.objects.all():
#             self.team_list.append((team.id, team.name))
#         self.fields['team'].choices = self.team_list
# 
# class MostAlertedElements(Form):
#     time_choices = (
#         ('1', 'Last 24hours'),
#         ('7', 'Last week'),
#         ('30', '4 weeks'),
#     )
#     result_limit_choices = (
#         ('10', '10'),
#         ('25', '25'),
#         ('50', '50'),
#         ('100', '100'),
#     )
# 
#     timerange = ChoiceField(choices=time_choices, label="Range")
#     result_limit = ChoiceField(choices=result_limit_choices, label="Results")
# 
# class IncidentsPerElement(Form):
#     time_choices = (
#         ('1', 'Last 24hours'),
#         ('7', 'Last week'),
#         ('30', '4 weeks'),
#     )
#     result_limit_choices = (
#         ('10', '10'),
#         ('25', '25'),
#         ('50', '50'),
#         ('100', '100'),
#     )
# 
#     timerange = ChoiceField(choices=time_choices, label="Range (days)")
#     result_limit = ChoiceField(choices=result_limit_choices, label="Results")
#     element = CharField(label="Element/Hostname")


from datetime import datetime
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from cito_engine.models import Team, Event
from . import actions
from .forms import AllIncidentsReportForm, EventsPerTeam, MostAlertedElements, IncidentsPerElement
            query_params['result_limit'] = int(form.cleaned_data.get('result_limit'))
            query_params['days'] = int(form.cleaned_data.get('timerange'))
            if team_id > 0:
                team = get_object_or_404(Team, pk=team_id)
                query_params['team_id'] = team_id
                render_vars['series_label'] = team.name
            else:
                render_vars['series_label'] = 'All teams'

            if event_id:
                query_params['event_id'] = int(event_id)

            render_vars['incidents'] = actions.get_incidents_per_event(**query_params)

            #get event sumary
            for i in render_vars['incidents']:
                e = Event.objects.get(pk=i['event_id'])
                i.update(summary=e.summary, team_name=e.team.name)

    else:
        render_vars['nothing_selected'] = True
    return render(request, 'reports_incidents_per_event.html', render_vars)


@login_required(login_url='/login/')
def report_most_alerted_elements(request):
    render_vars = dict()
    render_vars['form'] = MostAlertedElements()
    render_vars['page_title'] = 'Most alerted elements'
    if request.method == 'POST':

# Path: app/reports/forms.py
# class AllIncidentsReportForm(Form):
# 
#     status_choices = (
#         (u'All', u'All'),
#         (u'Active', u'Active'),
#         (u'Acknowledged', u'Acknowledged'),
#         (u'Cleared', u'Cleared')
#     )
#     event_severity = (
#         (u'All', u'All'),
#         (u'S0', u'S0'),
#         (u'S1', u'S1'),
#         (u'S2', u'S2'),
#         (u'S3', u'S3'),
#     )
# 
#     team = ChoiceField(label="Team")
#     from_date = CharField(label="From")
#     to_date = CharField(label="To")
#     severity = ChoiceField(choices=event_severity, label='Severity')
#     csv_export = BooleanField(label="Export as CSV", required=False)
# 
#     def __init__(self, *args, **kwargs):
#         super(AllIncidentsReportForm, self).__init__(*args, **kwargs)
#         self.team_list = [(0, u'All')]
#         for team in Team.objects.all():
#             self.team_list.append((team.id, team.name))
#         self.fields['team'].choices = self.team_list
# 
#     def clean(self):
#         cleaned_data = super(AllIncidentsReportForm, self).clean()
#         from_date = cleaned_data.get('from_date')
#         to_date = cleaned_data.get('to_date')
#         try:
#             datetime.strptime(from_date, '%Y-%m-%d')
#         except Exception as e:
#             self._errors["from_date"] = self.error_class(['Invalid date'])
# 
#         try:
#             datetime.strptime(to_date, '%Y-%m-%d')
#         except Exception as e:
#             self._errors["to_date"] = self.error_class(['Invalid date'])
# 
#         return cleaned_data
# 
# class EventsPerTeam(Form):
# 
#     time_choices = (
#         ('1', 'Last 24hours'),
#         ('7', 'Last week'),
#         ('30', '4 weeks'),
#     )
#     result_limit_choices = (
#         ('10', '10'),
#         ('25', '25'),
#         ('50', '50'),
#         ('100', '100'),
#     )
# 
#     team = ChoiceField(label="Team")
#     timerange = ChoiceField(choices=time_choices, label="Range")
#     event_id = IntegerField(required=False, min_value=1, label="Event ID")
#     result_limit = ChoiceField(choices=result_limit_choices, label="Results")
# 
#     def __init__(self, *args, **kwargs):
#         super(EventsPerTeam, self).__init__(*args, **kwargs)
#         # change a widget attribute:
#         self.fields['event_id'].widget.attrs["placeholder"] = 'Search by EventID or leave blank'
#         self.team_list = [(0, u'All')]
#         for team in Team.objects.all():
#             self.team_list.append((team.id, team.name))
#         self.fields['team'].choices = self.team_list
# 
# class MostAlertedElements(Form):
#     time_choices = (
#         ('1', 'Last 24hours'),
#         ('7', 'Last week'),
#         ('30', '4 weeks'),
#     )
#     result_limit_choices = (
#         ('10', '10'),
#         ('25', '25'),
#         ('50', '50'),
#         ('100', '100'),
#     )
# 
#     timerange = ChoiceField(choices=time_choices, label="Range")
#     result_limit = ChoiceField(choices=result_limit_choices, label="Results")
# 
# class IncidentsPerElement(Form):
#     time_choices = (
#         ('1', 'Last 24hours'),
#         ('7', 'Last week'),
#         ('30', '4 weeks'),
#     )
#     result_limit_choices = (
#         ('10', '10'),
#         ('25', '25'),
#         ('50', '50'),
#         ('100', '100'),
#     )
# 
#     timerange = ChoiceField(choices=time_choices, label="Range (days)")
#     result_limit = ChoiceField(choices=result_limit_choices, label="Results")
#     element = CharField(label="Element/Hostname")


# Path: app/reports/views.py
from datetime import datetime
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from cito_engine.models import Team, Event
from . import actions
from .forms import AllIncidentsReportForm, EventsPerTeam, MostAlertedElements, IncidentsPerElement

            query_params['result_limit'] = int(form.cleaned_data.get('result_limit'))
            query_params['days'] = int(form.cleaned_data.get('timerange'))
            if team_id > 0:
                team = get_object_or_404(Team, pk=team_id)
                query_params['team_id'] = team_id
                render_vars['series_label'] = team.name
            else:
                render_vars['series_label'] = 'All teams'

            if event_id:
                query_params['event_id'] = int(event_id)

            render_vars['incidents'] = actions.get_incidents_per_event(**query_params)

            #get event sumary
            for i in render_vars['incidents']:
                e = Event.objects.get(pk=i['event_id'])
                i.update(summary=e.summary, team_name=e.team.name)

    else:
        render_vars['nothing_selected'] = True
    return render(request, 'reports_incidents_per_event.html', render_vars)


@login_required(login_url='/login/')
def report_most_alerted_elements(request):
    render_vars = dict()
    render_vars['form'] = MostAlertedElements()
    render_vars['page_title'] = 'Most alerted elements'
    if request.method == 'POST':

        form = MostAlertedElements(request.POST)