Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add new app for emails

Roland Geider0003ff34f3d6
diff --git a/requirements.txt b/requirements.txt
index d8996e7c3..d9aea60e7 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -7,7 +7,7 @@ django-browserid==1.0
django-recaptcha
reportlab
django_mobile
-django-formtools
+django-formtools==1.0
bleach
python-mimeparse
pillow
diff --git a/wger/email/__init__.py b/wger/email/__init__.py
new file mode 100644
index 000000000..009b7dfe6
--- /dev/null
+++ b/wger/email/__init__.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+
+from wger import get_version
+
+VERSION = get_version()
+default_app_config = 'wger.email.apps.Config'
diff --git a/wger/email/apps.py b/wger/email/apps.py
new file mode 100644
index 000000000..d0ecd6102
--- /dev/null
+++ b/wger/email/apps.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+from django.apps import AppConfig
+
+
+class Config(AppConfig):
+ name = 'wger.email'
+ verbose_name = "Email"
+
+ def ready(self):
+ pass
diff --git a/wger/email/forms.py b/wger/email/forms.py
new file mode 100644
index 000000000..0daf6f385
--- /dev/null
+++ b/wger/email/forms.py
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+
+from django.utils.translation import (
+ pgettext,
+ ugettext_lazy as _
+)
+from django.forms import (
+ Form,
+ CharField,
+ Textarea
+)
+
+
+class EmailListForm(Form):
+ '''
+ Small form to send emails
+ '''
+
+ subject = CharField(label=pgettext('Subject', 'As in "email subject"'))
+ body = CharField(widget=Textarea, label=pgettext('Content', 'As in "content of an email"'))
diff --git a/wger/email/management/__init__.py b/wger/email/management/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wger/email/management/commands/__init__.py b/wger/email/management/commands/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wger/email/management/commands/send-mass-emails.py b/wger/email/management/commands/send-mass-emails.py
new file mode 100644
index 000000000..2335836e7
--- /dev/null
+++ b/wger/email/management/commands/send-mass-emails.py
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+from django.core import mail
+from django.conf import settings
+
+from django.core.management.base import BaseCommand
+from wger.email.models import CronEntry
+
+
+class Command(BaseCommand):
+ '''
+ Sends the prepared mass emails
+ '''
+
+ def handle(self, *args, **options):
+ '''
+ Send some mails and remove them from the list
+ '''
+ if CronEntry.objects.count():
+ for email in CronEntry.objects.all()[:100]:
+ mail.send_mail(email.log.subject,
+ email.log.body,
+ settings.DEFAULT_FROM_EMAIL,
+ [email.email],
+ fail_silently=True)
+ email.delete()
diff --git a/wger/email/migrations/0001_initial.py b/wger/email/migrations/0001_initial.py
new file mode 100644
index 000000000..3c1f13f01
--- /dev/null
+++ b/wger/email/migrations/0001_initial.py
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+from django.conf import settings
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('gym', '0004_auto_20151003_2357'),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='CronEntry',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('email', models.EmailField(max_length=254)),
+ ],
+ ),
+ migrations.CreateModel(
+ name='Log',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('date', models.DateField(auto_now=True)),
+ ('subject', models.CharField(max_length=100)),
+ ('body', models.TextField()),
+ ('gym', models.ForeignKey(editable=False, to='gym.Gym')),
+ ('user', models.ForeignKey(editable=False, to=settings.AUTH_USER_MODEL)),
+ ],
+ ),
+ migrations.AddField(
+ model_name='cronentry',
+ name='log',
+ field=models.ForeignKey(editable=False, to='email.Log'),
+ ),
+ ]
diff --git a/wger/email/migrations/__init__.py b/wger/email/migrations/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wger/email/models.py b/wger/email/models.py
new file mode 100644
index 000000000..e53a27977
--- /dev/null
+++ b/wger/email/models.py
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+from django.contrib.auth.models import User
+from django.db import models
+
+from wger.gym.models import Gym
+
+
+class Log(models.Model):
+ '''
+ A log of a sent email
+ '''
+
+ date = models.DateField(auto_now=True)
+ '''
+ Date when the log was created
+ '''
+
+ user = models.ForeignKey(User,
+ editable=False)
+ '''
+ The user that created the email
+ '''
+
+ gym = models.ForeignKey(Gym,
+ editable=False,
+ related_name='email_log'
+ )
+ '''
+ Gym this log belongs to
+ '''
+
+ subject = models.CharField(max_length=100)
+ '''
+ The email subject
+ '''
+
+ body = models.TextField()
+ '''
+ The email body
+ '''
+
+ def __unicode__(self):
+ '''
+ Return a more human-readable representation
+ '''
+ return self.subject
+
+
+class CronEntry(models.Model):
+ '''
+ Simple list of emails to be sent by a cron job
+ '''
+
+ log = models.ForeignKey(Log,
+ editable=False)
+ '''
+ Foreign key to email log with subject and body
+ '''
+
+ email = models.EmailField()
+ '''
+ The email address
+ '''
+
+ def __unicode__(self):
+ '''
+ Return a more human-readable representation
+ '''
+ return self.email
diff --git a/wger/email/templates/email/form.html b/wger/email/templates/email/form.html
new file mode 100644
index 000000000..26b26f40f
--- /dev/null
+++ b/wger/email/templates/email/form.html
@@ -0,0 +1,39 @@
+{% extends "base.html" %}
+{% load wger_extras %}
+{% load i18n %}
+
+{% block title %}
+ Emailverteiler -
+ {% if email_type == 'starter' %}
+ Starter
+ {% else %}
+ Studio
+ {% endif %}
+{% endblock %}
+
+{% block content %}
+<form action="{{ form_action }}"
+ method="post"
+ class="form-horizontal">
+ {% render_form_fields form "Vorschau erzeugen" %}
+</form>
+{% endblock %}
+
+{% block sidebar %}
+ <div class="well">
+ <h4>Benutzung</h4>
+ <ol>
+ <li>Geben Sie die nötigen Daten in das Formular ein.</li>
+ <li>Die Manager bekommen eine Vorschau-Email, zur Kontrolle.</li>
+ <li>
+ Sie können beliebig oft den Inhalt korrigieren und über die
+ Vorschau-Emails kontrollieren.
+ </li>
+ <li>
+ Wenn Sie mit dem Inhalt zufrieden sind, können Sie die Emails
+ freigeben, sie weden in den nächsten Studen automatisch
+ verschickt
+ </li>
+ </ol>
+ </div>
+{% endblock %}
\ No newline at end of file
diff --git a/wger/email/templates/email/overview.html b/wger/email/templates/email/overview.html
new file mode 100644
index 000000000..bc97faea8
--- /dev/null
+++ b/wger/email/templates/email/overview.html
@@ -0,0 +1,29 @@
+{% extends "base.html" %}
+{% load wger_extras i18n %}
+
+{% block title %}{% Email %}{% endblock %}
+
+{% block content %}
+
+ <p>Bitte wählen Sie die Empfänger</p>
+
+ <h4>Log</h4>
+ <table class="table">
+ <tr>
+ <th>{% trans "Date" %}</th>
+ <th>Typ</th>
+ <th>Betreff</th>
+ </tr>
+ {% for email in email_list %}
+ <tr>
+ <td>{{ email.date }}</td>
+ <td>{{ email.get_type_display }}</td>
+ <td>{{ email.subject }}</td>
+ </tr>
+ {% endfor %}
+ </table>
+
+{% endblock %}
+
+{% block sidebar %}
+{% endblock %}
\ No newline at end of file
diff --git a/wger/email/templates/email/preview.html b/wger/email/templates/email/preview.html
new file mode 100644
index 000000000..5de2e81e8
--- /dev/null
+++ b/wger/email/templates/email/preview.html
@@ -0,0 +1,68 @@
+{% extends "base.html" %}
+{% load wger_extras i18n %}
+
+{% block title %}
+ Emailverteiler (Vorschau) -
+ {% if email_type == 'starter' %}
+ Starter
+ {% else %}
+ Studio
+ {% endif %}
+{% endblock %}
+
+{% block content %}
+
+<h3>Aktueller Entwurf</h3>
+<form action="{{ form_action }}"
+ method="post"
+ class="form-horizontal">
+ {% csrf_token %}
+ <input type="hidden" name="{{ stage_field }}" value="2" />
+ <input type="hidden" name="{{ hash_field }}" value="{{ hash_value }}" />
+ <input type="hidden" name="body" value="{{ form.cleaned_data.body }}" />
+ <input type="hidden" name="subject" value="{{ form.cleaned_data.subject }}" />
+
+ <table class="table">
+ <tr>
+ <th>Betreff</th>
+ <td><pre>{{ form.cleaned_data.subject }}</pre></td>
+ </tr>
+ <tr>
+ <th>Inhalt</th>
+ <td><pre>{{ form.cleaned_data.body }}</pre></td>
+ </tr>
+ <tr>
+ <td colspan="2">
+ {% render_form_submit 'Emails verschicken' %}
+ </td>
+ </tr>
+</table>
+</form>
+
+<h3>Korrigieren</h3>
+<form action="{{ form_action }}"
+ method="post"
+ class="form-horizontal">
+ {% render_form_fields form 'Korrigieren' %}
+</form>
+{% endblock %}
+
+{% block sidebar %}
+
+ <div class="well">
+ <h4>Benutzung</h4>
+ <ol>
+ <li>Geben Sie die nötigen Daten in das Formular ein.</li>
+ <li>Die Manager bekommen eine Vorschau-Email, zur Kontrolle.</li>
+ <li>
+ Sie können beliebig oft den Inhalt korrigieren und über die
+ Vorschau-Emails kontrollieren.
+ </li>
+ <li>
+ Wenn Sie mit dem Inhalt zufrieden sind, können Sie die Emails
+ freigeben, sie weden in den nächsten Studen automatisch
+ verschickt
+ </li>
+ </ol>
+ </div>
+{% endblock %}
\ No newline at end of file
diff --git a/wger/email/urls.py b/wger/email/urls.py
new file mode 100644
index 000000000..f2d5346f7
--- /dev/null
+++ b/wger/email/urls.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+from django.conf.urls import url, include
+
+from wger.email.forms import EmailListForm
+from wger.email.views import email_lists
+
+
+# sub patterns for email lists
+patterns_email = [
+ url(r'^overview/(?P<gym_pk>\d+)$',
+ email_lists.EmailLogListView.as_view(),
+ name='overview'),
+ url(r'^add/(?P<gym_pk>\d+)$',
+ email_lists.EmailListFormPreview(EmailListForm),
+ name='add'),
+]
+
+
+urlpatterns = [
+ url(r'^email/', include(patterns_email, namespace="email")),
+]
diff --git a/wger/email/views/__init__.py b/wger/email/views/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wger/email/views/email_lists.py b/wger/email/views/email_lists.py
new file mode 100644
index 000000000..8ac43b5d2
--- /dev/null
+++ b/wger/email/views/email_lists.py
@@ -0,0 +1,131 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import datetime
+
+from django.conf import settings
+from django.http import HttpResponseRedirect, HttpResponseForbidden
+from django.core import mail
+from django.core.urlresolvers import reverse
+from django.shortcuts import get_object_or_404
+from django.views import generic
+
+from formtools.preview import FormPreview
+
+from wger.gym.models import Gym
+from wger.email.models import CronEntry, Log
+from wger.utils.generic_views import WgerFormMixin
+
+
+class EmailLogListView(WgerFormMixin, generic.ListView):
+ '''
+ Shows a list with all sent emails
+ '''
+
+ model = Log
+ context_object_name = "email_list"
+ template_name = 'email/overview.html'
+ permission_required = 'email.add_emaillog'
+ login_required = True
+
+ def get_queryset(self):
+ '''
+ Can only view emails for own gym
+ '''
+ return Log.objects.filter(gym_id=self.kwargs['gym_pk'])
+
+ def dispatch(self, request, *args, **kwargs):
+ '''
+ Can only add contract types in own gym
+ '''
+ if not request.user.is_authenticated():
+ return HttpResponseForbidden()
+
+ if request.user.userprofile.gym_id != int(self.kwargs['gym_pk']):
+ return HttpResponseForbidden()
+
+ return super(EmailLogListView, self).dispatch(request, *args, **kwargs)
+
+
+class EmailListFormPreview(FormPreview):
+ preview_template = 'email/preview.html'
+ form_template = 'email/form.html'
+ list_type = None
+ gym = None
+
+ def parse_params(self, *args, **kwargs):
+ '''
+ Save the current recipient type
+ '''
+ self.gym = get_object_or_404(Gym, pk=int(kwargs['gym_pk']))
+
+ def get_context(self, request, form):
+ '''
+ Context for template rendering
+
+ Also, check for permissions here. While it is ugly and doesn't really
+ belong here, it seems it's the best way to do it in a FormPreview
+ '''
+ if not request.user.is_authenticated() or\
+ request.user.userprofile.gym_id != self.gym.id or \
+ not request.user.has_perms('core.change_emailcron'):
+ return HttpResponseForbidden()
+
+ context = super(EmailListFormPreview, self).get_context(request, form)
+ context['form_action'] = reverse('email:email:add',
+ kwargs={'gym_pk': self.gym.pk})
+
+ return context
+
+ def process_preview(self, request, form, context):
+ '''
+ Send an email to the managers with the current content
+ '''
+ # for email in User.objects.filter(**managers of gym**):
+ # mail.send_mail(form.cleaned_data['subject'],
+ # form.cleaned_data['body'],
+ # settings.DEFAULT_FROM_EMAIL,
+ # [email.email],
+ # fail_silently=False)
+ return context
+
+ def done(self, request, cleaned_data):
+ '''
+ Collect appropriate emails and save to database to send for later
+ '''
+ emails = []
+
+ # Select all users in the gym
+ # TODO: select only members
+ for member in self.gym.userprofile_set.all():
+ if member.user.email:
+ emails.append(member.user.email)
+
+ # Make list unique, so people don't get duplicate emails
+ emails = list(set(emails))
+
+ # Save an email log...
+ email_log = Log()
+ email_log.gym = self.gym
+ email_log.user = request.user
+ email_log.body = cleaned_data['body']
+ email_log.subject = cleaned_data['subject']
+ email_log.save()
+
+ # ...and bulk create cron entries
+ CronEntry.objects.bulk_create([CronEntry(log=email_log, email=email) for email in emails])
+
+ return HttpResponseRedirect(reverse('email:email:overview'))
diff --git a/wger/gym/templates/gym/member_list.html b/wger/gym/templates/gym/member_list.html
index 7a0458f93..aa29ecefa 100644
--- a/wger/gym/templates/gym/member_list.html
+++ b/wger/gym/templates/gym/member_list.html
@@ -296,4 +296,42 @@ $(document).ready( function () {
{% endfor %}
</table>
{% endif %}
+
+
+
+{# #}
+{# Email lists #}
+{# #}
+{% if perms.email.change_log %}
+<div class="btn-group pull-right">
+ <button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown">
+ {% trans "Actions" %} <span class="caret"></span>
+ </button>
+ <ul class="dropdown-menu" role="menu">
+ <li>
+ <a href="{% url 'email:email:add' gym.id %}">{% trans "Add"%}</a>
+ </li>
+ </ul>
+</div>
+
+<h4>{% trans "Emails" %}</h4>
+<table class="table">
+ {% for email in gym.email_log.all %}
+ <tr>
+ <td>
+ {{ email.date }}
+ </td>
+ <td>
+ {{ email.subject|truncatewords:15 }}
+ </td>
+ </tr>
+ {% empty %}
+ <tr>
+ <td>
+ {% trans "Nothing found" %}
+ </td>
+ </tr>
+ {% endfor %}
+</table>
+{% endif %}
{% endblock %}
diff --git a/wger/settings_global.py b/wger/settings_global.py
index a523b67f8..23edf736b 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -59,6 +59,7 @@ INSTALLED_APPS = (
'wger.utils',
'wger.config',
'wger.gym',
+ 'wger.email',
# reCaptcha support, see https://github.com/praekelt/django-recaptcha
'captcha',
diff --git a/wger/urls.py b/wger/urls.py
index 884ab09d9..4380a554e 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -139,6 +139,7 @@ urlpatterns = i18n_patterns(
url(r'software/', include('wger.software.urls', namespace='software', app_name='software')),
url(r'config/', include('wger.config.urls', namespace='config', app_name='config')),
url(r'gym/', include('wger.gym.urls', namespace='gym', app_name='gym')),
+ url(r'email/', include('wger.email.urls', namespace='email')),
url(r'^browserid/', include('django_browserid.urls')),
url(r'^sitemap\.xml$',
'django.contrib.sitemaps.views.sitemap',