PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/validation/models.py

https://code.google.com/p/mango-py/
Python | 81 lines | 65 code | 16 blank | 0 comment | 3 complexity | e10b3fd71c8ba11cefd0791fb0987673 MD5 | raw file
 1from datetime import datetime
 2from django.core.exceptions import ValidationError
 3from django.db import models
 4from django.test import TestCase
 5
 6
 7def validate_answer_to_universe(value):
 8    if value != 42:
 9        raise ValidationError('This is not the answer to life, universe and everything!', code='not42')
10
11class ModelToValidate(models.Model):
12    name = models.CharField(max_length=100)
13    created = models.DateTimeField(default=datetime.now)
14    number = models.IntegerField(db_column='number_val')
15    parent = models.ForeignKey('self', blank=True, null=True, limit_choices_to={'number': 10})
16    email = models.EmailField(blank=True)
17    url = models.URLField(blank=True)
18    url_verify = models.URLField(blank=True, verify_exists=True)
19    f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe])
20
21    def clean(self):
22        super(ModelToValidate, self).clean()
23        if self.number == 11:
24            raise ValidationError('Invalid number supplied!')
25
26class UniqueFieldsModel(models.Model):
27    unique_charfield = models.CharField(max_length=100, unique=True)
28    unique_integerfield = models.IntegerField(unique=True)
29    non_unique_field = models.IntegerField()
30
31class CustomPKModel(models.Model):
32    my_pk_field = models.CharField(max_length=100, primary_key=True)
33
34class UniqueTogetherModel(models.Model):
35    cfield = models.CharField(max_length=100)
36    ifield = models.IntegerField()
37    efield = models.EmailField()
38
39    class Meta:
40        unique_together = (('ifield', 'cfield',), ['ifield', 'efield'])
41
42class UniqueForDateModel(models.Model):
43    start_date = models.DateField()
44    end_date = models.DateTimeField()
45    count = models.IntegerField(unique_for_date="start_date", unique_for_year="end_date")
46    order = models.IntegerField(unique_for_month="end_date")
47    name = models.CharField(max_length=100)
48
49class CustomMessagesModel(models.Model):
50    other  = models.IntegerField(blank=True, null=True)
51    number = models.IntegerField(db_column='number_val',
52        error_messages={'null': 'NULL', 'not42': 'AAARGH', 'not_equal': '%s != me'},
53        validators=[validate_answer_to_universe]
54    )
55
56class Author(models.Model):
57    name = models.CharField(max_length=100)
58
59class Article(models.Model):
60    title = models.CharField(max_length=100)
61    author = models.ForeignKey(Author)
62    pub_date = models.DateTimeField(blank=True)
63
64    def clean(self):
65        if self.pub_date is None:
66            self.pub_date = datetime.now()
67
68class Post(models.Model):
69    title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
70    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
71    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
72    posted = models.DateField()
73
74    def __unicode__(self):
75        return self.name
76
77class FlexibleDatePost(models.Model):
78    title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
79    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
80    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
81    posted = models.DateField(blank=True, null=True)