Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add mailgun support

  • Loading branch information...
commit d382db20a393a49707e7ae95ff576aacea61eb6f 1 parent 47cc747
@amleczko amleczko authored
Showing with 50 additions and 1 deletion.
  1. +2 −1  restarter/notify/__init__.py
  2. +48 −0 restarter/notify/views.py
View
3  restarter/notify/__init__.py
@@ -8,11 +8,12 @@ def main(global_config, **settings):
config.add_route('notify', '/notify')
config.add_route('fb_register_notify', '/notify/fb/register')
config.add_route('fb_sell_notify', '/notify/fb/sell')
+ config.add_route('fb_newcompany_notify', '/notify/fb/newcompany')
config.add_route('page_product_notify', '/notify/page/product')
config.add_route('page_company_notify', '/notify/page/company')
config.add_route('page_demand_notify', '/notify/page/demand')
config.add_route('page_newsitem_notify', '/notify/page/newsitem')
- config.add_route('fb_newcompany_notify', '/notify/fb/newcompany')
+ config.add_route('mailgun_photos', '/mailgun/photos/{uid}')
config.scan()
return config.make_wsgi_app()
View
48 restarter/notify/views.py
@@ -1,7 +1,10 @@
import json
+#import requests
+import hashlib, hmac
from pyramid.view import view_config
from pyramid_mailer import get_mailer
+from pyramid.httpexceptions import HTTPForbidden
from restarter.notify import sms, mailing, facebook
@@ -185,3 +188,48 @@ def notify(request):
sms.send_sms.delay(login, password, phone_message, phone)
return 'OK'
+
+
+def verify(api_key, token, timestamp, signature):
+ return signature == hmac.new(key=api_key,
+ msg='{}{}'.format(timestamp, token),
+ digestmod=hashlib.sha256).hexdigest()
+
+
+@view_config(route_name='mailgun_photos', renderer='json', request_method='POST')
+def mailgun_photos(request):
+
+ settings = request.registry.settings
+ api = settings.get('mailgun.api')
+ token = request.params.get('token')
+ signature = request.params.get('signature')
+ timestamp = request.params.get('timestamp')
+
+ isvalid = verify(api, token, timestamp, signature)
+ if not isvalid:
+ raise HTTPForbidden()
+
+ sender = request.params.get('sender')
+ subject = request.params.get('subject', '')
+ body_plain = request.params.get('body-plain', '')
+ body_without_quotes = request.params.get('stripped-text', '')
+ attachments_length = int(request.params.get('attachment-count','0'))
+ attachments = []
+
+ for n in range(1,attachments_length+1):
+ attachments.append(request.params.get('attachment-%s' % n))
+
+ import pdb; pdb.set_trace()
+
+ return 'OK'
+
+ #uid = request.params['uid']
+ #r = requests.post("https://api.mailgun.net/v2/routes",
+ # auth=("api", api),
+ # data=[
+ # ("priority", 1),
+ # ("expression", "match_recipient('%s@facciamoadesso.mailgun.org')" % uid),
+ # ("action", "forward('%s/mailgun/newmail/%s')" % (request.application_url, uid)),
+ # ("action", "stop()")
+ # ])
+ #return r.text
Please sign in to comment.
Something went wrong with that request. Please try again.