def list_members(request, microcosm_id): offset = int(request.GET.get('offset', 0)) microcosm_url, params, headers = Microcosm.build_request(request.get_host(), id=microcosm_id, offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(microcosm_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) microcosm = Microcosm.from_api_response(responses[microcosm_url]) roles_url, params, headers = RoleList.build_request(request.META['HTTP_HOST'], id=microcosm_id, offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(roles_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) roles = RoleList.from_api_response(responses[roles_url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'site_section': 'memberships', 'content': microcosm, 'memberships': roles, 'item_type': 'microcosm', 'pagination': build_pagination_links(responses[roles_url]['roles']['links'], roles.items) } return render(request, members_list_template, view_data)
def edit_microcosm(request, microcosm_id): 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]), } if request.method == 'POST': form = microcosm_edit_form(request.POST) if form.is_valid(): microcosm_request = Microcosm.from_edit_form(form.cleaned_data) try: microcosm_response = microcosm_request.update(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('single-microcosm', args=(microcosm_response.id,))) else: view_data['form'] = form return render(request, microcosm_form_template, view_data) if request.method == 'GET': try: microcosm = Microcosm.retrieve(request.get_host(), id=microcosm_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) view_data['form'] = microcosm_edit_form(microcosm.as_dict) return render(request, microcosm_form_template, view_data)
def create_microcosm(request, parent_id=0): 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]), } if request.method == 'POST': form = microcosm_create_form(request.POST) if form.is_valid(): microcosm_request = Microcosm.from_create_form(form.cleaned_data) try: microcosm_response = microcosm_request.create( request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect( reverse('single-microcosm', args=(microcosm_response.id, ))) else: view_data['form'] = form return render(request, microcosm_form_template, view_data) if request.method == 'GET': view_data['form'] = microcosm_create_form(initial=dict( parentId=parent_id)) view_data['parentId'] = parent_id return render(request, microcosm_form_template, view_data)
def list_updates(request): if not request.access_token: try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) view_data = { 'user': False, 'site_section': 'updates', 'site': Site(responses[request.site_url]), } else: # pagination offset offset = int(request.GET.get('offset', 0)) url, params, headers = UpdateList.build_request(request.get_host(), offset=offset, 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) updates_list = UpdateList(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'content': updates_list, 'pagination': build_pagination_links(responses[url]['updates']['links'], updates_list.updates), 'site_section': 'updates', 'site': Site(responses[request.site_url]), } return render(request, list_template, view_data)
def edit(request, comment_id): """ Edit a comment. """ 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]), } if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment_request = Comment.from_edit_form(form.cleaned_data) try: comment = comment_request.update(request.get_host(), access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) try: process_attachments(request, comment) except ValidationError: try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) comment_form = CommentForm( initial = { 'itemId': comment.item_id, 'itemType': comment.item_type, 'comment_id': comment.id, 'markdown': request.POST['markdown'], }) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'content': comment, 'comment_form': comment_form, 'error': 'Sorry, one of your files was over 5MB. Please try again.', } return render(request, form_template, view_data) if comment.meta.links.get('commentPage'): return HttpResponseRedirect(build_comment_location(comment)) else: return HttpResponseRedirect(reverse('single-comment', args=(comment.id,))) else: view_data['form'] = form return render(request, form_template, view_data) if request.method == 'GET': try: comment = Comment.retrieve(request.get_host(), comment_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) view_data['form'] = CommentForm(comment.as_dict) return render(request, form_template, 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)
def edit_microcosm(request, microcosm_id): 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]), } if request.method == 'POST': form = microcosm_edit_form(request.POST) if not form.is_valid(): view_data['form'] = form return render(request, microcosm_form_template, view_data) payload = form.cleaned_data if request.POST.get('remove_logo'): payload['logoUrl'] = '' payload['removeLogo'] = True elif request.FILES.has_key('logo'): file_request = FileMetadata.from_create_form( request.FILES['logo'], ) try: metadata = file_request.create( request.get_host(), request.access_token, width=64, height=64, ) except APIException as exc: return respond_with_error(request, exc) logo_url = get_subdomain_url( request.get_host()) + '/api/v1/files/' + metadata.file_hash if hasattr(metadata, 'file_ext'): logo_url = logo_url + '.' + metadata.file_ext payload['logoUrl'] = logo_url microcosm_request = Microcosm.from_edit_form(payload) try: microcosm_response = microcosm_request.update( request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect( reverse('single-microcosm', args=(microcosm_response.id, ))) if request.method == 'GET': try: microcosm = Microcosm.retrieve(request.get_host(), id=microcosm_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) view_data['form'] = microcosm_edit_form(microcosm.as_dict) return render(request, microcosm_form_template, view_data)
def create(request): """ Create a comment, processing any attachments (including deletion of attachments) and redirecting to the single comment form if there are any validation errors. """ # TODO: determine whether the single comment creation form will use this view. # Remove the conditional if not. if request.method == 'POST': form = CommentForm(request.POST) # If invalid, load single comment view showing validation errors. if not form.is_valid(): 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]), 'form': form, } return render(request, form_template, view_data) # Create comment with API. comment_request = Comment.from_create_form(form.cleaned_data) try: comment = comment_request.create(request.get_host(), access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) try: process_attachments(request, comment) except ValidationError: try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) comment_form = CommentForm( initial = { 'itemId': comment.item_id, 'itemType': comment.item_type, 'comment_id': comment.id, 'markdown': request.POST['markdown'], } ) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'content': comment, 'comment_form': comment_form, 'error': 'Sorry, one of your files was over 5MB. Please try again.', } return render(request, form_template, view_data) # API returns which page in the thread this comments appear in, so redirect there. if comment.meta.links.get('commentPage'): return HttpResponseRedirect(build_comment_location(comment))
def edit_microcosm(request, microcosm_id): 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]), } if request.method == 'POST': form = microcosm_edit_form(request.POST) if not form.is_valid(): view_data['form'] = form return render(request, microcosm_form_template, view_data) payload = form.cleaned_data if request.POST.get('remove_logo'): payload['logoUrl'] = '' payload['removeLogo'] = True elif request.FILES.has_key('logo'): file_request = FileMetadata.from_create_form( request.FILES['logo'], ) try: metadata = file_request.create( request.get_host(), request.access_token, width=64, height=64, ) except APIException as exc: return respond_with_error(request, exc) logo_url = get_subdomain_url(request.get_host()) + '/api/v1/files/' + metadata.file_hash if hasattr(metadata, 'file_ext'): logo_url = logo_url + '.' + metadata.file_ext payload['logoUrl'] = logo_url microcosm_request = Microcosm.from_edit_form(payload) try: microcosm_response = microcosm_request.update(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('single-microcosm', args=(microcosm_response.id,))) if request.method == 'GET': try: microcosm = Microcosm.retrieve(request.get_host(), id=microcosm_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) view_data['form'] = microcosm_edit_form(microcosm.as_dict) return render(request, microcosm_form_template, view_data)
def settings(request): if request.method == 'POST': # Update global settings for notifications. postdata = { 'sendEmail': bool(request.POST.get('profile_receive_email')), 'sendSMS': False, } try: GlobalOptions.update(request.get_host(), postdata, request.access_token) except APIException as exc: return respond_with_error(request, exc) # Update settings for each notification type. for x in range(1, 10): if request.POST.get('id_' + str(x)): postdata = { 'id': int(request.POST['id_' + str(x)]), 'sendEmail': bool(request.POST.get('send_email_' + str(x))), 'sendSMS': False, } try: UpdatePreference.update(request.get_host(), request.POST['id_' + str(x)], postdata, request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('updates-settings')) if request.method == 'GET': url, params, headers = UpdatePreference.build_request(request.get_host(), request.access_token) request.view_requests.append(grequests.get(url, params=params, headers=headers)) url2, params2, headers2 = GlobalOptions.build_request(request.get_host(), request.access_token) request.view_requests.append(grequests.get(url2, params=params2, headers=headers2)) try: responses = response_list_to_dict(grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) preference_list = UpdatePreference.from_list(responses[url]) global_options = GlobalOptions.from_api_response(responses[url2]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'content': preference_list, 'globaloptions': global_options, } return render(request, settings_template, view_data)
def list_members(request, microcosm_id): try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 microcosm_url, params, headers = Microcosm.build_request( request.get_host(), id=microcosm_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(microcosm_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) microcosm = Microcosm.from_api_response(responses[microcosm_url]) roles_url, params, headers = RoleList.build_request( request.META['HTTP_HOST'], id=microcosm_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(roles_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) roles = RoleList.from_api_response(responses[roles_url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'site_section': 'memberships', 'content': microcosm, 'memberships': roles, 'item_type': 'microcosm', 'pagination': build_pagination_links(responses[roles_url]['roles']['links'], roles.items) } return render(request, members_list_template, view_data)
def list(request): # Offset for paging of huddles try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 unread = bool(request.GET.get('unread', False)) huddle_url, params, headers = HuddleList.build_request(request.get_host(), offset=offset, unread=unread, 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) huddles = HuddleList(responses[huddle_url]) filter_name = [] if unread: filter_name.append("unread") view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': huddles, 'unread': unread, 'pagination': build_pagination_links(responses[huddle_url]['huddles']['links'], huddles.huddles) } return render(request, list_template, view_data)
def single_microcosm(request, microcosm_id): # Pagination offset of items within the microcosm. try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 microcosm_url, params, headers = Microcosm.build_request(request.get_host(), id=microcosm_id, offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(microcosm_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) microcosm = Microcosm.from_api_response(responses[microcosm_url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': microcosm, 'item_type': 'microcosm', 'pagination': build_pagination_links(responses[microcosm_url]['items']['links'], microcosm.items) } return render(request, microcosm_single_template, view_data)
def list(request): # Offset for paging of huddles try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 huddle_url, params, headers = HuddleList.build_request(request.get_host(), 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) huddles = HuddleList(responses[huddle_url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': huddles, 'pagination': build_pagination_links(responses[huddle_url]['huddles']['links'], huddles.huddles) } return render(request, list_template, view_data)
def ignored(request): try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 url, params, headers = Ignored.build_request( request.get_host(), offset=offset, 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) ignoredItems = Ignored.from_api_response(responses[url]) view_data = { 'user': Profile( responses[request.whoami_url], summary=False ) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': ignoredItems, 'pagination': build_pagination_links(responses[url]['ignored']['links'], ignoredItems), 'site_section': 'ignored', } return render(request, template_name, view_data)
def ignored(request): try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 url, params, headers = Ignored.build_request( request.get_host(), offset=offset, 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) ignoredItems = Ignored.from_api_response(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': ignoredItems, 'pagination': build_pagination_links(responses[url]['ignored']['links'], ignoredItems), 'site_section': 'ignored', } return render(request, template_name, view_data)
def single(request): searchParams = request.GET.dict() searchParams['type'] = ['conversation','event','profile','huddle'] searchParams['since'] = -1 url, params, headers = Search.build_request(request.get_host(), params=searchParams, 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) search = Search.from_api_response(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': search, 'site_section': 'today' } if responses[url].get('results'): view_data['pagination'] = build_pagination_links( responses[url]['results']['links'], search.results ) return render(request, single_template, view_data)
def single(request): searchParams = dict(request.GET._iterlists()) if searchParams.get('defaults'): searchParams['inTitle'] = 'true' searchParams['sort'] = 'date' url, params, headers = Search.build_request( request.get_host(), params=searchParams, 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) search = Search.from_api_response(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': search, } if responses[url].get('results'): view_data['pagination'] = build_pagination_links( responses[url]['results']['links'], search.results) return render(request, single_template, view_data)
def delete_microcosm(request, microcosm_id): try: microcosm = Microcosm.retrieve(request.get_host(), microcosm_id, access_token=request.access_token) microcosm.delete(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse(list_microcosms))
def newest(request, conversation_id): """ Get redirected to the first unread post in a conversation """ try: response = Conversation.newest(request.get_host(), conversation_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) # because redirects are always followed, we can't just get the 'location' value response = response['comments']['links'] for link in response: if link['rel'] == 'self': response = link['href'] response = str.replace(str(response), '/api/v1', '') pr = urlparse(response) queries = parse_qs(pr[4]) frag = "" if queries.get('comment_id'): frag = 'comment' + queries['comment_id'][0] del queries['comment_id'] # queries is a dictionary of 1-item lists (as we don't re-use keys in our query string) # urlencode will encode the lists into the url (offset=[25]) etc. So get the values straight. for (key, value) in queries.items(): queries[key] = value[0] queries = urlencode(queries) response = urlunparse((pr[0], pr[1], pr[2], pr[3], queries, frag)) return HttpResponseRedirect(response)
def single(request, profile_id): """ Display a single profile by ID. """ # Fetch profile details. profile_url, params, headers = Profile.build_request(request.get_host(), profile_id, access_token=request.access_token) request.view_requests.append(grequests.get(profile_url, params=params, headers=headers)) # Fetch items created by this profile. search_q = 'type:conversation type:event type:huddle type:comment authorId:%s' % profile_id search_params = {'limit': 10, 'q': search_q, 'sort': 'date'} search_url, params, headers = Search.build_request(request.get_host(), search_params, access_token=request.access_token) request.view_requests.append(grequests.get(search_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) user = Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None profile = Profile(responses[profile_url], summary=False) view_data = { 'user': user, 'content': profile, 'item_type': 'profile', 'site': Site(responses[request.site_url]), 'search': Search.from_api_response(responses[search_url]), 'site_section': 'people' } return render(request, single_template, view_data)
def list(request): """ Display a list of the user's sites or an empty state. """ if request.access_token is None: return HttpResponseRedirect(reverse('site-home')) try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 sites_url, params, headers = SiteList.build_request(request.META['HTTP_HOST'], offset=offset, access_token=request.access_token) request.view_requests.append(grequests.get(sites_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) sites = SiteList(responses[sites_url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': request.site, 'content': sites, 'pagination': build_pagination_links(responses[sites_url]['sites']['links'], sites.sites) } return render(request, SitesView.template_list, view_data)
def single(request): searchParams = dict(request.GET._iterlists()) if searchParams.get('defaults'): searchParams['inTitle'] = 'true' searchParams['sort'] = 'date' url, params, headers = Search.build_request(request.get_host(), params=searchParams, 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) search = Search.from_api_response(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': search, } if responses[url].get('results'): view_data['pagination'] = build_pagination_links(responses[url]['results']['links'], search.results) 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 list(request): # Record offset for paging of profiles. try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 top = bool(request.GET.get('top', False)) q = request.GET.get('q', "") following = bool(request.GET.get('following', False)) online = bool(request.GET.get('online', False)) profiles_url, params, headers = ProfileList.build_request(request.get_host(), offset=offset, top=top, q=q, following=following, online=online, access_token=request.access_token) request.view_requests.append(grequests.get(profiles_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) profiles = ProfileList(responses[profiles_url]) subtitle = False if q != "" and len(q) == 1: subtitle = "names starting with %s" % (q.upper()) filter_name = [] if following: filter_name.append("following") if online: filter_name.append("online now") if top: filter_name.append("most comments") if len(filter_name) < 1: filter_name.append("sorted alphabetically") view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': profiles, 'pagination': build_pagination_links(responses[profiles_url]['profiles']['links'], profiles.profiles), 'q': q, 'top': top, 'following': following, 'alphabet': string.ascii_lowercase, 'site_section': 'people', 'filter_name': ", ".join(filter_name), 'subtitle': subtitle, 'online': online } return render(request, list_template, view_data)
def source(request, comment_id): """ Retrieve the markdown source for a comment. """ try: response = Comment.source(request.get_host(), comment_id, request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponse(response, content_type='application/json')
def index(request): 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) if request.whoami_url else None, 'site': request.site, } return render(request, DevelopersView.template, view_data)
def attachments(request, comment_id): """ Retrieve a comment's attachments. """ try: response = Attachment.source(request.get_host(), type=Comment.api_path_fragment, id=comment_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponse(response, content_type='application/json')
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, conversation_id): """ Delete a conversation and be redirected to the parent microcosm. """ conversation = Conversation.retrieve(request.get_host(), conversation_id, access_token=request.access_token) try: conversation.delete(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('single-microcosm', args=(conversation.microcosm_id,)))
def invite(request, huddle_id): """ Invite participants to a huddle. """ ids = [int(x) for x in request.POST.get('invite_profile_id').split()] try: Huddle.invite(request.get_host(), huddle_id, ids, request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('single-huddle', args=(huddle_id,)))
def incontext(request, comment_id): """ Redirect to the user's first unread comment in a list of comments. """ try: response = Comment.incontext(request.get_host(), comment_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) redirect = build_newest_comment_link(response, request) return HttpResponseRedirect(redirect)
def newest(request, conversation_id): """ Redirect to the user's first unread post in the conversation. """ try: response = Conversation.newest(request.get_host(), conversation_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) redirect = build_newest_comment_link(response, request) return HttpResponseRedirect(redirect)
def newest(request, huddle_id): """ Get redirected to the first unread post in a huddle """ try: response = Huddle.newest(request.get_host(), huddle_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) redirect = build_newest_comment_link(response) return HttpResponseRedirect(redirect)
def delete(request, huddle_id): """ Delete a huddle and be redirected to the parent microcosm. """ try: huddle = Huddle.retrieve(request.get_host(), huddle_id, access_token=request.access_token) huddle.delete(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('list-huddle'))
def newest(request, huddle_id): """ Get redirected to the first unread post in a huddle """ try: response = Huddle.newest(request.get_host(), huddle_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) redirect = build_newest_comment_link(response, request) return HttpResponseRedirect(redirect)
def create(request): if request.method == 'GET': 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) if request.whoami_url else None, 'site': request.site, 'site_name': request.GET['name'] if request.GET.get('name') else None } return render(request, SitesView.template_create, view_data) if request.method == 'POST': 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) if request.whoami_url else None, 'site': request.site, } # Request from landing page if request.POST.get('new_site_name'): view_data['site_name'] = request.POST['new_site_name'] return render(request, SitesView.template_create, view_data) # Not from landing page, assume user is logged in and create the site. new_site_data = { 'title': request.POST['site_name'], 'description': request.POST['site_description'], 'subdomainKey': request.POST['site_subdomain'], } site_request = Site.from_dict(new_site_data) try: site_request.create(request.META['HTTP_HOST'], request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(reverse('dashboard-sites'))
def edit(request, conversation_id): """ Edit a conversation. """ 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(): conv_request = Conversation.from_edit_form(form.cleaned_data) try: conv_response = conv_request.update(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect( reverse('single-conversation', args=(conv_response.id, ))) else: view_data['form'] = form return render(request, form_template, view_data) if request.method == 'GET': conversation = Conversation.retrieve(request.get_host(), id=conversation_id, access_token=request.access_token) view_data['form'] = edit_form.from_conversation_instance(conversation) 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, conversation_id): """ Delete a conversation and be redirected to the parent microcosm. """ conversation = Conversation.retrieve(request.get_host(), conversation_id, access_token=request.access_token) try: conversation.delete(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect( reverse('single-microcosm', args=(conversation.microcosm_id, )))
def mark_read(request): """ Mark a scope (e.g. site or microcosm) as read for the authenticated user. """ scope = { 'itemType': request.POST.get('item_type'), 'itemId': int(request.POST.get('item_id')), } try: Profile.mark_read(request.get_host(), scope, request.access_token) except APIException as exc: return respond_with_error(request, exc) return HttpResponseRedirect(request.POST.get('return_path'))
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 list(request): url, params, headers = Trending.build_request(request.get_host(), 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) trending = Trending.from_api_response(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': trending, 'pagination': build_pagination_links(responses[url]['items']['links'], trending.items), 'site_section': 'trending' } return render(request, list_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 delete(request, comment_id): """ Delete a comment and be redirected to the item. """ try: comment = Comment.retrieve(request.get_host(), comment_id, access_token=request.access_token) comment.delete(request.get_host(), request.access_token) except APIException as exc: return respond_with_error(request, exc) if comment.item_type == 'event': return HttpResponseRedirect( reverse('single-event', args=(comment.item_id, ))) elif comment.item_type == 'conversation': return HttpResponseRedirect( reverse('single-conversation', args=(comment.item_id, ))) else: return HttpResponseRedirect(reverse('microcosm-list'))
def root_microcosm(request): # Pagination offset of items within the microcosm. try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 microcosm_id = 0 microcosm_url, params, headers = Microcosm.build_request( request.get_host(), id=microcosm_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(microcosm_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) microcosm = Microcosm.from_api_response(responses[microcosm_url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': microcosm, 'item_type': 'microcosm', 'pagination': build_pagination_links(responses[microcosm_url]['items']['links'], microcosm.items) } return render(request, microcosm_root_template, view_data)
def single(request, profile_id): """ Display a single profile by ID. """ # Fetch profile details. profile_url, params, headers = Profile.build_request( request.get_host(), profile_id, access_token=request.access_token) request.view_requests.append( grequests.get(profile_url, params=params, headers=headers)) # Fetch items created by this profile. search_q = 'type:conversation type:event type:huddle type:comment authorId:%s' % profile_id search_params = {'limit': 10, 'q': search_q, 'sort': 'date'} search_url, params, headers = Search.build_request( request.get_host(), search_params, access_token=request.access_token) request.view_requests.append( grequests.get(search_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) user = Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None profile = Profile(responses[profile_url], summary=False) view_data = { 'user': user, 'content': profile, 'item_type': 'profile', 'site': Site(responses[request.site_url]), 'search': Search.from_api_response(responses[search_url]), 'site_section': 'people' } return render(request, single_template, view_data)
def single(request): searchParams = request.GET.dict() searchParams['type'] = ['conversation', 'event', 'profile', 'huddle'] searchParams['since'] = -1 url, params, headers = Search.build_request( request.get_host(), params=searchParams, 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) search = Search.from_api_response(responses[url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'content': search, 'site_section': 'today' } if responses[url].get('results'): view_data['pagination'] = build_pagination_links( responses[url]['results']['links'], search.results) return render(request, single_template, view_data)
def rsvp(request, event_id): """ Create an attendee (RSVP) for an event. An attendee can be in one of four states: invited, yes, maybe, no. """ responses = response_list_to_dict(grequests.map(request.view_requests)) user = Profile(responses[request.whoami_url], summary=False) attendee = [ dict(rsvp=request.POST['rsvp'], profileId=user.id), ] try: response = Event.rsvp_api(request.get_host(), event_id, user.id, attendee, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: return HttpResponseBadRequest() return HttpResponseRedirect(reverse('single-event', args=(event_id, )))
def edit(request, comment_id): """ Edit a comment. """ 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]), } if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment_request = Comment.from_edit_form(form.cleaned_data) try: comment = comment_request.update( request.get_host(), access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) try: process_attachments(request, comment) except ValidationError: try: responses = response_list_to_dict( grequests.map(request.view_requests)) except APIException as exc: return respond_with_error(request, exc) comment_form = CommentForm( initial={ 'itemId': comment.item_id, 'itemType': comment.item_type, 'comment_id': comment.id, 'markdown': request.POST['markdown'], }) view_data = { 'user': Profile(responses[request.whoami_url], summary=False), 'site': Site(responses[request.site_url]), 'content': comment, 'comment_form': comment_form, 'error': 'Sorry, one of your files was over 5MB. Please try again.', } return render(request, form_template, view_data) if comment.meta.links.get('commentPage'): return HttpResponseRedirect(build_comment_location(comment)) else: return HttpResponseRedirect( reverse('single-comment', args=(comment.id, ))) else: view_data['form'] = form return render(request, form_template, view_data) if request.method == 'GET': try: comment = Comment.retrieve(request.get_host(), comment_id, access_token=request.access_token) except APIException as exc: return respond_with_error(request, exc) view_data['form'] = CommentForm(comment.as_dict) return render(request, form_template, view_data)
def edit_members(request, microcosm_id, group_id): if request.method == 'POST': pass elif request.method == 'GET': try: offset = int(request.GET.get('offset', 0)) except ValueError: offset = 0 microcosm_url, params, headers = Microcosm.build_request( request.get_host(), id=microcosm_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(microcosm_url, params=params, headers=headers)) role_url, params, headers = Role.build_request( request.get_host(), microcosm_id=microcosm_id, id=group_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(role_url, params=params, headers=headers)) criteria_url, params, headers = RoleCriteriaList.build_request( request.get_host(), microcosm_id=microcosm_id, id=group_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(criteria_url, params=params, headers=headers)) profiles_url, params, headers = RoleProfileList.build_request( request.get_host(), microcosm_id=microcosm_id, id=group_id, offset=offset, access_token=request.access_token) request.view_requests.append( grequests.get(profiles_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) microcosm = Microcosm.from_api_response(responses[microcosm_url]) role = Role.from_api_response(responses[role_url]) criteria = RoleCriteriaList(responses[criteria_url]) profiles = RoleProfileList(responses[profiles_url]) view_data = { 'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None, 'site': Site(responses[request.site_url]), 'site_section': 'memberships', 'content': microcosm, 'role': role, 'criteria': criteria, 'profiles': profiles, 'item_type': 'memberships', 'state_edit': True, 'pagination': build_pagination_links(responses[microcosm_url]['items']['links'], microcosm.items) } return render(request, members_form_template, view_data)
def members_api(request, microcosm_id): data = json.loads(request.body) if data.has_key('deleteRole'): # Delete roleId = data['deleteRole'] try: response = Role.delete_api(request.get_host(), microcosm_id, roleId, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: print 'role delete: ' + response.text return HttpResponseBadRequest() # Need to return a stub here to allow the callee (AJAX) to be happy return HttpResponse( '{"context": "","status": 200,"data": {}, "error": null}') elif data.has_key('role'): # Create or update role = Role.from_summary(data['role']) role.microcosm_id = int(microcosm_id) # Create or update the role if role.id == 0: try: response = Role.create_api(request.get_host(), role, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: print 'role: ' + response.text return HttpResponseBadRequest() role = Role.from_summary(response.json()['data']) else: try: response = Role.update_api(request.get_host(), role, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.found: print json.dumps(role.as_dict()) print 'role: ' + response.text return HttpResponseBadRequest() # Delete all existing criteria and then add the new ones try: response = RoleCriteria.delete_all_api(request.get_host(), role.microcosm_id, role.id, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: print 'role criteria delete all: ' + response.text return HttpResponseBadRequest() if data.has_key('criteria') and len(data['criteria']) > 0: # Loop for clob in data['criteria']: crit = RoleCriteria.from_summary(clob) if crit.id == 0: try: response = RoleCriteria.create_api( request.get_host(), role.microcosm_id, role.id, crit, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: print 'role criteria: ' + response.text return HttpResponseBadRequest() crit = RoleCriteria.from_summary(response.json()['data']) else: try: response = RoleCriteria.update_api( request.get_host(), role.microcosm_id, role.id, crit, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: print 'role criteria: ' + response.text return HttpResponseBadRequest() crit = RoleCriteria.from_summary(response.json()['data']) # Delete all existing role profiles and then add the new ones try: response = RoleProfile.delete_all_api(request.get_host(), role.microcosm_id, role.id, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: print 'role profile delete all: ' + response.text return HttpResponseBadRequest() if data.has_key('profiles') and len(data['profiles']) > 0: # Loop pids = [] for pid in data['profiles']: pids.append({'id': int(pid)}) try: response = RoleProfile.update_api(request.get_host(), role.microcosm_id, role.id, pids, request.access_token) except APIException as exc: return respond_with_error(request, exc) if response.status_code != requests.codes.ok: print 'role profiles: ' + response.text return HttpResponseBadRequest() # Need to return a stub here to allow the callee (AJAX) to be happy return HttpResponse( '{"context": "","status": 200,"data": {}, "error": null}') else: return HttpResponseBadRequest()