]> git.openstreetmap.org Git - osqa.git/commitdiff
Converted the new question notification template.
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Tue, 1 Jun 2010 14:42:17 +0000 (14:42 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Tue, 1 Jun 2010 14:42:17 +0000 (14:42 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@351 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/models/user.py
forum/skins/default/templates/notifications/base.html [new file with mode: 0644]
forum/skins/default/templates/notifications/newquestion.html
forum/subscriptions.py
forum/templatetags/email_tags.py
forum/utils/mail.py

index 62eeac3aeb6fc9d6d1c3dae21b1f0b05356cdb1a..339c02d727f64fd6ecb8878bca8211eb313d5cf1 100644 (file)
@@ -158,7 +158,7 @@ class User(BaseModel, DjangoUser):
     def get_vote_count_today(self):\r
         today = datetime.date.today()\r
         return self.actions.filter(canceled=False, action_type__in=("voteup", "votedown"),\r
-                action_date__range=(today - datetime.timedelta(days=1), today)).count()\r
+                action_date__gte=(today - datetime.timedelta(days=1))).count()\r
 \r
     def get_reputation_by_upvoted_today(self):\r
         today = datetime.datetime.now()\r
@@ -170,7 +170,7 @@ class User(BaseModel, DjangoUser):
     def get_flagged_items_count_today(self):\r
         today = datetime.date.today()\r
         return self.actions.filter(canceled=False, action_type="flag",\r
-                action_date__range=(today - datetime.timedelta(days=1), today)).count()\r
+                action_date__gte=(today - datetime.timedelta(days=1))).count()\r
 \r
     @true_if_is_super_or_staff\r
     def can_view_deleted_post(self, post):\r
diff --git a/forum/skins/default/templates/notifications/base.html b/forum/skins/default/templates/notifications/base.html
new file mode 100644 (file)
index 0000000..9783bc7
--- /dev/null
@@ -0,0 +1,49 @@
+{% load extra_filters extra_tags i18n email_tags %}
+
+{% declare %}
+    logo_location := os.path.join(str(settings.UPFILES_FOLDER), os.path.basename(str(settings.APP_LOGO)))
+{% enddeclare %}
+{% embedmedia logo_location as logo %}
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <style type="text/css">
+            body {
+                background: #FFF;
+                font-size: 12px;
+                line-height: 150%;
+                margin: 0;
+                padding: 0;
+                color: #000;
+                font-family: sans-serif;
+            }
+
+            #wrapper {
+                width: 600px;
+                margin: auto;
+                padding: 0;
+            }
+
+            a img {
+                border: none;
+            }
+        </style>
+    </head>
+    <body>
+        <a href="{% fullurl index %}">
+            <img src="cid:logo" title="{% trans "home" %}" alt="{{settings.APP_TITLE}} logo"/>
+        </a>
+        <br />
+        <p>{{ settings.APP_TITLE }}</p>
+    <br /><br />
+    <div id="wrapper">
+            <div id="room">
+                <div id="CALeft">
+                    {% block content%}
+                    {% endblock%}
+                </div>
+            </div>
+            <div class="spacer3"></div>
+        </div>
+    </body>
+</html>
\ No newline at end of file
index a5497166708e4c56189e852dabde84f4010f6a0f..b0ee0f045c573caea556b271296cb03f3ef59b1a 100644 (file)
@@ -1,27 +1,54 @@
-{% extends "email_base.html" %}\r
-{% load i18n %}\r
-{% load extra_tags %}\r
+{% load i18n extra_tags email_tags %}\r
 \r
-{% block content %}\r
-    <p>{% trans "Hello" %} {% user_var username %},</p>\r
+{% declare %}\r
+    prefix = settings.EMAIL_SUBJECT_PREFIX\r
+    app_name = settings.APP_SHORT_NAME\r
+    question_author = question.author.username\r
+    app_url = settings.APP_URL\r
+    question_url = question.get_absolute_url()\r
+    question_title = question.title\r
+    question_tags = question.tagnames\r
+{% enddeclare %}\r
 \r
