Working book shop.
[prawokultury.git] / questions / models.py
index bc7d960..746ad84 100644 (file)
@@ -1,11 +1,47 @@
 # -*- coding: utf-8
 from datetime import datetime
+from django.conf import settings
 from django.contrib.sites.models import Site
 from django.core.mail import EmailMultiAlternatives
 from django.db import models
 from django.template import loader, Context
 from django.utils.translation import ugettext_lazy as _
 from markupfield.fields import MarkupField
+from taggit.models import TagBase, GenericTaggedItemBase
+from taggit_autosuggest.managers import TaggableManager
+from fnpdjango.utils.text.slughifi import slughifi
+
+
+class TagCategory(models.Model):
+    name = models.CharField(verbose_name=_('Name'), unique = True, max_length = 100)
+    slug = models.SlugField(verbose_name=_('Slug'), unique = True, max_length = 100)
+
+    class Meta:
+        verbose_name = _("Tag Category")
+        verbose_name_plural = _("Tag Categories")
+        
+    def __unicode__(self):
+        return self.name
+    
+    
+class Tag(TagBase):
+    def slugify(self, tag, i=None):
+        slug = slughifi(tag)
+        if i is not None:
+            slug += "_%d" % i
+        return slug
+
+    category = models.ForeignKey(TagCategory, blank = True, null = True, on_delete = models.SET_NULL, related_name = 'tags')
+    click_count = models.IntegerField(null = False, default = 0)
+    
+    class Meta:
+        verbose_name = _("Tag")
+        verbose_name_plural = _("Tags")
+
+
+class TagItem(GenericTaggedItemBase):
+    tag = models.ForeignKey(Tag, related_name="items")
 
 class Question(models.Model):
     email = models.EmailField(_('contact e-mail'), null=True, blank=True)
@@ -24,6 +60,8 @@ class Question(models.Model):
         help_text=_('Check to display answered question on site.'))
     published_at = models.DateTimeField(_('published at'), null=True, blank=True, db_index=True)
 
+    tags = TaggableManager(through=TagItem, blank=True)
+
     class Meta:
         ordering = ['-created_at']
         verbose_name = _('question')
@@ -54,11 +92,33 @@ class Question(models.Model):
         msg.attach_alternative(html_content, "text/html")
         msg.send()
 
+    def ack_author(self):
+        if not self.email:
+            return
+        site = Site.objects.get_current()
+        context = Context({
+                'question': self,
+                'site': site,
+            })
+        text_content = loader.get_template('questions/ack_mail.txt'
+            ).render(context)
+        html_content = loader.get_template('questions/ack_mail.html'
+            ).render(context)
+        msg = EmailMultiAlternatives(
+            u'Twoje pytanie zostaƂo zarejestrowane w serwisie %s.' % site.domain,
+            text_content, settings.SERVER_EMAIL, [self.email])
+        msg.attach_alternative(html_content, "text/html")
+        msg.send()
+
     def save(self, *args, **kwargs):
         now = datetime.now()
+        notify = False
         if self.answered and not self.answered_at:
-            self.notify_author()
+            notify = True
             self.answered_at = now
         if self.published and not self.published_at:
             self.published_at = now
-        super(Question, self).save(*args, **kwargs)
+        ret = super(Question, self).save(*args, **kwargs)
+        if notify:
+            self.notify_author()
+        return ret