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

Renew as volunteer code changes Views, HTML and new url changes #1

Merged
merged 21 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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),
),
]
2 changes: 1 addition & 1 deletion mainapp/migrations/0092_auto_20180828_1429.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Migration(migrations.Migration):
name='LocalBody',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body_type', models.CharField(choices=[('corporation', 'corporation'), ('muncipality', 'municipality'), ('panchayat', 'panchayat')], max_length=20)),
('body_type', models.CharField(choices=[('corporation', 'corporation'), ('municipality', 'municipality'), ('panchayat', 'panchayat')], max_length=20)),
('name', models.CharField(max_length=200)),
('district', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mainapp.District')),
],
Expand Down
14 changes: 14 additions & 0 deletions mainapp/migrations/0093_merge_20180828_1750.py
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 = [
]
Loading