# Path: tags/models.py
# class Tag(models.Model):
#     TAG_GROUPS = (
#         (1, "School"),
#         (2, "Liberation"),
#         (3, "Other")
#     )
#     id = models.AutoField(primary_key=True)
#     name = models.CharField(max_length=100, unique=True)
#     slug = models.SlugField(blank=False, max_length=100)
#     group = models.IntegerField(choices=TAG_GROUPS, default=1)
#     description = models.TextField(blank=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name, max_length=100)
#         super(Tag, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("tag", kwargs={"tagId": self.id, "slug": self.slug})
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: proposals/models.py
# class Proposal(Content):
#     PROPOSAL_STATUS_CHOICES = (
#         (1, "Open for discussion"),
#         (2, "Work in progress"),
#         (3, "Going to Student Council"),
#         (4, "Resolved")
#     )
#     title = models.CharField(max_length=100)
#     text = models.TextField()
#     slug = models.SlugField(default="slug", max_length=100)
#     tags = models.ManyToManyField(Tag, related_name="proposals")
#     status = models.IntegerField(choices=PROPOSAL_STATUS_CHOICES, default=1)
#     student_council = models.ForeignKey(StudentCouncil,
#                                         related_name="proposals",
#                                         null=True)
# 
#     objects = ProposalManager()
# 
#     def __unicode__(self):
#         return self.title
# 
#     def __str__(self):
#         return self.title
# 
#     def get_content_type():
#         """
#         Gets ContentType of Proposal model, caching result in ``_content_type``
#         when first retrieved.
# 
#         :returns: ContentType of Proposal model
#         :rtype:   :mod:`django.contrib.contenttypes.models.ContentType`
#         """
#         if not hasattr(Proposal, '_content_type'):
#             Proposal._content_type = ContentType.objects.get(
#                 app_label="proposals", model="proposal")
#         return Proposal._content_type
# 
#     def save(self, *args, **kwargs):
#         """
#         Create slug, save proposal and for initial save add
#         vote by proposer.
#         """
#         is_initial = not self.pk
#         self.slug = slugify(self.title, max_length=100)
#         super(Proposal, self).save(*args, **kwargs)
#         # when the proposal is first created, add a vote by the proposer
#         if is_initial:
#             Vote.objects.create(user=self.user, content=self, isVoteUp=True)
# 
#     def get_absolute_url(self):
#         """
#         Get the proposal's URL using :mod:`django.core.urlresolvers.reverse`
#         """
#         return reverse("proposal", kwargs={"proposal_id": self.id,
#                                            "slug": self.slug})
# 
#     def get_votes_up_percentage(self):
#         """
#         Get percentage of votes which are up votes
#         """
#         votes_up = self.upVotes
#         votes_total = votes_up + self.downVotes
#         if votes_total == 0:
#             return 0
#         else:
#             return (votes_up/votes_total) * 100
# 
#     def get_votes_down_percentage(self):
#         """
#         Get percentage of votes which are down votes
#         """
#         votes_up_percentage = self.get_votes_up_percentage()
#         return 100 - votes_up_percentage
# 
#     @staticmethod
#     def _hours_since(date):
#         utc_now = datetime.datetime.utcnow()
#         utc_event = datetime.datetime.utcfromtimestamp(date.timestamp())
#         return (utc_now - utc_event).total_seconds() / 3600.0
# 
#     @staticmethod
#     def _weight_instance(hour_age, gravity=1.8):
#         return 1 / pow((hour_age+2), gravity)
# 
#     @staticmethod
#     def _proximity_coefficient():
#         return 1
# 
#     def get_rank(self):
#         rank = 0
# 
#         # Take sum of weighted value for each comment
#         comments = Comment.objects.filter(proposal=self)
#         for comment in comments:
#             rank += Proposal._weight_instance(
#                     hour_age=Proposal._hours_since(comment.createdAt)) * 4
# 
#         votes = self.user.get_votes()
#         # Vote.get_votes(self)
#         for vote in votes:
#             hour_age = Proposal._hours_since(vote.createdAt)
#             if vote.isVoteUp:
#                 rank += Proposal._weight_instance(hour_age) * 2
#             else:
#                 rank += Proposal._weight_instance(hour_age) * 1
# 
#         return rank * Proposal._proximity_coefficient()
# 
#     def get_visible_comments(self, reply_to=None, sort="popularity"):
#         return Comment.objects.get_visible_comments(
#             proposal=self, reply_to=reply_to, sort=sort)
# 
#     def is_new(self):
#         """
#         :return: True if the proposal is less than an hour old
#         """
#         timesince = datetime.datetime.now() - self.createdAt
#         return timesince < datetime.timedelta(hours=1)


from django.shortcuts import render
from tags.models import Tag
from proposals.models import Proposal
from django.http.response import HttpResponsePermanentRedirect,\
    HttpResponseRedirect, Http404
from django.contrib import messages
from django.core.urlresolvers import reverse



def tag(request, tagId, slug):
    try:
        tag = Tag.objects.get(id=tagId)
    except:
        raise Http404

    # redirect requests with the wrong slug to the correct page
    if not slug == tag.slug:
        return HttpResponsePermanentRedirect(tag.get_absolute_url())

    template = "tag.html"  # main HTML
    proposals_template = "proposal_list.html"  # just the proposals

    # sort by popularity by default
    proposals = Proposal.objects.get_visible_proposals(tag=tag, sort="popular")
    sort = "popular"

    if request.GET.get("sort") == "newest":

