|
|
@@ -2,9 +2,11 @@ from datetime import datetime, timedelta
|
|
2
|
2
|
import copy
|
|
3
|
3
|
|
|
4
|
4
|
from django.contrib.auth.decorators import login_required
|
|
|
5
|
+from django.core.mail import EmailMessage
|
|
5
|
6
|
from django.core.urlresolvers import reverse
|
|
6
|
7
|
from django.http import HttpResponseRedirect
|
|
7
|
8
|
from django.shortcuts import render
|
|
|
9
|
+from django.template.loader import render_to_string
|
|
8
|
10
|
from django.utils.translation import ugettext as _, get_language
|
|
9
|
11
|
|
|
10
|
12
|
import arrow
|
|
|
@@ -13,6 +15,7 @@ from graphos.renderers import gchart
|
|
13
|
15
|
from graphos.sources.model import ModelDataSource
|
|
14
|
16
|
|
|
15
|
17
|
from counter.models import *
|
|
|
18
|
+from counter.utils import parseSeumReason
|
|
16
|
19
|
|
|
17
|
20
|
|
|
18
|
21
|
@login_required
|
|
|
@@ -101,3 +104,55 @@ def get(request, id_counter):
|
|
101
|
104
|
'seumFrequency': seumFrequency,
|
|
102
|
105
|
'myCounter': myCounter,
|
|
103
|
106
|
})
|
|
|
107
|
+
|
|
|
108
|
+
|
|
|
109
|
+@login_required
|
|
|
110
|
+def reset_counter(request):
|
|
|
111
|
+ # Update Form counter
|
|
|
112
|
+ if request.method == 'POST':
|
|
|
113
|
+ # create a form instance and populate it with data from the request:
|
|
|
114
|
+ data = dict(request.POST)
|
|
|
115
|
+
|
|
|
116
|
+ who = Counter.objects.get(pk=int(data['who'][0]))
|
|
|
117
|
+ reason = data['reason'][0]
|
|
|
118
|
+ if 'counter' in data.keys():
|
|
|
119
|
+ counter = Counter.objects.get(pk=int(data['counter'][0]))
|
|
|
120
|
+ else:
|
|
|
121
|
+ try:
|
|
|
122
|
+ counter = Counter.objects.get(trigramme=data['trigramme'][0])
|
|
|
123
|
+ except Counter.DoesNotExist:
|
|
|
124
|
+ return HttpResponseRedirect(data['redirect'][0])
|
|
|
125
|
+
|
|
|
126
|
+ reset = Reset(counter=counter, who=who, reason=data['reason'][0])
|
|
|
127
|
+
|
|
|
128
|
+ # we check that the seumer is the autenticated user
|
|
|
129
|
+ if reset.who.user is None or reset.who.user != request.user:
|
|
|
130
|
+ return HttpResponseRedirect(data['redirect'][0])
|
|
|
131
|
+
|
|
|
132
|
+ reset.save()
|
|
|
133
|
+
|
|
|
134
|
+ # Now we deal with the hashtags
|
|
|
135
|
+ keywords = parseSeumReason(reason)
|
|
|
136
|
+ Hashtag.objects.bulk_create([Hashtag(reset=reset, keyword=keyword) for keyword in keywords])
|
|
|
137
|
+
|
|
|
138
|
+ # We send the emails only to those who want
|
|
|
139
|
+ emails = [u['email'] for u in Counter.objects.filter(email_notifications=True).values('email')]
|
|
|
140
|
+ # Now send emails to everyone
|
|
|
141
|
+ if reset.who is None or reset.who == counter:
|
|
|
142
|
+ selfSeum = True
|
|
|
143
|
+ else:
|
|
|
144
|
+ selfSeum = False
|
|
|
145
|
+ text_of_email = render_to_string(
|
|
|
146
|
+ 'seumEmail.txt', {'reason': data['reason'][0],
|
|
|
147
|
+ 'name': counter.name,
|
|
|
148
|
+ 'who': reset.who,
|
|
|
149
|
+ 'selfSeum': selfSeum,
|
|
|
150
|
+ })
|
|
|
151
|
+ email_to_send = EmailMessage(
|
|
|
152
|
+ '[SeumBook] ' + counter.trigramme + ' a le seum',
|
|
|
153
|
+ text_of_email,
|
|
|
154
|
+ 'SeumMan <seum@merigoux.ovh>', emails, [],
|
|
|
155
|
+ reply_to=emails)
|
|
|
156
|
+ email_to_send.send(fail_silently=True)
|
|
|
157
|
+
|
|
|
158
|
+ return HttpResponseRedirect(data['redirect'][0])
|