# Path: periods/forms.py
# class PeriodForm(forms.ModelForm):
#     class Meta:
# 
# Path: periods/models.py
# def today():
#     def timezone(self):
#     def first_days(self):
#     def cycle_count(self):
#     def get_previous_period(self, previous_to):
#     def get_next_period(self, after=None):
#     def get_cache_key(self, data_type):
#     def get_cycle_lengths(self):
#     def get_sorted_cycle_lengths(self):
#     def get_full_name(self):
#     def get_short_name(self):
#     def __str__(self):
#     def choices(cls, blank=False):
#     def __str__(self):
#     def _get_ordinal_value(self, index):
#     def cycle_length_minimum(self):
#     def cycle_length_maximum(self):
#     def _get_statistics_value(self, method_name, num_values_required=1):
#     def cycle_length_mean(self):
#     def cycle_length_median(self):
#     def cycle_length_mode(self):
#     def cycle_length_standard_deviation(self):
#     def current_cycle_length(self):
#     def first_date(self):
#     def first_day(self):
#     def set_start_date_and_day(self, min_timestamp):
#     def predicted_events(self):
#     def __str__(self):
#     def get_from_server(from_date):
#     def get_for_date(cls, from_date, to_date):
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def add_to_permissions_group(sender, instance, **kwargs):
# def create_statistics(sender, instance, **kwargs):
# def update_statistics(sender, instance, **kwargs):
# class User(AbstractEmailUser):
# class LabelChoicesEnum(enum.Enum):
# class FlowLevel(LabelChoicesEnum):
# class FlowColor(LabelChoicesEnum):
# class ClotSize(LabelChoicesEnum):
# class CrampLevel(LabelChoicesEnum):
# class FlowEvent(models.Model):
# class Statistics(models.Model):
#     class Meta:
# class AerisData(models.Model):
#     SPOTTING = 0
#     LIGHT = 1
#     MEDIUM = 2
#     HEAVY = 3
#     VERY_HEAVY = 4
#     PINK = 0
#     LIGHT_RED = 1
#     RED = 2
#     DARK_RED = 3
#     BROWN = 4
#     BLACK = 5
#     SMALL = 0
#     MEDIUM = 1
#     LARGE = 2
#     SLIGHT = 0
#     MODERATE = 1
#     SEVERE = 2
# 
# Path: periods/serializers.py
# class NullableEnumField(serializers.ChoiceField):
# class FlowEventSerializer(serializers.ModelSerializer):
#     class Meta:
# class FlowEventFilter(django_filters.FilterSet):
#     class Meta:
# class StatisticsSerializer(serializers.ModelSerializer):
#     class Meta:
#     def __init__(self, enum, **kwargs):
#     def to_internal_value(self, data):


from collections import Counter
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.dateparse import parse_datetime
from django.views.generic import CreateView, TemplateView, UpdateView
from extra_views import ModelFormSetView
from jsonview.views import JsonView
from rest_framework import permissions, status, viewsets
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework.views import APIView
from periods import forms as period_forms, models as period_models, serializers
import datetime
import itertools
import math
import pytz
class ApiAuthenticateView(APIView):
    http_method_names = ['post']
    permission_classes = [permissions.AllowAny]

    def post(self, request, *args, **kwargs):
        user = None
        error = ''
        status_code = status.HTTP_400_BAD_REQUEST

        try:
            email = request.data['email']
            password = request.data['password']
            user = auth.authenticate(username=email, password=password)
            if not user:
                status_code = status.HTTP_401_UNAUTHORIZED
                error = "Invalid credentials"
        except KeyError as e:
            error = "Missing required field '%s'" % e.args[0]

        if user:
            return Response({'token': user.auth_token.key})

        return Response({'error': error}, status=status_code)


class AerisView(LoginRequiredMixin, JsonView):
    def get_context_data(self, **kwargs):
        context = super(AerisView, self).get_context_data(**kwargs)
        from_date = self.request.GET.get('min_timestamp')
        to_date = self.request.GET.get('max_timestamp')

