gnocchi-ask / gnocchi / ask / models.py

The default branch has multiple heads

from django.db import models
from django.core.urlresolvers import reverse

from taggit.managers import TaggableManager

from datetime import datetime

class VisibleManager(models.Manager):
    def visible(self):
        return self.get_query_set().filter(visible=True)

class Question(models.Model):
    user = models.ForeignKey('auth.User', related_name='questions_asked')
    question = models.CharField(max_length=1024)
    content = models.TextField()

    created = models.DateTimeField(default=datetime.now)
    accepted = models.ForeignKey('Response', null=True, blank=True,
        related_name='accepted_for')

    visible = models.BooleanField(default=True)

    tags = TaggableManager(blank=True)

    objects = VisibleManager()

    class Meta:
        ordering = ('-created',)
    def __unicode__(self):
        return self.question
    def get_absolute_url(self):
        return reverse('question-detail', kwargs={'question_id': self.pk})
    def votes(self):
        return Vote.objects.filter(response__question=self).count()

class ResponseManager(VisibleManager):
    def get_query_set(self):
        qset = super(ResponseManager, self).get_query_set()
        return qset.annotate(
            score=models.Sum('votes__weight')
        ).order_by('-score', 'created')

class Response(models.Model):
    user = models.ForeignKey('auth.User', related_name='question_responses')
    question = models.ForeignKey(Question, related_name='responses')
    created = models.DateTimeField(default=datetime.now)

    content = models.TextField()
    visible = models.BooleanField(default=True)
    objects = ResponseManager()

    class Meta:
        permissions = (
            ('can_vote', 'Can vote on responses'),
        )
    def vote_up_url(self):
        return reverse('question-vote', kwargs={
            'question_id': self.question.id,
            'response_id': self.id,
            'direction': 'up',
        })
    def vote_down_url(self):
        return reverse('question-vote', kwargs={
            'question_id': self.question.id,
            'response_id': self.id,
            'direction': 'down',
        })
    def get_accept_url(self):
        return reverse('response-accept', kwargs={
            'question_id': self.question.id,
            'response_id': self.id,
        })
    def score(self):
        return self.vote_set.aggregate(score=models.Sum('weight'))['score']

class Vote(models.Model):
    user = models.ForeignKey('auth.User', related_name='response_votes')
    response = models.ForeignKey(Response, related_name='votes')
    weight = models.IntegerField(default=1)

    class Meta:
        unique_together = (
            ('user', 'response'),
        )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.