diff --git a/com/models.py b/com/models.py index 65f8558e..90d62b82 100644 --- a/com/models.py +++ b/com/models.py @@ -1,7 +1,8 @@ -from django.db import models +from django.db import models, transaction from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse_lazy, reverse from django.conf import settings +from django.core.mail import EmailMessage from core.models import User from club.models import Club @@ -70,7 +71,7 @@ class Weekmail(models.Model): """ The weekmail class """ - title = models.CharField(_("title"), max_length=64) + title = models.CharField(_("title"), max_length=64, blank=True) intro = models.TextField(_("intro"), blank=True) joke = models.TextField(_("joke"), blank=True) protip = models.TextField(_("protip"), blank=True) @@ -81,9 +82,18 @@ class Weekmail(models.Model): ordering = ['-id'] def send(self): - print("Sending weekmail n°" + str(self.id)) - self.sent = True - self.save() + with transaction.atomic(): + print("Sending weekmail n°" + str(self.id)) + email = EmailMessage( + subject=self.title, + body="\n\n".join([self.intro, self.joke, self.protip, self.conclusion]), + from_email=settings.DEFAULT_FROM_EMAIL, + to=[], + bcc=Sith.objects.first().weekmail_destinations.split(' '), + ) + self.sent = True + self.save() + Weekmail().save() class WeekmailArticle(models.Model): weekmail = models.ForeignKey(Weekmail, related_name="articles", verbose_name=_("weekmail")) diff --git a/com/templates/com/weekmail.jinja b/com/templates/com/weekmail.jinja index dfe2b57c..60811b73 100644 --- a/com/templates/com/weekmail.jinja +++ b/com/templates/com/weekmail.jinja @@ -5,8 +5,13 @@ {% endblock %} {% block content %} -

{% trans %}Weekmail{% endtrans %}

- {{ object }} +

{% trans %}Weekmail{% endtrans %}

+{{ object }} +
+ {% csrf_token %} + {{ form.as_p() }} +

+
{% endblock %} diff --git a/com/urls.py b/com/urls.py index 731e3327..c63230c1 100644 --- a/com/urls.py +++ b/com/urls.py @@ -7,6 +7,7 @@ urlpatterns = [ url(r'^sith/edit/info$', InfoMsgEditView.as_view(), name='info_edit'), url(r'^sith/edit/index$', IndexEditView.as_view(), name='index_edit'), url(r'^weekmail$', WeekmailEditView.as_view(), name='weekmail'), + url(r'^weekmail/club/(?P[0-9]+)/new_article$', WeekmailArticleCreateView.as_view(), name='weekmail_article'), url(r'^news$', NewsListView.as_view(), name='news_list'), url(r'^news/admin$', NewsAdminListView.as_view(), name='news_admin_list'), url(r'^news/create$', NewsCreateView.as_view(), name='news_new'), diff --git a/com/views.py b/com/views.py index ee33491a..2dba00ad 100644 --- a/com/views.py +++ b/com/views.py @@ -223,8 +223,14 @@ class NewsDetailView(CanViewMixin, DetailView): class WeekmailEditView(UpdateView): model = Weekmail template_name = 'com/weekmail.jinja' + fields = ['title', 'intro', 'joke', 'protip', 'conclusion'] + success_url = reverse_lazy('com:weekmail') def get_object(self, queryset=None): - return self.model.objects.order_by('-id').first() + weekmail = self.model.objects.filter(sent=False).order_by('-id').first() + if not weekmail.title: + now = timezone.now() + weekmail.title = _("Weekmail of the ") + (now + timedelta(days=6 - now.weekday())).strftime('%d/%m/%Y') + return weekmail diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 4f57c253..5b30f2b8 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -16,7 +16,7 @@ from core.utils import resize_image from club.models import Club, Membership from subscription.models import Subscription from counter.models import Customer, ProductType, Product, Counter -from com.models import Sith +from com.models import Sith, Weekmail from election.models import Election, Role, Candidature, ElectionList @@ -87,6 +87,7 @@ class Command(BaseCommand): club_root.save() Sith().save() + Weekmail().save() p = Page(name='Index') p.set_lock(root)