Commits

Anonymous committed 93030a8

Quick Unicode fix

Comments (0)

Files changed (1)

contact_form/forms.py

 """
 
 
-import sha
-
 from django import newforms as forms
 from django.conf import settings
 from django.core.mail import send_mail
     from_email = settings.DEFAULT_FROM_EMAIL
     
     recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS]
-    
-    subject = "[%s] Message sent through the web site" % Site.objects.get_current().name
+
+    subject_template_name = "contact_form/contact_form_subject.txt"
     
     template_name = 'contact_form/contact_form.txt'
+
+    _context = None
     
     def message(self):
         """
             template_name = self.template_name()
         else:
             template_name = self.template_name
-        t = loader.get_template(template_name)
-        return t.render(RequestContext(self.request, self.cleaned_data))
+        return loader.render_to_string(template_name,
+                                       self.get_context())
     
-    def _get_message_dict(self):
+    def subject(self):
+        """
+        Renders the subject of the message to a string.
+        
+        """
+        subject = loader.render_to_string(self.subject_template_name,
+                                          self.get_context())
+        return ''.join(subject.splitlines())
+    
+    def get_context(self):
+        if not self.is_valid():
+            raise ValueError("Cannot generate Context from invalid contact form")
+        if self._context is None:
+            self._context = RequestContext(self.request,
+                                           dict(self.cleaned_data,
+                                                site=Site.objects.get_current()))
+        return self._context
+    
+    def get_message_dict(self):
         if not self.is_valid():
             raise ValueError("Message cannot be sent from invalid contact form")
         message_dict = {}
         Builds and sends the email message.
         
         """
-        send_mail(fail_silently=fail_silently, **self._get_message_dict())
+        send_mail(fail_silently=fail_silently, **self.get_message_dict())
 
 
 class AkismetContactForm(ContactForm):
     
     """
     def clean_body(self):
-        if 'body' in self.cleaned_data and hasattr(settings, 'AKISMET_API_KEY') and settings.AKISMET_API_KEY:
+        if 'body' in self.cleaned_data and getattr(settings, 'AKISMET_API_KEY', ''):
             from akismet import Akismet
+            from django.utils.encoding import smart_str
             akismet_api = Akismet(key=settings.AKISMET_API_KEY,
                                   blog_url='http://%s/' % Site.objects.get_current().domain)
             if akismet_api.verify_key():
                                  'referer': self.request.META.get('HTTP_REFERER', ''),
                                  'user_ip': self.request.META.get('REMOTE_ADDR', ''),
                                  'user_agent': self.request.META.get('HTTP_USER_AGENT', '') }
-                if akismet_api.comment_check(self.cleaned_data['body'], data=akismet_data, build_data=True):
+                if akismet_api.comment_check(smart_str(self.cleaned_data['body']), data=akismet_data, build_data=True):
                     raise forms.ValidationError(u"Akismet thinks this message is spam")
         return self.cleaned_data['body']