Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing email notifications that did not work correctly #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions badges/badge/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .db import Badge
from .db import Award
from .notification_helpers import send_badge_creation_notification
from .notification_helpers import send_badge_published_notification
from .notification_helpers import send_badge_awarded_notification


Expand Down Expand Up @@ -136,6 +137,7 @@ def publish_badge(uri):
badge_db.author_uri,
reverse('badge_view', args=(badge_db.pk, )),
)
send_badge_published_notification(_badge2dict(badge_db))
return True


Expand Down
13 changes: 13 additions & 0 deletions badges/badge/notification_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ def send_badge_creation_notification(badge):
context=context
)

def send_badge_published_notification(badge):
subject_template = 'emails/badge_published_subject.txt'
text_template = 'emails/badge_published.txt'
html_template = 'emails/badge_published.html'
context = {'badge': fetch_badge_resources(badge)}
return send_notification_i18n(
badge['author_uri'],
subject_template,
text_template,
html_template,
context=context
)


def send_badge_awarded_notification(badge, expert_uri):
subject_template = 'emails/badge_awarded_subject.txt'
Expand Down
6 changes: 5 additions & 1 deletion badges/badge/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.serializers.json import DjangoJSONEncoder
from django import http
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -70,6 +70,10 @@ def create(request):
@require_login
def preview(request, badge_id):
badge = badge_api.get_badge(badge_api.id2uri(badge_id))

if badge['published_date']:
return HttpResponseRedirect(reverse('badge_view', args=[badge_id]))

fetch_badge_resources(badge)
user = request.session['user']
user['is_author'] = _user_is_author(badge, user)
Expand Down
2 changes: 1 addition & 1 deletion badges/badges/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,4 @@ def skip_suspicious_operations(record):
#################################################################
MANDRILL_API_KEY = ""
EMAIL_BACKEND = "djrill.mail.backends.djrill.DjrillBackend"
DEFAULT_FROM_EMAIL = "[email protected]"
DEFAULT_FROM_EMAIL = "P2PU team <[email protected]>"
17 changes: 16 additions & 1 deletion badges/dashboard/templatetags/projects_tags.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
from django import template
from django.template.defaultfilters import mark_safe
from subprocess import Popen, PIPE

register = template.Library()


@register.filter
def purge_content(value):
return mark_safe(value.replace('<p>&nbsp;</p>', ''))
return mark_safe(value.replace('<p>&nbsp;</p>', ''))

@register.filter
def html2text(value):
"""
Pipes given HTML string into the text browser W3M, which renders it.
Rendered text is grabbed from STDOUT and returned.
"""
try:
cmd = "w3m -dump -T text/html -O ascii"
proc = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
return proc.communicate(str(value))[0]
except OSError:
# something bad happened, so just return the input
return value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look how text content is generated from HTML emails for the Mechanical MOOC: https://github.com/p2pu/mechanical-mooc/blob/master/mail/views.py#L22-L27.

17 changes: 9 additions & 8 deletions badges/templates/emails/badge_created.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@
Howdy!
</p>
<p>
Your badge {{ badge.title }} is published, live, and ready-to-go.
Your badge {{ badge.title }} is created. Woohoo!
</p>
<p>
Your Badge is created, but that doesn't mean that people can apply for it yet. If you haven't already, then you
must publish it and let the world see your creation. Publish it by pressing on the "Publish" button
<a href="http://badges.p2pu.org{% url badge_preview badge.id %}">here</a>.
</p>
<table>
<tbody>
<tr>
<td>
<a href="http://badges.p2pu.org{% url badge_view badge.id %}">
<a href="http://badges.p2pu.org{% url badge_preview badge.id %}">
<img src="http://badges.p2pu.org{{ badge.image.url }}" width="160px"/>
</a>
</td>
<td style="padding-left:20px; text-valign:top;">
<p>{{ badge.title }}</p>
<p>{{ badge.description|purge_content }}</p>
<p>{{ badge.requirements }}</p>
<p>{{ badge.requirements|purge_content }}</p>
</td>
</tr>
</tbody>
</table>
<p>
Your Badge now exists on badges.p2pu.org and learners can apply for it immediately. As the Badge's creator, this badge will automatically appear on your profile.
</p>
<p>
Tell the world about your spiffy Badge! {% include 'emails/social_buttons.html' %}
<p>As the Badge's creator, this badge will automatically appear on your profile.
</p>

