class ServiceManagement(View): @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.add_service')) def post(self, request): form = ServiceForms.Basic.create_from_request(request) if not form.is_valid(): raise ValidationException(request, form) if form.cleaned_data['remote_id'].services.filter(name=form.cleaned_data['name']).exists(): raise ApiException( request=request, message=_('Assigned remote already has service with name: "{service_name}"').format( service_name=form.cleaned_data['name'] ), status_code=HTTPStatus.CONFLICT ) service = Service() form.fill(service) service.save() return SingleResponse(request, service, status=HTTPStatus.CREATED, serializer=ServiceSerializer.Base) @method_decorator(token_required) @method_decorator(permission_required('core.read_service')) def get(self, request): services = ServiceFilter(request.GET, queryset=Service.objects.all(), request=request).qs return PaginationResponse(request, services, serializer=ServiceSerializer.Base)
class ProjectManagement(View): @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.add_project')) def post(self, request): form = ProjectForms.Basic.create_from_request(request) if not form.is_valid(): raise ValidationException(request, form) project = Project() form.fill(project) project.save() return SingleResponse(request, project, status=HTTPStatus.CREATED, serializer=ProjectSerializer.Detail) @method_decorator(token_required) @method_decorator(permission_required('core.read_project')) def get(self, request): projects = ProjectFilter(request.GET, queryset=Project.objects.all(), request=request).qs return PaginationResponse(request, projects, serializer=ProjectSerializer.Base)
class RemoteDetail(View): @method_decorator(token_required) @method_decorator(permission_required('core.read_remote')) def get(self, request, remote_id): try: remote = Remote.objects.get(pk=remote_id) except Remote.DoesNotExist: raise ApiException(request, _('Remote does not exist.'), status_code=HTTPStatus.NOT_FOUND) if not has_object_permission('check_remote', request.user, remote): raise ApiException(request, _('User is unauthorized.'), status_code=HTTPStatus.FORBIDDEN) return SingleResponse(request, remote, serializer=RemoteSerializer.Base) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.add_remote')) def put(self, request, remote_id): try: remote = Remote.objects.get(pk=remote_id) except Remote.DoesNotExist: raise ApiException(request, _('Remote does not exist.'), status_code=HTTPStatus.NOT_FOUND) form = RemoteForms.Basic.create_from_request(request) if not form.is_valid(): raise ValidationException(request, form) if remote.name != form.cleaned_data['name']: if form.cleaned_data['project_id'].remotes.filter(name=form.cleaned_data['name']).exists(): raise ApiException( request=request, message=_('Assigned project already has remote with name: "{remote_name}"').format( remote_name=form.cleaned_data['name'] ), status_code=HTTPStatus.CONFLICT ) form.fill(remote) remote.save() return SingleResponse(request, data=remote, status=HTTPStatus.OK, serializer=RemoteSerializer.Base) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.delete_remote')) def delete(self, request, remote_id): try: remote = Remote.objects.get(pk=remote_id) except Remote.DoesNotExist: raise ApiException(request, _('Remote does not exist.'), status_code=HTTPStatus.NOT_FOUND) remote.services.all().delete() remote.delete() return HttpResponse(status=HTTPStatus.NO_CONTENT)
class ServiceDetail(View): @method_decorator(token_required) @method_decorator(permission_required('core.read_service')) def get(self, request, service_id): try: service = Service.objects.get(pk=service_id) except Service.DoesNotExist: raise ApiException(request, _('Service does not exist.'), status_code=HTTPStatus.NOT_FOUND) return SingleResponse(request, service, serializer=ServiceSerializer.Base) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.add_service')) def put(self, request, service_id): try: service = Service.objects.get(pk=service_id) except Service.DoesNotExist: raise ApiException(request, _('Service does not exist.'), status_code=HTTPStatus.NOT_FOUND) form = ServiceForms.Basic.create_from_request(request) if not form.is_valid(): raise ValidationException(request, form) if service.name != form.cleaned_data['name']: if form.cleaned_data['remote_id'].services.filter(name=form.cleaned_data['name']).exists(): raise ApiException( request=request, message=_('Assigned remote already has service with name: "{service_name}"').format( service_name=form.cleaned_data['name'] ), status_code=HTTPStatus.CONFLICT ) form.fill(service) service.save() return SingleResponse(request, data=service, status=HTTPStatus.OK, serializer=ServiceSerializer.Base) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.delete_service')) def delete(self, request, service_id): try: service = Service.objects.get(pk=service_id) except Service.DoesNotExist: raise ApiException(request, _('Service does not exist.'), status_code=HTTPStatus.NOT_FOUND) service.delete() return HttpResponse(status=HTTPStatus.NO_CONTENT)
class UserManagement(View): @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.add_user')) def post(self, request): form = UserForms.Create.create_from_request(request) if not form.is_valid(): raise ValidationException(request, form) password = form.cleaned_data.get('password') role = form.cleaned_data.get('role') user = User() form.fill(user) user.set_password(password) user.username = user.email assign_role(user, role.name) user.is_active = False user.save() return SingleResponse(request, user, status=HTTPStatus.CREATED, serializer=UserSerializer.Base) @method_decorator(token_required) @method_decorator(permission_required('core.read_user')) def get(self, request): users = UserFilter(request.GET, queryset=User.objects.all(), request=request).qs return PaginationResponse(request, users, serializer=UserSerializer.Base)
class ProjectDetail(View): @method_decorator(token_required) @method_decorator(permission_required('core.read_project')) def get(self, request, project_id): try: project = Project.objects.get(pk=project_id) except Project.DoesNotExist: raise ApiException(request, _('Project does not exist.'), status_code=HTTPStatus.NOT_FOUND) if not has_object_permission('check_project', request.user, project): raise ApiException(request, _('User is unauthorized.'), status_code=HTTPStatus.FORBIDDEN) return SingleResponse(request, project, serializer=ProjectSerializer.Detail) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.add_project')) def put(self, request, project_id): try: project = Project.objects.get(pk=project_id) except Project.DoesNotExist: raise ApiException(request, _('Project does not exist.'), status_code=HTTPStatus.NOT_FOUND) form = ProjectForms.Basic.create_from_request(request) if not form.is_valid(): raise ValidationException(request, form) form.fill(project) project.save() return SingleResponse(request, data=project, status=HTTPStatus.OK, serializer=ProjectSerializer.Detail) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.delete_project')) def delete(self, request, project_id): try: project = Project.objects.get(pk=project_id) except Project.DoesNotExist: raise ApiException(request, _('Project does not exist.'), status_code=HTTPStatus.NOT_FOUND) for remote in project.remotes.all(): remote.services.all().delete() remote.delete() project.delete() return HttpResponse(status=HTTPStatus.NO_CONTENT)
class UserDetail(View): @method_decorator(token_required) @method_decorator(permission_required('core.read_user')) def get(self, request, user_id): try: user = User.objects.get(pk=user_id) except User.DoesNotExist: raise ApiException(request, _('User does not exist.'), status_code=HTTPStatus.NOT_FOUND) return SingleResponse(request, user, serializer=UserSerializer.Detail) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.add_user')) def put(self, request, user_id): try: user = User.objects.get(pk=user_id) except User.DoesNotExist: raise ApiException(request, _('User does not exist.'), status_code=HTTPStatus.NOT_FOUND) form = UserForms.Update.create_from_request(request) if not form.is_valid(): raise ValidationException(request, form) email = form.cleaned_data.get('email') assign_projects = form.cleaned_data.get('assign_projects') role = form.cleaned_data.get('role') if email != user.email: if User.objects.filter(email=email).exists(): raise ApiException(request, _('Email is already used.'), status_code=HTTPStatus.CONFLICT) form.fill(user) user.username = email if assign_projects is not None: user.assign_projects(request, assign_projects) if role: clear_roles(user) assign_role(user, role.value) user.save() return SingleResponse(request, data=user, status=HTTPStatus.OK, serializer=UserSerializer.Base) @transaction.atomic @method_decorator(token_required) @method_decorator(permission_required('core.delete_user')) def delete(self, request, user_id): try: user = User.objects.get(pk=user_id) except User.DoesNotExist: raise ApiException(request, _('User does not exist.'), status_code=HTTPStatus.NOT_FOUND) user.delete() return HttpResponse(status=HTTPStatus.NO_CONTENT)