Source

django-feature-request / feature_request / models.py

from django.db import models
from django.contrib.auth.models import User

from feature_request.choices import STATUS_CHOICES, DECLINE_CHOICES
from feature_request.constants import SUBMITTED, EDITING, REVIEWING, APPROVED, DENIED
from django.contrib import messages

#===============================================================================
class FeatureRequestManager(models.Manager):

    #---------------------------------------------------------------------------
    def get_my_completed_features(self, user):
        return self.get_my_features(user).filter(is_complete=True)

    #---------------------------------------------------------------------------
    def get_my_submitted_features(self, user):
        return self.get_my_features(user).filter(status__in=[SUBMITTED,REVIEWING,APPROVED,DENIED])

    #---------------------------------------------------------------------------
    def get_my_unsubmitted_features(self, user):
        return self.get_my_features(user).filter(status=EDITING)

    #---------------------------------------------------------------------------
    def get_my_features(self, user):
        return self.filter(active=True, member=user).order_by("is_complete")

    #---------------------------------------------------------------------------
    def get_features(self):
        return self.filter(active=True).order_by("is_complete")

    #---------------------------------------------------------------------------
    def get_users_feature_request(self, feature_id, user, value=None):

        try:
            value=FeatureRequest.objects.get(id=feature_id, member=user, active=True)
        except:
            pass

        return value

    #---------------------------------------------------------------------------
    def delete_request(self, request, feature_request):

        if not feature_request:
            log.error("Empty feature request passed to delete_request!")
            return

        feature_request.active=False
        feature_request.save()
        messages.add_message(request, messages.ERROR, 'Feature request "%s" has been removed.' % feature_request.request_title)

    #---------------------------------------------------------------------------
    def submit_request(self, request, feature_request):

        if not feature_request:
            log.error("Empty feature request passed to submit_request!")
            return

        feature_request.status=SUBMITTED
        feature_request.save()
        messages.add_message(request, messages.ERROR, 'Feature request "%s" has been submitted.' % feature_request.request_title)

#===============================================================================
class FeatureRequest(models.Model):
    
    member = models.ForeignKey(User)
    create_date = models.DateTimeField(auto_now_add=True)
    last_update = models.DateTimeField(auto_now=True)
    request_title = models.CharField(max_length=50, blank=False)
    request = models.TextField(max_length=5000,blank=True)

    active = models.BooleanField(default=True, db_index=True)

    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default=EDITING)
    decline_type = models.CharField(max_length=10, choices=DECLINE_CHOICES, blank=True, null=True)
    decline_comments = models.TextField(max_length=100,blank=True, null=True)

    is_complete = models.BooleanField(default=False)
    complete_date = models.DateField(blank=True, null=True)
    complete_comments = models.TextField(max_length=100,blank=True, null=True)

    objects = FeatureRequestManager()

    # --------------------------------------------------------------------------
    def __unicode__(self):
        return u'%s' % self.id

    #---------------------------------------------------------------------------
    @property
    def is_editable(self, value=False):
        if self.status == EDITING:
            value=True
        print "Status -->%s<--" % self.status
        return value
    
    #---------------------------------------------------------------------------
    @property
    def score(self):
        from voting.models import Vote
        return Vote.objects.get_score(self).get('score', 0)