a1c5fbdac1f2dc916f9e557898d332b65aec024d
[copyspeak.git] / src / words / models.py
1 from django.core.urlresolvers import reverse
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
4
5
6 ALIGNMENTS = (
7     (u'lawful', u'Lawful'),
8     (u'neutral', u'Neutral'),
9     (u'chaotic', u'Chaotic'),
10 )
11
12
13 class Word(models.Model):
14     word = models.CharField(_('word'), max_length=255, db_index=True)
15     slug = models.SlugField(_('slug'), unique=True)
16     alignment = models.CharField(_('alignment'), max_length=64, choices=ALIGNMENTS)
17     examples = models.TextField(_('examples'))
18     usage = models.TextField(_('usage'))
19     recommendations = models.TextField(_('recommendations'))
20
21     class Meta:
22         verbose_name = _('word')
23         verbose_name_plural = _('words')
24         ordering = ('word',)
25
26     def __unicode__(self):
27         return self.word
28
29     def get_absolute_url(self):
30         return reverse('words_word', args=[self.slug])
31
32     def get_next(self):
33         try:
34             return Word.objects.filter(alignment=self.alignment, word__gt=self.word)[0]
35         except IndexError:
36             next_alignment = ALIGNMENTS[([a[0] for a in ALIGNMENTS].index(self.alignment) + 1) % len(ALIGNMENTS)][0]
37             return Word.objects.filter(alignment=next_alignment)[0]
38
39     def get_previous(self):
40         words = Word.objects.order_by('-word')
41         try:
42             return words.filter(alignment=self.alignment, word__lt=self.word)[0]
43         except IndexError:
44             prev_alignment = ALIGNMENTS[([a[0] for a in ALIGNMENTS].index(self.alignment) - 1) % len(ALIGNMENTS)][0]
45             return words.filter(alignment=prev_alignment)[0]