# Path: project/apps/chinup/models.py
# class Metric(models.Model):
#     name = models.CharField(max_length=100)
#     description_worst = models.TextField(null=True, blank=True)
#     description_best = models.TextField(null=True, blank=True)
#     daily = models.BooleanField(default=True)
#     monthly = models.BooleanField(default=False)
#     boolean = models.BooleanField(default=False)
# 
#     def how_often_string(self):
#         if self.daily:
#             return "daily"
# 
#         if self.monthly:
#             return "monthly"
# 
#     def is_boolean_string(self):
#         if self.boolean:
#             return "boolean"
#         else:
#             return ""
# 
#     def average_span(self, day_span=30):
#         avg = MetricRecord.objects.filter(
#             metric=self,
#             datetime__gt=datetime.datetime.today() - timedelta(days=day_span),
#             datetime__lt=datetime.datetime.today()
#         ).aggregate(models.Avg('measurement'))
# 
#         return avg['measurement__avg']
# 
#     def __unicode__(self):
#         return "%s (%s) %s" % (self.name, self.how_often_string(), self.is_boolean_string())
# 
# class MetricRecord(models.Model):
#     metric = models.ForeignKey(Metric, unique_for_date="datetime")
#     datetime = models.DateField()
#     measurement = models.IntegerField(default=5, blank=True)
#     notes = models.TextField(null=True, blank=True)
# 
#     def save(self, *args, **kwargs):
#         # Boolean should default to 0, not 5!
#         if self.pk is None and self.metric.boolean:
#             self.measurement = 0
# 
#         super(MetricRecord, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return "Record for %s %s on %s" % (self.measurement, self.metric.name, self.datetime)


import datetime
import json
from datetime import timedelta
from dateutil import parser
from django.db.models import Avg
from django.http import HttpResponse
from django.shortcuts import render
from .models import Metric, MetricRecord
    return combined

def home(request):
    daily_metric_span = int(request.GET.get('daily_metric_span', 30))

    notes = MetricRecord.objects.filter(notes__isnull=False).exclude(notes__exact="")
    notes = notes.values_list('notes', 'datetime').order_by('-datetime')[:10]

    return render(request, 'chinup/home.html', {
        'daily_metric_span': daily_metric_span,
        'daily_metrics': append_avg_measurement(Metric.objects.filter(daily=True, boolean=False), day_span=daily_metric_span),
        'monthly_metrics': append_avg_measurement(Metric.objects.filter(monthly=True, boolean=False), day_span=120),
        'notes': notes,
        }
    )

def input(request):
    date_string = request.GET.get('date', None)

    if date_string is not None and date_string is not '':
        date = parser.parse(date_string)
    else:
        date = datetime.date.today()

    day_of_month = date.day

    metrics = Metric.objects.all()

    if day_of_month != 1:
        # Not first day of month, so filter out monthly

# Path: project/apps/chinup/models.py
# class Metric(models.Model):
#     name = models.CharField(max_length=100)
#     description_worst = models.TextField(null=True, blank=True)
#     description_best = models.TextField(null=True, blank=True)
#     daily = models.BooleanField(default=True)
#     monthly = models.BooleanField(default=False)
#     boolean = models.BooleanField(default=False)
# 
#     def how_often_string(self):
#         if self.daily:
#             return "daily"
# 
#         if self.monthly:
#             return "monthly"
# 
#     def is_boolean_string(self):
#         if self.boolean:
#             return "boolean"
#         else:
#             return ""
# 
#     def average_span(self, day_span=30):
#         avg = MetricRecord.objects.filter(
#             metric=self,
#             datetime__gt=datetime.datetime.today() - timedelta(days=day_span),
#             datetime__lt=datetime.datetime.today()
#         ).aggregate(models.Avg('measurement'))
# 
#         return avg['measurement__avg']
# 
#     def __unicode__(self):
#         return "%s (%s) %s" % (self.name, self.how_often_string(), self.is_boolean_string())
# 
# class MetricRecord(models.Model):
#     metric = models.ForeignKey(Metric, unique_for_date="datetime")
#     datetime = models.DateField()
#     measurement = models.IntegerField(default=5, blank=True)
#     notes = models.TextField(null=True, blank=True)
# 
#     def save(self, *args, **kwargs):
#         # Boolean should default to 0, not 5!
#         if self.pk is None and self.metric.boolean:
#             self.measurement = 0
# 
#         super(MetricRecord, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return "Record for %s %s on %s" % (self.measurement, self.metric.name, self.datetime)


# Path: project/apps/chinup/views.py
import datetime
import json
from datetime import timedelta
from dateutil import parser
from django.db.models import Avg
from django.http import HttpResponse
from django.shortcuts import render
from .models import Metric, MetricRecord

    return combined

def home(request):
    daily_metric_span = int(request.GET.get('daily_metric_span', 30))

    notes = MetricRecord.objects.filter(notes__isnull=False).exclude(notes__exact="")
    notes = notes.values_list('notes', 'datetime').order_by('-datetime')[:10]

    return render(request, 'chinup/home.html', {
        'daily_metric_span': daily_metric_span,
        'daily_metrics': append_avg_measurement(Metric.objects.filter(daily=True, boolean=False), day_span=daily_metric_span),
        'monthly_metrics': append_avg_measurement(Metric.objects.filter(monthly=True, boolean=False), day_span=120),
        'notes': notes,
        }
    )

def input(request):
    date_string = request.GET.get('date', None)

    if date_string is not None and date_string is not '':
        date = parser.parse(date_string)
    else:
        date = datetime.date.today()

    day_of_month = date.day

    metrics = Metric.objects.all()

    if day_of_month != 1:
        # Not first day of month, so filter out monthly

        metrics = Metric.objects.filter(daily=True, monthly=False)