Skip to content

Commit

Permalink
CCC: Automated reminders 1 day beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
flavour committed Dec 18, 2019
1 parent 2aa0fb3 commit 4aef01b
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b'4c634549b' (2019-12-18 17:18:18)
b'2aa0fb366' (2019-12-18 17:51:01)
164 changes: 157 additions & 7 deletions modules/templates/CCC/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,6 @@ def hrm_training_event_notification(record, selected):
"""

from gluon import URL
from s3 import s3_parse_datetime

if isinstance(selected, basestring):
# Deserialize the vars from s3task
Expand Down Expand Up @@ -1260,10 +1259,16 @@ def hrm_training_event_notification(record, selected):

base_url = "%s%s" % (settings.get_base_public_url(),
URL(c="hrm", f="training_event", args=[record_id, "training"]))

date = record.get("start_date")
if isinstance(date, basestring):
from s3 import s3_parse_datetime
date = s3_parse_datetime(date, "%Y-%m-%d %H:%M:%S")

message = "You have been invited to an Event:\n\nEvent name: %s\nEvent description: %s\nStarts: %s\nLead Organisation: %s\nVenue name: %s\nDistrict: %s\nStreet Address: %s\nPostcode: %s\nContact Name: %s\nTelephone: %s\nEmail: %s\nWebsite: %s\n\nYou can respond to the invite here: %s" % \
(event_name,
record.get("comments") or "",
etable.start_date.represent(s3_parse_datetime(record.get("start_date"), "%Y-%m-%d %H:%M:%S")),
etable.start_date.represent(date),
etable.organisation_id.represent(record.get("organisation_id"), show_link=False),
tags.get("venue_name")["value"],
location.L3,
Expand Down Expand Up @@ -1301,6 +1306,7 @@ def hrm_training_event_notification(record, selected):
def hrm_training_event_reminder(r, **attr):
"""
Send reminder notification to Invitees
- interactive
"""

import json
Expand All @@ -1322,13 +1328,49 @@ def hrm_training_event_reminder(r, **attr):
current.session.confirmation = T("Reminder sent")
redirect(URL(args=None))

# -------------------------------------------------------------------------
def hrm_training_event_reminder_day(record_id):
"""
Send reminder notification to Invitees
- automated 1 day before Opportunity
"""

db = current.db
s3db = current.s3db

table = s3db.hrm_training_event
record = db(table.id == record_id).select(table.id,
table.name,
table.start_date,
table.comments,
table.location_id,
table.organisation_id,
limitby = (0, 1)
).first()

ttable = s3db.hrm_training
query = (ttable.training_event_id == record_id) & \
(ttable.deleted == False)
trainings = db(query).select(ttable.person_id)
selected = [t.person_id for t in trainings]

req_need_notification(record, selected)

settings.tasks.hrm_training_event_reminder_day = hrm_training_event_reminder_day

# -------------------------------------------------------------------------
def hrm_training_event_postprocess(form):
"""
Create Site based on other fields
* Create Site based on other fields
* Schedule Reminder
"""

training_event_id = form.vars.id
form_vars_get = form.vars.get

training_event_id = form_vars_get("id")


# Create Site based on other fields

db = current.db
s3db = current.s3db
Expand Down Expand Up @@ -1377,6 +1419,38 @@ def hrm_training_event_postprocess(form):
db(etable.id == training_event_id).update(site_id = record["site_id"])
set_realm_entity(etable, training_event_id, force_update=True)

# Schedule Reminder

from dateutil.relativedelta import relativedelta
start_time = form_vars_get("start_date") - relativedelta(days = 1)
if start_time < current.request.utcnow:
return

import json
ttable = s3db.scheduler_task
task_name = "settings_task"
args = ["hrm_training_event_reminder_day"]
vars = {"record_id": training_event_id}
query = (ttable.task_name == task_name) & \
(ttable.args == json.dumps(args)) & \
(ttable.vars == json.dumps(vars))
exists = db(query).select(ttable.id,
ttable.start_time,
limitby = (0, 1)
).first()
if exists:
if exists.start_time != start_time:
exists.update_record(start_time = start_time)
else:
current.s3task.schedule_task("settings_task",
args = ["hrm_training_event_reminder_day"],
vars = {"record_id": training_event_id},
start_time = start_time,
#period = 300, # seconds
timeout = 300, # seconds
repeats = 1 # run once
)

# -------------------------------------------------------------------------
def customise_hrm_training_event_resource(r, tablename):

Expand Down Expand Up @@ -2951,7 +3025,6 @@ def project_task_create_onaccept(form):
def customise_project_task_resource(r, tablename):

from s3 import S3OptionsFilter, S3SQLCustomForm, S3TextFilter


current.response.s3.crud_strings[tablename] = Storage(
label_create = T("New Message"),
Expand Down Expand Up @@ -3090,6 +3163,47 @@ def postp(r, output):

settings.customise_project_task_controller = customise_project_task_controller

# -------------------------------------------------------------------------
def req_need_onaccept(form):
"""
Schedule Reminder
"""

form_vars_get = form.vars.get

from dateutil.relativedelta import relativedelta
start_time = form_vars_get("date") - relativedelta(days = 1)
if start_time < current.request.utcnow:
return

need_id = form_vars_get("id")

import json
ttable = current.s3db.scheduler_task
task_name = "settings_task"
start_time = form_vars_get("date") - relativedelta(days = 1)
args = ["req_need_reminder_day"]
vars = {"record_id": need_id}
query = (ttable.task_name == task_name) & \
(ttable.args == json.dumps(args)) & \
(ttable.vars == json.dumps(vars))
exists = current.db(query).select(ttable.id,
ttable.start_time,
limitby = (0, 1)
).first()
if exists:
if exists.start_time != start_time:
exists.update_record(start_time = start_time)
else:
current.s3task.schedule_task("settings_task",
args = ["req_need_reminder_day"],
vars = {"record_id": need_id},
start_time = start_time,
#period = 300, # seconds
timeout = 300, # seconds
repeats = 1 # run once
)

# -------------------------------------------------------------------------
def req_need_organisation_onaccept(form):
"""
Expand Down Expand Up @@ -3195,7 +3309,6 @@ def req_need_notification(record, selected):
"""

from gluon import URL
from s3 import s3_parse_datetime

if isinstance(selected, basestring):
# Deserialize the vars from s3task
Expand Down Expand Up @@ -3253,10 +3366,16 @@ def req_need_notification(record, selected):

base_url = "%s%s" % (settings.get_base_public_url(),
URL(c="req", f="need", args=[record_id, "need_person"]))

date = record.get("date")
if isinstance(date, basestring):
from s3 import s3_parse_datetime
date = s3_parse_datetime(date, "%Y-%m-%d %H:%M:%S")

message = "You have been invited to an Opportunity:\n\nTitle: %s\nOrganisation: %s\nStart Date: %s\nDistrict: %s\nStreet Address: %s\nPostcode: %s\nDescription: %s\nContact: %s\nSkill: %s\nAge Restrictions: %s\nPractical Information: %s\nParking Options: %s\nWhat to Bring: %s\n\nYou can respond to the invite here: %s" % \
(event_name,
organisation,
ntable.date.represent(s3_parse_datetime(record.get("date"), "%Y-%m-%d %H:%M:%S")),
ntable.date.represent(date),
location.L3,
location.addr_street or "",
location.addr_postcode or "",
Expand Down Expand Up @@ -3301,6 +3420,7 @@ def req_need_notification(record, selected):
def req_need_reminder(r, **attr):
"""
Send reminder notification to Invitees
- interactive
"""

import json
Expand All @@ -3322,6 +3442,35 @@ def req_need_reminder(r, **attr):
current.session.confirmation = T("Reminder sent")
redirect(URL(args=None))

# -------------------------------------------------------------------------
def req_need_reminder_day(record_id):
"""
Send reminder notification to Invitees
- automated 1 day before Opportunity
"""

db = current.db
s3db = current.s3db

table = s3db.req_need
record = db(table.id == record_id).select(table.id,
table.name,
table.date,
table.description,
table.location_id,
limitby = (0, 1)
).first()

nptable = s3db.req_need_person
query = (nptable.need_id == record_id) & \
(nptable.deleted == False)
links = db(query).select(nptable.person_id)
selected = [l.person_id for l in links]

req_need_notification(record, selected)

settings.tasks.req_need_reminder_day = req_need_reminder_day

# -------------------------------------------------------------------------
def customise_req_need_resource(r, tablename):

Expand Down Expand Up @@ -3474,6 +3623,7 @@ def customise_req_need_resource(r, tablename):
),
filter_widgets = filter_widgets,
list_fields = list_fields,
onaccept = req_need_onaccept,
)

s3db.configure("req_need_organisation",
Expand Down

0 comments on commit 4aef01b

Please sign in to comment.