PageRenderTime 203ms CodeModel.GetById 100ms app.highlight 9ms RepoModel.GetById 92ms app.codeStats 0ms

/tests/modeltests/fixtures/models.py

https://code.google.com/p/mango-py/
Python | 100 lines | 95 code | 0 blank | 5 comment | 1 complexity | 62c7d04fd5b433db62af63676d6dfc0a MD5 | raw file
  1"""
  237. Fixtures.
  3
  4Fixtures are a way of loading data into the database in bulk. Fixure data
  5can be stored in any serializable format (including JSON and XML). Fixtures
  6are identified by name, and are stored in either a directory named 'fixtures'
  7in the application directory, or in one of the directories named in the
  8``FIXTURE_DIRS`` setting.
  9"""
 10
 11from django.contrib.auth.models import Permission
 12from django.contrib.contenttypes import generic
 13from django.contrib.contenttypes.models import ContentType
 14from django.db import models, DEFAULT_DB_ALIAS
 15from django.conf import settings
 16
 17
 18class Category(models.Model):
 19    title = models.CharField(max_length=100)
 20    description = models.TextField()
 21
 22    def __unicode__(self):
 23        return self.title
 24
 25    class Meta:
 26        ordering = ('title',)
 27
 28class Article(models.Model):
 29    headline = models.CharField(max_length=100, default='Default headline')
 30    pub_date = models.DateTimeField()
 31
 32    def __unicode__(self):
 33        return self.headline
 34
 35    class Meta:
 36        ordering = ('-pub_date', 'headline')
 37
 38class Blog(models.Model):
 39    name = models.CharField(max_length=100)
 40    featured = models.ForeignKey(Article, related_name='fixtures_featured_set')
 41    articles = models.ManyToManyField(Article, blank=True,
 42                                      related_name='fixtures_articles_set')
 43
 44    def __unicode__(self):
 45        return self.name
 46
 47
 48class Tag(models.Model):
 49    name = models.CharField(max_length=100)
 50    tagged_type = models.ForeignKey(ContentType, related_name="fixtures_tag_set")
 51    tagged_id = models.PositiveIntegerField(default=0)
 52    tagged = generic.GenericForeignKey(ct_field='tagged_type',
 53                                       fk_field='tagged_id')
 54
 55    def __unicode__(self):
 56        return '<%s: %s> tagged "%s"' % (self.tagged.__class__.__name__,
 57                                         self.tagged, self.name)
 58
 59class PersonManager(models.Manager):
 60    def get_by_natural_key(self, name):
 61        return self.get(name=name)
 62
 63class Person(models.Model):
 64    objects = PersonManager()
 65    name = models.CharField(max_length=100)
 66    def __unicode__(self):
 67        return self.name
 68
 69    class Meta:
 70        ordering = ('name',)
 71
 72    def natural_key(self):
 73        return (self.name,)
 74
 75class SpyManager(PersonManager):
 76    def get_query_set(self):
 77        return super(SpyManager, self).get_query_set().filter(cover_blown=False)
 78
 79class Spy(Person):
 80    objects = SpyManager()
 81    cover_blown = models.BooleanField(default=False)
 82
 83class Visa(models.Model):
 84    person = models.ForeignKey(Person)
 85    permissions = models.ManyToManyField(Permission, blank=True)
 86
 87    def __unicode__(self):
 88        return '%s %s' % (self.person.name,
 89                          ', '.join(p.name for p in self.permissions.all()))
 90
 91class Book(models.Model):
 92    name = models.CharField(max_length=100)
 93    authors = models.ManyToManyField(Person)
 94
 95    def __unicode__(self):
 96        return '%s by %s' % (self.name,
 97                          ' and '.join(a.name for a in self.authors.all()))
 98
 99    class Meta:
100        ordering = ('name',)