Created
December 19, 2013 23:27
-
-
Save infernoboy/8048011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from datetime import datetime | |
from django.db.models.query import QuerySet | |
from django.contrib.auth.models import User | |
from django.db.models import Count | |
from orca.settings import STATIC_URL | |
from random import randint | |
from autoslug import AutoSlugField | |
BASE_URL = '/comic/' | |
class GeneralQuerySet(QuerySet): | |
def random(self): | |
count = self.count() | |
try: | |
index = randint(0, count - 1) | |
return self[index] | |
except: | |
return None | |
class GeneralManager(models.Manager): | |
def get_query_set(self): | |
return GeneralQuerySet(self.model) | |
class CommonInfo(models.Model): | |
created = models.DateTimeField(default=datetime.utcnow, editable=False) | |
class Meta: | |
abstract = True | |
class Book(CommonInfo): | |
name = models.CharField(max_length=100, verbose_name='book') | |
description = models.TextField(null=True) | |
author = models.ForeignKey(User, null=True, blank=True, editable=False) | |
slug = AutoSlugField(unique=True, populate_from='name') | |
chapters = models.ManyToManyField('Chapter', related_name='chapters', blank=True, editable=True) | |
objects = GeneralManager() | |
def __unicode__(self): | |
return self.name | |
def get_absolute_url(self): | |
return '%s%s/' % (BASE_URL, self.slug,) | |
class Chapter(CommonInfo): | |
name = models.CharField(max_length=100, verbose_name='chapter') | |
book = models.ForeignKey(Book) | |
slug = AutoSlugField(populate_from='name', unique_with='book') | |
part = models.ForeignKey('Part', blank=True, null=True) | |
objects = GeneralManager() | |
def __unicode__(self): | |
return '%s - %s - %s' % (self.book.name, self.part.name if self.part else '(no part)', self.name,) | |
def get_absolute_url(self, output_type=None): | |
return '%s%s/%s/' % (BASE_URL, self.book.slug, self.slug,) | |
class Part(CommonInfo): | |
name = models.CharField(max_length=100, verbose_name='part', unique=True) | |
slug = AutoSlugField(populate_from='name') | |
def __unicode__(self): | |
return self.name | |
class Comic(CommonInfo): | |
name = models.CharField(max_length=100, verbose_name='comic') | |
description = models.TextField(null=True) | |
file_name = models.CharField(max_length=40, blank=True) | |
chapter = models.ForeignKey(Chapter) | |
comments = models.ManyToManyField('Comment', related_name='comments', blank=True, null=True, editable=False) | |
slug = AutoSlugField(unique_with=('chapter', 'chapter__book',), populate_from='name') | |
objects = GeneralManager() | |
def __unicode__(self): | |
return self.name | |
def image_tag(self): | |
return '<img src="/media/orca/comic/%d/%s" style="max-width:300px;max-height:300px;" />' % (self.chapter.book.author.id, self.file_name,) | |
def get_absolute_url(self, output_type='html'): | |
return '%s%s/%s/%s.%s' % (BASE_URL, self.chapter.book.slug, self.chapter.slug, self.slug, output_type,) | |
def get_image_url(self): | |
return STATIC_URL + 'orca/comic/%d/%s' % (self.chapter.book.author.id, self.file_name,) | |
image_tag.short_description = 'Comic' | |
image_tag.allow_tags = True | |
class CommentChildrenManager(models.Manager): | |
def top(self): | |
return self.get_query_set().filter(parent=None) | |
class Comment(CommonInfo): | |
author_name = models.CharField(max_length=100) | |
email = models.EmailField(blank=True, null=True) | |
comment = models.TextField() | |
comic = models.ForeignKey(Comic) | |
parent = models.ForeignKey('self', related_name='op', blank=True, null=True) | |
children = models.ManyToManyField('self', related_name='childs', blank=True, null=True, symmetrical=False) | |
objects = CommentChildrenManager() | |
def __unicode__(self): | |
return '%s: %s: %s' % (self.comic, self.author_name, self.comment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment