def watchers(request): if request.method == 'POST': if 'watcher_id' in request.POST: watchers = request.POST.getlist('watcher_id') for w in watchers: if request.POST.get('delete_watcher_' + str(w)): Watcher.delete(request.get_host(), w, request.access_token) else: postdata = { 'id': int(w), 'sendEmail': bool(request.POST.get('send_email_' + str(w))), 'receiveSMS': False, } Watcher.update(request.get_host(), int(w), postdata, request.access_token) return HttpResponseRedirect(reverse('watchers')) if request.method == 'GET': # pagination offset offset = int(request.GET.get('offset', 0)) url, params, headers = WatcherList.build_request(request.get_host(), offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(url, params=params, headers=headers)) responses = response_list_to_dict(grequests.map(request.view_requests)) watchers_list = WatcherList(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'content': watchers_list, 'pagination': build_pagination_links(responses[url]['watchers']['links'], watchers_list.watchers) } return render(request, watchers_template, view_data)
def list(request): if request.method == 'POST': if 'watcher_id' in request.POST: watchers = request.POST.getlist('watcher_id') # TODO: get rid of casts for w in watchers: if request.POST.get('delete_watcher_' + str(w)): Watcher.delete(request.get_host(), w, request.access_token) else: postdata = { 'id': int(w), 'sendEmail': bool(request.POST.get('send_email_' + str(w))), 'receiveSMS': False, } Watcher.update(request.get_host(), int(w), postdata, request.access_token) return HttpResponseRedirect(reverse('list-watchers')) if request.method == 'GET': # pagination offset try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 url, params, headers = WatcherList.build_request(request.get_host(), offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(url, params=params, headers=headers)) responses = response_list_to_dict(grequests.map(request.view_requests)) watchers_list = WatcherList(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'content': watchers_list, 'pagination': build_pagination_links(responses[url]['watchers']['links'], watchers_list.watchers) } return render(request, WatcherView.list_template, view_data)
def single(request): postdata = { 'updateTypeId': int(request.POST.get('updateTypeId')), # used on create for communication defaults 'itemType': request.POST.get('itemType'), 'itemId': int(request.POST.get('itemId')), } if request.POST.get('delete'): Watcher.delete(request.get_host(), postdata, request.access_token) return HttpResponse() elif request.POST.get('patch'): postdata = { 'itemType': request.REQUEST.get('itemType'), 'itemId': int(request.REQUEST.get('itemId')), 'sendEmail': "true" == request.REQUEST.get('emailMe') } response = Watcher.update(request.get_host(), postdata, request.access_token) if response.status_code == requests.codes.ok: return HttpResponse() else: return HttpResponseBadRequest() else: responsedata = Watcher.create(request.get_host(), postdata, request.access_token) return HttpResponse(responsedata, content_type='application/json')