# Path: tags/models.py
# class Tag(models.Model):
#     TAG_GROUPS = (
#         (1, "School"),
#         (2, "Liberation"),
#         (3, "Other")
#     )
#     id = models.AutoField(primary_key=True)
#     name = models.CharField(max_length=100, unique=True)
#     slug = models.SlugField(blank=False, max_length=100)
#     group = models.IntegerField(choices=TAG_GROUPS, default=1)
#     description = models.TextField(blank=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name, max_length=100)
#         super(Tag, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("tag", kwargs={"tagId": self.id, "slug": self.slug})
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: proposals/models.py
# class Proposal(Content):
#     PROPOSAL_STATUS_CHOICES = (
#         (1, "Open for discussion"),
#         (2, "Work in progress"),
#         (3, "Going to Student Council"),
#         (4, "Resolved")
#     )
#     title = models.CharField(max_length=100)
#     text = models.TextField()
#     slug = models.SlugField(default="slug", max_length=100)
#     tags = models.ManyToManyField(Tag, related_name="proposals")
#     status = models.IntegerField(choices=PROPOSAL_STATUS_CHOICES, default=1)
#     student_council = models.ForeignKey(StudentCouncil,
#                                         related_name="proposals",
#                                         null=True)
# 
#     objects = ProposalManager()
# 
#     def __unicode__(self):
#         return self.title
# 
#     def __str__(self):
#         return self.title
# 
#     def get_content_type():
#         """
#         Gets ContentType of Proposal model, caching result in ``_content_type``
#         when first retrieved.
# 
#         :returns: ContentType of Proposal model
#         :rtype:   :mod:`django.contrib.contenttypes.models.ContentType`
#         """
#         if not hasattr(Proposal, '_content_type'):
#             Proposal._content_type = ContentType.objects.get(
#                 app_label="proposals", model="proposal")
#         return Proposal._content_type
# 
#     def save(self, *args, **kwargs):
#         """
#         Create slug, save proposal and for initial save add
#         vote by proposer.
#         """
#         is_initial = not self.pk
#         self.slug = slugify(self.title, max_length=100)
#         super(Proposal, self).save(*args, **kwargs)
#         # when the proposal is first created, add a vote by the proposer
#         if is_initial:
#             Vote.objects.create(user=self.user, content=self, isVoteUp=True)
# 
#     def get_absolute_url(self):
#         """
#         Get the proposal's URL using :mod:`django.core.urlresolvers.reverse`
#         """
#         return reverse("proposal", kwargs={"proposal_id": self.id,
#                                            "slug": self.slug})
# 
#     def get_votes_up_percentage(self):
#         """
#         Get percentage of votes which are up votes
#         """
#         votes_up = self.upVotes
#         votes_total = votes_up + self.downVotes
#         if votes_total == 0:
#             return 0
#         else:
#             return (votes_up/votes_total) * 100
# 
#     def get_votes_down_percentage(self):
#         """
#         Get percentage of votes which are down votes
#         """
#         votes_up_percentage = self.get_votes_up_percentage()
#         return 100 - votes_up_percentage
# 
#     @staticmethod
#     def _hours_since(date):
#         utc_now = datetime.datetime.utcnow()
#         utc_event = datetime.datetime.utcfromtimestamp(date.timestamp())
#         return (utc_now - utc_event).total_seconds() / 3600.0
# 
#     @staticmethod
#     def _weight_instance(hour_age, gravity=1.8):
#         return 1 / pow((hour_age+2), gravity)
# 
#     @staticmethod
#     def _proximity_coefficient():
#         return 1
# 
#     def get_rank(self):
#         rank = 0
# 
#         # Take sum of weighted value for each comment
#         comments = Comment.objects.filter(proposal=self)
#         for comment in comments:
#             rank += Proposal._weight_instance(
#                     hour_age=Proposal._hours_since(comment.createdAt)) * 4
# 
#         votes = self.user.get_votes()
#         # Vote.get_votes(self)
#         for vote in votes:
#             hour_age = Proposal._hours_since(vote.createdAt)
#             if vote.isVoteUp:
#                 rank += Proposal._weight_instance(hour_age) * 2
#             else:
#                 rank += Proposal._weight_instance(hour_age) * 1
# 
#         return rank * Proposal._proximity_coefficient()
# 
#     def get_visible_comments(self, reply_to=None, sort="popularity"):
#         return Comment.objects.get_visible_comments(
#             proposal=self, reply_to=reply_to, sort=sort)
# 
#     def is_new(self):
#         """
#         :return: True if the proposal is less than an hour old
#         """
#         timesince = datetime.datetime.now() - self.createdAt
#         return timesince < datetime.timedelta(hours=1)


# Path: tags/views.py
from django.shortcuts import render
from tags.models import Tag
from proposals.models import Proposal
from django.http.response import HttpResponsePermanentRedirect,\
    HttpResponseRedirect, Http404
from django.contrib import messages
from django.core.urlresolvers import reverse




def tag(request, tagId, slug):
    try:
        tag = Tag.objects.get(id=tagId)
    except:
        raise Http404

    # redirect requests with the wrong slug to the correct page
    if not slug == tag.slug:
        return HttpResponsePermanentRedirect(tag.get_absolute_url())

    template = "tag.html"  # main HTML
    proposals_template = "proposal_list.html"  # just the proposals

    # sort by popularity by default
    proposals = Proposal.objects.get_visible_proposals(tag=tag, sort="popular")
    sort = "popular"

    if request.GET.get("sort") == "newest":

        proposals = Proposal.objects.get_visible_proposals(