# Path: topics/models.py
# 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()
# 
#     # Topic meta class
#     class Meta:
#         db_table = 'topics'
#         ordering = ('title', )
# 
#     def __unicode__(self):
#         return self.title
# 
#     def vote_total(self):
#         """
#         Accumulates the votes via aggregation
#         """
#         votes = self.votes.aggregate(
#             total=models.Sum('vote')
#         )['total']
# 
#         if self.is_canonical:
#             for ref in self.references.all():
#                 votes += ref.vote_total()
# 
#         return votes
# 
# class Vote(TimeStampedModel):
#     """
#     Simple voting model that stores an up or down vote for a particular topic
#     associated with a particular IP address (and time of day).
#     """
# 
#     DATEFMT  = "%a %b %d, %Y at %H:%M"
#     BALLOT   = Choices((-1, 'downvote', 'downvote'), (1, 'upvote', 'upvote'), (0, 'novote', 'novote'))
# 
#     # Vote fields
#     vote    = models.SmallIntegerField(choices=BALLOT, default=BALLOT.upvote)
#     topic   = models.ForeignKey(Topic, related_name='votes')
#     ipaddr  = models.GenericIPAddressField()
# 
#     # Custom voting manager
#     objects = VotingManager()
# 
#     # Vote meta class
#     class Meta:
#         db_table = 'voting'
#         ordering = ('-created',)
# 
#     def __unicode__(self):
#         action = {
#             -1: "-1",
#              0: "--",
#              1: "+1",
#         }[self.vote]
# 
#         return "{} for \"{}\" ({} on {})".format(
#             action, self.topic, self.ipaddr, self.modified.strftime(self.DATEFMT)
#         )


from django.contrib import admin
from topics.models import Topic, Vote
from django.db.models import Sum
##########################################################################
## Imports
##########################################################################


##########################################################################
## Admin Views
##########################################################################

class VoteInline(admin.TabularInline):

    model = Vote
    extra = 0
    readonly_fields = ('created', )


class TopicAdmin(admin.ModelAdmin):

    inlines = [
        VoteInline,
    ]
    readonly_fields = ('slug', 'created', 'vote_total')
    list_display = ["title", "vote_total"]


##########################################################################
## Model Registration
##########################################################################

admin.site.register(Topic, TopicAdmin)

# Path: topics/models.py
# 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()
# 
#     # Topic meta class
#     class Meta:
#         db_table = 'topics'
#         ordering = ('title', )
# 
#     def __unicode__(self):
#         return self.title
# 
#     def vote_total(self):
#         """
#         Accumulates the votes via aggregation
#         """
#         votes = self.votes.aggregate(
#             total=models.Sum('vote')
#         )['total']
# 
#         if self.is_canonical:
#             for ref in self.references.all():
#                 votes += ref.vote_total()
# 
#         return votes
# 
# class Vote(TimeStampedModel):
#     """
#     Simple voting model that stores an up or down vote for a particular topic
#     associated with a particular IP address (and time of day).
#     """
# 
#     DATEFMT  = "%a %b %d, %Y at %H:%M"
#     BALLOT   = Choices((-1, 'downvote', 'downvote'), (1, 'upvote', 'upvote'), (0, 'novote', 'novote'))
# 
#     # Vote fields
#     vote    = models.SmallIntegerField(choices=BALLOT, default=BALLOT.upvote)
#     topic   = models.ForeignKey(Topic, related_name='votes')
#     ipaddr  = models.GenericIPAddressField()
# 
#     # Custom voting manager
#     objects = VotingManager()
# 
#     # Vote meta class
#     class Meta:
#         db_table = 'voting'
#         ordering = ('-created',)
# 
#     def __unicode__(self):
#         action = {
#             -1: "-1",
#              0: "--",
#              1: "+1",
#         }[self.vote]
# 
#         return "{} for \"{}\" ({} on {})".format(
#             action, self.topic, self.ipaddr, self.modified.strftime(self.DATEFMT)
#         )


# Path: topics/admin.py
from django.contrib import admin
from topics.models import Topic, Vote
from django.db.models import Sum

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


##########################################################################
## Admin Views
##########################################################################

class VoteInline(admin.TabularInline):

    model = Vote
    extra = 0
    readonly_fields = ('created', )


class TopicAdmin(admin.ModelAdmin):

    inlines = [
        VoteInline,
    ]
    readonly_fields = ('slug', 'created', 'vote_total')
    list_display = ["title", "vote_total"]


##########################################################################
## Model Registration
##########################################################################

admin.site.register(Topic, TopicAdmin)

admin.site.register(Vote)