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()