def projects(request): if request.method == 'POST': from project_actions import add_user_to_project, remove_user_from_project from project_actions import add_group_to_project, remove_group_from_project if request.POST['action'] == 'add': project_id = request.POST['project_id'] name = request.POST['name'] pa_project = PAProject.objects.get(id=project_id) try: pa_user = PAUser.objects.get(name=name) add_user_to_project(pa_project, pa_user) except: pa_group = PAGroup.objects.get(name=name) add_group_to_project(pa_project, pa_group) elif request.POST['action'] == 'remove_user': project_access_id = request.POST['user_with_access_id'] pa_project_access = PAUserProjectAccess.objects.get(id=project_access_id) remove_user_from_project(pa_project_access) elif request.POST['action'] == 'remove_group': project_access_id = request.POST['group_with_access_id'] pa_project_access = PAGroupProjectAccess.objects.get(id=project_access_id) remove_group_from_project(pa_project_access) else: raise Exception("Unsupported POST action") template = loader.get_template('projects.html') context = RequestContext(request, { 'projects': PAProject.objects.all(), }) return HttpResponse(template.render(context))
def create(self, request, project_pk): group_pk = request.data["id"] project = self.get_project(project_pk) group = self.get_group(group_pk) if not PAGroupProjectAccess.objects.filter(project=project, group=group).exists(): group_project_access = add_group_to_project(project, group) serializer = PAGroupProjectAccessSerializer(group_project_access) return Response(serializer.data, status.HTTP_201_CREATED) else: return Response({u"detail": "Group already has access to project"}, status.HTTP_400_BAD_REQUEST)