# 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
            rating.place2.lat, rating.place2.lon,
            rating.score,
            # Inlcude the lat/lon, iff there is user_info for the rating.  If
            # not, just use None.
            rating.user_info and rating.user_info.lat,
            rating.user_info and rating.user_info.lon])

    return response


class MainUIView (views.TemplateView):
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        context = super(MainUIView, self).get_context_data(**kwargs)

        session_key = self.request.session.session_key
        if not self.request.session.exists(session_key):
            self.request.session.create()
            session_key = self.request.session.session_key

        # Using the browser session, get the current user info (or create it if
        # none exists for the current session).
        session = Session.objects.get(session_key=session_key)
        user_info = UserInfo.objects.get_or_create(session=session)[0]
        criteria = Criterion.objects.all()
        site = Site.objects.get_current()

        try:
            site_config = SiteConfiguration.objects.get(site=site)

# 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

            rating.place2.lat, rating.place2.lon,
            rating.score,
            # Inlcude the lat/lon, iff there is user_info for the rating.  If
            # not, just use None.
            rating.user_info and rating.user_info.lat,
            rating.user_info and rating.user_info.lon])

    return response


class MainUIView (views.TemplateView):
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        context = super(MainUIView, self).get_context_data(**kwargs)

        session_key = self.request.session.session_key
        if not self.request.session.exists(session_key):
            self.request.session.create()
            session_key = self.request.session.session_key

        # Using the browser session, get the current user info (or create it if
        # none exists for the current session).
        session = Session.objects.get(session_key=session_key)
        user_info = UserInfo.objects.get_or_create(session=session)[0]
        criteria = Criterion.objects.all()
        site = Site.objects.get_current()

        try:
            site_config = SiteConfiguration.objects.get(site=site)

        except SiteConfiguration.DoesNotExist: