# Path: voting/models.py
# class Vote(TimeStampedModel):
#     """
#     Generic vote object for up and down voting things
#     """
# 
#     BALLOT   = Choices((-1, 'downvote', 'downvote'), (1, 'upvote', 'upvote'), (0, 'novote', 'novote'))
# 
#     # Data fields for the voting object
#     vote     = models.SmallIntegerField( choices=BALLOT, default=BALLOT.novote )
#     user     = models.ForeignKey( 'auth.User', related_name='votes' )
# 
#     # Content types for a generic relationship (e.g. vote anything)
#     content_type   = models.ForeignKey( ContentType )
#     object_id      = models.PositiveIntegerField()
#     content_object = GenericForeignKey( 'content_type', 'object_id' )
# 
#     # Set a custom manager for the Vote object
#     objects   = VotingManager()
# 
#     def __str__(self):
#         action = {
#             -1: "down voted",
#             0:  "no voted",
#             1:  "up voted",
#         }[self.vote]
# 
#         return u"%s %s %s" % (unicode(self.user), action, unicode(self.content_object))
# 
#     class Meta:
#         db_table = "voting"
#         get_latest_by = "modified"
#         verbose_name  = "vote"
#         verbose_name_plural = "votes"
#         unique_together = ('object_id', 'user', 'content_type')
# 
# Path: minent/utils.py
# def normalize(text):
# def signature(text):
# def htmlize(text):
# def normalize_query(terms):
# def memoized(fget):
#     def fget_memoized(self):
#     def __init__(self, wall_clock=True):
#     def __enter__(self):
#     def __exit__(self, type, value, tb):
#     def __str__(self):
# def timeit(func, wall_clock=True):
#     def timer_wrapper(*args, **kwargs):
# def humanizedelta(*args, **kwargs):
# class Timer(object):
# 
# Path: fugato/managers.py
# class QuestionManager(models.Manager):
# 
#     def dedupe(self, raise_for_exceptions=False, **kwargs):
#         """
#         Essentially a GET or CREATE method that checks if a duplicate
#         question already exists in the database by its signature. If
#         raise_for_exceptions is True, then will raise a DuplicateQuestion
#         exception, otherwise it will return None.
# 
#         Returns question, created where created is a Boolean
#         """
#         qsig = signature(kwargs['text'])
#         query = self.filter(signature=qsig)
#         if query.exists():
#             if raise_for_exceptions:
#                 raise DuplicateQuestion()
#             return query.first(), False
# 
#         return self.create(**kwargs), True
# 
#     def unanswered(self):
#         """
#         Returns any question that is unanswered
#         """
#         return self.get_queryset().unanswered()
# 
#     def count_votes(self):
#         """
#         Returns questions annotated with the number of votes they have.
#         """
#         return self.get_queryset().count_votes()
# 
#     def count_answers(self):
#         """
#         Returns questions annotated with the number of answers they have.
#         """
#         return self.get_queryset().count_answers()
# 
#     def search(self, terms):
#         """
#         Produces an icontains lookup on the question title.
#         """
#         return self.get_queryset().search(terms)
# 
#     def get_queryset(self):
#         """
#         Return a QuestionQuerySet instead of the standard queryset.
#         """
#         return QuestionQuerySet(self.model, using=self._db)
# 
# class AnswerManager(models.Manager):
# 
#     def count_votes(self):
#         """
#         Returns questions annotated with the number of votes they have.
#         """
#         return self.get_queryset().count_votes()
# 
# 
#     def get_queryset(self):
#         """
#         Return a QuestionQuerySet instead of the standard queryset.
#         """
#         return AnswerQuerySet(self.model, using=self._db)


import time
from slugify import slugify
from django.db import models
from voting.models import Vote
from operator import itemgetter
from minent.utils import nullable
from autoslug import AutoSlugField
from django.core.urlresolvers import reverse
from model_utils.models import TimeStampedModel
from fugato.managers import QuestionManager, AnswerManager
from django.contrib.contenttypes.fields import GenericRelation
        return tag in [tag.text for tag in self.tags.all()]

    def set_answer_order_by_votes(self):
        """
        A helper function that calls the `set_answer_order` function and
        passes in the order based on the sum of the votes.
        """
        # Construct the aggregation query
        query = self.answers.count_votes().values('id', 'num_votes')

        order = [
            a['id'] for a in sorted(query, key=itemgetter('num_votes'), reverse=True)
        ]

        self.set_answer_order(order)

    def __str__(self):
        return self.text


class Answer(TimeStampedModel):

    text     = models.TextField(                                                 # The text of the answer (markdown)
                null=False, blank=False,
                help_text="Edit in Markdown"
               )
    text_rendered = models.TextField( editable=False, null=False )               # HTML rendered details of the question
    related  = models.ManyToManyField( 'self' )                                  # Links between related responses
    author   = models.ForeignKey( 'auth.User', related_name="answers" )          # The author of the answer
    question = models.ForeignKey( 'fugato.Question', related_name="answers" )    # The question this answer answers

