278071e43ef4da82ce081eb043b2e89915080928
[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 Categories")
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, related_name = 'tags')
35     click_count = models.IntegerField(null = False, default = 0)
36     
37     class Meta:
38         verbose_name = _("Tag")
39         verbose_name_plural = _("Tags")
40
41
42 class TagItem(GenericTaggedItemBase):
43     tag = models.ForeignKey(Tag, related_name="items")
44  
45
46 class Question(models.Model):
47     email = models.EmailField(_('contact e-mail'), null=True, blank=True)
48     question = models.TextField(_('question'), db_index=False)
49     created_at = models.DateTimeField(_('created at'), auto_now_add=True)
50     changed_at = models.DateTimeField(_('changed at'), auto_now=True)
51     approved = models.BooleanField(_('approved'), default=False)
52     edited_question = models.TextField(_('edited question'), db_index=False, null=True, blank=True,
53             help_text=_("Leave empty if question doesn't need editing."))
54     answer = MarkupField(_('answer'), markup_type='textile_pl', blank=True,
55             help_text=_('Use <a href="http://txstyle.org/">Textile</a> syntax.'))
56     answered_by = models.CharField(_('answered by'), max_length=127, null=True, blank=True)
57     answered = models.BooleanField(_('answered'), db_index=True, default=False,
58             help_text=_('Check to send the answer to user.'))
59     answered_at = models.DateTimeField(_('answered at'), null=True, blank=True, db_index=True)
60     published = models.BooleanField(_('published'), db_index=True, default=False,
61         help_text=_('Check to display answered question on site.'))
62     published_at = models.DateTimeField(_('published at'), null=True, blank=True, db_index=True)
63
64     tags = TaggableManager(through=TagItem, blank=True)
65
66     class Meta:
67         ordering = ['-created_at']
68         verbose_name = _('question')
69         verbose_name_plural = _('questions')
70
71     def __unicode__(self):
72         return self.edited_question or self.question
73
74     @models.permalink
75     def get_absolute_url(self):
76         return ('questions_question', (self.pk,))
77
78     def notify_author(self):
79         if not self.email:
80             return
81         site = Site.objects.get_current()
82         context = Context({
83                 'question': self,
84                 'site': site,
85             })
86         text_content = loader.get_template('questions/answered_mail.txt'
87             ).render(context)
88         html_content = loader.get_template('questions/answered_mail.html'
89             ).render(context)
90         msg = EmailMultiAlternatives(
91             u'Odpowiedź na Twoje pytanie w serwisie %s.' % site.domain,
92             text_content, settings.SERVER_EMAIL, [self.email])
93         msg.attach_alternative(html_content, "text/html")
94         msg.send()
95
96     def ack_author(self):
97         if not self.email:
98             return
99         site = Site.objects.get_current()
100         context = Context({
101                 'question': self,
102                 'site': site,
103             })
104         text_content = loader.get_template('questions/ack_mail.txt'
105             ).render(context)
106         html_content = loader.get_template('questions/ack_mail.html'
107             ).render(context)
108         msg = EmailMultiAlternatives(
109             u'Twoje pytanie zostało zarejestrowane w serwisie %s.' % site.domain,
110             text_content, settings.SERVER_EMAIL, [self.email])
111         msg.attach_alternative(html_content, "text/html")
112         msg.send()
113
114     def save(self, *args, **kwargs):
115         now = datetime.now()
116         notify = False
117         if self.answered and not self.answered_at:
118             notify = True
119             self.answered_at = now
120         if self.published and not self.published_at:
121             self.published_at = now
122         ret = super(Question, self).save(*args, **kwargs)
123         if notify:
124             self.notify_author()
125         return ret