def edit(request, event_id): """ Edit an event. """ try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'state_edit': True } if request.method == 'POST': form = edit_form(request.POST) if form.is_valid(): event_request = Event.from_edit_form(form.cleaned_data) try: event_response = event_request.update(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('single-event', args=(event_response.id,))) else: view_data['form'] = form view_data['microcosm_id'] = form['microcosmId'] return render(request, form_template, view_data) if request.method == 'GET': try: event = Event.retrieve(request.get_host(), id=event_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) view_data['form'] = edit_form.from_event_instance(event) view_data['microcosm_id'] = event.microcosm_id try: view_data['attendees'] = Event.get_attendees(host=request.get_host(), id=event_id, access_token=request.access_token) attendees_json = [] for attendee in view_data['attendees'].items.items: attendees_json.append({ 'id': attendee.profile.id, 'profileName': attendee.profile.profile_name, 'avatar': attendee.profile.avatar, 'sticky': 'true' }) if len(attendees_json) > 0: view_data['attendees_json'] = json.dumps(attendees_json) except APIException: # Missing RSVPs is not critical, but we should know if it doesn't work. logger.error(str(APIException)) pass return render(request, form_template, view_data)
def delete(request, event_id): """ Delete an event and be redirected to the parent microcosm. """ event = Event.retrieve(request.get_host(), event_id, access_token=request.access_token) try: event.delete(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('single-microcosm', args=(event.microcosm_id,)))
def delete(request, event_id): """ Delete an event and be redirected to the parent microcosm. """ event = Event.retrieve(request.get_host(), event_id, access_token=request.access_token) try: event.delete(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect( reverse('single-microcosm', args=(event.microcosm_id, )))
def moderate(request): """ View for moderation actions on a single item. """ microcosm_id = request.POST.get('microcosm_id') if request.method == 'POST': if request.POST.get('action') == 'move': if request.POST.get('item_type') == 'event': event = Event.retrieve(request.get_host(), request.POST.get('item_id'), access_token=request.access_token) event.microcosm_id = int(microcosm_id) event.meta = {'editReason': 'Moderator moved item'} event.update(request.get_host(), request.access_token) elif request.POST.get('item_type') == 'conversation': conversation = Conversation.retrieve(request.get_host(), request.POST.get('item_id'), access_token=request.access_token) conversation.microcosm_id = int(microcosm_id) conversation.meta = {'editReason': 'Moderator moved item'} conversation.update(request.get_host(), request.access_token) else: # These are all PATCH requests and we need the item in question first if request.POST.get('item_type') == 'conversation': url, params, headers = Conversation.build_request( request.get_host(), request.POST.get('item_id'), access_token=request.access_token ) if request.POST.get('item_type') == 'event': url, params, headers = Event.build_request( request.get_host(), request.POST.get('item_id'), access_token=request.access_token ) if request.POST.get('item_type') == 'microcosm': url, params, headers = Microcosm.build_request( request.get_host(), request.POST.get('item_id'), access_token=request.access_token ) # And then to execute the PATCH against the item if request.POST.get('action') == 'delete': payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/deleted', 'value': True}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) elif request.POST.get('action') == 'undelete': payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/deleted', 'value': False}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) elif request.POST.get('action') == 'approve': payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/moderated', 'value': False}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) elif request.POST.get('action') == 'pin': payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/sticky', 'value': True}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) elif request.POST.get('action') == 'unpin': payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/sticky', 'value': False}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) elif request.POST.get('action') == 'open': payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/open', 'value': True}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) elif request.POST.get('action') == 'close': payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/open', 'value': False}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) return HttpResponseRedirect(reverse('single-microcosm', args=(microcosm_id,)))
def item(request): """ View for moderation actions on a single item. """ if request.method == 'POST': if request.POST.get('action') == 'move': if request.POST.get('item_type') == 'event': event = Event.retrieve(request.get_host(), request.POST.get('item_id'), access_token=request.access_token) event.microcosm_id = int(request.POST.get('microcosm_id')) event.meta = {'editReason': 'Moderator moved item'} event.update(request.get_host(), request.access_token) elif request.POST.get('item_type') == 'conversation': conversation = Conversation.retrieve(request.get_host(), request.POST.get('item_id'), access_token=request.access_token) conversation.microcosm_id = int(request.POST.get('microcosm_id')) conversation.meta = {'editReason': 'Moderator moved item'} conversation.update(request.get_host(), request.access_token) elif request.POST.get('action') == 'delete': if request.POST.get('item_type') == 'conversation': url, params, headers = Conversation.build_request(request.get_host(), request.POST.get('item_id'), access_token=request.access_token) if request.POST.get('item_type') == 'event': url, params, headers = Event.build_request(request.get_host(), request.POST.get('item_id'), access_token=request.access_token) payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/deleted', 'value': True}]) headers['Content-Type'] = 'application/json' requests.patch(url, payload, headers=headers) return HttpResponseRedirect(reverse('single-microcosm', args=(request.POST.get('microcosm_id'),))) if request.method == 'GET': if request.GET.get('item_type') == 'conversation': url, params, headers = Conversation.build_request(request.get_host(), request.GET.get('item_id'), 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)) content = Conversation.from_api_response(responses[url]) elif request.GET.get('item_type') == 'event': url, params, headers = Event.build_request(request.get_host(), request.GET.get('item_id'), 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)) content = Event.from_api_response(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'content': content, 'item_type': request.GET.get('item_type'), 'action': request.GET.get('action'), } if request.GET.get('action') == 'move': # Fetch list of microcosms to supply in form. view_data['microcosms'] = MicrocosmList.retrieve(request.get_host(), access_token=request.access_token) return render(request, 'forms/moderation_item.html', view_data)
def edit(request, event_id): """ Edit an event. """ try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'state_edit': True } if request.method == 'POST': form = edit_form(request.POST) if form.is_valid(): event_request = Event.from_edit_form(form.cleaned_data) try: event_response = event_request.update(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect( reverse('single-event', args=(event_response.id, ))) else: view_data['form'] = form view_data['microcosm_id'] = form['microcosmId'] return render(request, form_template, view_data) if request.method == 'GET': try: event = Event.retrieve(request.get_host(), id=event_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) view_data['form'] = edit_form.from_event_instance(event) view_data['microcosm_id'] = event.microcosm_id try: view_data['attendees'] = Event.get_attendees( host=request.get_host(), id=event_id, access_token=request.access_token) attendees_json = [] for attendee in view_data['attendees'].items.items: attendees_json.append({ 'id': attendee.profile.id, 'profileName': attendee.profile.profile_name, 'avatar': attendee.profile.avatar, 'sticky': 'true' }) if len(attendees_json) > 0: view_data['attendees_json'] = json.dumps(attendees_json) except APIException: # Missing RSVPs is not critical, but we should know if it doesn't work. logger.error(str(APIException)) pass return render(request, form_template, view_data)