Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volunteer gathering #979

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
006df55
Create renew_volunteer.html
muneebkattody Aug 26, 2018
a54342c
initial changes for adding new view for renew as Volunteers
jinojossy93 Aug 27, 2018
1c84c96
optimized code for sending verification sms to 80k users
sudhinbabu Aug 27, 2018
6b94481
Merge branch 'master' into optimize_volunteer_sms
sudhinbabu Aug 27, 2018
3b0cfb7
code cleanup
sudhinbabu Aug 27, 2018
502df77
sample csv
sudhinbabu Aug 27, 2018
7d3eaba
requirements updated
sudhinbabu Aug 28, 2018
3c56773
Merge branch 'master' into optimize_volunteer_sms
sudhinbabu Aug 28, 2018
7a00a61
added new view and url for fail scenario
jinojossy93 Aug 28, 2018
bc0557c
Merge branch 'master' of github.com:IEEEKeralaSection/rescuekerala in…
jinojossy93 Aug 28, 2018
3e0b0a9
populated the html
jinojossy93 Aug 28, 2018
ff2ca51
added csrf token in html
jinojossy93 Aug 28, 2018
ca9a8c3
fixed csrf token issue
jinojossy93 Aug 28, 2018
b394a10
added new hidden field to access selcted ditrict value
jinojossy93 Aug 28, 2018
dc47d40
data migration for local bodies
sudhinbabu Aug 28, 2018
d6c6ff3
Merge branch 'master' into optimize_volunteer_sms
sudhinbabu Aug 28, 2018
4e8ca5c
Merge branch 'master' into volunteer_gathering
sudhinbabu Aug 28, 2018
33d246a
Merge branch 'optimize_volunteer_sms' into volunteer_gathering
sudhinbabu Aug 28, 2018
cbc0213
gitignore changed
sudhinbabu Aug 28, 2018
e40d7e3
migration typo fix
sudhinbabu Aug 28, 2018
1ac8462
resolving migrations conflict
sudhinbabu Aug 28, 2018
f32cc2c
need to fix the form validation error
jinojossy93 Aug 28, 2018
27c1ab2
merged with volunteer_gathering
jinojossy93 Aug 28, 2018
3a18d0f
Merge pull request #3 from jinojossy93/renew_as_volunteer
jinojossy93 Aug 28, 2018
c246139
resovled migration conflicts
sudhinbabu Aug 28, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ mysite.log
venv
.idea/*
media/
logs/*.log
13 changes: 13 additions & 0 deletions floodrelief/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ def get_list(text):
'simple': {
'format': '%(levelname)s %(message)s'
},
'command': {
'format': '%(asctime)s %(message)s'
},
},
'handlers': {
'file': {
Expand All @@ -188,6 +191,12 @@ def get_list(text):
'filename': 'mysite.log',
'formatter': 'verbose'
},
'send_volunteer_sms': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'logs/send_volunteer_sms.log',
'formatter': 'command'
},
},
'loggers': {
'django': {
Expand All @@ -199,6 +208,10 @@ def get_list(text):
'handlers': ['file'],
'level': 'DEBUG',
},
'send_volunteer_sms': {
'handlers': ['send_volunteer_sms'],
'level': 'DEBUG',
},
}
}

Expand Down
Empty file added logs/.addthisfolderingit
Empty file.
107 changes: 107 additions & 0 deletions mainapp/management/commands/sendvolunteersms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import os

from django.core.management.base import BaseCommand
from django.core.cache import cache
import requests
from threading import Thread
from datetime import datetime

from django.conf import settings
import csv
from dateutil import parser
import time

import logging
import calendar


logger = logging.getLogger('send_volunteer_sms')


# python manage.py sendvolunteersms
# python manage.py sendvolunteersms /tmp/volunteer.csv
# python manage.py sendvolunteersms --clearcache=1
class Command(BaseCommand):
# SMS_API_URL = "http://api.esms.kerala.gov.in/fastclient/SMSclient.php"
SMS_API_URL = "http://127.0.0.1:8000/test_send_sms/"
API_USERNAME = os.environ.get("SMS_USER")
API_PASSWORD = os.environ.get("SMS_PASSWORD")

DEFAULT_CSV = os.path.join(settings.BASE_DIR,
'mainapp/management/commands/smsvolunteer.csv')
BATCH_MAX = 10

msg_url_template = "http://keralarescue.in/c/{sendID}/{timestamp}"
message_template = "Thank you for registering to volunteer. Please click here to confirm {url}"
success_check_cache_key = "SendingFailed_{phone}"

def add_arguments(self, parser):
parser.add_argument('path', nargs='?', type=str)
parser.add_argument('--clearcache', nargs='?', type=bool)

@property
def volunteers(self):
with open(self.path, "r") as volunteers:
for volunteer in csv.DictReader(volunteers):
yield volunteer

@staticmethod
def clean_timestamp(timestamp):
# not clear about this logic just copied from -> sms.py
timestamp = parser.parse(timestamp)
timestamp = calendar.timegm(timestamp.utctimetuple())
return str(timestamp)[-4:]

def send_sms(self, payload):
res = requests.get(self.SMS_API_URL, params=payload)
if res.status_code in (200, 201):
cache.set(self.success_check_cache_key.format(
phone=payload["numbers"]), True)
else:
logger.info("failed {} {}".format())

def process_batch(self, batch):
tasks = []
for payload in batch:
self.total_count += 1
t = Thread(target=self.send_sms,
args=(payload,))
tasks.append(t)
t.start()

for task in tasks:
t.join()

def handle(self, *args, **options):
if options["clearcache"]:
logger.info("clearing cache for sendvolunteersms.")
cache.delete_pattern(self.success_check_cache_key.format(phone="*"))
else:
t1 = time.time()
self.path = options["path"] if options["path"] else self.DEFAULT_CSV
batch = []
batch_count = 0
self.total_count = 0
logger.info("STARTING sendvolunteersms.")

for volunteer in self.volunteers:
msg_url = self.msg_url_template.format(sendID=volunteer["ID"],
timestamp="{:%Y-%m-%d %H:%M}".format(datetime.now()))
message = self.message_template.format(url=msg_url)
payload = {'username': self.API_USERNAME,
'password': self.API_PASSWORD,
'message': message,
'numbers': volunteer["phone"]}
if not cache.get(
self.success_check_cache_key.format(
phone=payload["numbers"])):
batch.append(payload)
batch_count += 1
if batch_count == self.BATCH_MAX:
self.process_batch(payload)
batch_count = 0
batch = []
if batch:
self.process_batch(batch)
logger.info("{} COMPLETED IN {} Seconds sendvolunteersms.".format(self.total_count,
time.time() - t1))
170 changes: 170 additions & 0 deletions mainapp/management/commands/smsvolunteer.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
ID,phone
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
1,0123456789
2,9876543210
3,0612345678
28 changes: 28 additions & 0 deletions mainapp/migrations/0092_auto_20180828_1428.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.1 on 2018-08-28 08:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mainapp', '0091_merge_20180825_1236'),
]

operations = [
migrations.AddField(
model_name='volunteer',
name='date_time',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='volunteer',
name='email',
field=models.EmailField(blank=True, max_length=250),
),
migrations.AddField(
model_name='volunteer',
name='local_body',
field=models.TextField(default='', max_length=250),
),
]
Loading