# Path: street_score/project/models.py
# class Criterion (models.Model):
#     prompt = models.TextField()
#     """ The question prompt, i.e. 'How clean is the street?'.
#         """
# 
#     def __unicode__(self):
#         return self.prompt
# 
#     class Meta:
#         verbose_name_plural = "criteria"
# 
# class Rating (TimeStampedModel):
#     criterion = models.ForeignKey('Criterion', related_name='ratings')
#     """ The criterion that this rating is for.
#         """
# 
#     place1 = models.ForeignKey('Place', related_name='+')
#     """ The first place that this rating compares
#         """
# 
#     place2 = models.ForeignKey('Place', related_name='+')
#     """ The second place that this rating compares
#         """
# 
#     score = models.IntegerField()
#     """ The rating score.  1 means that place1 "wins" over place2 for the given
#         criterion.  -1 means that place2 "wins".
#         """
# 
#     user_info = models.ForeignKey('sessions.UserInfo', null=True, related_name='ratings')
#     """ The information for the user that made this rating.  Not required, but
#         useful for data analysis.
#         """
# 
#     def __unicode__(self):
#         meaning = ({
#             -1: 'more {0} than',
#             1:  'less {0} than',
#             0:  'as {0} as',
#         })
# 
#         return ('Place #{p1} is {rating} place #{p2}').format(
#                 p1=self.place1, p2=self.place2,
#                 rating=meaning[self.score].format(self.criterion.prompt))
# 
#     @property
#     def question(self):
#         """
#         The question string to which the rating is a response.
#         """
#         return self.criterion.prompt
# 
# class UserInfo (TimeStampedModel):
#     lat = models.FloatField(null=True)
#     lon = models.FloatField(null=True)
#     """ The user's location.
#         """
# 
#     SOURCES = (
#         ('ip', 'IP Address'),
#         ('html5', 'HTML5 Geolocation API'),
#     )
#     location_source = models.CharField(max_length=32, choices=SOURCES)
#     location_data = models.CharField(max_length=2048)
#     """ The method by which the location was obtained, and any additional
#         information required to recreate the location.
#         """
# 
#     session = models.OneToOneField('sessions.Session')
#     """ The Django browser session.
#         """
# 
#     def __unicode__(self):
#         return u'User for session {key}'.format(key=self.session.session_key)
# 
#     class Meta:
#         app_label = 'sessions'
#         db_table = 'project_userinfo'
#         verbose_name_plural = 'User info'
# 
# class SiteConfiguration (models.Model):
#     site = models.OneToOneField('sites.Site', related_name='config')
#     google_analytics_key = models.CharField(max_length=256, null=True, blank=True)
#     addthis_key = models.CharField(max_length=256, null=True, blank=True)
#     addthis_title = models.CharField(max_length=256, null=True, blank=True)
# 
#     about_title = models.CharField(max_length=256, null=True, blank=True)
#     about_text = models.TextField(null=True, blank=True)
#     about_text_is_html = models.BooleanField(blank=True, default=False)
# 
#     def __unicode__(self):
#         return 'Configuration for {0}'.format(self.site.name)
# 
#     class Meta:
#         app_label = 'sites'
#         db_table = 'project_siteconfiguration'


import csv
import datetime
import logging
from django.contrib.sessions.models import Session
from django.contrib.sites.models import Site
from django.db import connections
from django.http import HttpResponse
from django.views import generic as views
from .models import Criterion, Rating, UserInfo, SiteConfiguration


log = logging.getLogger(__name__)

def csv_data(request):
    now = datetime.datetime.utcnow()

    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=beautifulst_data_%s.csv' % (now.strftime('%Y%m%d%H%M%S'), )

    # Get the data
    ratings = Rating.objects.all().select_related()

    # Init the csv writer

# Path: street_score/project/models.py
# class Criterion (models.Model):
#     prompt = models.TextField()
#     """ The question prompt, i.e. 'How clean is the street?'.
#         """
# 
#     def __unicode__(self):
#         return self.prompt
# 
#     class Meta:
#         verbose_name_plural = "criteria"
# 
# class Rating (TimeStampedModel):
#     criterion = models.ForeignKey('Criterion', related_name='ratings')
#     """ The criterion that this rating is for.
#         """
# 
#     place1 = models.ForeignKey('Place', related_name='+')
#     """ The first place that this rating compares
#         """
# 
#     place2 = models.ForeignKey('Place', related_name='+')
#     """ The second place that this rating compares
#         """
# 
#     score = models.IntegerField()
#     """ The rating score.  1 means that place1 "wins" over place2 for the given
#         criterion.  -1 means that place2 "wins".
#         """
# 
#     user_info = models.ForeignKey('sessions.UserInfo', null=True, related_name='ratings')
#     """ The information for the user that made this rating.  Not required, but
#         useful for data analysis.
#         """
# 
#     def __unicode__(self):
#         meaning = ({
#             -1: 'more {0} than',
#             1:  'less {0} than',
#             0:  'as {0} as',
#         })
# 
#         return ('Place #{p1} is {rating} place #{p2}').format(
#                 p1=self.place1, p2=self.place2,
#                 rating=meaning[self.score].format(self.criterion.prompt))
# 
#     @property
#     def question(self):
#         """
#         The question string to which the rating is a response.
#         """
#         return self.criterion.prompt
# 
# class UserInfo (TimeStampedModel):
#     lat = models.FloatField(null=True)
#     lon = models.FloatField(null=True)
#     """ The user's location.
#         """
# 
#     SOURCES = (
#         ('ip', 'IP Address'),
#         ('html5', 'HTML5 Geolocation API'),
#     )
#     location_source = models.CharField(max_length=32, choices=SOURCES)
#     location_data = models.CharField(max_length=2048)
#     """ The method by which the location was obtained, and any additional
#         information required to recreate the location.
#         """
# 
#     session = models.OneToOneField('sessions.Session')
#     """ The Django browser session.
#         """
# 
#     def __unicode__(self):
#         return u'User for session {key}'.format(key=self.session.session_key)
# 
#     class Meta:
#         app_label = 'sessions'
#         db_table = 'project_userinfo'
#         verbose_name_plural = 'User info'
# 
# class SiteConfiguration (models.Model):
#     site = models.OneToOneField('sites.Site', related_name='config')
#     google_analytics_key = models.CharField(max_length=256, null=True, blank=True)
#     addthis_key = models.CharField(max_length=256, null=True, blank=True)
#     addthis_title = models.CharField(max_length=256, null=True, blank=True)
# 
#     about_title = models.CharField(max_length=256, null=True, blank=True)
#     about_text = models.TextField(null=True, blank=True)
#     about_text_is_html = models.BooleanField(blank=True, default=False)
# 
#     def __unicode__(self):
#         return 'Configuration for {0}'.format(self.site.name)
# 
#     class Meta:
#         app_label = 'sites'
#         db_table = 'project_siteconfiguration'


# Path: street_score/project/views.py
import csv
import datetime
import logging
from django.contrib.sessions.models import Session
from django.contrib.sites.models import Site
from django.db import connections
from django.http import HttpResponse
from django.views import generic as views
from .models import Criterion, Rating, UserInfo, SiteConfiguration



log = logging.getLogger(__name__)

def csv_data(request):
    now = datetime.datetime.utcnow()

    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=beautifulst_data_%s.csv' % (now.strftime('%Y%m%d%H%M%S'), )

    # Get the data
    ratings = Rating.objects.all().select_related()

    # Init the csv writer

    writer = csv.writer(response)