Beispiel #1
0
    def to_internal_value(self, data):
        """
        function before getting new instance
        even before validate
        we need to get the moduleID instance by pk
        we need to get the envID instance by pk
        we need to check the data by hand
        """
        valid_data = {}

        # deal with name
        try:
            name = data['name']
        except KeyError:
            raise ParamNotEnoughException('name')
        self.validate_name(name)
        valid_data.update(name=name)

        # deal with envID
        try:
            env_id = data['envID']
        except KeyError:
            raise ParamNotEnoughException('envID')
        self.validate_id(env_id)
        try:
            envID = Environments.objects.get(id=env_id)
        except ObjectDoesNotExist:
            raise ObjectNotExistException(env_id)
        valid_data.update(envID=envID)

        # deal with moduleID
        try:
            module_id = data['moduleID']
        except KeyError:
            raise ParamNotEnoughException('moduleID')
        self.validate_id(module_id)
        try:
            moduleID = BasicModule.objects.get(id=module_id)
        except ObjectDoesNotExist:
            raise ObjectNotExistException(module_id)
        valid_data.update(moduleID=module_id)

        # no required config
        try:
            config = data['config']
        except KeyError:
            config = None
        if config:
            self.validate_config(config)
            valid_data.update(config=json.dumps(config))

        return valid_data
Beispiel #2
0
 def update(self, request, pk=None):
     """
     update a
     :param request:
     :param pk:
     :return:
     """
     if not pk:
         raise ParamNotEnoughException('id')
     try:
         instance = VerModules.view(pk=pk)
     except ObjectDoesNotExist:
         raise ObjectNotExistException(pk)
     serializer = VersionModuleSerializer(data=request.data)
     if serializer.is_valid(raise_exception=True):
         instance.name = serializer.validated_data['name']
         instance.verID = serializer.validated_data['verID']
         instance.moduleID = serializer.validated_data['moduleID']
         instance.config = serializer.validated_data.get(
             'config', instance.config)
         try:
             instance.save()
         except IntegrityError:
             raise DBIntegrityException(instance.name)
         return Response(serializer.data)
Beispiel #3
0
    def destroy(self, request, pk=None):
        """
        here must check if the module reference by others

        :param instance: instance to be delete
        :return: none
        """
        if not pk:
            raise ParamNotEnoughException('id')
        try:
            # check if the object is existing
            instance = BasicModule.objects.get(pk=pk)
        except ObjectDoesNotExist:
            raise ObjectNotExistException(pk)
        # check if the instance be referenced
        rely = instance.vers.count() or instance.envs.count()
        # rely =  0 if len(EnvModules.objects.filter(moduleID=instance)[:1]) == 0 and \
        #              len(VerModules.objects.filter(moduleID=instance)[:1]) == 0 else 1
        if rely:
            raise DBRelyOnException(pk)
        else:
            instance.delete()
            return Response({
                "code": 0,
                'message': '{0} delete successfully'.format(pk)
            })
Beispiel #4
0
    def retrieve(self, request, pk=None):
        """
        get an entry detail by giving the pk key

        :param request: rest framework request
        :param pk: primary key
        :return: rest framework response
        """
        if not pk:
            raise ParamNotEnoughException('id')
        try:
            instance = VerModules.view(pk)
        except ObjectDoesNotExist:
            raise ObjectNotExistException(pk)
        serializer = VersionModuleSerializer(instance)
        return Response(serializer.data)
Beispiel #5
0
    def retrieve(self, request, pk=None):
        """
        get an entry detail by giving the pk key

        :param request: rest framework request
        :param pk: primary key
        :return: rest framework reaponse
        """
        if not pk:
            raise ParamNotEnoughException('id')
        try:
            instance = Environments.view(pk)
        except ObjectDoesNotExist:
            raise ObjectNotExistException(pk)
        # add the module information
        instance.details = instance.entries.all()
        serializer = EnvironmentSerializer(instance)
        return Response(serializer.data)
Beispiel #6
0
    def destroy(self, request, pk=None):
        """
        version module can be deleted directly

        :param instance: instance to be delete
        :return: none
        """
        if not pk:
            raise ParamNotEnoughException('id')
        try:
            # check if the object is existing
            instance = VerModules.objects.get(id=pk)
        except ObjectDoesNotExist:
            raise ObjectNotExistException(pk)
        instance.delete()
        return Response({
            "code": 0,
            'message': '{0} delete successfully'.format(pk)
        })
Beispiel #7
0
    def destroy(self, request, pk=None):
        """
        here must check if the module reference by others

        :param instance: instance to be delete
        :return: none
        """
        if not pk:
            raise ParamNotEnoughException('id')
        try:
            # check if the object is existing
            instance = Environments.objects.get(pk=pk)
        except ObjectDoesNotExist:
            raise ObjectNotExistException(pk)
        instance.delete()
        return Response({
            "code": 0,
            'message': '{0} delete successfully'.format(pk)
        })
    def to_internal_value(self, data):
        """
        check value exist and value legal
        """
        valid_data = {}

        try:
            name = data['name']
        except KeyError:
            raise ParamNotEnoughException('name')
        self.validate_name(name)
        valid_data.update(name=name)

        try:
            description = data['description']
        except KeyError:
            description = None
        if description:
            self.validate_description(description)
            valid_data.update(description=description)

        return valid_data
Beispiel #9
0
 def update(self, request, pk=None):
     """
     update a
     :param request:
     :param pk:
     :return:
     """
     if not pk:
         raise ParamNotEnoughException('id')
     try:
         instance = Environments.view(pk=pk)
     except ObjectDoesNotExist:
         raise ObjectNotExistException(pk)
     serializer = EnvironmentSerializer(data=request.data)
     if serializer.is_valid(raise_exception=True):
         instance.name = serializer.validated_data['name']
         instance.description = serializer.validated_data.get(
             'description', instance.description)
         try:
             instance.save()
         except IntegrityError:
             raise DBIntegrityException(instance.name)
         return Response(EnvironmentSerializer(instance).data)