def confirm(request): """ View for moderation actions on a single item. """ 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]) elif request.GET.get('item_type') == 'microcosm': url, params, headers = Microcosm.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 = Microcosm.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 confirm(request): """ View for moderation actions on a single item. """ if request.method == 'POST': 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) 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.POST.get('item_type') == 'event': url, params, headers = Event.build_request( request.get_host(), request.POST.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]) elif 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) request.view_requests.append( grequests.get(url, params=params, headers=headers)) responses = response_list_to_dict( grequests.map(request.view_requests)) content = Microcosm.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.POST.get('item_type'), 'action': request.POST.get('action'), } if request.POST.get('action') == 'move': # Fetch list of microcosms to supply in form. microcosms = MicrocosmList.retrieve( request.get_host(), access_token=request.access_token) if request.POST.get('item_type') == 'microcosm': newlist = [] nuke = content.id found = False for m in microcosms.microcosms: if m['id'] == nuke: found = True nuke = m['id'] elif m.get('parent_id') and m['parent_id'] == nuke: found = True nuke = m['id'] else: if found: nuke = 0 newlist.append(m) microcosms.microcosms = newlist view_data['microcosms'] = microcosms return render(request, 'forms/moderation_item.html', view_data)
def single(request, event_id): """ Display a single event with comments and attendees. """ # Comment offset. offset = int(request.GET.get('offset', 0)) # Create request for event resource. event_url, event_params, event_headers = Event.build_request(request.get_host(), id=event_id, offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(event_url, params=event_params, headers=event_headers)) # Create request for event attendees. att_url, att_params, att_headers = Event.build_attendees_request(request.get_host(), event_id, request.access_token) request.view_requests.append(grequests.get(att_url, params=att_params, headers=att_headers)) # Perform requests and instantiate view objects. try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) event = Event.from_api_response(responses[event_url]) comment_form = CommentForm(initial=dict(itemId=event_id, itemType='event')) user = Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None attendees = AttendeeList(responses[att_url]) attendees_yes = [] attendees_invited = [] user_is_attending = False for attendee in attendees.items.items: if attendee.rsvp == 'yes': attendees_yes.append(attendee) if request.whoami_url: if attendee.profile.id == user.id: user_is_attending = True elif attendee.rsvp == 'maybe': attendees_invited.append(attendee) # Determine whether the event spans more than one day and if it has expired. # TODO: move stuff that is purely rendering to the template. today = datetime.datetime.now() if hasattr(event, 'when'): end_date = event.when + datetime.timedelta(minutes=event.duration) is_same_day = False if end_date.strftime('%d%m%y') == event.when.strftime('%d%m%y'): is_same_day = True event_dates = { 'type': 'multiple' if not is_same_day else 'single', 'end': end_date } is_expired = True if int(end_date.strftime('%s')) < int(today.strftime('%s')) else False else: event_dates = { 'type': 'tba' } is_expired = False # Why is this a minimum of 10%? rsvp_percentage = event.rsvp_percentage if len(attendees_yes) and event.rsvp_percentage < 10: rsvp_percentage = 10 # Fetch attachments for all comments on this page. # TODO: the code that does this should be in one place. attachments = {} for comment in event.comments.items: c = comment.as_dict if 'attachments' in c: c_attachments = Attachment.retrieve(request.get_host(), "comments", c['id'], access_token=request.access_token) attachments[str(c['id'])] = c_attachments view_data = { 'user': user, 'site': Site(responses[request.site_url]), 'content': event, 'comment_form': comment_form, 'pagination': build_pagination_links(responses[event_url]['comments']['links'], event.comments), 'item_type': 'event', 'attendees': attendees, 'attendees_yes': attendees_yes, 'attendees_invited': attendees_invited, 'user_is_attending': user_is_attending, 'event_dates': event_dates, 'rsvp_num_attending': len(attendees_yes), 'rsvp_num_invited': len(attendees_invited), 'rsvp_percentage': rsvp_percentage, 'is_expired': is_expired, 'attachments': attachments } return render(request, single_template, view_data)
def testEventAsDict(self): data = json.loads(open(os.path.join(TEST_ROOT, 'data', 'event_without_comment.json')).read())['data'] event = Event.from_api_response(data) event.as_dict()
def testCommentedEventInit(self): data = json.loads(open(os.path.join(TEST_ROOT, 'data', 'event_with_comment.json')).read())['data'] Event.from_api_response(data)
def confirm(request): """ View for moderation actions on a single item. """ if request.method == 'POST': 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 ) 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.POST.get('item_type') == 'event': url, params, headers = Event.build_request( request.get_host(), request.POST.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]) elif 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 ) request.view_requests.append(grequests.get(url, params=params, headers=headers)) responses = response_list_to_dict(grequests.map(request.view_requests)) content = Microcosm.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.POST.get('item_type'), 'action': request.POST.get('action'), } if request.POST.get('action') == 'move': # Fetch list of microcosms to supply in form. microcosms = MicrocosmList.retrieve( request.get_host(), access_token=request.access_token ) if request.POST.get('item_type') == 'microcosm': newlist = [] nuke = content.id found = False for m in microcosms.microcosms: if m['id'] == nuke: found = True nuke = m['id'] elif m.get('parent_id') and m['parent_id'] == nuke: found = True nuke = m['id'] else: if found: nuke = 0 newlist.append(m) microcosms.microcosms = newlist view_data['microcosms'] = microcosms return render(request, 'forms/moderation_item.html', view_data)
def testEventAsDict(self): data = json.loads( open(os.path.join(TEST_ROOT, 'data', 'event_without_comment.json')).read())['data'] event = Event.from_api_response(data) event.as_dict()
def testCommentedEventInit(self): data = json.loads( open(os.path.join(TEST_ROOT, 'data', 'event_with_comment.json')).read())['data'] Event.from_api_response(data)
def testCommentedEventInit(self): data = json.loads(open(os.path.join(TEST_ROOT, "data", "event_with_comment.json")).read())["data"] Event.from_api_response(data)
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 single(request, event_id): """ Display a single event with comments and attendees. """ # Comment offset. try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 # Create request for event resource. event_url, event_params, event_headers = Event.build_request( request.get_host(), id=event_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(event_url, params=event_params, headers=event_headers)) # Create request for event attendees. att_url, att_params, att_headers = Event.build_attendees_request( request.get_host(), event_id, request.access_token) request.view_requests.append( grequests.get(att_url, params=att_params, headers=att_headers)) # Perform requests and instantiate view objects. try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) event = Event.from_api_response(responses[event_url]) comment_form = CommentForm(initial=dict(itemId=event_id, itemType='event')) user = Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None attendees = AttendeeList(responses[att_url]) attendees_yes = [] attendees_invited = [] user_is_attending = False for attendee in attendees.items.items: if attendee.rsvp == 'yes': attendees_yes.append(attendee) if request.whoami_url: if attendee.profile.id == user.id: user_is_attending = True elif attendee.rsvp == 'maybe': attendees_invited.append(attendee) # Determine whether the event spans more than one day and if it has expired. # TODO: move stuff that is purely rendering to the template. today = datetime.datetime.now() if hasattr(event, 'when'): end_date = event.when + datetime.timedelta(minutes=event.duration) is_same_day = False if end_date.strftime('%d%m%y') == event.when.strftime('%d%m%y'): is_same_day = True event_dates = { 'type': 'multiple' if not is_same_day else 'single', 'end': end_date } is_expired = True if int(end_date.strftime('%s')) < int( today.strftime('%s')) else False else: event_dates = {'type': 'tba'} is_expired = False # Why is this a minimum of 10%? rsvp_percentage = event.rsvp_percentage if len(attendees_yes) and event.rsvp_percentage < 10: rsvp_percentage = 10 # Fetch attachments for all comments on this page. # TODO: the code that does this should be in one place. attachments = {} for comment in event.comments.items: c = comment.as_dict if 'attachments' in c: c_attachments = Attachment.retrieve( request.get_host(), "comments", c['id'], access_token=request.access_token) attachments[str(c['id'])] = c_attachments view_data = { 'user': user, 'site': Site(responses[request.site_url]), 'content': event, 'comment_form': comment_form, 'pagination': build_pagination_links(responses[event_url]['comments']['links'], event.comments), 'item_type': 'event', 'attendees': attendees, 'attendees_yes': attendees_yes, 'attendees_invited': attendees_invited, 'user_is_attending': user_is_attending, 'event_dates': event_dates, 'rsvp_num_attending': len(attendees_yes), 'rsvp_num_invited': len(attendees_invited), 'rsvp_percentage': rsvp_percentage, 'is_expired': is_expired, 'attachments': attachments } return render(request, single_template, view_data)