diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-05-20 07:44:28 +0200 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-05-20 07:44:28 +0200 |
commit | 2207a7db83e85abf1d4cfe88a1c340e1332533f3 (patch) | |
tree | 072fc90337315f34716bdd237347a19412c74cd5 | |
parent | Improved email notifications. (diff) | |
download | newspipe-2207a7db83e85abf1d4cfe88a1c340e1332533f3.tar.gz newspipe-2207a7db83e85abf1d4cfe88a1c340e1332533f3.tar.bz2 newspipe-2207a7db83e85abf1d4cfe88a1c340e1332533f3.zip |
Send notification messages to platform users.
-rw-r--r-- | pyaggr3g470r/emails.py | 13 | ||||
-rw-r--r-- | pyaggr3g470r/forms.py | 5 | ||||
-rw-r--r-- | pyaggr3g470r/templates/admin/dashboard.html | 14 | ||||
-rw-r--r-- | pyaggr3g470r/views.py | 13 |
4 files changed, 37 insertions, 8 deletions
diff --git a/pyaggr3g470r/emails.py b/pyaggr3g470r/emails.py index d11fd16e..5b80fce0 100644 --- a/pyaggr3g470r/emails.py +++ b/pyaggr3g470r/emails.py @@ -65,7 +65,7 @@ def send_email(mfrom, mto, feed, article): # # Notifications # -def send_heroku(user, subject, plaintext): +def send_heroku(user=None, bcc="", subject="", plaintext=""): """ Send an email on Heroku via Postmark. """ @@ -73,8 +73,11 @@ def send_heroku(user, subject, plaintext): message = PMMail(api_key = conf.POSTMARK_API_KEY, subject = subject, sender = conf.ADMIN_EMAIL, - to = user.email, text_body = plaintext) + if bcc != "" and None == user: + message.bcc = bcc + elif bcc == "" and None != user: + message.to = user.email message.send() except Exception as e: pyaggr3g470r_log.error(str(e)) @@ -90,7 +93,7 @@ def information_message(subject, plaintext): if conf.ON_HEROKU: # Postmark has a limit of twenty recipients per message in total. for i in xrange(0, len(emails), 20): - send_heroku(", ".join(emails[i:i+20]), subject, plaintext) + send_heroku(bcc=", ".join(emails[i:i+20]), subject=subject, plaintext=plaintext) else: pass @@ -101,7 +104,7 @@ def new_account_notification(user): plaintext = """Hello,\n\nYour account has been created. Click on the following link to confirm it:\n%s\n\nSee you,""" % \ (conf.PLATFORM_URL + 'confirm_account/' + user.activation_key) if conf.ON_HEROKU: - send_heroku(user, "[pyAggr3g470r] Account creation", plaintext) + send_heroku(user=user, subject="[pyAggr3g470r] Account creation", plaintext=plaintext) else: pass @@ -112,7 +115,7 @@ def new_account_activation(user): plaintext = """Hello,\n\nYour account has been activated. You can now connect to the platform:\n%s\n\nSee you,""" % \ (conf.PLATFORM_URL) if conf.ON_HEROKU: - send_heroku(user, "[pyAggr3g470r] Account activated", plaintext) + send_heroku(user=user, subject="[pyAggr3g470r] Account activated", plaintext=plaintext) else: pass diff --git a/pyaggr3g470r/forms.py b/pyaggr3g470r/forms.py index d241b90a..57076428 100644 --- a/pyaggr3g470r/forms.py +++ b/pyaggr3g470r/forms.py @@ -111,3 +111,8 @@ class ProfileForm(Form): self.nickname.errors.append(lazy_gettext('This nickname has invalid characters. Please use letters, numbers, dots and underscores only.')) return False return True + +class InformationMessageForm(Form): + subject = TextField(lazy_gettext("Subject"), [validators.Required(lazy_gettext("Please enter a subject."))]) + message = TextAreaField(lazy_gettext("Message"), [validators.Required(lazy_gettext("Please enter a content."))]) + submit = SubmitField(lazy_gettext("Send")) diff --git a/pyaggr3g470r/templates/admin/dashboard.html b/pyaggr3g470r/templates/admin/dashboard.html index b15097d5..911ce670 100644 --- a/pyaggr3g470r/templates/admin/dashboard.html +++ b/pyaggr3g470r/templates/admin/dashboard.html @@ -40,7 +40,19 @@ {% endfor %} </tbody> </table> -<br /> <a href="/admin/create_user/" class="btn btn-default">{{ _('Add a new user') }}</a> +<h1>{{ _('Send notification messages') }}</h1> +<form action="" method="post"> + {{ form.hidden_tag() }} + + {{ form.subject.label }} + {{ form.subject(class_="form-control") }} {% for error in form.subject.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %} + + {{ form.message.label }} + {{ form.message(class_="form-control", rows=8) }} {% for error in form.message.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %} + + <br /> + {{ form.submit(class_="btn") }} +</form> </div> {% endblock %} diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index a9c588a2..4f5c4fb2 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -43,7 +43,7 @@ import export import emails if not conf.ON_HEROKU: import search as fastsearch -from forms import SignupForm, SigninForm, AddFeedForm, ProfileForm +from forms import SignupForm, SigninForm, AddFeedForm, ProfileForm, InformationMessageForm from pyaggr3g470r import app, db, allowed_file, babel from pyaggr3g470r.models import User, Feed, Article, Role from pyaggr3g470r.decorators import feed_access_required @@ -698,8 +698,17 @@ def dashboard(): """ Adminstrator's dashboard. """ + form = InformationMessageForm() + + if request.method == 'POST': + if form.validate(): + try: + emails.information_message(form.subject.data, form.message.data) + except Exception as e: + flash(gettext('Problem while sending email') + ': ' + str(e), 'danger') + users = User.query.all() - return render_template('admin/dashboard.html', users=users, current_user=g.user) + return render_template('admin/dashboard.html', users=users, current_user=g.user, form=form) @app.route('/admin/create_user/', methods=['GET', 'POST']) @app.route('/admin/edit_user/<int:user_id>/', methods=['GET', 'POST']) |