# 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: tagging/models.py
# class Tag(TimeStampedModel):
# 
#     text        = models.CharField(max_length=100, unique=True)
#     slug        = AutoSlugField(populate_from='text', slugify=slugify, unique=True)
#     description = models.CharField(max_length=255, **nullable)
#     creator     = models.ForeignKey('auth.User', related_name='tags')
#     is_synonym  = models.BooleanField(default=False)
#     head_word   = models.ForeignKey('self', related_name='synonyms', **nullable)
# 
#     ## Set custom tag manager
#     objects     = TagManager()
# 
#     class Meta:
#         db_table = "tags"
#         get_latest_by = "created"
# 
#     def __str__(self):
#         return self.text
# 
# Path: users/mixins.py
# class LoginRequired(object):
#     """
#     Ensures that user must be authenticated in order to access view.
#     """
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequired, self).dispatch(*args, **kwargs)
# 
# Path: users/permissions.py
# class IsAuthorOrReadOnly(permissions.BasePermission):
#     """
#     Object-level permission to allow only owners of an object to edit.
#     Note, this permission assumes there is an `author` attribute on the
#     object that maps to an `auth.User` instance.
#     """
# 
#     def has_object_permission(self, request, view, obj):
#         if request.method in permissions.SAFE_METHODS:
#             return True
# 
#         return obj.author == request.user
# 
# Path: tagging/serializers.py
# class CSVTagSerializer(serializers.Serializer):
#     """
#     Deserializes a JSON request containing CSV tagged data.
#     """
# 
#     @staticmethod
#     def serialize_question(question):
#         """
#         Returns a dictionary of serialized data from the question object.
#         """
#         return {
#             'tags': [tag.text for tag in question.tags.all()],
#             'csv_tags': question.tags.all().to_csv(),
#         }
# 
# 
#     ## CSV Fields
#     csv_tags = CSVField(validators=[MaxLength(limit=5, things="tags")])
#     tags     = serializers.ListField(child=serializers.CharField(), read_only=True)


from fugato.models import *
from voting.models import Vote
from tagging.models import Tag
from fugato.serializers import *
from voting.serializers import *
from rest_framework import viewsets
from users.mixins import LoginRequired
from users.permissions import IsAuthorOrReadOnly
from tagging.serializers import CSVTagSerializer
from django.views.generic import DetailView, ListView
from rest_framework import status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import detail_route, list_route
    Authenticated web application view that serves all context and content
    to kick off the Backbone front-end application.
    """

    model = Question
    template_name = "fugato/list.html"
    context_object_name = 'question_list'
    paginate_by = 20

    def get_queryset(self):
        """
        Performs filtering on the queryset based on the query arguments.
        """
        queryset = super(QuestionList, self).get_queryset()

        # Get possible tag and sort options from the query string
        self.search_by = self.request.GET.get('search', "").strip()
        self.sorted_by = self.request.GET.get('sort', 'recent').lower()
        self.tagged_by = self.request.GET.get('tag', None)

        # Filter the queryset by the search term
        if self.search_by:
            queryset = queryset.search(self.search_by)

        # Filter the queryset by the tag object
        if self.tagged_by:
            # Convert the query string into a Tag object
            try:
                self.tagged_by = Tag.objects.get(slug=self.tagged_by)
                queryset = self.tagged_by.questions.all()

# 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: tagging/models.py
# class Tag(TimeStampedModel):
# 
#     text        = models.CharField(max_length=100, unique=True)
#     slug        = AutoSlugField(populate_from='text', slugify=slugify, unique=True)
#     description = models.CharField(max_length=255, **nullable)
#     creator     = models.ForeignKey('auth.User', related_name='tags')
#     is_synonym  = models.BooleanField(default=False)
#     head_word   = models.ForeignKey('self', related_name='synonyms', **nullable)
# 
#     ## Set custom tag manager
#     objects     = TagManager()
# 
#     class Meta:
#         db_table = "tags"
#         get_latest_by = "created"
# 
#     def __str__(self):
#         return self.text
# 
# Path: users/mixins.py
# class LoginRequired(object):
#     """
#     Ensures that user must be authenticated in order to access view.
#     """
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequired, self).dispatch(*args, **kwargs)
# 
# Path: users/permissions.py
# class IsAuthorOrReadOnly(permissions.BasePermission):
#     """
#     Object-level permission to allow only owners of an object to edit.
#     Note, this permission assumes there is an `author` attribute on the
#     object that maps to an `auth.User` instance.
#     """
# 
#     def has_object_permission(self, request, view, obj):
#         if request.method in permissions.SAFE_METHODS:
#             return True
# 
#         return obj.author == request.user
# 
# Path: tagging/serializers.py
# class CSVTagSerializer(serializers.Serializer):
#     """
#     Deserializes a JSON request containing CSV tagged data.
#     """
# 
#     @staticmethod
#     def serialize_question(question):
#         """
#         Returns a dictionary of serialized data from the question object.
#         """
#         return {
#             'tags': [tag.text for tag in question.tags.all()],
#             'csv_tags': question.tags.all().to_csv(),
#         }
# 
# 
#     ## CSV Fields
#     csv_tags = CSVField(validators=[MaxLength(limit=5, things="tags")])
#     tags     = serializers.ListField(child=serializers.CharField(), read_only=True)


# Path: fugato/views.py
from fugato.models import *
from voting.models import Vote
from tagging.models import Tag
from fugato.serializers import *
from voting.serializers import *
from rest_framework import viewsets
from users.mixins import LoginRequired
from users.permissions import IsAuthorOrReadOnly
from tagging.serializers import CSVTagSerializer
from django.views.generic import DetailView, ListView
from rest_framework import status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import detail_route, list_route

    Authenticated web application view that serves all context and content
    to kick off the Backbone front-end application.
    """

    model = Question
    template_name = "fugato/list.html"
    context_object_name = 'question_list'
    paginate_by = 20

    def get_queryset(self):
        """
        Performs filtering on the queryset based on the query arguments.
        """
        queryset = super(QuestionList, self).get_queryset()

        # Get possible tag and sort options from the query string
        self.search_by = self.request.GET.get('search', "").strip()
        self.sorted_by = self.request.GET.get('sort', 'recent').lower()
        self.tagged_by = self.request.GET.get('tag', None)

        # Filter the queryset by the search term
        if self.search_by:
            queryset = queryset.search(self.search_by)

        # Filter the queryset by the tag object
        if self.tagged_by:
            # Convert the query string into a Tag object
            try:
                self.tagged_by = Tag.objects.get(slug=self.tagged_by)
                queryset = self.tagged_by.questions.all()

            except Tag.DoesNotExist: