-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.py
More file actions
1440 lines (1227 loc) · 69.2 KB
/
Copy pathnotifications.py
File metadata and controls
1440 lines (1227 loc) · 69.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""The main processing unit for all notifications
This module contains the core functions in the sms queue
"""
import csv
import re
import datetime
import uuid
import pytz
import random
import json
from dateutil.relativedelta import relativedelta
from datetime import date
from django.conf import settings
from django.contrib.auth.hashers import make_password
from django.core.mail import EmailMultiAlternatives
from django.db import transaction, connection
from django.db.models import Q, Sum, Count, IntegerField, Min, Max, Avg
from django.forms.models import model_to_dict
from django.utils import timezone
from django.template.loader import render_to_string
from jinja2 import Template, FileSystemLoader
from jinja2.environment import Environment
try:
if re.match('.+LivHealth', settings.SITE_NAME):
# try importing stuff from LivHealth
from hashids import Hashids
from .terminal_output import Terminal
from .models import SMSQueue, MessageTemplates, Recipients, Campaign, SubCounty, Ward, Village
from .serializers import RecepientSerializer
from .odk_forms import OdkForms
from .odk_choices_parser import ImportODKChoices
my_hashids = Hashids(min_length=5, salt=settings.SECRET_KEY)
elif settings.SITE_NAME == 'Pazuri Records':
# try importing stuff from PazuriPoultry
from vendor.terminal_output import Terminal
from poultry.models import SMSQueue, MessageTemplates, Personnel, Campaign, Farm, SubscriptionPayment, Batch, Production, EventsSchedule, EventsList, OtherEvents, Farm, IncomeExpense, PERSONNEL_DESIGNATION_CHOICES
from common_func.common_tasks import Emails
elif settings.SITE_NAME == 'BoxGirls M&E System':
from vendor.terminal_output import Terminal
from .common_tasks import Emails
elif settings.SITE_NAME == 'Church Register':
from .terminal_output import Terminal
from .common_tasks import Emails
elif settings.SITE_NAME == 'Co-Infection Data Hub':
from vendor.terminal_output import Terminal
from vendor.common_tasks import Emails
elif settings.SITE_NAME == 'Badili Innovations':
from .terminal_output import Terminal
from .common_tasks import Emails
elif settings.SITE_NAME == 'MAD-tech-AMR System':
from badili_common.terminal_output import Terminal
from badili_common.common_tasks import Emails
else:
from vendor.terminal_output import Terminal
except Exception as e:
raise
terminal = Terminal()
# we are deprecating Sentry via raven
if settings.SITE_NAME != 'Badili Innovations':
from raven import Client
sentry = Client(settings.SENTRY_DSN)
current_tz = pytz.timezone(settings.TIMEZONE)
timezone.activate(current_tz)
class Notification():
def __init__(self):
self.time_formats = ['now', 'today', 'tomorrow', 'yesterday']
self.at_ok_sending_status_codes = [100, 101, 102]
try:
# email environment
self.email_obj = Emails()
except:
# we might not be needing the email module as of now
pass
self.env = Environment()
self.env.loader = FileSystemLoader(settings.TEMPLATES[0]['DIRS'][0])
# testing messages
if hasattr(settings, 'TESTING_PREFIX'):
self.testing_numbers = ['%s%s' % (settings.TESTING_PREFIX, x) for x in range(settings.TESTING_PHONE_NUMBERS_START, settings.TESTING_PHONE_NUMBERS_END)]
else:
self.testing_numbers = []
def process_test_data(self, input_file):
"""Given an input file, imports the data to the DB
Allows initialization of base data to the database.
"""
terminal.tprint('Processing the file %s...' % input_file, 'info')
try:
transaction.set_autocommit(False)
with open(input_file, 'rt') as in_file:
test_data = csv.DictReader(in_file, delimiter=',', quotechar='"')
for row in test_data:
self.process_test_message(row)
transaction.commit()
except Exception as e:
transaction.rollback()
sentry.captureException()
if settings.DEBUG: terminal.tprint(str(e), 'fail')
terminal.tprint("The input file '%s' with test data has been processed successfully..." % input_file, 'info')
def process_test_message(self, mssg):
# if the message to be sent is empty, just ignore the line
if mssg['message'].strip() == '':
terminal.tprint('We have an empty message, nothing to do here...', 'info')
return
# generate the message uuid
mssg_uuid = uuid.uuid5(uuid.NAMESPACE_X500, mssg['message'])
# check if we need to add a campaign
if mssg['campaign'] != '':
try:
cur_campaign = Campaign.objects.filter(campaign_name=mssg['campaign']).get()
except Campaign.DoesNotExist:
cur_campaign = self.save_auto_campaign(mssg['campaign'])
else:
cur_campaign = None
# check if we have a sending time
cur_time = timezone.localtime(timezone.now())
print(mssg['sending_time'])
if mssg['sending_time'] != '':
mssg_sending_time = mssg['sending_time'].strip()
# check if the data specified is correct, else throw an error
if mssg_sending_time in self.time_formats:
if mssg_sending_time == 'now' or mssg_sending_time == 'today':
schedule_time = cur_time.strftime('%Y-%m-%d %H:%M:%S')
elif mssg_sending_time == 'tomorrow':
schedule_time = (cur_time + datetime.timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')
elif mssg_sending_time == 'yesterday':
schedule_time = (cur_time + datetime.timedelta(days=-1)).strftime('%Y-%m-%d %H:%M:%S')
else:
try:
schedule_time = timezone.datetime.strptime(mssg['sending_time'], '%Y-%m-%d %H:%M:%S')
except ValueError as e:
terminal.tprint(str(e), 'fail')
raise ValueError("Incorrect sending time specified. The sending time can only be '%s' or a valid date time string eg. 2019-09-23 14:41:00" % ', '.join(self.time_formats))
else:
schedule_time = cur_time.strftime('%Y-%m-%d %H:%M:%S')
# check if the message is already added to the template
try:
msg_template = MessageTemplates.objects.filter(uuid=mssg_uuid, campaign=cur_campaign).get()
except MessageTemplates.DoesNotExist:
msg_template = self.add_message_template(mssg['message'], mssg_uuid, cur_campaign)
# split the messages into parts if need be
messages = self.check_message_length(mssg['message'].strip())
# split the recipients of the message and add to the queues
for rec in mssg['recepient_nos'].split(','):
rec = rec.strip()
if len(rec) == 0:
continue
if re.search('^\+\d+$', rec) is None:
err_mssg = 'The recipients phone number must begin with a plus(+) sign and contain only integers'
terminal.tprint(err_mssg, 'fail')
raise Exception(err_mssg)
try:
recipient = Recipients.objects.filter(recipient_no=rec).get()
# everything is now really good... so lets add this to the queue
# Django saves all the dates and times to the database in the UTC timezone
# loop through the messages and add them to the queue
for cur_mssg in messages:
queue_item = SMSQueue(
template=msg_template,
message=cur_mssg,
recipient=recipient,
recipient_no=rec,
msg_status='SCHEDULED',
schedule_time=schedule_time
)
queue_item.full_clean()
queue_item.save()
except Recipients.DoesNotExist:
recipient = self.add_recipient(rec)
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def check_message_length(self, message):
"""
Given a message, check if it is within the acceptable message length, if not, split it into parts
Args:
message (string): The message to check its length
Returns
An array of strings with the messages. In the array the messages are ordered in order that they should be sent
"""
if len(message) > settings.SMS_MAX_LENGTH:
# using range determine the indexes of the string to slice
# iterate through the indexes and get the subset of the message
# append the subsets to an array
# return the array
messages = []
mssg_parts = range(0, len(message), settings.SMS_MAX_LENGTH)
for i, j in zip(mssg_parts, range(len(mssg_parts))):
messages.append('%s %d/%d' % (message[i:i + settings.SMS_MAX_LENGTH], j + 1, len(mssg_parts)))
# return the messages
return messages
else:
return [message]
def save_auto_campaign(self, campaign_name):
"""Save a campaign since it does not exist
Returns:
The campaign object which has been created
"""
try:
cur_campaign = Campaign(
campaign_name=campaign_name
)
cur_campaign.full_clean()
cur_campaign.save()
return cur_campaign
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise
def add_recipient(self, recipient_no, first_name=None, other_names=None):
"""Adds a recipient to the database since they don't exist
The recipient does not exist, so lets add to them to the database
Args:
recipient_no (string): the phone number of the recipient
first_name (string | optional): the first name
other_names (string | optional): the other names of the reepient
Returns:
Returns the created recipient
"""
try:
# if the names haven't been provided, using a faker, populate placeholder names
if first_name is None:
fake_p = Faker()
first_name = fake_p.name().split(' ')[0]
if other_names is None:
other_names = fake_p.name().split(' ')[1]
recipient = Recipients(
recipient_no=recipient_no,
first_name=first_name,
other_names=other_names
)
recipient.full_clean()
recipient.save()
return recipient
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def add_message_template(self, template, uuid, campaign):
"""Adds a message template to the database
Adds a message template to the database since it does not exist
Args:
template (string): The message template to add to the database
campaign (Campaign or None): The campaign to associate the message to
Returns:
Returns the saved campaign
"""
try:
mssg_template = MessageTemplates(template=template, uuid=uuid)
if campaign is not None:
mssg_template.campaign = campaign
mssg_template.full_clean()
mssg_template.save()
return mssg_template
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def process_scheduled_sms(self, provider):
"""Processes the scheduled SMS and puts them in the sending queue
Fetches all the scheduled SMSes from the databases and adds them to a sending queue
"""
cur_time = timezone.localtime(timezone.now())
cur_time_str = cur_time.strftime('%Y-%m-%d %H:%M:%S')
use_provider = provider
try:
gateway_ids = list(settings.SMS_GATEWAYS['gateways'].keys())
# get the statuses to use to fetch the sms to process
statuses_to_use = ['SCHEDULED']
statuses_keys = list(settings.AT_STATUS_CODES.keys())
for status_code in statuses_keys:
if status_code not in self.at_ok_sending_status_codes:
statuses_to_use.append(settings.AT_STATUS_CODES[status_code])
if provider not in gateway_ids and provider is not None:
raise Exception("'%s' is not configured as a gateway provider. Select from '%s'" % (provider, ', '.join(gateway_ids)))
if provider is None:
use_provider = random.choice(list(settings.SMS_GATEWAYS['gateways'].keys()))
# fetch the sms whose sending schedule time has passed
sms2send = SMSQueue.objects.filter(schedule_time__lte=cur_time_str, msg_status__in=statuses_to_use).order_by('id').all()
for sched_sms in sms2send:
if sched_sms.recipient_no in self.testing_numbers:
# we have a testing message, don't send it
continue
# print('%s: %s - %s' % (sched_sms.id, sched_sms.schedule_time, sched_sms.recipient_no))
print('Seconds Diff\nCur Time = %s, Sched Time = %s\n````````````\n%.1f -- %d\n--\n' % (cur_time_str, sched_sms.schedule_time, (cur_time - sched_sms.schedule_time).total_seconds(), settings.MESSAGE_VALIDITY * 60 * 60))
if (cur_time - sched_sms.schedule_time).total_seconds() > settings.MESSAGE_VALIDITY * 60 * 60:
if settings.DEBUG: print('The message is expired...')
sentry.captureMessage("Expired message to %s: '%s'" % (sched_sms.recipient_no, sched_sms.message), level='warning', extra={'cur_time': cur_time_str, 'scheduled_time': sched_sms.schedule_time.strftime('%Y-%m-%d %H:%M:%S'), 'message_validity': '%d Sec' % settings.MESSAGE_VALIDITY * 60 * 60})
sched_sms.msg_status = 'EXPIRED'
sched_sms.full_clean()
sched_sms.save()
continue
# if we have testing messages, don't send them
if use_provider == 'at':
terminal.tprint('Sending the SMS via AT...', 'info')
self.send_via_at(sched_sms)
elif use_provider == 'nexmo':
terminal.tprint('Sending the SMS via Nexmo...', 'info')
self.send_via_nexmo(sched_sms)
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
# def queue_via_at(self, mssg):
def configure_at(self):
"""Configures and initializes AfricasTalking as an SMS gateway provider
Using the settings provided in the settings file, configures and initializes AT as an SMS gateway
"""
import africastalking
username = settings.SMS_GATEWAYS['gateways']['at']['USERNAME']
api_key = settings.SMS_GATEWAYS['gateways']['at']['KEY']
# print("AT: Using the creds: %s - %s" % (username, api_key))
africastalking.initialize(username, api_key)
self.at_sms = africastalking.SMS
def send_via_at(self, mssg):
"""Submits a message to be sent via the AT gateway
Args:
The message object as JSON to be sent
"""
try:
# queue the message to be sent via africastalking. Once queued, update the database with the queue status
cur_time = timezone.localtime(timezone.now())
cur_time = cur_time.strftime('%Y-%m-%d %H:%M:%S')
# lets send the messages synchronously... should be changed to async
# How does AT identify a message when a callback is given
this_resp = self.at_sms.send(mssg.message, [mssg.recipient_no])
# print(this_resp)
if len(this_resp['SMSMessageData']['Recipients']) == 0:
# print(mssg)
sentry.captureMessage("Message not sent.", level='info', extra={'at_response': this_resp, 'message': mssg.message, 'recipient': mssg.recipient_no, 'sender_id': settings.AT_SENDER_ID})
# raise Exception(this_resp['SMSMessageData']['Message'])
elif this_resp['SMSMessageData']['Recipients'][0]['statusCode'] in self.at_ok_sending_status_codes:
# if the message is processed well, add the results to the db
mssg.in_queue = 0
mssg.queue_time = cur_time
mssg.provider_id = this_resp['SMSMessageData']['Recipients'][0]['messageId']
mssg.msg_status = settings.AT_STATUS_CODES[this_resp['SMSMessageData']['Recipients'][0]['statusCode']]
mssg.full_clean()
mssg.save()
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def process_at_report(self, request):
"""Process a notification from AT gateway
"""
try:
# get the smsqueue and update its status
delivery_type = request.GET.get('type')
if delivery_type == 'delivery':
self.process_at_delivery_report(request)
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def process_at_delivery_report(self, request):
"""Process a delivery notification from africastalking
"""
try:
# get the smsqueue and update its status
sms_id = request.POST.get('id')
sms_status = request.POST.get('status')
queue_instance = SMSQueue.objects.filter(provider_id=sms_id).get()
if sms_status in settings.AT_FINAL_DELIVERY_STATUS:
# the sms has a final delivery status... so lets add this to the database
queue_instance.msg_status = sms_status
cur_time = timezone.localtime(timezone.now())
cur_time = cur_time.strftime('%Y-%m-%d %H:%M:%S')
queue_instance.delivery_time = cur_time
queue_instance.full_clean()
queue_instance.save()
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def configure_nexmo(self):
"""Configure the NEXMO SMS gateway
"""
import nexmo
key = settings.SMS_GATEWAYS['gateways']['nexmo']['KEY']
secret = settings.SMS_GATEWAYS['gateways']['nexmo']['SECRET']
# print("NEXMO: Using the creds: %s - %s" % (key, secret))
self.nexmo = nexmo.Client(key=key, secret=secret)
def send_via_nexmo(self, mssg):
"""Sends a message using the configured NEXMO SMS gateway
Args:
mssg (json); The message to be sent
"""
try:
# queue the message to be sent via africastalking. Once queued, update the database with the queue status
cur_time = timezone.localtime(timezone.now())
cur_time = cur_time.strftime('%Y-%m-%d %H:%M:%S')
# for nexmo we need to strip out the preceeding +. All our numbers have a preceeding +
recipient = mssg.recipient_no.split('+')[1]
this_resp = self.nexmo.send_message({
'from': 'Wangoru Kihara',
'to': recipient,
'text': mssg.message,
'ttl': settings.SMS_VALIDITY * 60 * 60 # specify a TTL since NEXMO allows this
})
print(this_resp)
if this_resp["messages"][0]["status"] == "0":
mssg.in_queue = 1
mssg.queue_time = cur_time
mssg.msg_status = settings.NEXMO_STATUS_CODES[int(this_resp["messages"][0]["status"])]
mssg.provider_id = this_resp['messages'][0]['message-id']
mssg.full_clean()
mssg.save()
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def process_nexmo_report(self, request):
"""Process a notification from NEXMO gateway
"""
try:
# get the smsqueue and update its status
delivery_type = request.GET.get('type')
if delivery_type == 'delivery':
self.process_nexmo_delivery_report(request)
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def process_nexmo_delivery_report(self, request):
"""Process a delivery notification from africastalking
"""
try:
# get the smsqueue and update its status
sms_id = request.POST.get('messageId')
sms_status = request.POST.get('status')
queue_instance = SMSQueue.objects.filter(provider_id=sms_id).get()
if sms_status in settings.NEXMO_FINAL_DELIVERY_STATUS:
# the sms has a final delivery status... so lets add this to the database
queue_instance.msg_status = settings.NEXMO_DELIVERY_CODES[sms_status]
mssg_time = request.POST.get('message-timestamp')
delivery_time = timezone.datetime.strptime(mssg_time, '%Y-%m-%d %H:%M:%S')
queue_instance.delivery_time = delivery_time
queue_instance.full_clean()
queue_instance.save()
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def get_notification_settings(self):
"""Get the settings defined for the different notifications
"""
try:
data_set = {
'campaigns': Campaign.objects.all(),
'templates': MessageTemplates.objects.order_by('template_name').all(),
'recipients': Recipients.objects.select_related('village__ward__sub_county').select_related('ward').select_related('sub_county').order_by('first_name').all(),
'recipient_types': Recipients.objects.order_by().values('designation').distinct(),
'sub_counties': SubCounty.objects.order_by('sub_county_name').all(),
'wards': [model_to_dict(rec) for rec in Ward.objects.order_by('ward_name').all()],
'villages': [model_to_dict(rec) for rec in Village.objects.order_by('village_name').all()],
}
return data_set
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def save_campaign(self, request):
try:
# get the campaign details and add them to the database
campaign_name = request.POST.get('campaign-name')
recipients = request.POST.getlist('recipients[]')
scheduled = request.POST.get('schedule-day')
transaction.set_autocommit(False)
if request.POST.get('object_id'):
# we are editing a campaign
campaign = Campaign.objects.filter(id=request.POST.get('object_id')).get()
campaign.campaign_name=campaign_name
campaign.recipients=','.join(recipients)
campaign.schedule_time=scheduled
else:
campaign = Campaign(
campaign_name=campaign_name,
recipients=','.join(recipients),
schedule_time=scheduled
)
campaign.full_clean()
campaign.save()
transaction.commit()
except Exception as e:
transaction.rollback()
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def save_template(self, request):
try:
# get the campaign details and add them to the database
template_name = request.POST.get('template-name')
template_type = request.POST.get('message-type')
template_message = request.POST.get('template-message')
campaign_id = request.POST.get('campaign')
sending_time = request.POST.get('sending-time') or None
mssg_uuid = uuid.uuid5(uuid.NAMESPACE_X500, template_message)
# get the campaign names for this template
campaign = Campaign.objects.filter(id=campaign_id).get()
transaction.set_autocommit(False)
if request.POST.get('object_id'):
template = MessageTemplates.objects.filter(id=request.POST.get('object_id')).get()
template.template_name=template_name
template.template_type=template_type
template.campaign=campaign
template.template=template_message
template.uuid=mssg_uuid
else:
template = MessageTemplates(
template_name=template_name,
template_type=template_type,
campaign=campaign,
template=template_message,
uuid=mssg_uuid
)
if sending_time is not None:
template.sending_time=sending_time
template.full_clean()
template.save()
transaction.commit()
except Exception as e:
transaction.rollback()
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def save_recipient(self, request):
try:
# get the campaign details and add them to the database
salutation = request.POST.get('salutation')
first_name = request.POST.get('first-name').strip()
other_names = request.POST.get('other-names').strip()
designation = request.POST.get('designation')
email = request.POST.get('email').strip()
cell_no = request.POST.get('cell_no').strip()
alternative_cell_no = request.POST.get('alternative_cell_no').strip() if request.POST.get('alternative_cell_no') != '' else None
sub_county_id = request.POST.get('sub-county')
ward_id = request.POST.get('ward').strip()
village_id = request.POST.get('village').strip()
# get the campaign names for this template
if sub_county_id == '-1' or sub_county_id == '':
sub_county = None
ward = None
village = None
else:
sub_county = SubCounty.objects.filter(id=sub_county_id).get()
if ward_id != '' and ward_id.isnumeric() is False:
# we have a new ward...
odk_choices_parser = ImportODKChoices()
new_ward = {
'label': ward_id,
'name': ward_id.replace("'.- ", '').lower(),
'ward_subcounty': sub_county.nick_name
}
ward = odk_choices_parser.process_ward(new_ward)
else:
ward = Ward.objects.filter(id=ward_id).get() if ward_id != '' else None
if village_id != '' and village_id.isnumeric() is False:
# we have a new village...
odk_choices_parser = ImportODKChoices()
new_village = {
'label': village_id,
'name': village_id.replace("'.- ", '').lower(),
'village_ward': ward.nick_name
}
village = odk_choices_parser.process_village(new_village)
else:
village = Village.objects.filter(id=village_id).get() if village_id != '' else None
transaction.set_autocommit(False)
if request.POST.get('object_id'):
recipient = Recipients.objects.filter(id=request.POST.get('object_id')).get()
recipient.salutation = salutation
recipient.first_name = first_name
recipient.other_names = other_names
recipient.designation = designation
recipient.cell_no = cell_no
recipient.alternative_cell_no = alternative_cell_no
recipient.recipient_email = email
recipient.village = village
recipient.ward = ward
recipient.sub_county = sub_county
else:
# fabricate a nick_name for the recipient
nick_name = '%s_%s' % (first_name.replace("'.- ", '').lower(), other_names.replace("'.- ", '').lower())
recipient = Recipients(
salutation=salutation,
first_name=first_name,
other_names=other_names,
designation=designation,
cell_no=cell_no,
alternative_cell_no=alternative_cell_no,
recipient_email=email,
nick_name=nick_name,
village=village,
ward=ward,
sub_county=sub_county
)
recipient.full_clean()
recipient.save()
transaction.commit()
except Exception as e:
transaction.rollback()
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def periodic_processing(self, provider):
"""Run the periodic procesing script.
This will process the received data and queue messages to be sent if need be
and also sends out the queued notifications via the respective channels
"""
# process the sms to be sent
self.process_notifications_data()
# submit the queued SMS to the provider
self.process_scheduled_sms(provider)
def process_notifications_data(self):
# if need be, crunch the data for feedback to the users
# Notifications are scheduled on the specified day at settings.SENDING_SPLIT
# cur_time = timezone.localtime(timezone.now())
cur_time = timezone.now()
parts = settings.SENDING_TIME.split(':')
sending_time = cur_time.replace(hour=int(parts[0]), minute=int(parts[1]), second=int(parts[2]))
split_seconds = (cur_time - sending_time).total_seconds()
print('\n%s: Splits\n````````````\n%.1f -- %.1f\n--\n' % (cur_time.strftime('%A %d-%m %H:%M:%S'), split_seconds, settings.SENDING_SPLIT))
# loop through the campaigns and determine the one that needs processing
campaigns = Campaign.objects.all()
odk_form = OdkForms()
i = 0
for campaign in campaigns:
if cur_time.strftime('%A') == campaign.schedule_time:
# print('\n%s campaign should be ran today...' % campaign.campaign_name)
# the campaign should ran today
if split_seconds > -1 and split_seconds < settings.SENDING_SPLIT:
# get the templates assigned to this campaign that needs to be processed
templates = MessageTemplates.objects.filter(campaign_id=campaign.id).all()
for template in templates:
# print(template.template_name)
if template.template_name == 'Management Weekly Report' or template.template_name == 'LivHealth Admin Weekly Feedback':
stats = self.management_weekly_report(odk_form, odk_form.sub_counties)
# get the users in this campaign
user_groups = campaign.recipients.split(',')
recipients = Recipients.objects.filter(designation__in=user_groups)
sub_counties_stats = {}
for recipient in recipients:
if template.template_name == 'SCVO Weekly Reminder':
# recipient_name
message = template.template % (recipient.first_name)
elif template.template_name == 'SCVO Weekly Report':
if recipient.sub_county.nick_name not in sub_counties_stats:
sc_stats = self.management_weekly_report(odk_form, [recipient.sub_county.nick_name, ''])
sub_county_name = str(odk_form.get_value_from_dictionary(recipient.sub_county.nick_name))
sub_counties_stats[recipient.sub_county.nick_name] = {'stats': sc_stats, 'sub_county_name': sub_county_name}
else:
sc_stats = sub_counties_stats[recipient.sub_county.nick_name]['stats']
sub_county_name = sub_counties_stats[recipient.sub_county.nick_name]['sub_county_name']
message = template.template % tuple([recipient.first_name] + [sub_county_name] + [sc_stats[0], sc_stats[2]])
elif template.template_name == 'Management Weekly Report' or template.template_name == 'LivHealth Admin Weekly Feedback':
# name, # syndromic reports, # abbatoirs, # ND1 reports, # agrovet reports from the last week
message = template.template % tuple([recipient.first_name] + stats)
if recipient.cell_no or recipient.alternative_cell_no:
# print('\n%s: %s' % (template.template_name, message))
odk_form.schedule_notification(template, recipient, message)
i = i + 1
# one recipent per template if debug is True
# if settings.DEBUG:
# break
# print(sub_counties_stats)
print('\nSent %d messages\n' % i)
def management_weekly_report(self, odk_form, sub_counties):
# get the number of reports received from the previous week, monday to sunday
today = timezone.datetime.today()
end_date = today + datetime.timedelta(days=1)
start_date = end_date - datetime.timedelta(days=7)
stats = odk_form.dash_stats(start_date, end_date, sub_counties, odk_form.all_species)
# get the number of ND1 reports
nd_reporting_q = """
SELECT count(*)
FROM nd_details as a INNER JOIN nd_reports as b on a.nd_report_id=b.id
WHERE nd_date_reported > '%s' AND nd_date_reported < '%s' AND sub_county IN %s
""" % (str(start_date), str(end_date), tuple(sub_counties))
# print(nd_reporting_q)
sh_reporting_q = """
SELECT count(*)
FROM sh_reports as a
WHERE report_date > '%s' AND report_date < '%s'
""" % (str(start_date), str(end_date))
# print(sh_reporting_q)
ag_reporting_q = """
SELECT count(*)
FROM ag_detail as a INNER JOIN ag_reports as b on a.ag_report_id=b.id
WHERE report_date > '%s' AND report_date < '%s'
""" % (str(start_date), str(end_date))
# print(ag_reporting_q)
with connection.cursor() as cursor:
cursor.execute(nd_reporting_q)
nd_reporting = cursor.fetchall()
cursor.execute(sh_reporting_q)
sh_reporting = cursor.fetchall()
cursor.execute(ag_reporting_q)
ag_reporting = cursor.fetchall()
return [stats['total_submissions'], sh_reporting[0][0], nd_reporting[0][0], ag_reporting[0][0]]
def get_sent_notifications(self):
# get the list of sent notifications
notifications = SMSQueue.objects.select_related('recipient').select_related('template').order_by('-schedule_time').all()
return {'notifications': notifications}
def send_email(self, email_settings):
try:
# print(email_settings)
template = self.env.get_template(email_settings['template'])
email_html = template.render(email_settings)
Emails.send_email(email_settings['recipient_email'], email_settings['sender_email'], None, email_settings['subject'], email_html)
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def send_periodic_reports(self):
# send report links of the previous periods
try:
livhealth_admins = Recipients.objects.filter(designation='livhealth_admin').exclude(recipient_email__isnull=True).exclude(recipient_email__exact='').all()
recipients = []
for admn in livhealth_admins:
recipients.append(admn.recipient_email)
# determine the period of the reports
today = datetime.datetime.now()
current_month = today.strftime('%B')
reports = []
# monthly report
lm = today + relativedelta(months=-1)
reports.append( ["%s of %d" % (lm.strftime('%B'), lm.year), my_hashids.encode(lm.year, lm.month)] )
if today.month % 3 == 1:
# we need a quarter report
if today.month // 3 == 0: reports.append( ["Quarter 4 of %d" % (today.year-1), my_hashids.encode(today.year-1, 16)] )
else: reports.append( ["Quarter %d of %d" % (today.month//3, today.year), my_hashids.encode(today.year, 12 + (today.month//3) )] )
if today.month % 6 == 1:
# we need a half year report
if today.month // 6 == 0: reports.append( ["2nd Half of %d" % (today.year-1), my_hashids.encode(today.year-1, 18)] )
else: reports.append( ["1st Half of %d" % today.year, my_hashids.encode(today.year, 17)] )
if today.month == 1:
# get the last year report
reports.append( ["Year %d" % (today.year-1), my_hashids.encode(today.year-1, 0)] )
plain_message = ""
email_message = ""
email_message_inner_template = """
<p>
<mj-text font-family="arial" font-size="16px" align="left" color="#808080"> <span style="color:#0098CE"><b><a href='%s'>%s</a></b></span></mj-text>
</p>
"""
for rep in reports:
cur_url = "%s/reports/%s" % (settings.LIVHEALTH_URL, rep[1])
plain_message = "%s\n%s" % ( plain_message, "%s: %s" % (rep[0], cur_url) )
email_message = "%s%s" % (email_message, email_message_inner_template % (cur_url, rep[0]) )
if settings.DEBUG: recipients = ['wangoru.kihara@badili.co.ke']
text_content = render_to_string('email-periodic-reports.txt', { 'message_details': plain_message, 'current_month': current_month })
html_content = render_to_string('email-periodic-reports.html', {'message_details': email_message, 'current_month': current_month })
email_subject = '[%s] Periodic Reports for %s %s' % (settings.SITE_NAME, current_month, today.strftime('%Y'))
msg = EmailMultiAlternatives(email_subject, text_content, settings.DEFAULT_FROM_EMAIL, recipients)
msg.attach_alternative(html_content, "text/html")
msg.send()
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
class PazuriNotification():
def __init__(self, cur_user_email=None):
self.time_formats = ['now', 'today', 'tomorrow', 'yesterday']
self.at_ok_sending_status_codes = [100, 101, 102]
try:
if cur_user_email is not None:
# save the cur user object
self.cur_user = Personnel.objects.filter(email=cur_user_email).get()
# get the farm of this user
self.cur_farm_id = self.cur_user.farm_id
self.cur_farm = Farm.objects.filter(id=self.cur_farm_id).get()
else:
self.cur_farm = None
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
def get_notification_settings(self, farm_id):
"""Get the settings defined for the different notifications
"""
try:
data_set = {
'campaigns': Campaign.objects.filter(farm_id=farm_id).all(),
'templates': MessageTemplates.objects.select_related().filter(campaign__farm_id=farm_id).order_by('template_name').all(),
'recipients': Personnel.objects.filter(farm_id=farm_id).order_by('first_name').all(),
'recipient_types': [k[0] for k in PERSONNEL_DESIGNATION_CHOICES],
}
return data_set
except Exception as e:
if settings.DEBUG: terminal.tprint(str(e), 'fail')
sentry.captureException()
raise Exception(str(e))
def get_sent_notifications(self):
# get the list of sent notifications
if self.cur_user.is_superuser:
notifications = SMSQueue.objects.select_related('recipient').select_related('template').order_by('-schedule_time').all()
else:
notifications = SMSQueue.objects.select_related('recipient').select_related('template').filter(recipient__farm_id=self.cur_farm_id).order_by('-schedule_time').all()
return {'notifications': notifications}
def save_recipient(self, request):
try:
# get the campaign details and add them to the database
salutation = request.POST.get('salutation')
first_name = request.POST.get('first-name').strip()
other_names = request.POST.get('other-names').strip()
designation = request.POST.get('designation')
email = request.POST.get('email').strip()
cell_no = request.POST.get('cell_no').strip()
alternative_cell_no = request.POST.get('alternative_cell_no').strip() if request.POST.get('alternative_cell_no') != '' else None
sub_county_id = request.POST.get('sub-county')
ward_id = request.POST.get('ward').strip()
village_id = request.POST.get('village').strip()
# get the campaign names for this template
if sub_county_id == '-1' or sub_county_id == '':
sub_county = None
ward = None
village = None
else:
sub_county = SubCounty.objects.filter(id=sub_county_id).get()
if ward_id != '' and ward_id.isnumeric() is False:
# we have a new ward...
odk_choices_parser = ImportODKChoices()
new_ward = {
'label': ward_id,
'name': ward_id.replace("'.- ", '').lower(),
'ward_subcounty': sub_county.nick_name
}
ward = odk_choices_parser.process_ward(new_ward)
else:
ward = Ward.objects.filter(id=ward_id).get() if ward_id != '' else None
if village_id != '' and village_id.isnumeric() is False:
# we have a new village...
odk_choices_parser = ImportODKChoices()
new_village = {
'label': village_id,
'name': village_id.replace("'.- ", '').lower(),
'village_ward': ward.nick_name
}
village = odk_choices_parser.process_village(new_village)
else:
village = Village.objects.filter(id=village_id).get() if village_id != '' else None
transaction.set_autocommit(False)
if request.POST.get('object_id'):
recipient = Recipients.objects.filter(id=request.POST.get('object_id')).get()
recipient.salutation = salutation
recipient.first_name = first_name
recipient.other_names = other_names
recipient.designation = designation
recipient.cell_no = cell_no
recipient.alternative_cell_no = alternative_cell_no
recipient.recipient_email = email
recipient.village = village
recipient.ward = ward