# Path: voting/models.py
# class Vote(TimeStampedModel):
#     """
#     Generic vote object for up and down voting things
#     """
# 
#     BALLOT   = Choices((-1, 'downvote', 'downvote'), (1, 'upvote', 'upvote'), (0, 'novote', 'novote'))
# 
#     # Data fields for the voting object
#     vote     = models.SmallIntegerField( choices=BALLOT, default=BALLOT.novote )
#     user     = models.ForeignKey( 'auth.User', related_name='votes' )
# 
#     # Content types for a generic relationship (e.g. vote anything)
#     content_type   = models.ForeignKey( ContentType )
#     object_id      = models.PositiveIntegerField()
#     content_object = GenericForeignKey( 'content_type', 'object_id' )
# 
#     # Set a custom manager for the Vote object
#     objects   = VotingManager()
# 
#     def __str__(self):
#         action = {
#             -1: "down voted",
#             0:  "no voted",
#             1:  "up voted",
#         }[self.vote]
# 
#         return u"%s %s %s" % (unicode(self.user), action, unicode(self.content_object))
# 
#     class Meta:
#         db_table = "voting"
#         get_latest_by = "modified"
#         verbose_name  = "vote"
#         verbose_name_plural = "votes"
#         unique_together = ('object_id', 'user', 'content_type')
# 
# Path: minent/utils.py
# def normalize(text):
# def signature(text):
# def htmlize(text):
# def normalize_query(terms):
# def memoized(fget):
#     def fget_memoized(self):
#     def __init__(self, wall_clock=True):
#     def __enter__(self):
#     def __exit__(self, type, value, tb):
#     def __str__(self):
# def timeit(func, wall_clock=True):
#     def timer_wrapper(*args, **kwargs):
# def humanizedelta(*args, **kwargs):
# class Timer(object):
# 
# Path: fugato/managers.py
# class QuestionManager(models.Manager):
# 
#     def dedupe(self, raise_for_exceptions=False, **kwargs):
#         """
#         Essentially a GET or CREATE method that checks if a duplicate
#         question already exists in the database by its signature. If
#         raise_for_exceptions is True, then will raise a DuplicateQuestion
#         exception, otherwise it will return None.
# 
#         Returns question, created where created is a Boolean
#         """
#         qsig = signature(kwargs['text'])
#         query = self.filter(signature=qsig)
#         if query.exists():
#             if raise_for_exceptions:
#                 raise DuplicateQuestion()
#             return query.first(), False
# 
#         return self.create(**kwargs), True
# 
#     def unanswered(self):
#         """
#         Returns any question that is unanswered
#         """
#         return self.get_queryset().unanswered()
# 
#     def count_votes(self):
#         """
#         Returns questions annotated with the number of votes they have.
#         """
#         return self.get_queryset().count_votes()
# 
#     def count_answers(self):
#         """
#         Returns questions annotated with the number of answers they have.
#         """
#         return self.get_queryset().count_answers()
# 
#     def search(self, terms):
#         """
#         Produces an icontains lookup on the question title.
#         """
#         return self.get_queryset().search(terms)
# 
#     def get_queryset(self):
#         """
#         Return a QuestionQuerySet instead of the standard queryset.
#         """
#         return QuestionQuerySet(self.model, using=self._db)
# 
# class AnswerManager(models.Manager):
# 
#     def count_votes(self):
#         """
#         Returns questions annotated with the number of votes they have.
#         """
#         return self.get_queryset().count_votes()
# 
# 
#     def get_queryset(self):
#         """
#         Return a QuestionQuerySet instead of the standard queryset.
#         """
#         return AnswerQuerySet(self.model, using=self._db)


# Path: fugato/models.py
import time
from slugify import slugify
from django.db import models
from voting.models import Vote
from operator import itemgetter
from minent.utils import nullable
from autoslug import AutoSlugField
from django.core.urlresolvers import reverse
from model_utils.models import TimeStampedModel
from fugato.managers import QuestionManager, AnswerManager
from django.contrib.contenttypes.fields import GenericRelation

        return tag in [tag.text for tag in self.tags.all()]

    def set_answer_order_by_votes(self):
        """
        A helper function that calls the `set_answer_order` function and
        passes in the order based on the sum of the votes.
        """
        # Construct the aggregation query
        query = self.answers.count_votes().values('id', 'num_votes')

        order = [
            a['id'] for a in sorted(query, key=itemgetter('num_votes'), reverse=True)
        ]

        self.set_answer_order(order)

    def __str__(self):
        return self.text


class Answer(TimeStampedModel):

    text     = models.TextField(                                                 # The text of the answer (markdown)
                null=False, blank=False,
                help_text="Edit in Markdown"
               )
    text_rendered = models.TextField( editable=False, null=False )               # HTML rendered details of the question
    related  = models.ManyToManyField( 'self' )                                  # Links between related responses
    author   = models.ForeignKey( 'auth.User', related_name="answers" )          # The author of the answer
    question = models.ForeignKey( 'fugato.Question', related_name="answers" )    # The question this answer answers

    votes    = GenericRelation( Vote, related_query_name='answers' )             # Votes for the goodness of the answer