Parcourir la source

Merge branch 'master' of Nim/wifi-with-me into master

jocelyn il y a 6 ans
Parent
commit
93049e5d4e

+ 13 - 2
README.md

@@ -68,9 +68,9 @@ accessible under *http://example.com/foo/* :
 
     URL_PREFIX='foo/'
 
-### Notifications
+### Email Notifications
 
-If you to receive notifications on each new contrib, customize those :
+If you want to receive email notifications on each new contrib, customize those :
 
 List of notification recipients:
 
@@ -80,6 +80,17 @@ Notification sender address:
 
     DEFAULT_FROM_EMAIL='notifier@example.tld'
 
+### Webhook Notifications
+
+If you want to send web notifications on each new contrib, install
+[requests](http://docs.python-requests.org/en/master/) and customize those :
+
+    WEBHOOK = True
+    WEBHOOK_URL = 'https://<domain>/<path>'
+    WEBHOOK_KEY = '<secret>'
+
+Then, on each contrib, an HTTP POST request wil be made to `WEBHOOK_URL` with a json dict containing `'key': WEBHOOK_KEY`
+and the notification as `'text'`.
 
 ### Data expiration
 

+ 1 - 0
requirements/base.txt

@@ -3,3 +3,4 @@ PyYAML>=3.11,<4.0
 django-request-token>=0.6,<0.7
 pytz
 sqlparse
+requests

+ 3 - 0
wifiwithme/apps/contribmap/views.py

@@ -19,6 +19,7 @@ from .models import Contrib
 from .decorators import prevent_robots
 from .tokens import ContribTokenManager, URLTokenManager, TokenError
 from .emails import send_contributor_email, send_moderator_emails
+from .webhooks import send_moderator_webhook
 
 
 @prevent_robots()
@@ -40,6 +41,8 @@ def add_contrib(request):
             send_moderator_emails(
                 contrib, admin_subject, admin_body, mgmt_token, request)
 
+            send_moderator_webhook(contrib, request)
+
             # Notification email to the author
             if contrib.email:
                 author_subject = get_template(

+ 18 - 0
wifiwithme/apps/contribmap/webhooks.py

@@ -0,0 +1,18 @@
+import json
+
+from django.conf import settings
+
+
+def send_moderator_webhook(contrib, request=None):
+    """ Sends a notification to a webhook
+    """
+    if settings.WEBHOOK:
+        import requests
+        text = "Nouvelle demande de %s" % contrib.name
+        if request is not None:
+            text += ": %s" % contrib.get_absolute_url(request)
+        requests.post(
+            settings.WEBHOOK_URL, data=json.dumps({
+                'text': text,
+                'key': settings.WEBHOOK_KEY,
+            }))

+ 2 - 0
wifiwithme/settings/base.py

@@ -147,3 +147,5 @@ DATA_EXPIRATION_REMINDERS = [
 ]
 
 SITE_URL = 'http://localhost:8000'
+
+WEBHOOK = False