# Path: topics/managers.py
# class TopicManager(models.Manager):
#     """
#     Helper functions for topic management
#     """
# 
#     def from_title(self, title):
#         """
#         A get or create from a slug function.
#         """
#         # Titlecase the topic
#         title = titlecase(title)
# 
#         # If the slug exists, return the object
#         query = self.filter(slug=slugify(title))
#         if query.exists():
#             return query.first()
# 
#         # Otherwise create the topic with the title
#         return self.create(title=title)
# 
#     def from_string(self, text):
#         """
#         Basically a get or create for a batch insert of topics separated by
#         newlines as written into a text field or similar.
#         """
#         for line in frozenset(text.splitlines()):
#             if line:
#                 yield self.from_title(line)
# 
#     def with_votes(self):
#         """
#         Annotates the topic model with a vote total.
#         """
#         return self.raw(VOTE_AGGREGATION_SQL)
# 
#     def mean_weight(self):
#         """
#         Returns the mean weight aggregation of all topics.
#         """
#         count = float(self.filter(is_canonical=True).count())
#         total = sum(float(topic.vote_total) for topic in self.with_votes())
#         return total / count
# 
# class VotingManager(models.Manager):
#     """
#     Helper functions for vote management
#     """
# 
#     def upvotes(self):
#         """
#         Returns the set of all up votes for a particular query.
#         """
#         return self.filter(vote=self.model.BALLOT.upvote)
# 
#     def downvotes(self):
#         """
#         Returns the set of all down votes for a particular query.
#         """
#         return self.filter(vote=self.model.BALLOT.downvote)
# 
#     def total(self):
#         """
#         Returns the sum of all up and down votes for a particular query.
#         """
#         return self.aggregate(
#             total=models.Sum('vote')
#         )
# 
#     def responses(self):
#         """
#         Returns an aggregation of IP address and the count per IP.
#         """
#         query = self.values("ipaddr")
#         query = query.annotate(responses=models.Count('ipaddr'))
#         query = query.order_by('ipaddr')
#         return query
# 
#     def response_stats(self):
#         """
#         Returns a list of statistics from the responses histogram.
#         """
#         return self.responses().aggregate(
#             num=models.Sum('responses'),
#             avg=models.Avg('responses'),
#             min=models.Min('responses'),
#             max=models.Max('responses'),
#         )
# 
#     def num_responses(self):
#         """
#         Returns the number of distinct IP addresses
#         """
#         return self.aggregate(r=models.Count('ipaddr', distinct=True))['r']
# 
#     def response_timeseries(self):
#         """
#         Returns a list of timeseries from the responses.
#         """
#         qss = QuerySetStats(self.all(), 'created')
#         return qss.time_series(timezone.now() - timedelta(days=7), timezone.now())


from django.db import models
from model_utils import Choices
from autoslug import AutoSlugField
from model_utils.models import TimeStampedModel
from topics.managers import TopicManager, VotingManager
# For license information, see LICENSE.txt
#
# ID: models.py [] benjamin@bengfort.com $

"""
Topic modeling for data survey analysis
"""

##########################################################################
## Imports
##########################################################################


##########################################################################
## Topic Models
##########################################################################

class Topic(TimeStampedModel):
    """
    Stores a topic, basically a string like a tag and manages it.
    """

    # Topic fields
    title     = models.CharField(max_length=128)
    slug      = AutoSlugField(populate_from='title', unique=True)
    link      = models.URLField(null=True, blank=True, default=None)
    refers_to = models.ForeignKey('self', related_name='references', null=True, blank=True, default=None)
    is_canonical = models.BooleanField(default=True)

    # Custom topic manager

# Path: topics/managers.py
# class TopicManager(models.Manager):
#     """
#     Helper functions for topic management
#     """
# 
#     def from_title(self, title):
#         """
#         A get or create from a slug function.
#         """
#         # Titlecase the topic
#         title = titlecase(title)
# 
#         # If the slug exists, return the object
#         query = self.filter(slug=slugify(title))
#         if query.exists():
#             return query.first()
# 
#         # Otherwise create the topic with the title
#         return self.create(title=title)
# 
#     def from_string(self, text):
#         """
#         Basically a get or create for a batch insert of topics separated by
#         newlines as written into a text field or similar.
#         """
#         for line in frozenset(text.splitlines()):
#             if line:
#                 yield self.from_title(line)
# 
#     def with_votes(self):
#         """
#         Annotates the topic model with a vote total.
#         """
#         return self.raw(VOTE_AGGREGATION_SQL)
# 
#     def mean_weight(self):
#         """
#         Returns the mean weight aggregation of all topics.
#         """
#         count = float(self.filter(is_canonical=True).count())
#         total = sum(float(topic.vote_total) for topic in self.with_votes())
#         return total / count
# 
# class VotingManager(models.Manager):
#     """
#     Helper functions for vote management
#     """
# 
#     def upvotes(self):
#         """
#         Returns the set of all up votes for a particular query.
#         """
#         return self.filter(vote=self.model.BALLOT.upvote)
# 
#     def downvotes(self):
#         """
#         Returns the set of all down votes for a particular query.
#         """
#         return self.filter(vote=self.model.BALLOT.downvote)
# 
#     def total(self):
#         """
#         Returns the sum of all up and down votes for a particular query.
#         """
#         return self.aggregate(
#             total=models.Sum('vote')
#         )
# 
#     def responses(self):
#         """
#         Returns an aggregation of IP address and the count per IP.
#         """
#         query = self.values("ipaddr")
#         query = query.annotate(responses=models.Count('ipaddr'))
#         query = query.order_by('ipaddr')
#         return query
# 
#     def response_stats(self):
#         """
#         Returns a list of statistics from the responses histogram.
#         """
#         return self.responses().aggregate(
#             num=models.Sum('responses'),
#             avg=models.Avg('responses'),
#             min=models.Min('responses'),
#             max=models.Max('responses'),
#         )
# 
#     def num_responses(self):
#         """
#         Returns the number of distinct IP addresses
#         """
#         return self.aggregate(r=models.Count('ipaddr', distinct=True))['r']
# 
#     def response_timeseries(self):
#         """
#         Returns a list of timeseries from the responses.
#         """
#         qss = QuerySetStats(self.all(), 'created')
#         return qss.time_series(timezone.now() - timedelta(days=7), timezone.now())


# Path: topics/models.py
from django.db import models
from model_utils import Choices
from autoslug import AutoSlugField
from model_utils.models import TimeStampedModel
from topics.managers import TopicManager, VotingManager

# For license information, see LICENSE.txt
#
# ID: models.py [] benjamin@bengfort.com $

"""
Topic modeling for data survey analysis
"""

##########################################################################
## Imports
##########################################################################


##########################################################################
## Topic Models
##########################################################################

class Topic(TimeStampedModel):
    """
    Stores a topic, basically a string like a tag and manages it.
    """

    # Topic fields
    title     = models.CharField(max_length=128)
    slug      = AutoSlugField(populate_from='title', unique=True)
    link      = models.URLField(null=True, blank=True, default=None)
    refers_to = models.ForeignKey('self', related_name='references', null=True, blank=True, default=None)
    is_canonical = models.BooleanField(default=True)

    # Custom topic manager

    objects = TopicManager()