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
16 def slugify(self, tag, i=None):
23 verbose_name = _("Tag")
24 verbose_name_plural = _("Tags")
27 class TagItem(GenericTaggedItemBase):
28 tag = models.ForeignKey(Tag, related_name="items")
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)
48 tags = TaggableManager(through=TagItem, blank=True)
51 ordering = ['-created_at']
52 verbose_name = _('question')
53 verbose_name_plural = _('questions')
55 def __unicode__(self):
56 return self.edited_question or self.question
59 def get_absolute_url(self):
60 return ('questions_question', (self.pk,))
62 def notify_author(self):
65 site = Site.objects.get_current()
70 text_content = loader.get_template('questions/answered_mail.txt'
72 html_content = loader.get_template('questions/answered_mail.html'
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")
83 site = Site.objects.get_current()
88 text_content = loader.get_template('questions/ack_mail.txt'
90 html_content = loader.get_template('questions/ack_mail.html'
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")
98 def save(self, *args, **kwargs):
101 if self.answered and not self.answered_at:
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)