Completed
Push — master ( 856bc1...ee6984 )
by Fox
40s
created

Command.handle()   B

Complexity

Conditions 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 2
c 4
b 1
f 0
dl 0
loc 27
rs 8.8571
1
#!/usr/bin/env python
2
# coding: utf-8
3
from __future__ import unicode_literals
4
import arrow
5
6
from django.conf import settings
7
from django.core.management.base import BaseCommand
8
from django.core.mail import send_mail
9
from django.template.loader import render_to_string
10
11
from django_th.models import Digest
12
13
14
class Command(BaseCommand):
15
16
    help = 'Trigger all data from cache in version 2'
17
18
    def handle(self, *args, **options):
19
        """
20
            get all the digest data to send to each user
21
        """
22
        now = arrow.utcnow().to(settings.TIME_ZONE)
23
        now = now.date()
24
25
        digest = Digest.objects.filter(date_end=str(now)).order_by('user',
26
                                                                   'date_end')
27
        users = digest.distinct('user')
28
29
        subject = 'Your digester'
30
31
        msg_plain = render_to_string('digest/email.txt',
32
                                     {'digest': digest,
33
                                      'subject': subject})
34
        msg_html = render_to_string('digest/email.html',
35
                                    {'digest': digest,
36
                                     'subject': subject})
37
        message = msg_plain
38
        from_email = settings.ADMINS
39
        recipient_list = ()
40
        for user in users:
41
            recipient_list += (user.user.email,)
42
43
        send_mail(subject, message, from_email, recipient_list,
44
                  html_message=msg_html)
45