Commit 83555a36 authored by Skia's avatar Skia Committed by Skia
Browse files

Add preferences and improve weekmail

parent d988c093
...@@ -3,9 +3,9 @@ from django.db import models, transaction ...@@ -3,9 +3,9 @@ from django.db import models, transaction
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse_lazy, reverse from django.core.urlresolvers import reverse_lazy, reverse
from django.conf import settings from django.conf import settings
from django.core.mail import EmailMessage from django.core.mail import EmailMultiAlternatives
from core.models import User from core.models import User, Preferences
from club.models import Club from club.models import Club
class Sith(models.Model): class Sith(models.Model):
...@@ -83,26 +83,34 @@ class Weekmail(models.Model): ...@@ -83,26 +83,34 @@ class Weekmail(models.Model):
ordering = ['-id'] ordering = ['-id']
def send(self): def send(self):
dest = [i[0] for i in Preferences.objects.filter(receive_weekmail=True).values_list('user__email')]
with transaction.atomic(): with transaction.atomic():
print("Sending weekmail n°" + str(self.id)) email = EmailMultiAlternatives(
email = EmailMessage(
subject=self.title, subject=self.title,
body=self.render(), body=self.render_text(),
from_email=settings.DEFAULT_FROM_EMAIL, from_email=settings.DEFAULT_FROM_EMAIL,
to=['skia@git.an'], to=Sith.objects.first().weekmail_destinations.split(' '),
bcc=Sith.objects.first().weekmail_destinations.split(' '), bcc=dest,
# TODO: Content-Type: text/html
) )
email.attach_alternative(self.render_html(), "text/html")
email.send() email.send()
self.sent = True self.sent = True
self.save() self.save()
Weekmail().save() Weekmail().save()
def render(self): def render_text(self):
return render(None, "com/weekmail_renderer.jinja", context={ return render(None, "com/weekmail_renderer_text.jinja", context={
'weekmail': self, 'weekmail': self,
}).content.decode('utf-8') }).content.decode('utf-8')
def render_html(self):
return render(None, "com/weekmail_renderer_html.jinja", context={
'weekmail': self,
}).content.decode('utf-8')
def __str__(self):
return "Weekmail %s (sent: %s) - %s" % (self.id, self.sent, self.title)
class WeekmailArticle(models.Model): class WeekmailArticle(models.Model):
weekmail = models.ForeignKey(Weekmail, related_name="articles", verbose_name=_("weekmail"), null=True) weekmail = models.ForeignKey(Weekmail, related_name="articles", verbose_name=_("weekmail"), null=True)
title = models.CharField(_("title"), max_length=64) title = models.CharField(_("title"), max_length=64)
......
...@@ -9,26 +9,26 @@ ...@@ -9,26 +9,26 @@
<ul> <ul>
{% for a in weekmail.articles.all() %} {% for a in weekmail.articles.all() %}
<li>[{{ a.club }}] {{ a.title }}</li> <li>[{{ a.club }}] {{ a.title }}</li>
{% endfor %} {%- endfor %}
</ul> </ul>
{% for a in weekmail.articles.all() %} {%- for a in weekmail.articles.all() %}
<h3>[{{ a.club }}] {{ a.title }}</h3> <h3>[{{ a.club }}] {{ a.title }}</h3>
{{ a.content|markdown }} {{ a.content|markdown }}
{% endfor %} {%- endfor -%}
{% if weekmail.joke %} {%- if weekmail.joke %}
<h3>{% trans %}Joke{% endtrans %}</h3> <h3>{% trans %}Joke{% endtrans %}</h3>
{{ weekmail.joke|markdown }} {{ weekmail.joke|markdown }}
{% endif %} {% endif -%}
{% if weekmail.protip %} {%- if weekmail.protip %}
<h3>{% trans %}Pro tip{% endtrans %}</h3> <h3>{% trans %}Pro tip{% endtrans %}</h3>
{{ weekmail.protip|markdown }} {{ weekmail.protip|markdown }}
{% endif %} {% endif -%}
{% if weekmail.conclusion %} {%- if weekmail.conclusion %}
<h3>{% trans %}Final word{% endtrans %}</h3> <h3>{% trans %}Final word{% endtrans %}</h3>
{{ weekmail.conclusion|markdown }} {{ weekmail.conclusion|markdown }}
{% endif %} {% endif -%}
# {{ weekmail.title }}
{%- if weekmail.intro %}
## {% trans %}Intro{% endtrans %}
{{ weekmail.intro }}
{% endif %}
## {% trans %}Table of content{% endtrans %}
{% for a in weekmail.articles.all() %}
* [{{ a.club }}] {{ a.title }}
{% endfor -%}
{% for a in weekmail.articles.all() %}
## [{{ a.club }}] {{ a.title }}
{{ a.content }}
{% endfor -%}
{%- if weekmail.joke %}
## {% trans %}Joke{% endtrans %}
{{ weekmail.joke }}
{% endif -%}
{%- if weekmail.protip %}
## {% trans %}Pro tip{% endtrans %}
{{ weekmail.protip }}
{% endif -%}
{%- if weekmail.conclusion %}
## {% trans %}Final word{% endtrans %}
{{ weekmail.conclusion }}
{% endif -%}
...@@ -6,6 +6,7 @@ urlpatterns = [ ...@@ -6,6 +6,7 @@ urlpatterns = [
url(r'^sith/edit/alert$', AlertMsgEditView.as_view(), name='alert_edit'), url(r'^sith/edit/alert$', AlertMsgEditView.as_view(), name='alert_edit'),
url(r'^sith/edit/info$', InfoMsgEditView.as_view(), name='info_edit'), url(r'^sith/edit/info$', InfoMsgEditView.as_view(), name='info_edit'),
url(r'^sith/edit/index$', IndexEditView.as_view(), name='index_edit'), url(r'^sith/edit/index$', IndexEditView.as_view(), name='index_edit'),
url(r'^sith/edit/weekmail_destinations$', WeekmailDestinationEditView.as_view(), name='weekmail_destinations'),
url(r'^weekmail$', WeekmailEditView.as_view(), name='weekmail'), url(r'^weekmail$', WeekmailEditView.as_view(), name='weekmail'),
url(r'^weekmail/preview$', WeekmailPreviewView.as_view(), name='weekmail_preview'), url(r'^weekmail/preview$', WeekmailPreviewView.as_view(), name='weekmail_preview'),
url(r'^weekmail/club/(?P<club_id>[0-9]+)/new_article$', WeekmailArticleCreateView.as_view(), name='weekmail_article'), url(r'^weekmail/club/(?P<club_id>[0-9]+)/new_article$', WeekmailArticleCreateView.as_view(), name='weekmail_article'),
......
...@@ -34,6 +34,11 @@ class ComTabsMixin(TabedViewMixin): ...@@ -34,6 +34,11 @@ class ComTabsMixin(TabedViewMixin):
'slug': 'weekmail', 'slug': 'weekmail',
'name': _("Weekmail"), 'name': _("Weekmail"),
}) })
tab_list.append({
'url': reverse('com:weekmail_destinations'),
'slug': 'weekmail_destinations',
'name': _("Weekmail destinations"),
})
tab_list.append({ tab_list.append({
'url': reverse('com:index_edit'), 'url': reverse('com:index_edit'),
'slug': 'index', 'slug': 'index',
...@@ -73,6 +78,11 @@ class IndexEditView(ComEditView): ...@@ -73,6 +78,11 @@ class IndexEditView(ComEditView):
current_tab = "index" current_tab = "index"
success_url = reverse_lazy('com:index_edit') success_url = reverse_lazy('com:index_edit')
class WeekmailDestinationEditView(ComEditView):
fields = ['weekmail_destinations']
current_tab = "weekmail_destinations"
success_url = reverse_lazy('com:weekmail_destinations')
# News # News
class NewsForm(forms.ModelForm): class NewsForm(forms.ModelForm):
...@@ -232,14 +242,15 @@ class WeekmailPreviewView(DetailView): ...@@ -232,14 +242,15 @@ class WeekmailPreviewView(DetailView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
"""Add rendered weekmail""" """Add rendered weekmail"""
kwargs = super(WeekmailPreviewView, self).get_context_data(**kwargs) kwargs = super(WeekmailPreviewView, self).get_context_data(**kwargs)
kwargs['weekmail_rendered'] = self.object.render() kwargs['weekmail_rendered'] = self.object.render_html()
return kwargs return kwargs
class WeekmailEditView(QuickNotifMixin, UpdateView): class WeekmailEditView(ComTabsMixin, QuickNotifMixin, UpdateView):
model = Weekmail model = Weekmail
template_name = 'com/weekmail.jinja' template_name = 'com/weekmail.jinja'
fields = ['title', 'intro', 'joke', 'protip', 'conclusion'] fields = ['title', 'intro', 'joke', 'protip', 'conclusion']
success_url = reverse_lazy('com:weekmail') success_url = reverse_lazy('com:weekmail')
current_tab = "weekmail"
def get_object(self, queryset=None): def get_object(self, queryset=None):
weekmail = self.model.objects.filter(sent=False).order_by('-id').first() weekmail = self.model.objects.filter(sent=False).order_by('-id').first()
...@@ -291,7 +302,7 @@ class WeekmailEditView(QuickNotifMixin, UpdateView): ...@@ -291,7 +302,7 @@ class WeekmailEditView(QuickNotifMixin, UpdateView):
kwargs['orphans'] = WeekmailArticle.objects.filter(weekmail=None) kwargs['orphans'] = WeekmailArticle.objects.filter(weekmail=None)
return kwargs return kwargs
class WeekmailArticleEditView(QuickNotifMixin, UpdateView): class WeekmailArticleEditView(ComTabsMixin, QuickNotifMixin, UpdateView):
"""Edit an article""" """Edit an article"""
model = WeekmailArticle model = WeekmailArticle
fields = ['title', 'content'] fields = ['title', 'content']
...@@ -299,6 +310,7 @@ class WeekmailArticleEditView(QuickNotifMixin, UpdateView): ...@@ -299,6 +310,7 @@ class WeekmailArticleEditView(QuickNotifMixin, UpdateView):
template_name = 'core/edit.jinja' template_name = 'core/edit.jinja'
success_url = reverse_lazy('com:weekmail') success_url = reverse_lazy('com:weekmail')
quick_notif_url_arg = "qn_weekmail_article_edit" quick_notif_url_arg = "qn_weekmail_article_edit"
current_tab = "weekmail"
class WeekmailArticleCreateView(QuickNotifMixin, CreateView): class WeekmailArticleCreateView(QuickNotifMixin, CreateView):
"""Post an article""" """Post an article"""
......
...@@ -86,7 +86,7 @@ class Command(BaseCommand): ...@@ -86,7 +86,7 @@ class Command(BaseCommand):
home_root.save() home_root.save()
club_root.save() club_root.save()
Sith().save() Sith(weekmail_destinations="etudiants@git.an personnel@git.an").save()
Weekmail().save() Weekmail().save()
p = Page(name='Index') p = Page(name='Index')
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0018_auto_20161224_0211'),
]
operations = [
migrations.AddField(
model_name='preferences',
name='receive_weekmail',
field=models.BooleanField(verbose_name='define if we want to receive the weekmail', default=False, help_text='Do you want to receive the weekmail'),
),
]
...@@ -487,12 +487,23 @@ class AnonymousUser(AuthAnonymousUser): ...@@ -487,12 +487,23 @@ class AnonymousUser(AuthAnonymousUser):
class Preferences(models.Model): class Preferences(models.Model):
user = models.OneToOneField(User, related_name="preferences") user = models.OneToOneField(User, related_name="preferences")
receive_weekmail = models.BooleanField(
_('do you want to receive the weekmail'),
default=False,
# help_text=_('Do you want to receive the weekmail?'),
)
show_my_stats = models.BooleanField( show_my_stats = models.BooleanField(
_('define if we show a users stats'), _('define if we show a users stats'),
default=False, default=False,
help_text=_('Show your account statistics to others'), help_text=_('Show your account statistics to others'),
) )
def get_display_name(self):
return self.user.get_display_name()
def get_absolute_url(self):
return self.user.get_absolute_url()
def get_directory(instance, filename): def get_directory(instance, filename):
return '.{0}/{1}'.format(instance.get_parent_path(), filename) return '.{0}/{1}'.format(instance.get_parent_path(), filename)
......
...@@ -67,6 +67,7 @@ ...@@ -67,6 +67,7 @@
<ul> <ul>
{% if user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) or user.is_root %} {% if user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) or user.is_root %}
<li><a href="{{ url('com:weekmail') }}">{% trans %}Weekmail{% endtrans %}</a></li> <li><a href="{{ url('com:weekmail') }}">{% trans %}Weekmail{% endtrans %}</a></li>
<li><a href="{{ url('com:weekmail_destinations') }}">{% trans %}Weekmail destinations{% endtrans %}</a></li>
<li><a href="{{ url('com:news_admin_list') }}">{% trans %}Moderate news{% endtrans %}</a></li> <li><a href="{{ url('com:news_admin_list') }}">{% trans %}Moderate news{% endtrans %}</a></li>
<li><a href="{{ url('com:index_edit') }}">{% trans %}Edit index page{% endtrans %}</a></li> <li><a href="{{ url('com:index_edit') }}">{% trans %}Edit index page{% endtrans %}</a></li>
<li><a href="{{ url('com:alert_edit') }}">{% trans %}Edit alert message{% endtrans %}</a></li> <li><a href="{{ url('com:alert_edit') }}">{% trans %}Edit alert message{% endtrans %}</a></li>
......
...@@ -40,6 +40,7 @@ urlpatterns = [ ...@@ -40,6 +40,7 @@ urlpatterns = [
url(r'^user/(?P<user_id>[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'), url(r'^user/(?P<user_id>[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'),
url(r'^user/(?P<user_id>[0-9]+)/profile_upload$', UserUploadProfilePictView.as_view(), name='user_profile_upload'), url(r'^user/(?P<user_id>[0-9]+)/profile_upload$', UserUploadProfilePictView.as_view(), name='user_profile_upload'),
url(r'^user/(?P<user_id>[0-9]+)/clubs$', UserClubView.as_view(), name='user_clubs'), url(r'^user/(?P<user_id>[0-9]+)/clubs$', UserClubView.as_view(), name='user_clubs'),
url(r'^user/(?P<user_id>[0-9]+)/prefs$', UserPreferencesView.as_view(), name='user_prefs'),
url(r'^user/(?P<user_id>[0-9]+)/groups$', UserUpdateGroupView.as_view(), name='user_groups'), url(r'^user/(?P<user_id>[0-9]+)/groups$', UserUpdateGroupView.as_view(), name='user_groups'),
url(r'^user/tools/$', UserToolsView.as_view(), name='user_tools'), url(r'^user/tools/$', UserToolsView.as_view(), name='user_tools'),
url(r'^user/(?P<user_id>[0-9]+)/account$', UserAccountView.as_view(), name='user_account'), url(r'^user/(?P<user_id>[0-9]+)/account$', UserAccountView.as_view(), name='user_account'),
......
...@@ -19,7 +19,7 @@ import logging ...@@ -19,7 +19,7 @@ import logging
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin
from core.views.forms import RegisteringForm, UserPropForm, UserProfileForm, LoginForm, UserGodfathersForm from core.views.forms import RegisteringForm, UserPropForm, UserProfileForm, LoginForm, UserGodfathersForm
from core.models import User, SithFile from core.models import User, SithFile, Preferences
from club.models import Club from club.models import Club
from subscription.models import Subscription from subscription.models import Subscription
...@@ -151,6 +151,11 @@ class UserTabsMixin(TabedViewMixin): ...@@ -151,6 +151,11 @@ class UserTabsMixin(TabedViewMixin):
'slug': 'edit', 'slug': 'edit',
'name': _("Edit"), 'name': _("Edit"),
}) })
tab_list.append({
'url': reverse('core:user_prefs', kwargs={'user_id': self.object.id}),
'slug': 'prefs',
'name': _("Preferences"),
})
if self.request.user.can_view(self.object): if self.request.user.can_view(self.object):
tab_list.append({ tab_list.append({
'url': reverse('core:user_clubs', kwargs={'user_id': self.object.id}), 'url': reverse('core:user_clubs', kwargs={'user_id': self.object.id}),
...@@ -378,6 +383,26 @@ class UserClubView(UserTabsMixin, CanViewMixin, DetailView): ...@@ -378,6 +383,26 @@ class UserClubView(UserTabsMixin, CanViewMixin, DetailView):
template_name = "core/user_clubs.jinja" template_name = "core/user_clubs.jinja"
current_tab = "clubs" current_tab = "clubs"
class UserPreferencesView(UserTabsMixin, CanEditMixin, UpdateView):
"""
Edit a user's preferences
"""
model = Preferences
pk_url_kwarg = "user_id"
template_name = "core/edit.jinja"
fields = ['receive_weekmail']
context_object_name = "profile"
current_tab = "prefs"
def get_object(self, queryset=None):
user = get_object_or_404(User, pk=self.kwargs['user_id'])
try:
return user.preferences
except:
pref = Preferences(user=user)
pref.save()
return pref
class UserUpdateGroupView(UserTabsMixin, CanEditPropMixin, UpdateView): class UserUpdateGroupView(UserTabsMixin, CanEditPropMixin, UpdateView):
""" """
Edit a user's groups Edit a user's groups
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment