forked from raksha-life/rescuekerala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jinojossy93/renew_as_volunteer
Renew as volunteer code changes Views, HTML and new url changes
- Loading branch information
Showing
15 changed files
with
760 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ mysite.log | |
venv | ||
.idea/* | ||
media/ | ||
logs/*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Generated by Django 2.1 on 2018-08-28 12:20 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('mainapp', '0092_auto_20180828_1429'), | ||
('mainapp', '0092_auto_20180828_1407'), | ||
] | ||
|
||
operations = [ | ||
] |
Oops, something went wrong.