Ver código fonte

Bugfix (login required but Telegram bot doesn't provide it)

Denis Merigoux 8 anos atrás
pai
commit
2a4122ecf7
2 arquivos alterados com 34 adições e 33 exclusões
  1. 2 6
      bot/views/telegram.py
  2. 32 27
      counter/views/counter.py

+ 2 - 6
bot/views/telegram.py

9
 from django.dispatch import receiver
9
 from django.dispatch import receiver
10
 from django.db.utils import IntegrityError
10
 from django.db.utils import IntegrityError
11
 from django.conf import settings
11
 from django.conf import settings
12
-from counter.views.counter import reset_counter
12
+from counter.views.counter import perform_reset
13
 
13
 
14
 import json
14
 import json
15
 import requests
15
 import requests
160
                 yes_counter = telegram_user.counter
160
                 yes_counter = telegram_user.counter
161
                 seum_message = m
161
                 seum_message = m
162
 
162
 
163
-            reset_counter({
164
-                'who': telegram_ser.counter.id,
165
-                'reason': seum_message,
166
-                'counter': yes_counter.id
167
-            })
163
+            reset_counter(telegram_ser.counter, binyes_counter, seum_message,)
168
     except TelegramUser.DoesNotExist:
164
     except TelegramUser.DoesNotExist:
169
         print('in that case we send a link to the user')
165
         print('in that case we send a link to the user')
170
         if chat['type'] == 'private' and chat['id'] == telegram_user_id:
166
         if chat['type'] == 'private' and chat['id'] == telegram_user_id:

+ 32 - 27
counter/views/counter.py

126
             except Counter.DoesNotExist:
126
             except Counter.DoesNotExist:
127
                 return HttpResponseRedirect(data['redirect'][0])
127
                 return HttpResponseRedirect(data['redirect'][0])
128
 
128
 
129
-        reset = Reset(counter=counter, who=who, reason=data['reason'][0])
130
-
131
         # we check that the seumer is the autenticated user
129
         # we check that the seumer is the autenticated user
132
-        if reset.who.user is None or reset.who.user != request.user:
130
+        if who.user is None or who.user != request.user:
133
             return HttpResponseRedirect(data['redirect'][0])
131
             return HttpResponseRedirect(data['redirect'][0])
134
 
132
 
135
-        reset.save()
133
+        reason = data['reason'][0]
134
+        perform_reset(who, counter, reason)
136
 
135
 
137
-        # Now we deal with the hashtags
138
-        keywords = parseSeumReason(reason)
139
-        Hashtag.objects.bulk_create([Hashtag(reset=reset, keyword=keyword) for keyword in keywords])
136
+    return HttpResponseRedirect(data['redirect'][0])
140
 
137
 
141
-        # We send the emails only to those who want
142
-        emails = [u['email'] for u in Counter.objects.filter(email_notifications=True).values('email')]
143
-        # Now send emails to everyone
144
-        if reset.who is None or reset.who == counter:
145
-            selfSeum = True
146
-        else:
147
-            selfSeum = False
148
-        text_of_email = render_to_string(
149
-            'seumEmail.txt', {'reason': data['reason'][0],
150
-                              'name': counter.name,
151
-                              'who': reset.who,
152
-                              'selfSeum': selfSeum,
153
-                              })
154
-        email_to_send = EmailMessage(
155
-            '[SeumBook] ' + counter.trigramme + ' a le seum',
156
-            text_of_email,
157
-            'SeumMan <seum@merigoux.ovh>', emails, [],
158
-            reply_to=emails)
159
-        email_to_send.send(fail_silently=True)
160
 
138
 
161
-    return HttpResponseRedirect(data['redirect'][0])
139
+def perform_reset(who, counter, reason):
140
+    reset = Reset(counter=counter, who=who, reason=reason)
141
+
142
+    reset.save()
143
+
144
+    # Now we deal with the hashtags
145
+    keywords = parseSeumReason(reason)
146
+    Hashtag.objects.bulk_create([Hashtag(reset=reset, keyword=keyword) for keyword in keywords])
147
+
148
+    # We send the emails only to those who want
149
+    emails = [u['email'] for u in Counter.objects.filter(email_notifications=True).values('email')]
150
+    # Now send emails to everyone
151
+    if reset.who is None or reset.who == counter:
152
+        selfSeum = True
153
+    else:
154
+        selfSeum = False
155
+    text_of_email = render_to_string(
156
+        'seumEmail.txt', {'reason': reason,
157
+                          'name': counter.name,
158
+                          'who': reset.who,
159
+                          'selfSeum': selfSeum,
160
+                          })
161
+    email_to_send = EmailMessage(
162
+        '[SeumBook] ' + counter.trigramme + ' a le seum',
163
+        text_of_email,
164
+        'SeumMan <seum@merigoux.ovh>', emails, [],
165
+        reply_to=emails)
166
+    email_to_send.send(fail_silently=True)