models.py 4.81 KB
Newer Older
Skia's avatar
Skia committed
1
from django.shortcuts import render
Skia's avatar
Skia committed
2
from django.db import models, transaction
Skia's avatar
Skia committed
3
from django.utils.translation import ugettext_lazy as _
4
from django.core.urlresolvers import reverse_lazy, reverse
Skia's avatar
Skia committed
5
from django.conf import settings
Skia's avatar
Skia committed
6
from django.core.mail import EmailMultiAlternatives
Skia's avatar
Skia committed
7

Skia's avatar
Skia committed
8
from core.models import User, Preferences
9
10
from club.models import Club

Skia's avatar
Skia committed
11
class Sith(models.Model):
12
    """A one instance class storing all the modifiable infos"""
Skia's avatar
Skia committed
13
14
15
    alert_msg = models.TextField(_("alert message"), default="", blank=True)
    info_msg = models.TextField(_("info message"), default="", blank=True)
    index_page = models.TextField(_("index page"), default="", blank=True)
Skia's avatar
Skia committed
16
    weekmail_destinations = models.TextField(_("weekmail destinations"), default="")
Skia's avatar
Skia committed
17
18
19
20
21
22
23

    def is_owned_by(self, user):
        return user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID)

    def __str__(self):
        return "⛩ Sith ⛩"

24
25
26
27
28
29
30
31
32
33
34
35
36
37
NEWS_TYPES = [
        ('NOTICE', _('Notice')),
        ('EVENT', _('Event')),
        ('WEEKLY', _('Weekly')),
        ('CALL', _('Call')),
        ]

class News(models.Model):
    """The news class"""
    title = models.CharField(_("title"), max_length=64)
    summary = models.TextField(_("summary"))
    content = models.TextField(_("content"))
    type = models.CharField(_("type"), max_length=16, choices=NEWS_TYPES, default="EVENT")
    club = models.ForeignKey(Club, related_name="news", verbose_name=_("club"))
Skia's avatar
Skia committed
38
    author = models.ForeignKey(User, related_name="owned_news", verbose_name=_("author"))
39
    is_moderated = models.BooleanField(_("is moderated"), default=False)
Skia's avatar
Skia committed
40
41
42
43
44
45
46
47
48
49
    moderator = models.ForeignKey(User, related_name="moderated_news", verbose_name=_("moderator"), null=True)

    def is_owned_by(self, user):
        return user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) or user == self.author

    def can_be_edited_by(self, user):
        return user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID)

    def can_be_viewed_by(self, user):
        return self.is_moderated or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID)
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

    def get_absolute_url(self):
        return reverse('com:news_detail', kwargs={'news_id': self.id})

    def __str__(self):
        return "%s: %s" % (self.type, self.title)

class NewsDate(models.Model):
    """
    A date class, useful for weekly events, or for events that just have no date

    This class allows more flexibilty managing the dates related to a news, particularly when this news is weekly, since
    we don't have to make copies
    """
    news = models.ForeignKey(News, related_name="dates", verbose_name=_("news_date"))
    start_date = models.DateTimeField(_('start_date'), null=True, blank=True)
    end_date = models.DateTimeField(_('end_date'), null=True, blank=True)

    def __str__(self):
        return "%s: %s - %s" % (self.news.title, self.start_date, self.end_date)
Skia's avatar
Skia committed
70
71
72
73
74

class Weekmail(models.Model):
    """
    The weekmail class
    """
Skia's avatar
Skia committed
75
    title = models.CharField(_("title"), max_length=64, blank=True)
Skia's avatar
Skia committed
76
77
78
79
80
81
82
83
84
85
    intro = models.TextField(_("intro"), blank=True)
    joke = models.TextField(_("joke"), blank=True)
    protip = models.TextField(_("protip"), blank=True)
    conclusion = models.TextField(_("conclusion"), blank=True)
    sent = models.BooleanField(_("sent"), default=False)

    class Meta:
        ordering = ['-id']

    def send(self):
Skia's avatar
Skia committed
86
        dest = [i[0] for i in Preferences.objects.filter(receive_weekmail=True).values_list('user__email')]
Skia's avatar
Skia committed
87
        with transaction.atomic():
Skia's avatar
Skia committed
88
            email = EmailMultiAlternatives(
Skia's avatar
Skia committed
89
                    subject=self.title,
Skia's avatar
Skia committed
90
                    body=self.render_text(),
Skia's avatar
Skia committed
91
                    from_email=settings.DEFAULT_FROM_EMAIL,
Skia's avatar
Skia committed
92
93
                    to=Sith.objects.first().weekmail_destinations.split(' '),
                    bcc=dest,
Skia's avatar
Skia committed
94
                    )
Skia's avatar
Skia committed
95
            email.attach_alternative(self.render_html(), "text/html")
Skia's avatar
Skia committed
96
            email.send()
Skia's avatar
Skia committed
97
98
99
            self.sent = True
            self.save()
            Weekmail().save()
Skia's avatar
Skia committed
100

Skia's avatar
Skia committed
101
102
    def render_text(self):
        return render(None, "com/weekmail_renderer_text.jinja", context={
Skia's avatar
Skia committed
103
104
105
            'weekmail': self,
            }).content.decode('utf-8')

Skia's avatar
Skia committed
106
107
108
109
110
111
112
113
    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)

Skia's avatar
Skia committed
114
class WeekmailArticle(models.Model):
Skia's avatar
Skia committed
115
    weekmail = models.ForeignKey(Weekmail, related_name="articles", verbose_name=_("weekmail"), null=True)
Skia's avatar
Skia committed
116
117
118
119
120
121
    title = models.CharField(_("title"), max_length=64)
    content = models.TextField(_("content"))
    author = models.ForeignKey(User, related_name="owned_weekmail_articles", verbose_name=_("author"))
    club = models.ForeignKey(Club, related_name="weekmail_articles", verbose_name=_("club"))
    rank = models.IntegerField(_('rank'), default=-1)