def validate_permission(self): cached_data = get_cached_user(self.cleaned_data.get('token')) university = get_object_or_404(University, slug_name=self.cleaned_data.get('slug')) if check_request_user_role(cached_data, ['president', 'admin']) and cached_data['university_id'] == \ university.pk: return True raise forms.ValidationError('User has no permission !', code=FORM_ERROR_CODE_MAP[4])
def grant_admin_permission_groups(request): if request.method == 'POST': form = GrantUserPermissionForm(request.POST) permission_group_list = [ int(i) for i in request.POST.getlist('permission_groups[]') ] if form.is_valid( ) and permission_group_list and check_request_user_role( request.POST['token'], ('president', )): user = form.authenticate() update_admin_permission_group(user, permission_group_list) return Response(data=response_message(code=200), status=status.HTTP_200_OK) return Response(data=response_message(message='Invalid inputs'), status=status.HTTP_400_BAD_REQUEST) return Response(data=response_message(code=405), status=status.HTTP_405_METHOD_NOT_ALLOWED)
def get_customer_upg_by_university(request): if request.method == 'GET': response_data = list() token = request.GET['token'] university = get_object_or_404( University, slug_name=request.GET['university_slug']) or None cached_data = get_cached_user(token) if not check_request_user_role(cached_data, ['admin', 'president', ]) or int(cached_data['university_id']) != \ university.pk: return Response(data=response_message(code=401), status=status.HTTP_401_UNAUTHORIZED) if not university: return Response(data=response_message(message='Invalid parameter'), status=status.HTTP_400_BAD_REQUEST) university_upg = CustomerUPG.customer_upg.get_org_deserved_customer_upg( university) for upg in university_upg: response_data.append(model_to_dict(upg)) return Response(data={'result': response_data}, status=status.HTTP_200_OK) return Response(data=response_message(code=405), status=status.HTTP_405_METHOD_NOT_ALLOWED)
def get_user(cached_data): if check_request_user_role(cached_data, ('customer', )): return get_object_or_404(Customer, pk=int(cached_data['user_id'])) else: return get_object_or_404(OrgAdmin, pk=int(cached_data['user_id']))
def get_customer(self, cached_data): if check_request_user_role(cached_data, ('customer', )): return get_object_or_404(Customer, pk=int(cached_data['user_id'])) else: return get_object_or_404(Customer, email=self.cleaned_data.get('customer'))