<p>
Expand Down
18 changes: 7 additions & 11 deletions badges/templates/emails/badge_created.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
{% load projects_tags %}{% load i18n %}{% blocktrans with title=badge.title decription=badge.description|purge_content requirements=badge.requirements%}
{% load projects_tags %}{% load i18n %}{% blocktrans with title=badge.title description=badge.description|purge_content requirements=badge.requirements|html2text %}
Howdy!

Your badge {{ title }} is published, live, and ready-to-go.
Your badge {{ badge.title }} is created. Woohoo!

{{ title }}
{{ description }}
{{ requirements }}
* Title: {{ title }}
* Description: {{ description }}
* Requirements: {{ requirements }}
{% endblocktrans %}

Your Badge now exists at http://badges.p2pu.org{% url badge_view badge.id %} and learners can apply for it immediately. As the Badge's creator, this badge will automatically appear on your profile.
Your Badge is created, but that doesn't mean that people can apply for it yet. If you haven't already, then you must publish it and let the world see your creation. Publish it by pressing on the "Publish" button at http://badges.p2pu.org{% url badge_preview badge.id %}

Tell the world about your spiffy Badge!

Google+ - https://plus.google.com/share?url=http://badges.p2pu.org{% url badge_view badge.id %}

Tweet - http://twitter.com/share?url=http://badges.p2pu.org{% url badge_view badge.id %}&text=Check out the spiffy badge I made!&via=p2pu
As the Badge's creator, this badge will automatically appear on your profile.

{% blocktrans %}
Yours in the future of learning,
Expand Down
42 changes: 42 additions & 0 deletions badges/templates/emails/badge_published.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends 'emails/base.html' %}
{% load i18n %}
{% load projects_tags %}

{% block body %}
<p>
Howdy!
</p>
<p>
Your badge {{ badge.title }} is published, live, and ready-to-go.
</p>
<table>
<tbody>
<tr>
<td>
<a href="http://badges.p2pu.org{% url badge_view badge.id %}">
<img src="http://badges.p2pu.org{{ badge.image.url }}" width="160px"/>
</a>
</td>
<td style="padding-left:20px; text-valign:top;">
<p>{{ badge.title }}</p>
<p>{{ badge.description|purge_content }}</p>
<p>{{ badge.requirements|purge_content }}</p>
</td>
</tr>
</tbody>
</table>
<p>
Your Badge now exists on <a href="http://badges.p2pu.org{% url badge_view badge.id %}">badges.p2pu.org</a> and learners can apply for it immediately.
</p>
<p>
Tell the world about your spiffy Badge! {% include 'emails/social_buttons.html' %}
</p>

<p>

</p>
<p>
Yours in the future of learning, <br/>
- The P2PU team
</p>
{% endblock %}
22 changes: 22 additions & 0 deletions badges/templates/emails/badge_published.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% load projects_tags %}{% load i18n %}{% blocktrans with title=badge.title description=badge.description|purge_content requirements=badge.requirements|html2text %}
Howdy!

Your badge {{ title }} is published, live, and ready-to-go.

* Title: {{ title }}
* Description: {{ description }}
* Requirements: {{ requirements }}
{% endblocktrans %}

Your Badge now exists at http://badges.p2pu.org{% url badge_view badge.id %} and learners can apply for it immediately.

Tell the world about your spiffy Badge!

Google+ - https://plus.google.com/share?url=http://badges.p2pu.org{% url badge_view badge.id %}

Tweet - http://twitter.com/share?url=http://badges.p2pu.org{% url badge_view badge.id %}&text=Check out the spiffy badge I made!&via=p2pu

{% blocktrans %}
Yours in the future of learning,
- The P2PU team
{% endblocktrans %}
1 change: 1 addition & 0 deletions badges/templates/emails/badge_published_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% load i18n %}{% blocktrans with badge_title=badge.title%}You've just published a {{ badge_title }} Badge. Congrats!{% endblocktrans %}