|
1 | 1 | from __future__ import absolute_import |
2 | 2 |
|
3 | 3 | import logging |
| 4 | +import json |
| 5 | +import requests |
| 6 | +import types |
4 | 7 |
|
5 | 8 | from django.contrib.contenttypes.models import ContentType |
6 | 9 | from django.core.cache import cache |
7 | | -from django.db import connection, IntegrityError |
| 10 | +from django.core.mail import send_mail |
| 11 | +from django.db import connection, IntegrityError, models |
8 | 12 |
|
9 | 13 | from celery import task |
10 | 14 | from pinax.eventlog.models import log |
11 | 15 |
|
| 16 | +from td import settings |
12 | 17 | from td.commenting.models import CommentTag |
13 | 18 | from td.imports.models import ( |
14 | 19 | EthnologueLanguageCode, |
|
27 | 32 | logger = logging.getLogger(__name__) |
28 | 33 |
|
29 | 34 |
|
| 35 | +class TaskStatus(object): |
| 36 | + def __init__(self, success=False): |
| 37 | + self.success = success |
| 38 | + self.message = [] |
| 39 | + |
| 40 | + |
30 | 41 | @task() |
31 | 42 | def create_comment_tag(instance): |
32 | 43 | try: |
@@ -191,3 +202,87 @@ def integrate_imb_language_data(): |
191 | 202 | language.country = country |
192 | 203 | language.source = imb |
193 | 204 | language.save() |
| 205 | + |
| 206 | + |
| 207 | +@task() |
| 208 | +def notify_external_apps(action="", instance=None): |
| 209 | + """ Make a POST request based on action types to urls registered in EXT_APP_PUSH under settings """ |
| 210 | + |
| 211 | + task_status = TaskStatus() |
| 212 | + |
| 213 | + if not isinstance(instance, models.Model): |
| 214 | + task_status.message.append("function is called with invalid 'instance'") |
| 215 | + return task_status |
| 216 | + if not isinstance(action, (str, unicode)) or action.strip() == "": |
| 217 | + task_status.message.append("function is called with invalid 'action'") |
| 218 | + return task_status |
| 219 | + if not isinstance(settings.EXT_APP_PUSH, types.ListType): |
| 220 | + task_status.message.append("settings.EXT_APP_PUSH is not a list") |
| 221 | + return task_status |
| 222 | + |
| 223 | + data = None |
| 224 | + |
| 225 | + if action == "CREATE": |
| 226 | + # If instance is created, serialize all concrete fields and assign them as data |
| 227 | + # serialized_instances = serializers.serialize('json', [instance]) |
| 228 | + # serialized_data = json.loads(serialized_instances)[0] |
| 229 | + # data = serialized_data |
| 230 | + # data.update({"code": instance.code}) |
| 231 | + task_status.message.append("%s is not supported at this moment" % action) |
| 232 | + return task_status |
| 233 | + elif action == "UPDATE": |
| 234 | + # If instance is edited, only pass the changed fields as data |
| 235 | + data = { |
| 236 | + "pk": instance.id, |
| 237 | + "code": instance.tracker.previous("code") if instance.tracker.has_changed("code") else instance.code, |
| 238 | + "fields": {key.replace("_id", ""): getattr(instance, key, "") for key in instance.tracker.changed().keys()} |
| 239 | + } |
| 240 | + elif action == "DELETE": |
| 241 | + # If instance is deleted, just pass the id |
| 242 | + # data = { |
| 243 | + # "pk": instance.id, |
| 244 | + # "code": instance.code, |
| 245 | + # } |
| 246 | + task_status.message.append("%s is not supported at this moment" % action) |
| 247 | + return task_status |
| 248 | + else: |
| 249 | + task_status.message.append("%s is not a valid option for 'action'" % action) |
| 250 | + return task_status |
| 251 | + |
| 252 | + # Include model name and action type in data to meet the spec. If action is 'CREATE', 'model' will be overridden by |
| 253 | + # class name instead of what the serializer sets. |
| 254 | + # Also include (previous) code to help identify records, especially the ones created by outside apps. |
| 255 | + data.update({ |
| 256 | + "model": instance.__class__.__name__, |
| 257 | + "action": action, |
| 258 | + }) |
| 259 | + |
| 260 | + data = [data] |
| 261 | + |
| 262 | + # NOTE: Maybe this can be abstracted into a function for easier testing |
| 263 | + for app in settings.EXT_APP_PUSH: |
| 264 | + url = app.get("url") |
| 265 | + if "key" in app: |
| 266 | + key = app.get("key") |
| 267 | + url += "?key=" + key if key is not None else "" |
| 268 | + headers = {'Content-Type': 'application/json'} |
| 269 | + |
| 270 | + post_to_ext_apps.delay(url, json.dumps(data), headers) |
| 271 | + |
| 272 | + task_status.success = True |
| 273 | + return task_status |
| 274 | + |
| 275 | + |
| 276 | +@task() |
| 277 | +def post_to_ext_apps(url, data, headers): |
| 278 | + response = requests.post(url, data=data, headers=headers) |
| 279 | + if response.status_code == 202 and response.content == "": |
| 280 | + return |
| 281 | + else: |
| 282 | + message = "POST to %s has failed with code: %s and message: %s" % (url, response.status_code, response.content) |
| 283 | + send_mail( |
| 284 | + "Ops CRM shim rejects POST", |
| 285 | + message, |
| 286 | + "admin@unfoldingword.org", |
| 287 | + ["vleong2332@gmail.com"], |
| 288 | + ) |
0 commit comments