Exemple #1
0
    def getType(self, type_):
        found = None
        for v in self.getTypes():
            if v['type'] == type_:
                found = v
                break

        if found is None:
            raise NotFound('type not found')

        logger.debug('Found type {0}'.format(found))
        return found
Exemple #2
0
    def delete(self) -> str:
        """
        Processes a DELETE request
        """
        if len(self._args) != 1:
            raise RequestError('Delete need one and only one argument')

        self.ensureAccess(self.model(), permissions.PERMISSION_ALL, root=True)  # Must have write permissions to delete

        try:
            self.model.objects.get(token=self._args[0]).delete()
        except self.model.DoesNotExist:
            raise NotFound('Element do not exists')

        return OK
Exemple #3
0
    def delete(self):
        """
        Processes a DELETE request
        """
        logger.debug('method DELETE for {0}, {1}'.format(self.__class__.__name__, self._args))
        if len(self._args) > 1:
            return self.processDetail()

        if len(self._args) != 1:
            raise RequestError('Delete need one and only one argument')

        self.ensureAccess(self.model(), permissions.PERMISSION_ALL, root=True)  # Must have write permissions to delete

        try:
            item = self.model.objects.get(uuid=self._args[0].lower())
            self.checkDelete(item)
            self.deleteItem(item)
        except self.model.DoesNotExist:
            raise NotFound('Element do not exists')

        return OK
Exemple #4
0
    def put(self):
        '''
        Processes a PUT request
        '''
        logger.debug('method PUT for {0}, {1}'.format(self.__class__.__name__,
                                                      self._args))
        self._params['_request'] = self._request

        deleteOnError = False

        if len(self._args) > 1:  # Detail?
            return self.processDetail()

        self.ensureAccess(
            self.model(), permissions.PERMISSION_ALL,
            root=True)  # Must have write permissions to create, modify, etc..

        try:
            # Extract fields
            args = self.readFieldsFromParams(self.save_fields)
            logger.debug('Args: {}'.format(args))
            self.beforeSave(args)
            # If tags is in save fields, treat it "specially"
            if 'tags' in self.save_fields:
                tags = args['tags']
                del args['tags']
            else:
                tags = None

            deleteOnError = False
            if len(self._args) == 0:  # create new
                item = self.model.objects.create(**args)
                deleteOnError = True
            else:  # Must have 1 arg
                # We have to take care with this case, update will efectively update records on db
                item = self.model.objects.get(uuid=self._args[0].lower())
                for v in self.remove_fields:
                    if v in args:
                        del args[v]
                item.__dict__.update(args)  # Update fields from args

            # Now if tags, update them
            if tags is not None:
                logger.debug('Updating tags: {}'.format(tags))
                item.tags = [
                    Tag.objects.get_or_create(tag=val)[0] for val in tags
                ]

        except self.model.DoesNotExist:
            raise NotFound('Item not found')
        except IntegrityError:  # Duplicate key probably
            raise RequestError('Element already exists (duplicate key error)')
        except SaveException as e:
            raise RequestError(unicode(e))
        except (RequestError, ResponseError):
            raise
        except Exception:
            logger.exception('Exception on put')
            raise RequestError('incorrect invocation to PUT')

        if not deleteOnError:
            self.checkSave(
                item
            )  # Will raise an exception if item can't be saved (only for modify operations..)

        # Store associated object if needed
        try:
            data_type = self._params.get('data_type', self._params.get('type'))
            if data_type is not None:
                item.data_type = data_type
                item.data = item.getInstance(self._params).serialize()

            item.save()

            res = self.item_as_dict(item)
            self.fillIntanceFields(item, res)
        except:
            if deleteOnError:
                item.delete()
            raise

        self.afterSave(item)

        return res
Exemple #5
0
 def invalidItemException(self, message=None):
     '''
     Raises a NotFound exception, with location info
     '''
     message = _('Item not found') if message is None else message
     raise NotFound(message)
Exemple #6
0
 def invalidItemException(self, message=None):
     '''
     Raises a NotFound exception, with location info
     '''
     message = _('Item not found') if message is None else None
     raise NotFound('{} {}: {}'.format(message, self.__class__, self._args))