new test + archived old test
[edumed.git] / contact / models.py
index f23fddf..7fe8f9c 100644 (file)
@@ -1,8 +1,6 @@
 # -*- coding: utf-8 -*-
-import yaml
-from django.core.files.storage import FileSystemStorage
 from django.db import models
-from django.utils.encoding import smart_unicode
+from django.db.models import permalink
 from django.utils.translation import ugettext_lazy as _
 from jsonfield import JSONField
 from . import app_settings
@@ -18,10 +16,10 @@ class Contact(models.Model):
     @staticmethod
     def pretty_print(value, for_html=False):
         if type(value) in (tuple, list, dict):
-            value = yaml.safe_dump(value, 
-                allow_unicode=True,
-                default_flow_style=False)
+            import yaml
+            value = yaml.safe_dump(value, allow_unicode=True, default_flow_style=False)
             if for_html:
+                from django.utils.encoding import smart_unicode
                 value = smart_unicode(value).replace(u" ", unichr(160))
         return value
 
@@ -33,6 +31,47 @@ class Contact(models.Model):
     def __unicode__(self):
         return unicode(self.created_at)
 
+    def get_form_class(self):
+        from contact.forms import contact_forms
+        return contact_forms.get(self.form_tag)
+
+    def get_update_form_class(self):
+        from contact.forms import update_forms
+        return update_forms.get(self.form_tag, self.get_form_class())
+
+    @permalink
+    def update_url(self):
+        form_class = self.get_update_form_class()
+        confirmation = form_class.confirmation_class.objects.get(contact=self)
+        return 'edit_form', [], {'form_tag': self.form_tag, 'contact_id': self.id, 'key': confirmation.key}
+
+    def send_confirmation(self, context=None, form=None):
+        from django.template.loader import render_to_string
+        from django.core.mail import send_mail
+        from django.contrib.sites.models import Site
+        if not form:
+            form_class = self.get_form_class()
+            form = form_class()
+        dictionary = form.get_dictionary(self)
+        mail_subject = render_to_string([
+            'contact/%s/mail_subject.txt' % self.form_tag,
+            'contact/mail_subject.txt',
+        ], dictionary, context).strip()
+        if form.result_page:
+            mail_body = render_to_string(
+                'contact/%s/results_email.txt' % self.form_tag,
+                {
+                    'contact': self,
+                    'results': form.results(self),
+                }, context)
+        else:
+            mail_body = render_to_string([
+                'contact/%s/mail_body.txt' % self.form_tag,
+                'contact/mail_body.txt',
+            ], dictionary, context)
+        site = Site.objects.get_current()
+        send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [self.contact], fail_silently=True)
+
 
 class Attachment(models.Model):
     contact = models.ForeignKey(Contact)
@@ -41,7 +80,7 @@ class Attachment(models.Model):
 
     @models.permalink
     def get_absolute_url(self):
-        return ('contact_attachment', [self.contact_id, self.tag])
+        return 'contact_attachment', [self.contact_id, self.tag]
 
 
 __import__(app_settings.FORMS_MODULE)