PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/johnny/tests/testapp/models.py

https://bitbucket.org/jmoiron/johnny-cache/
Python | 202 lines | 156 code | 34 blank | 12 comment | 2 complexity | 12539d33c8d14c0d8b23ccf61f09d2cc MD5 | raw file
  1#!/usr/bin/env python
  2# -*- coding: utf-8 -*-
  3
  4"""Test models for Johnny-Cache"""
  5
  6from django.db import models
  7from django.db.models import permalink
  8from django.conf import settings
  9from django.utils.translation import ugettext_lazy as _
 10
 11#from basic.people.models import Person
 12
 13class Issue24Model(models.Model):
 14    one = models.PositiveIntegerField()
 15    two = models.PositiveIntegerField()
 16
 17class User(models.Model):
 18    """User model."""
 19    first_name = models.CharField('first name', blank=True, max_length=128)
 20    last_name = models.CharField('last name', blank=True, max_length=128)
 21    username = models.CharField('username', blank=True, max_length=128)
 22
 23    def __repr__(self):
 24        return '<User: %s %s>' % (self.first_name, self.last_name)
 25
 26class PersonType(models.Model):
 27    """Person type model."""
 28    title = models.CharField(_('title'), max_length=100)
 29    slug = models.SlugField(_('slug'), unique=True)
 30
 31    class Meta:
 32        verbose_name = _('person type')
 33        verbose_name_plural = _('person types')
 34        ordering = ('title',)
 35
 36    def __unicode__(self):
 37        return '%s' % self.title
 38
 39    @permalink
 40    def get_absolute_url(self):
 41        return ('person_type_detail', None, {'slug': self.slug})
 42
 43# some details left out of the Person model, in order to avoid a requirement
 44# on python-dateutil
 45
 46class Person(models.Model):
 47    """Person model."""
 48    GENDER_CHOICES = (
 49        (1, 'Male'),
 50        (2, 'Female'),
 51    )
 52    user = models.ForeignKey(User, blank=True, null=True)
 53    first_name = models.CharField(_('first name'), blank=True, max_length=100)
 54    middle_name = models.CharField(_('middle name'), blank=True, max_length=100)
 55    last_name = models.CharField(_('last name'), blank=True, max_length=100)
 56    slug = models.SlugField(_('slug'), unique=True)
 57    gender = models.PositiveSmallIntegerField(_('gender'), choices=GENDER_CHOICES, blank=True, null=True)
 58    mugshot = models.FileField(_('mugshot'), upload_to='mugshots', blank=True)
 59    mugshot_credit = models.CharField(_('mugshot credit'), blank=True, max_length=200)
 60    birth_date = models.DateField(_('birth date'), blank=True, null=True)
 61    person_types = models.ManyToManyField(PersonType, blank=True)
 62    website = models.URLField(_('website'), blank=True, verify_exists=True)
 63
 64    class Meta:
 65        verbose_name = _('person')
 66        verbose_name_plural = _('people')
 67        ordering = ('last_name', 'first_name',)
 68
 69    def __unicode__(self):
 70        return u'%s' % self.full_name
 71
 72    @property
 73    def full_name(self):
 74        return u'%s %s' % (self.first_name, self.last_name)
 75
 76    @permalink
 77    def get_absolute_url(self):
 78        return ('person_detail', None, {'slug': self.slug})
 79
 80
 81class Genre(models.Model):
 82    """Genre model"""
 83    title = models.CharField(max_length=100)
 84    slug = models.SlugField(unique=True)
 85
 86    class Meta:
 87        ordering = ('title',)
 88
 89    def __unicode__(self):
 90        return '%s' % self.title
 91
 92    @permalink
 93    def get_absolute_url(self):
 94        return ('book_genre_detail', None, { 'slug': self.slug })
 95
 96
 97class Publisher(models.Model):
 98    """Publisher"""
 99    title = models.CharField(max_length=100)
100    prefix = models.CharField(max_length=20, blank=True)
101    slug = models.SlugField(unique=True)
102    website = models.URLField(blank=True, verify_exists=False)
103
104    class Meta:
105        ordering = ('title',)
106
107    def __unicode__(self):
108        return '%s' % self.full_title
109
110    @property
111    def full_title(self):
112        return '%s %s' % (self.prefix, self.title)
113
114    @permalink
115    def get_absolute_url(self):
116        return ('book_publisher_detail', None, { 'slug':self.slug })
117
118
119class Book(models.Model):
120    """Listing of books"""
121    title = models.CharField(max_length=255)
122    prefix = models.CharField(max_length=20, blank=True)
123    subtitle = models.CharField(blank=True, max_length=255)
124    slug = models.SlugField(unique=True)
125    authors = models.ManyToManyField(Person, limit_choices_to={'person_types__slug__exact': 'author'}, related_name='books')
126    isbn = models.CharField(max_length=14, blank=True)
127    pages = models.PositiveSmallIntegerField(blank=True, null=True, default=0)
128    publisher = models.ForeignKey(Publisher, blank=True, null=True)
129    published = models.DateField(blank=True, null=True)
130    cover = models.FileField(upload_to='books', blank=True)
131    description = models.TextField(blank=True)
132    genre = models.ManyToManyField(Genre, blank=True)
133    created = models.DateTimeField(auto_now_add=True)
134    modified = models.DateTimeField(auto_now=True)
135
136    class Meta:
137        ordering = ('title',)
138
139    def __unicode__(self):
140        return '%s' % self.full_title
141
142    @property
143    def full_title(self):
144        if self.prefix:
145            return '%s %s' % (self.prefix, self.title)
146        else:
147            return '%s' % self.title
148
149    @permalink
150    def get_absolute_url(self):
151        return ('book_detail', None, { 'slug': self.slug })
152
153    @property
154    def amazon_url(self):
155        if self.isbn:
156            try:
157                return 'http://www.amazon.com/dp/%s/?%s' % (self.isbn, settings.AMAZON_AFFILIATE_EXTENTION)
158            except:
159                return 'http://www.amazon.com/dp/%s/' % self.isbn
160        return ''
161
162
163class Highlight(models.Model):
164    """Highlights from books"""
165    user = models.ForeignKey(User)
166    book = models.ForeignKey(Book)
167    highlight = models.TextField()
168    page = models.CharField(blank=True, max_length=20)
169    created = models.DateTimeField(auto_now_add=True)
170    modified = models.DateTimeField(auto_now=True)
171
172    def __unicode__(self):
173        return '%s' % self.highlight
174
175    @permalink
176    def get_absolute_url(self):
177        return ('book_detail', None, { 'slug': self.book.slug })
178
179class Page(models.Model):
180    """Page model"""
181    user = models.ForeignKey(User)
182    book = models.ForeignKey(Book)
183    current_page = models.PositiveSmallIntegerField(default=0)
184    created = models.DateTimeField(auto_now_add=True)
185
186    class Meta:
187        ordering = ('-created',)
188
189    def __unicode__(self):
190        return '%s' % self.current_page
191
192class Milk(models.Model):
193    """A meaningless model designed to test unicode ability.  This might screw
194    up databases that can't handle unicode table/column names."""
195    name = models.CharField(blank=True, max_length=20, db_column=u'名前')
196    chocolate = models.BooleanField(blank=True, db_column=u'チョコレート')
197
198    class Meta:
199        db_table = u'ミルク'
200
201
202