Source

ytmanager / mcyoutube / models.py

Full commit
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse

from google.appengine.api.labs import taskqueue

import gdata.youtube, gdata.alt.appengine
from gdata.youtube.service import YOUTUBE_USER_FEED_URI, YOUTUBE_PLAYLIST_FEED_URI, YouTubeService

from mcutils.middlewares import get_request


def get_current_user():
    return YoutubeUser.objects.get(username=get_request().session.get('username'))


class YoutubeUser(models.Model):
    username = models.CharField(primary_key=True, max_length=100)

    def __unicode__(self):
        return self.username


class PlaylistUserManager(models.Manager):
    def get_query_set(self):
        username = get_request().session.get('username')
        return super(PlaylistUserManager, self).get_query_set().filter(user=username)


class Playlist(models.Model):
    id = models.CharField(primary_key=True, max_length=20)
    user = models.ForeignKey(YoutubeUser)
    title = models.CharField(max_length=100)
    description = models.TextField()

    entries_count = models.PositiveSmallIntegerField()
    is_ready = models.BooleanField()

    objects = models.Manager()
    user_objects = PlaylistUserManager()

    def is_favorites(self):
        return self.id == 'favorites'

    def __unicode__(self):
        return self.id


class PlaylistEntryManager(models.Manager):
    def get_query_set(self):
        return models.query.QuerySet(self.model, using=self._db).order_by('position')


class PlaylistEntry(models.Model):
    id = models.CharField(primary_key=True, max_length=20, db_index=True)
    video_id = models.CharField(max_length=20, db_index=True)
    title = models.CharField(max_length=100)
    description = models.TextField()
    playlist = models.ForeignKey(Playlist)

    position = models.PositiveSmallIntegerField(null=True, db_index=True)

    rating = models.DecimalField(null=True, max_digits=9, decimal_places=7)
    rating_count = models.PositiveIntegerField(null=True)
    rating_max = models.PositiveSmallIntegerField(null=True)
    rating_min = models.PositiveSmallIntegerField(null=True)

    favorite_count = models.PositiveIntegerField(null=True)
    comments_count = models.PositiveIntegerField(null=True)
    views_count = models.PositiveIntegerField(null=True)

    author_username = models.CharField(max_length=128)

    objects = PlaylistEntryManager()

    def is_mine(self):
        if self.playlist.user_id == get_request().session['username']:
            return True
        else:
            return False

    def __unicode__(self):
        return self.id