1540191cfc69a23b0fe4bfbfda91048ddba7e060
[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 Tag(TagBase):
16     def slugify(self, tag, i=None):
17         slug = slughifi(tag)
18         if i is not None:
19             slug += "_%d" % i
20         return slug
21
22     class Meta:
23         verbose_name = _("Tag")
24         verbose_name_plural = _("Tags")
25
26
27 class TagItem(GenericTaggedItemBase):
28     tag = models.ForeignKey(Tag, related_name="items")
29  
30
31 class Question(models.Model):
32     email = models.EmailField(_('contact e-mail'), null=True, blank=True)
33     question = models.TextField(_('question'), db_index=True)
34     created_at = models.DateTimeField(_('created at'), auto_now_add=True)
35     changed_at = models.DateTimeField(_('changed at'), auto_now=True)
36     approved = models.BooleanField(_('approved'), default=False)
37     edited_question = models.TextField(_('edited question'), db_index=True, null=True, blank=True,
38             help_text=_("Leave empty if question doesn't need editing."))
39     answer = MarkupField(_('answer'), markup_type='textile_pl', blank=True,
40             help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.'))
41     answered = models.BooleanField(_('answered'), db_index=True, default=False,
42             help_text=_('Check to send the answer to user.'))
43     answered_at = models.DateTimeField(_('answered at'), null=True, blank=True, db_index=True)
44     published = models.BooleanField(_('published'), db_index=True, default=False,
45         help_text=_('Check to display answered question on site.'))
46     published_at = models.DateTimeField(_('published at'), null=True, blank=True, db_index=True)
47
48     tags = TaggableManager(through=TagItem)
49
50     class Meta:
51         ordering = ['-created_at']
52         verbose_name = _('question')
53         verbose_name_plural = _('questions')
54
55     def __unicode__(self):
56         return self.edited_question or self.question
57
58     @models.permalink
59     def get_absolute_url(self):
60         return ('questions_question', (self.pk,))
61
62     def notify_author(self):
63         if not self.email:
64             return
65         site = Site.objects.get_current()
66         context = Context({
67                 'question': self,
68                 'site': site,
69             })
70         text_content = loader.get_template('questions/answered_mail.txt'
71             ).render(context)
72         html_content = loader.get_template('questions/answered_mail.html'
73             ).render(context)
74         msg = EmailMultiAlternatives(
75             u'Odpowiedź na Twoje pytanie w serwisie %s.' % site.domain,
76             text_content, settings.SERVER_EMAIL, [self.email])
77         msg.attach_alternative(html_content, "text/html")
78         msg.send()
79
80     def ack_author(self):
81         if not self.email:
82             return
83         site = Site.objects.get_current()
84         context = Context({
85                 'question': self,
86                 'site': site,
87             })
88         text_content = loader.get_template('questions/ack_mail.txt'
89             ).render(context)
90         html_content = loader.get_template('questions/ack_mail.html'
91             ).render(context)
92         msg = EmailMultiAlternatives(
93             u'Twoje pytanie zostało zarejestrowane w serwisie %s.' % site.domain,
94             text_content, settings.SERVER_EMAIL, [self.email])
95         msg.attach_alternative(html_content, "text/html")
96         msg.send()
97
98     def save(self, *args, **kwargs):
99         now = datetime.now()
100         notify = False
101         if self.answered and not self.answered_at:
102             notify = True
103             self.answered_at = now
104         if self.published and not self.published_at:
105             self.published_at = now
106         ret = super(Question, self).save(*args, **kwargs)
107         if notify:
108             self.notify_author()
109         return ret