|
| 1 | +""" |
| 2 | + Proxy a new push notification to the push service |
| 3 | + and then gorush. Maybe save as update. |
| 4 | +""" |
| 5 | + |
| 6 | +from django import forms |
| 7 | +from django.conf import settings |
| 8 | +from django.contrib.auth.models import User |
| 9 | +from django.utils import timezone |
| 10 | +from hackfsu_com.views.generic import ApiView |
| 11 | +from hackfsu_com.util.forms import JsonField |
| 12 | +from hackfsu_com.util import acl |
| 13 | +from api.models import HackathonUpdate, Hackathon |
| 14 | + |
| 15 | +import requests |
| 16 | + |
| 17 | +class CreatePushView(ApiView): |
| 18 | + class RequestForm(forms.Form): |
| 19 | + title = forms.CharField() |
| 20 | + body = forms.CharField() |
| 21 | + isUpdate = forms.IntegerField() |
| 22 | + |
| 23 | + http_method_names = ['post'] |
| 24 | + request_form_class = RequestForm |
| 25 | + access_manager = acl.AccessManager(acl_accept=[acl.group_organizer]) |
| 26 | + |
| 27 | + def work(self, request, req, res): |
| 28 | + url = "{}/push/new".format(settings.PUSH_HOST) |
| 29 | + payload = { |
| 30 | + 'title': req['title'], |
| 31 | + 'message': req['body'] |
| 32 | + } |
| 33 | + resp = requests.post(url, json=payload) |
| 34 | + |
| 35 | + |
| 36 | + # Optional save as HackathonUpdate |
| 37 | + if req['isUpdate'] is 1: |
| 38 | + update = HackathonUpdate( |
| 39 | + hackathon=Hackathon.objects.current(), |
| 40 | + title=req['title'], |
| 41 | + content=req['body'] |
| 42 | + ) |
| 43 | + update.save() |
0 commit comments