Starting work on tag categories - creating tag categories via admin
[prawokultury.git] / questions / models.py
1 # -*- coding: utf-8
2 from datetime import datetime
3 from django.conf import settings
4 from django.contrib.sites.models import Site
5 from django.core.mail import EmailMultiAlternatives
6 from django.db import models
7 from django.template import loader, Context
8 from django.utils.translation import ugettext_lazy as _
9 from markupfield.fields import MarkupField
10 from taggit.models import TagBase, GenericTaggedItemBase
11 from taggit_autosuggest.managers import TaggableManager
12 from fnpdjango.utils.text.slughifi import slughifi
13
14
15 class TagCategory(models.Model):
16     name = models.CharField(verbose_name=_('Name'), unique = True, max_length = 100)
17     slug = models.SlugField(verbose_name=_('Slug'), unique = True, max_length = 100)
18
19     class Meta:
20         verbose_name = _("Tag Category")
21         verbose_name_plural = _("Tag Categries")
22         
23     def __unicode__(self):
24         return self.name
25     
26     
27 class Tag(TagBase):
28     def slugify(self, tag, i=None):
29         slug = slughifi(tag)
30         if i is not None:
31             slug += "_%d" % i
32         return slug
33
34     category = models.ForeignKey(TagCategory, blank = True, null = True, on_delete = models.SET_NULL)
35         
36     class Meta:
37         verbose_name = _("Tag")
38         verbose_name_plural = _("Tags")
39
40
41 class TagItem(GenericTaggedItemBase):
42     tag = models.ForeignKey(Tag, related_name="items")
43  
44
45 class Question(models.Model):
46     email = models.EmailField(_('contact e-mail'), null=True, blank=True)
47     question = models.TextField(_('question'), db_index=True)
48     created_at = models.DateTimeField(_('created at'), auto_now_add=True)
49     changed_at = models.DateTimeField(_('changed at'), auto_now=True)
50     approved = models.BooleanField(_('approved'), default=False)
51     edited_question = models.TextField(_('edited question'), db_index=True, null=True, blank=True,
52             help_text=_("Leave empty if question doesn't need editing."))
53     answer = MarkupField(_('answer'), markup_type='textile_pl', blank=True,
54             help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.'))
55     answered = models.BooleanField(_('answered'), db_index=True, default=False,
56             help_text=_('Check to send the answer to user.'))
57     answered_at = models.DateTimeField(_('answered at'), null=True, blank=True, db_index=True)
58     published = models.BooleanField(_('published'), db_index=True, default=False,
59         help_text=_('Check to display answered question on site.'))
60     published_at = models.DateTimeField(_('published at'), null=True, blank=True, db_index=True)
61
62     tags = TaggableManager(through=TagItem, blank=True)
63
64     class Meta:
65         ordering = ['-created_at']
66         verbose_name = _('question')
67         verbose_name_plural = _('questions')
68
69     def __unicode__(self):
70         return self.edited_question or self.question
71
72     @models.permalink
73     def get_absolute_url(self):
74         return ('questions_question', (self.pk,))
75
76     def notify_author(self):
77         if not self.email:
78             return
79         site = Site.objects.get_current()
80         context = Context({
81                 'question': self,
82                 'site': site,
83             })
84         text_content = loader.get_template('questions/answered_mail.txt'
85             ).render(context)
86         html_content = loader.get_template('questions/answered_mail.html'
87             ).render(context)
88         msg = EmailMultiAlternatives(
89             u'Odpowiedź na Twoje pytanie w serwisie %s.' % site.domain,
90             text_content, settings.SERVER_EMAIL, [self.email])
91         msg.attach_alternative(html_content, "text/html")
92         msg.send()
93
94     def ack_author(self):
95         if not self.email:
96             return
97         site = Site.objects.get_current()
98         context = Context({
99                 'question': self,
100                 'site': site,
101             })
102         text_content = loader.get_template('questions/ack_mail.txt'
103             ).render(context)
104         html_content = loader.get_template('questions/ack_mail.html'
105             ).render(context)
106         msg = EmailMultiAlternatives(
107             u'Twoje pytanie zostało zarejestrowane w serwisie %s.' % site.domain,
108             text_content, settings.SERVER_EMAIL, [self.email])
109         msg.attach_alternative(html_content, "text/html")
110         msg.send()
111
112     def save(self, *args, **kwargs):
113         now = datetime.now()
114         notify = False
115         if self.answered and not self.answered_at:
116             notify = True
117             self.answered_at = now
118         if self.published and not self.published_at:
119             self.published_at = now
120         ret = super(Question, self).save(*args, **kwargs)
121         if notify:
122             self.notify_author()
123         return ret