def webhooks_handler(request): """Webhook endpoint handler.""" required = set(['name', 'object_id', 'system', 'user_id']) if not required.issubset(request.POST): return HttpResponseBadRequest(content=get_message('missing_field')) try: subdomain = subdomain_from_url(request.POST['system']) profile = UserProfile.objects.get(subdomain=subdomain) except UserProfile.DoesNotExist: return HttpResponseBadRequest(content=get_message('unknown_system', system=cgi.escape(request.POST['system']))) callback = request.POST['object_id'] name = request.POST['name'] user_id = request.POST['user_id'] webhook = WebHook(callback, name, subdomain, user_id) if request.POST['name'] == 'callback.verify': if 'verifier' not in request.POST: message = get_message('missing_verifier') return HttpResponseBadRequest(content=message) verifier = request.POST['verifier'] token = profile.oauth_token secret = profile.oauth_secret verify_webhook(webhook, verifier, token, secret) return HttpResponse("Verified") event = Event( name=request.POST['name'], user=request.POST['user_id'] == '1' and 'Admin' or 'Staff', datetime=datetime.now(), object_id=request.POST['object_id'], subdomain=profile ) event.save() return HttpResponse(twitter_notification(event, subdomain))
def process_login(request): """Process login request. Redirects the user to the OAuth authorization URL which is handled by the `callback` view. """ subdomain = request.POST['url'] if not subdomain: request.session['error'] = get_message('missing_url') return HttpResponseRedirect('/login/') client = simpleoauth.Client(OAuthSettings(subdomain)) try: callback = request.build_absolute_uri(location='/oauth/callback/') token = client.request_token(callback) except simpleoauth.OAuthError, e: request.session['error'] = str(e) return HttpResponseRedirect('/login/')
request.session['error'] = str(e) return HttpResponseRedirect('/login/') profile = UserProfile( oauth_token=token.key, oauth_secret=token.secret, subdomain=subdomain ) profile.save() callback = request.build_absolute_uri(location='/webhooks/handler/') try: create_webhook(callback, subdomain, token.key, token.secret) except Error, e: request.session['error'] = get_message('not_admin') return HttpResponseRedirect('/login/') request.session['system'] = subdomain return HttpResponseRedirect('/') @require_http_methods(['POST']) def webhooks_handler(request): """Webhook endpoint handler.""" required = set(['name', 'object_id', 'system', 'user_id']) if not required.issubset(request.POST): return HttpResponseBadRequest(content=get_message('missing_field')) try: subdomain = subdomain_from_url(request.POST['system']) profile = UserProfile.objects.get(subdomain=subdomain)