# Path: periods/forms.py
# class PeriodForm(forms.ModelForm):
#     class Meta:
# 
# Path: periods/models.py
# def today():
#     def timezone(self):
#     def first_days(self):
#     def cycle_count(self):
#     def get_previous_period(self, previous_to):
#     def get_next_period(self, after=None):
#     def get_cache_key(self, data_type):
#     def get_cycle_lengths(self):
#     def get_sorted_cycle_lengths(self):
#     def get_full_name(self):
#     def get_short_name(self):
#     def __str__(self):
#     def choices(cls, blank=False):
#     def __str__(self):
#     def _get_ordinal_value(self, index):
#     def cycle_length_minimum(self):
#     def cycle_length_maximum(self):
#     def _get_statistics_value(self, method_name, num_values_required=1):
#     def cycle_length_mean(self):
#     def cycle_length_median(self):
#     def cycle_length_mode(self):
#     def cycle_length_standard_deviation(self):
#     def current_cycle_length(self):
#     def first_date(self):
#     def first_day(self):
#     def set_start_date_and_day(self, min_timestamp):
#     def predicted_events(self):
#     def __str__(self):
#     def get_from_server(from_date):
#     def get_for_date(cls, from_date, to_date):
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def add_to_permissions_group(sender, instance, **kwargs):
# def create_statistics(sender, instance, **kwargs):
# def update_statistics(sender, instance, **kwargs):
# class User(AbstractEmailUser):
# class LabelChoicesEnum(enum.Enum):
# class FlowLevel(LabelChoicesEnum):
# class FlowColor(LabelChoicesEnum):
# class ClotSize(LabelChoicesEnum):
# class CrampLevel(LabelChoicesEnum):
# class FlowEvent(models.Model):
# class Statistics(models.Model):
#     class Meta:
# class AerisData(models.Model):
#     SPOTTING = 0
#     LIGHT = 1
#     MEDIUM = 2
#     HEAVY = 3
#     VERY_HEAVY = 4
#     PINK = 0
#     LIGHT_RED = 1
#     RED = 2
#     DARK_RED = 3
#     BROWN = 4
#     BLACK = 5
#     SMALL = 0
#     MEDIUM = 1
#     LARGE = 2
#     SLIGHT = 0
#     MODERATE = 1
#     SEVERE = 2
# 
# Path: periods/serializers.py
# class NullableEnumField(serializers.ChoiceField):
# class FlowEventSerializer(serializers.ModelSerializer):
#     class Meta:
# class FlowEventFilter(django_filters.FilterSet):
#     class Meta:
# class StatisticsSerializer(serializers.ModelSerializer):
#     class Meta:
#     def __init__(self, enum, **kwargs):
#     def to_internal_value(self, data):


# Path: periods/views.py
from collections import Counter
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.dateparse import parse_datetime
from django.views.generic import CreateView, TemplateView, UpdateView
from extra_views import ModelFormSetView
from jsonview.views import JsonView
from rest_framework import permissions, status, viewsets
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework.views import APIView
from periods import forms as period_forms, models as period_models, serializers
import datetime
import itertools
import math
import pytz

class ApiAuthenticateView(APIView):
    http_method_names = ['post']
    permission_classes = [permissions.AllowAny]

    def post(self, request, *args, **kwargs):
        user = None
        error = ''
        status_code = status.HTTP_400_BAD_REQUEST

        try:
            email = request.data['email']
            password = request.data['password']
            user = auth.authenticate(username=email, password=password)
            if not user:
                status_code = status.HTTP_401_UNAUTHORIZED
                error = "Invalid credentials"
        except KeyError as e:
            error = "Missing required field '%s'" % e.args[0]

        if user:
            return Response({'token': user.auth_token.key})

        return Response({'error': error}, status=status_code)


class AerisView(LoginRequiredMixin, JsonView):
    def get_context_data(self, **kwargs):
        context = super(AerisView, self).get_context_data(**kwargs)
        from_date = self.request.GET.get('min_timestamp')
        to_date = self.request.GET.get('max_timestamp')

        data = period_models.AerisData.get_for_date(from_date, to_date)