def get(self, request, server_id=None): HasUser.load(self, request) try: if server_id is None: servers = MaBoSSServer.objects.all() servers = [ server for server in servers if server.user == request.user or server.user.is_staff ] serializer = MaBoSSServerSerializer(servers, many=True) return Response(serializer.data) else: server = MaBoSSServer.objects.get(id=server_id) if server.user != self.user: raise PermissionDenied serializer = MaBoSSServerSerializer(server) return Response(serializer.data) except MaBoSSServer.DoesNotExist: raise Http404
def put(self, request): HasUser.load(self, request) self.user.email = request.data['email'] self.user.save() return Response(status=status.HTTP_200_OK)
def post(self, request): HasUser.load(self, request) Project(user=self.user, name=request.data['name'], description=request.data['description']).save() return Response(status=status.HTTP_200_OK)
def post(self, request): HasUser.load(self, request) MaBoSSServer(user=self.user, desc=request.POST['desc'], host=request.POST['host'], port=request.POST['port']).save() return Response(status=status.HTTP_200_OK)
def load(self, request, project_id): HasUser.load(self, request) try: project = Project.objects.get(id=project_id) if project.user != self.user: raise PermissionDenied self.project = project except Project.DoesNotExist: raise NotFound
def delete(self, request, server_id): HasUser.load(self, request) try: server = MaBoSSServer.objects.get(id=server_id) if server.user != self.user: raise PermissionDenied server.delete() return Response(status=status.HTTP_200_OK) except MaBoSSServer.DoesNotExist: raise Http404
def delete(self, request, project_id): HasUser.load(self, request) try: project = Project.objects.get(id=project_id) if project.user != self.user: raise PermissionDenied project.delete() return Response(status=status.HTTP_200_OK) except Project.DoesNotExist: raise NotFound
def put(self, request, project_id): HasUser.load(self, request) try: project = Project.objects.get(id=project_id) if project.user != self.user: raise PermissionDenied project.name = request.data['name'] project.description = request.data['description'] project.save() return Response(status=status.HTTP_200_OK) except Project.DoesNotExist: raise NotFound
def load(self, request, project_id): HasUser.load(self, request) try: if self.user.is_anonymous: guest_user = User.objects.get(username="******") project = Project.objects.get(user=guest_user) else: project = Project.objects.get(id=project_id) if project.user != self.user: raise PermissionDenied self.project = project except Project.DoesNotExist: raise NotFound
def get(self, request, server_id): HasUser.load(self, request) try: server = MaBoSSServer.objects.get(id=server_id) if server.user != self.user and not server.user.is_staff: raise PermissionDenied mbcli = MaBoSSClient(server.host, int(server.port), timeout=1) mbcli.close() return Response(status=status.HTTP_200_OK, data=True) except MaBoSSServer.DoesNotExist: raise Http404 except (timeout, TimeoutError, ConnectionRefusedError): return Response(status=status.HTTP_200_OK, data=False)
def put(self, request, server_id): HasUser.load(self, request) try: server = MaBoSSServer.objects.get(id=server_id) if server.user != self.user: raise PermissionDenied server.desc = request.data['desc'] server.host = request.data['host'] server.port = int(request.data['port']) server.save() return Response(status=status.HTTP_200_OK) except MaBoSSServer.DoesNotExist: raise Http404
def get(self, request, project_id=None): HasUser.load(self, request) try: if project_id is None: projects = Project.objects.filter(user=request.user) serializer = ProjectSerializer(projects, many=True) return Response(serializer.data) else: project = Project.objects.get(id=project_id) if project.user != self.user: raise PermissionDenied serializer = ProjectSerializer(project) return Response(serializer.data) except Project.DoesNotExist: raise NotFound
def get(self, request): HasUser.load(self, request) return Response({'email': self.user.email})