def single(request, conversation_id): # Offset of comments. try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 conversation_url, params, headers = Conversation.build_request( request.get_host(), id=conversation_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(conversation_url, params=params, headers=headers)) try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) conversation = Conversation.from_api_response(responses[conversation_url]) comment_form = CommentForm( initial=dict(itemId=conversation_id, itemType='conversation')) # get attachments attachments = {} for comment in conversation.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': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': conversation, 'comment_form': comment_form, 'pagination': build_pagination_links( responses[conversation_url]['comments']['links'], conversation.comments), 'item_type': 'conversation', 'attachments': attachments } return render(request, single_template, view_data)
def single(request, comment_id): """ Display a single comment. """ url, params, headers = Comment.build_request( request.get_host(), id=comment_id, access_token=request.access_token) request.view_requests.append( grequests.get(url, params=params, headers=headers)) try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) content = Comment.from_api_response(responses[url]) comment_form = CommentForm( initial={ 'itemId': content.item_id, 'itemType': content.item_type, 'comment_id': content.id, }) # Fetch any attachments on the comment. attachments = {} c = content.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': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': content, 'comment_form': comment_form, 'attachments': attachments } return render(request, single_template, view_data)
def single(request, huddle_id): # Comment offset. try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 huddle_url, params, headers = Huddle.build_request(request.get_host(), id=huddle_id, offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(huddle_url, params=params, headers=headers)) try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) huddle = Huddle.from_api_response(responses[huddle_url]) comment_form = CommentForm(initial=dict(itemId=huddle_id, itemType='huddle')) # Fetch attachments. attachments = {} for comment in huddle.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 # Fetch huddle participants. participants_json = [p.as_dict for p in huddle.participants] view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': huddle, 'comment_form': comment_form, 'pagination': build_pagination_links(responses[huddle_url]['comments']['links'], huddle.comments), 'item_type': 'huddle', 'attachments': attachments, 'participants_json': json.dumps(participants_json) } return render(request, single_template, view_data)
def single(request, conversation_id): # Offset of comments. try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 conversation_url, params, headers = Conversation.build_request(request.get_host(), id=conversation_id, offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(conversation_url, params=params, headers=headers)) try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) conversation = Conversation.from_api_response(responses[conversation_url]) comment_form = CommentForm(initial=dict(itemId=conversation_id, itemType='conversation')) # get attachments attachments = {} for comment in conversation.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': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': conversation, 'comment_form': comment_form, 'pagination': build_pagination_links(responses[conversation_url]['comments']['links'], conversation.comments), 'item_type': 'conversation', 'attachments': attachments } return render(request, single_template, view_data)
def single(request, comment_id): """ Display a single comment. """ url, params, headers = Comment.build_request(request.get_host(), id=comment_id, access_token=request.access_token) request.view_requests.append(grequests.get(url, params=params, headers=headers)) try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) content = Comment.from_api_response(responses[url]) comment_form = CommentForm( initial={ 'itemId': content.item_id, 'itemType': content.item_type, 'comment_id': content.id, } ) # Fetch any attachments on the comment. attachments = {} c = content.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': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': content, 'comment_form': comment_form, 'attachments': attachments } return render(request, single_template, 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 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)