add moderation command
authorymh <ymh.work@gmail.com>
Fri, 06 Mar 2015 00:58:34 +0100
changeset 481 efd1aaee4b0c
parent 480 2ae9914d4147
child 482 99f650e5b772
child 487 ef34954a7f9f
add moderation command
.settings/org.eclipse.core.resources.prefs
src/hdalab/management/commands/send_moderation_mail.py
src/hdalab/templates/mails/moderation_notice.html
src/hdalab/templates/mails/moderation_notice.txt
--- a/.settings/org.eclipse.core.resources.prefs	Thu Mar 05 16:58:40 2015 +0100
+++ b/.settings/org.eclipse.core.resources.prefs	Fri Mar 06 00:58:34 2015 +0100
@@ -47,6 +47,7 @@
 encoding//src/hdalab/management/commands/query_dbpedia.py=utf-8
 encoding//src/hdalab/management/commands/query_geo_inclusion.py=utf-8
 encoding//src/hdalab/management/commands/query_wikipedia_category.py=utf-8
+encoding//src/hdalab/management/commands/send_moderation_mail.py=utf-8
 encoding//src/hdalab/management/utils.py=utf-8
 encoding//src/hdalab/migrations/0001_initial.py=utf-8
 encoding//src/hdalab/migrations/0002_dataviz.py=utf-8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hdalab/management/commands/send_moderation_mail.py	Fri Mar 06 00:58:34 2015 +0100
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+'''
+Created on Mar 5, 2015
+
+@author: ymh
+'''
+
+import logging
+
+from django.conf import settings
+from django.contrib.auth import get_user_model
+from django.core import mail
+from django.core.mail.message import EmailMultiAlternatives
+from django.core.management.base import NoArgsCommand
+from django.template.base import TemplateDoesNotExist
+from django.template.loader import render_to_string
+
+from hdalab.models.renkan import HdalabRenkan
+
+
+User = get_user_model()
+logger = logging.getLogger(__name__)
+
+TEMPLATE_NAME = "mails/moderation_notice"
+class Command(NoArgsCommand):
+    
+    def handle_noargs(self, **options):
+        # query renkan to moderate
+        renkan_query = HdalabRenkan.objects.filter(state=HdalabRenkan.MODERATED)
+        
+        renkan_count = renkan_query.count()
+        
+        if renkan_count == 0:
+            logger.info("Send moderation email : no renkan to moderate. exiting")
+        logger.debug("Send moderation email : %d renkan(s) to moderate", renkan_count)
+        
+        renkan_list = list(renkan_query.select_related()[:20])
+        
+        connection = mail.get_connection()
+        try:
+            connection.open()
+            for email_recipient in User.objects.filter(is_staff=True):
+                if not email_recipient.email:
+                    continue
+    
+                msg_html = ""
+                msg_txt = ""
+                try:
+                    context = {
+                        'renkan_count': renkan_count,
+                        'renkan_list': renkan_list,
+                        'WEB_URL': settings.WEB_URL,
+                        'email_recipient': email_recipient
+                    }
+                    msg_html = render_to_string(TEMPLATE_NAME+".txt", context)
+                    msg_txt = render_to_string(TEMPLATE_NAME+".html", context)
+                except TemplateDoesNotExist:
+                    logger.error("Mail template %s not found", TEMPLATE_NAME)
+                    return
+                except Exception as e:
+                    logger.error("Error rendering template %s : %r", TEMPLATE_NAME, e)
+                    raise e
+        
+                if not msg_html and not msg_txt:
+                    logger.info("Send moderation email: nothing to email exiting")
+                    continue
+                emsg = EmailMultiAlternatives("Moderation Renkan", msg_txt, settings.DEFAULT_FROM_EMAIL, [email_recipient.email], connection=connection)
+                emsg.attach_alternative(msg_html, "text/html")
+                emsg.send(fail_silently=True)
+        finally:
+            connection.close()
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hdalab/templates/mails/moderation_notice.html	Fri Mar 06 00:58:34 2015 +0100
@@ -0,0 +1,212 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<!-- Template from https://github.com/leemunroe/html-email-template -->
+<head>
+<meta name="viewport" content="width=device-width" />
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<title>Renkan à modérer</title>
+<style>
+/* -------------------------------------
+        GLOBAL
+------------------------------------- */
+* {
+    margin: 0;
+    padding: 0;
+    font-family: "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
+    font-size: 100%;
+    line-height: 1.6;
+}
+
+img {
+    max-width: 100%;
+}
+
+body {
+    -webkit-font-smoothing: antialiased;
+    -webkit-text-size-adjust: none;
+    width: 100%!important;
+    height: 100%;
+}
+
+
+/* -------------------------------------
+        ELEMENTS
+------------------------------------- */
+a {
+    color: #348eda;
+}
+
+.btn-primary {
+    text-decoration: none;
+    color: #FFF;
+    background-color: #348eda;
+    border: solid #348eda;
+    border-width: 10px 20px;
+    line-height: 2;
+    font-weight: bold;
+    margin-right: 10px;
+    text-align: center;
+    cursor: pointer;
+    display: inline-block;
+    border-radius: 25px;
+}
+
+.btn-secondary {
+    text-decoration: none;
+    color: #FFF;
+    background-color: #aaa;
+    border: solid #aaa;
+    border-width: 10px 20px;
+    line-height: 2;
+    font-weight: bold;
+    margin-right: 10px;
+    text-align: center;
+    cursor: pointer;
+    display: inline-block;
+    border-radius: 25px;
+}
+
+.last {
+    margin-bottom: 0;
+}
+
+.first {
+    margin-top: 0;
+}
+
+.padding {
+    padding: 10px 0;
+}
+
+
+/* -------------------------------------
+        BODY
+------------------------------------- */
+table.body-wrap {
+    width: 100%;
+    padding: 20px;
+}
+
+table.body-wrap .container {
+    border: 1px solid #f0f0f0;
+}
+
+
+/* -------------------------------------
+        FOOTER
+------------------------------------- */
+table.footer-wrap {
+    width: 100%;    
+    clear: both!important;
+}
+
+.footer-wrap .container p {
+    font-size: 12px;
+    color: #666;
+    
+}
+
+table.footer-wrap a {
+    color: #999;
+}
+
+
+/* -------------------------------------
+        TYPOGRAPHY
+------------------------------------- */
+h1, h2, h3 {
+    font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
+    line-height: 1.1;
+    margin-bottom: 15px;
+    color: #000;
+    margin: 40px 0 10px;
+    line-height: 1.2;
+    font-weight: 200;
+}
+
+h1 {
+    font-size: 36px;
+}
+h2 {
+    font-size: 28px;
+}
+h3 {
+    font-size: 22px;
+}
+
+p, ul, ol {
+    margin-bottom: 10px;
+    font-weight: normal;
+    font-size: 14px;
+}
+
+ul li, ol li {
+    margin-left: 5px;
+    list-style-position: inside;
+}
+
+/* ---------------------------------------------------
+        RESPONSIVENESS
+
+------------------------------------------------------ */
+
+/* Set a max-width, and make it display as block so it will automatically stretch to that width, but will also shrink down on a phone or something */
+.container {
+    display: block!important;
+    max-width: 600px!important;
+    margin: 0 auto!important; /* makes it centered */
+    clear: both!important;
+}
+
+/* Set the padding on the td rather than the div for Outlook compatibility */
+.body-wrap .container {
+    padding: 20px;
+}
+
+/* This should also be a block element, so that it will fill 100% of the .container */
+.content {
+    max-width: 600px;
+    margin: 0 auto;
+    display: block;
+}
+
+/* Let's make sure tables in the content area are 100% wide */
+.content table {
+    width: 100%;
+}
+
+</style>
+</head>
+
+<body bgcolor="#f6f6f6">
+
+<!-- body -->
+<table class="body-wrap">
+    <tr>
+        <td></td>
+        <td class="container" bgcolor="#FFFFFF">
+
+            <!-- content -->
+            <div class="content">
+            <table>
+                <tr>
+                    <td>
+                        <p>Bonjour {{ email_recipient.username }},</p>
+                        <p>Vous avez {{ renkan_count }} renkan à modérer.</p>
+                        <p>Vous pouvez le faire à l'adresse suivante : <a href="{{WEB_URL}}{% url 'manage_renkans' %}">{{WEB_URL}}{% url 'manage_renkans' %}</a></p>
+                        <p></p>
+                        <p>Bonne journée.</p>
+                    </td>
+                </tr>
+            </table>
+            </div>
+            <!-- /content -->
+            
+        </td>
+        <td></td>
+    </tr>
+</table>
+<!-- /body -->
+
+</body>
+</html>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hdalab/templates/mails/moderation_notice.txt	Fri Mar 06 00:58:34 2015 +0100
@@ -0,0 +1,6 @@
+Bonjour {{ email_recipient.username }},
+
+Vous avez {{ renkan_count }} renkan à modérer.
+Vous pouvez le faire à l'adresse suivante : {{WEB_URL}}{% url 'manage_renkans' %}
+
+Bonne journée.