-    <p>\r
-        {% blocktrans with question.author.username as author_name and settings.APP_SHORT_NAME as app_title and settings.APP_URL as app_url and question.get_absolute_url as question_url and question.title as question_title and question.tagnames as question_tags %}\r
-        {{ author_name }} has just posted a new question on {{ app_title }}, with title\r
-        <a href="{{ app_url }}{{ question_url }}">{{ question_title }}</a> and tagged <em>{{ question_tags }}</em>:\r
+{% email %}\r
+    {% subject %}{% blocktrans %}{{ prefix }} New question on {{ app_name }}{% endblocktrans %}{% endsubject %}\r
+\r
+    {% htmlcontent notifications/base.html %}\r
+        <p>{% trans "Hello" %} {{ recipient.username }},</p>\r
+\r
+        <p>\r
+            {% blocktrans %}\r
+            {{ question_author }} has just posted a new question on {{ app_name }}, with title\r
+            <a href="{{ app_url }}{{ question_url }}">{{ question_title }}</a> and tagged <em>{{ question_tags }}</em>:\r
+            {% endblocktrans %}\r
+        </p>\r
+\r
+        <blockquote>\r
+            {{ question.html|safe }}\r
+        </blockquote>\r
+\r
+        <p>{% trans "Don't forget to come over and cast your vote." %}</p>\r
+\r
+        <p>{% blocktrans %}Sincerely,<br />\r
+        Forum Administrator{% endblocktrans %}</p>\r
+    {% endhtmlcontent %}\r
+\r
+    {% textcontent %}\r
+        {% trans "Hello" %} {{ recipient.username }}\r
+    \r
+        {% blocktrans %}\r
+        {{ question_author }} has just posted a new question on {{ app_name }}, with title\r
+        "{{ question_title }}" and tagged {{ question_tags }}:\r
         {% endblocktrans %}\r
-    </p>\r
 \r
-    <blockquote>\r
-        {{ question.html|safe }}\r
-    </blockquote>\r
 \r
-    <p>\r
-    {% blocktrans %}\r
-        Don't forget to come over and cast your vote.\r
-    {% endblocktrans %}\r
-    </p>\r
+        {{ question.body|safe }}\r
+\r
+\r
+        {% trans "Don't forget to come over and cast your vote." %}\r
+\r
+        {% blocktrans %}Sincerely,\r
+        Forum Administrator{% endblocktrans %}\r
+    {% endtextcontent %}\r
+{% endemail %}\r
 \r
-    <p>{% blocktrans %}Sincerely,<br />\r
-    Forum Administrator{% endblocktrans %}</p>\r
-{% endblock %}\r
index 1ee99e3039655c80037e24b58760984100cceead..c808ce9af16d864cd4f9e63417d9654a04ba6d46 100644 (file)
@@ -2,7 +2,7 @@ import os
 import re\r
 import datetime\r
 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer\r
-from forum.utils.mail import send_email\r
+from forum.utils.mail import send_email, send_template_email\r
 from django.utils.translation import ugettext as _\r
 from forum.actions import AskAction, AnswerAction, CommentAction, AcceptAnswerAction, UserJoinsAction, QuestionViewAction\r
 from forum import settings\r
@@ -24,19 +24,21 @@ def create_recipients_dict(usr_list):
 def question_posted(action, new):\r
     question = action.node\r
 \r
-    subscribers = User.objects.values('email', 'username').filter(\r
+    subscribers = User.objects.filter(\r
             Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |\r
             (Q(subscription_settings__new_question_watched_tags='i') &\r
               Q(marked_tags__name__in=question.tagnames.split(' ')) &\r
               Q(tag_selections__reason='good'))\r
     ).exclude(id=question.author.id).distinct()\r
 \r
-    recipients = create_recipients_dict(subscribers)\r
+    #recipients = create_recipients_dict(subscribers)\r
 \r
-    send_email(settings.EMAIL_SUBJECT_PREFIX + _("New question on %(app_name)s") % dict(app_name=settings.APP_SHORT_NAME),\r
-               recipients, "notifications/newquestion.html", {\r
-        'question': question,\r
-    })\r
+    send_template_email(subscribers, "notifications/newquestion.html", {'question': question})\r
+\r
+    #send_email(settings.EMAIL_SUBJECT_PREFIX + _("New question on %(app_name)s") % dict(app_name=settings.APP_SHORT_NAME),\r
+    #           recipients, "notifications/newquestion.html", {\r
+    #    'question': question,\r
+    #})\r
 \r
     if question.author.subscription_settings.questions_asked:\r
         subscription = QuestionSubscription(question=question, user=question.author)\r
index bb21973eb77bd6a06075dfed7e6eb6a0c2d462b1..b28983755d5df8e235938202bf38e8085ee426db 100644 (file)
@@ -1,5 +1,6 @@
 from django import template
 from forum import settings
+from forum.utils.mail import create_and_send_mail_messages
 
 register = template.Library()
 
@@ -17,7 +18,7 @@ class MultiUserMailMessage(template.Node):
             self.nodelist.render(context)
             messages.append((recipient, context['subject'], context['htmlcontent'], context['textcontent'], context['embeddedmedia']))
 
-        create_mail_messages(messages)
+        create_and_send_mail_messages(messages)
 
 @register.tag
 def email(parser, token):
@@ -90,65 +91,9 @@ def embedmedia(parser, token):
 
     return EmbedMediaNode(location, alias)
 
-from email.mime.multipart import MIMEMultipart
-from email.mime.text import MIMEText
-from email.mime.image import MIMEImage
 
-from django.core.mail import DNS_NAME
-from smtplib import SMTP
-import email.Charset
-import socket
 
 
-def create_mail_messages(messages):
-    sender = '%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))
-
-    connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
-                local_hostname=DNS_NAME.get_fqdn())
-
-    try:
-        if (bool(settings.EMAIL_USE_TLS)):
-            connection.ehlo()
-            connection.starttls()
-            connection.ehlo()
-
-        if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
-            connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
-
-        if sender is None:
-            sender = str(settings.DEFAULT_FROM_EMAIL)
-
-        for recipient, subject, html, text, media in messages:
-            msgRoot = MIMEMultipart('related')
-            msgRoot['Subject'] = subject
-            msgRoot['From'] = sender
-            msgRoot['To'] =  '%s <%s>' % (recipient.username, recipient.email)
-            msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')
-
-            msgAlternative = MIMEMultipart('alternative')
-            msgRoot.attach(msgAlternative)
-
-            msgAlternative.attach(MIMEText(text, _charset='utf-8'))
-            msgAlternative.attach(MIMEText(html, 'html', _charset='utf-8'))
-
-            for alias, location in media.items():
-                fp = open(location, 'rb')
-                msgImage = MIMEImage(fp.read())
-                fp.close()
-                msgImage.add_header('Content-ID', '<'+alias+'>')
-                msgRoot.attach(msgImage)
-
-            try:
-                connection.sendmail(sender, [recipient.email], msgRoot.as_string())
-            except Exception, e:
-                pass
-
-        try:
-            connection.quit()
-        except socket.sslerror:
-            connection.close()
-    except Exception, e:
-        print e
 
     
     
