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_autosuggest.managers import TaggableManager
13 class Question(models.Model):
14 email = models.EmailField(_('contact e-mail'), null=True, blank=True)
15 question = models.TextField(_('question'), db_index=True)
16 created_at = models.DateTimeField(_('created at'), auto_now_add=True)
17 changed_at = models.DateTimeField(_('changed at'), auto_now=True)
18 approved = models.BooleanField(_('approved'), default=False)
19 edited_question = models.TextField(_('edited question'), db_index=True, null=True, blank=True,
20 help_text=_("Leave empty if question doesn't need editing."))
21 answer = MarkupField(_('answer'), markup_type='textile_pl', blank=True,
22 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.'))
23 answered = models.BooleanField(_('answered'), db_index=True, default=False,
24 help_text=_('Check to send the answer to user.'))
25 answered_at = models.DateTimeField(_('answered at'), null=True, blank=True, db_index=True)
26 published = models.BooleanField(_('published'), db_index=True, default=False,
27 help_text=_('Check to display answered question on site.'))
28 published_at = models.DateTimeField(_('published at'), null=True, blank=True, db_index=True)
30 tags = TaggableManager()
33 ordering = ['-created_at']
34 verbose_name = _('question')
35 verbose_name_plural = _('questions')
37 def __unicode__(self):
38 return self.edited_question or self.question
41 def get_absolute_url(self):
42 return ('questions_question', (self.pk,))
44 def notify_author(self):
47 site = Site.objects.get_current()
52 text_content = loader.get_template('questions/answered_mail.txt'
54 html_content = loader.get_template('questions/answered_mail.html'
56 msg = EmailMultiAlternatives(
57 u'Odpowiedź na Twoje pytanie w serwisie %s.' % site.domain,
58 text_content, settings.SERVER_EMAIL, [self.email])
59 msg.attach_alternative(html_content, "text/html")
65 site = Site.objects.get_current()
70 text_content = loader.get_template('questions/ack_mail.txt'
72 html_content = loader.get_template('questions/ack_mail.html'
74 msg = EmailMultiAlternatives(
75 u'Twoje pytanie zostało zarejestrowane w serwisie %s.' % site.domain,
76 text_content, settings.SERVER_EMAIL, [self.email])
77 msg.attach_alternative(html_content, "text/html")
80 def save(self, *args, **kwargs):
83 if self.answered and not self.answered_at:
85 self.answered_at = now
86 if self.published and not self.published_at:
87 self.published_at = now
88 ret = super(Question, self).save(*args, **kwargs)