2017-11-16 00:43:10 +01:00
|
|
|
from typing import Any, Dict, Optional, Text
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
2017-10-09 16:54:32 +02:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2017-10-09 16:54:32 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2017-10-16 21:44:14 +02:00
|
|
|
GCI_MESSAGE_TEMPLATE = u'**{actor}** {action} the task [{task_name}]({task_url}).'
|
2017-11-28 22:38:11 +01:00
|
|
|
GCI_SUBJECT_TEMPLATE = u'{student_name}'
|
2017-10-09 16:54:32 +02:00
|
|
|
|
|
|
|
|
2017-11-28 23:08:44 +01:00
|
|
|
def build_instance_url(instance_id: str) -> str:
|
2017-11-28 22:38:11 +01:00
|
|
|
return "https://codein.withgoogle.com/dashboard/task-instances/{}/".format(instance_id)
|
|
|
|
|
2017-10-09 16:54:32 +02:00
|
|
|
class UnknownEventType(Exception):
|
|
|
|
pass
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_abandon_event_body(payload: Dict[Text, Any]) -> Text:
|
2017-10-09 16:54:32 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
2017-10-16 21:44:14 +02:00
|
|
|
actor=payload['task_claimed_by'],
|
2017-11-02 20:09:48 +01:00
|
|
|
action='{}ed'.format(payload['event_type']),
|
2017-10-09 16:54:32 +02:00
|
|
|
task_name=payload['task_definition_name'],
|
2017-11-28 22:38:11 +01:00
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
2017-10-09 16:54:32 +02:00
|
|
|
)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_submit_event_body(payload: Dict[Text, Any]) -> Text:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
|
|
|
actor=payload['task_claimed_by'],
|
2017-11-02 20:09:48 +01:00
|
|
|
action='{}ted'.format(payload['event_type']),
|
2017-10-16 21:44:14 +02:00
|
|
|
task_name=payload['task_definition_name'],
|
2017-11-28 22:38:11 +01:00
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_comment_event_body(payload: Dict[Text, Any]) -> Text:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
|
|
|
actor=payload['author'],
|
2017-11-02 20:09:48 +01:00
|
|
|
action='{}ed on'.format(payload['event_type']),
|
2017-10-16 21:44:14 +02:00
|
|
|
task_name=payload['task_definition_name'],
|
2017-11-28 22:38:11 +01:00
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_claim_event_body(payload: Dict[Text, Any]) -> Text:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
|
|
|
actor=payload['task_claimed_by'],
|
2017-11-02 20:09:48 +01:00
|
|
|
action='{}ed'.format(payload['event_type']),
|
2017-10-16 21:44:14 +02:00
|
|
|
task_name=payload['task_definition_name'],
|
2017-11-28 22:38:11 +01:00
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_approve_event_body(payload: Dict[Text, Any]) -> Text:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
|
|
|
actor=payload['author'],
|
2017-11-02 20:09:48 +01:00
|
|
|
action='{}d'.format(payload['event_type']),
|
2017-10-16 21:44:14 +02:00
|
|
|
task_name=payload['task_definition_name'],
|
2017-11-28 22:38:11 +01:00
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2017-12-17 23:56:29 +01:00
|
|
|
def get_approve_pending_pc_event_body(payload: Dict[Text, Any]) -> Text:
|
|
|
|
template = "{} (pending parental consent).".format(GCI_MESSAGE_TEMPLATE.rstrip('.'))
|
|
|
|
return template.format(
|
|
|
|
actor=payload['author'],
|
|
|
|
action='approved',
|
|
|
|
task_name=payload['task_definition_name'],
|
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
|
|
|
)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_needswork_event_body(payload: Dict[Text, Any]) -> Text:
|
2017-11-02 20:16:11 +01:00
|
|
|
template = "{} for more work.".format(GCI_MESSAGE_TEMPLATE.rstrip('.'))
|
|
|
|
return template.format(
|
|
|
|
actor=payload['author'],
|
|
|
|
action='submitted',
|
|
|
|
task_name=payload['task_definition_name'],
|
2017-11-28 22:38:11 +01:00
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
2017-11-02 20:16:11 +01:00
|
|
|
)
|
2017-10-09 16:54:32 +02:00
|
|
|
|
2018-01-02 22:07:42 +01:00
|
|
|
def get_extend_event_body(payload: Dict[Text, Any]) -> Text:
|
|
|
|
template = "{} by {days} day(s).".format(GCI_MESSAGE_TEMPLATE.rstrip('.'),
|
|
|
|
days=payload['extension_days'])
|
|
|
|
return template.format(
|
|
|
|
actor=payload['author'],
|
|
|
|
action='extended the deadline for',
|
|
|
|
task_name=payload['task_definition_name'],
|
|
|
|
task_url=build_instance_url(payload['task_instance']),
|
|
|
|
)
|
|
|
|
|
2017-10-09 16:54:32 +02:00
|
|
|
@api_key_only_webhook_view("Google-Code-In")
|
|
|
|
@has_request_variables
|
2017-12-06 12:51:48 +01:00
|
|
|
def api_gci_webhook(request: HttpRequest, user_profile: UserProfile, stream: Text=REQ(default='gci'),
|
|
|
|
payload: Dict[Text, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2017-10-09 16:54:32 +02:00
|
|
|
event = get_event(payload)
|
|
|
|
if event is not None:
|
|
|
|
body = get_body_based_on_event(event)(payload)
|
|
|
|
subject = GCI_SUBJECT_TEMPLATE.format(
|
2017-11-28 22:38:11 +01:00
|
|
|
student_name=payload['task_claimed_by']
|
2017-10-09 16:54:32 +02:00
|
|
|
)
|
|
|
|
check_send_stream_message(user_profile, request.client,
|
|
|
|
stream, subject, body)
|
|
|
|
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
EVENTS_FUNCTION_MAPPER = {
|
|
|
|
'abandon': get_abandon_event_body,
|
2017-11-02 20:16:11 +01:00
|
|
|
'approve': get_approve_event_body,
|
2017-12-17 23:56:29 +01:00
|
|
|
'approve-pending-pc': get_approve_pending_pc_event_body,
|
2017-11-02 20:16:11 +01:00
|
|
|
'claim': get_claim_event_body,
|
2017-10-16 21:44:14 +02:00
|
|
|
'comment': get_comment_event_body,
|
2018-01-02 22:07:42 +01:00
|
|
|
'extend': get_extend_event_body,
|
2017-11-02 20:16:11 +01:00
|
|
|
'needswork': get_needswork_event_body,
|
2017-10-16 21:44:14 +02:00
|
|
|
'submit': get_submit_event_body,
|
2017-10-09 16:54:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_event(payload: Dict[Text, Any]) -> Optional[Text]:
|
2017-11-02 20:09:48 +01:00
|
|
|
event = payload['event_type']
|
2017-10-09 16:54:32 +02:00
|
|
|
if event in EVENTS_FUNCTION_MAPPER:
|
|
|
|
return event
|
|
|
|
|
2017-10-16 21:44:14 +02:00
|
|
|
raise UnknownEventType(u"Event '{}' is unknown and cannot be handled".format(event)) # nocoverage
|
2017-10-09 16:54:32 +02:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_body_based_on_event(event: Text) -> Any:
|
2017-10-09 16:54:32 +02:00
|
|
|
return EVENTS_FUNCTION_MAPPER[event]
|