index 4d11f854d710a625f9b5a5f690b51fe4faf87136..8c80f537490e91a36bf7bafaa8e8d3f55a1111a5 100644 (file)
@@ -2,9 +2,9 @@ import email
 import socket\r
 import os\r
 \r
-from email.MIMEMultipart import MIMEMultipart\r
-from email.MIMEText import MIMEText\r
-from email.MIMEImage import MIMEImage\r
+from email.mime.multipart import MIMEMultipart\r
+from email.mime.text import MIMEText\r
+from email.mime.image import MIMEImage\r
 \r
 from django.core.mail import DNS_NAME\r
 from smtplib import SMTP\r
@@ -130,4 +130,60 @@ def send_email(subject, recipients, template, context={}, sender=None, images=[]
         thread.setDaemon(True)\r
         thread.start()\r
     else:\r
-        send_msg_list(msgs)
\ No newline at end of file
+        send_msg_list(msgs)\r
+\r
+\r
+def send_template_email(recipients, template, context):\r
+    t = loader.get_template(template)\r
+    context.update(dict(recipients=recipients, settings=settings))\r
+    t.render(Context(context))\r
+\r
+def create_and_send_mail_messages(messages):\r
+    sender = '%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))\r
+\r
+    connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),\r
+                local_hostname=DNS_NAME.get_fqdn())\r
+\r
+    try:\r
+        if (bool(settings.EMAIL_USE_TLS)):\r
+            connection.ehlo()\r
+            connection.starttls()\r
+            connection.ehlo()\r
+\r
+        if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:\r
+            connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))\r
+\r
+        if sender is None:\r
+            sender = str(settings.DEFAULT_FROM_EMAIL)\r
+\r
+        for recipient, subject, html, text, media in messages:\r
+            msgRoot = MIMEMultipart('related')\r
+            msgRoot['Subject'] = subject\r
+            msgRoot['From'] = sender\r
+            msgRoot['To'] =  '%s <%s>' % (recipient.username, recipient.email)\r
+            msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')\r
+\r
+            msgAlternative = MIMEMultipart('alternative')\r
+            msgRoot.attach(msgAlternative)\r
+\r
+            msgAlternative.attach(MIMEText(text, _charset='utf-8'))\r
+            msgAlternative.attach(MIMEText(html, 'html', _charset='utf-8'))\r
+\r
+            for alias, location in media.items():\r
+                fp = open(location, 'rb')\r
+                msgImage = MIMEImage(fp.read())\r
+                fp.close()\r
+                msgImage.add_header('Content-ID', '<'+alias+'>')\r
+                msgRoot.attach(msgImage)\r
+\r
+            try:\r
+                connection.sendmail(sender, [recipient.email], msgRoot.as_string())\r
+            except Exception, e:\r
+                pass\r
+\r
+        try:\r
+            connection.quit()\r
+        except socket.sslerror:\r
+            connection.close()\r
+    except Exception, e:\r
+        print e\r