예제 #1
0
 def get_spec(self):
     return specify.get_spec(
         method='post',
         description='Checks current user password',
         body_parameters=('User password',
                          auth_schemes.PasswordCheckScheme),
         responses=(responses.SuccessResponseJustOk(),
                    responses.ValidationErrorResponse(),
                    responses.OperationErrorResponse('Incorrect password')),
         security=self.security)
예제 #2
0
    def on_patch(self, req, resp, **kwargs):
        self.init_req(req)
        kwargs = user_context(self, req, resp, **kwargs)
        if not self.has_modify_operation:  # pragma: no cover
            return self.method_not_allowed_response(resp, 'PATCH')

        if not check_permission(
                self, req, 'modify',
                self.get_object(resp, self.model_class, **kwargs)):
            self.set_response(
                resp,
                responses.ForbiddenErrorResponse(
                    data=errors.AuthErrors.Forbidden))
            return

        try:
            scheme = self.scheme_class_modify(
            ) if self.scheme_class_modify else self.scheme_class()
            if self.check_request_data(req, resp, scheme):
                if self.update_object(
                        req, resp, self.model_class, self.scheme_class_modify
                        if self.scheme_class_modify else self.scheme_class,
                        **kwargs):
                    data = self.get_object_data(resp, self.model_class,
                                                self.scheme_class_get,
                                                **kwargs)
                    if data:
                        self.set_response(
                            resp,
                            responses.SuccessResponseObjectInfo(data=data))
                    else:
                        self.set_response(resp,
                                          responses.SuccessResponseJustOk())
        except IntegrityError as e:
            try:
                msg = str(e).split('\n')[1].split(':')[1]
            except:
                msg = str(e)

            self.set_response(resp, AlreadyExistsResponse(self.entity, msg))
        except FkConstraintViolation as e:
            self.set_response(
                resp,
                responses.FkConstraintViolationResponse(
                    data=errors.CommonErrors.get_fk_violation_error(
                        e.table_name, e.column_name, e.value)))
        except ValidationError as e:
            self.set_response(
                resp, responses.ValidationErrorResponse(data=e.messages))
        except NoResultFound:
            self.set_response(resp, responses.ObjectNotFoundErrorResponse())
        except Exception as e:
            log.error(e)
            #self.set_response(resp, responses.OperationErrorResponse(data=errors.CommonErrors.DataError))
            self.set_response(resp, OperationalError(e))
예제 #3
0
 def get_spec(self):
     return swagger.specify.get_spec(
         method=self.method,
         description=self.description,
         path_parameters=self.path_parameters,
         query_parameters=self.query_parameters,
         body_parameters=self.body_parameters,
         responses=(responses.SuccessResponseJustOk(),
                    responses.ObjectNotFoundErrorResponse()) +
         self.additional_responses,
         security=self.get_security(method=self.method))
예제 #4
0
 def get_spec(self):
     return swagger.specify.get_spec(
         method='post',
         description='Restores object to the specified revision',
         path_parameters=({
             'name': 'id',
             'description': 'ID of revision to restore to'
         }, ),
         responses=(responses.SuccessResponseJustOk(),
                    responses.ObjectNotFoundErrorResponse()),
         security=self.security)
예제 #5
0
    def proceed(self, req, resp, **kwargs):

        if (not hasattr(self, 'no_auth_needed') or self.no_auth_needed == False) and \
                not check_permission(self, req, self.action, self.get_object(resp, self.model_class, **kwargs)):
            self.set_response(
                resp,
                responses.ForbiddenErrorResponse(
                    data=errors.AuthErrors.Forbidden))
            return

        try:
            obj = self.get_object(resp, self.model_class, **kwargs)
            if self.check_conditions(obj, self.pre_conditions,
                                     lambda a, b: a == b):
                if self.check_conditions(obj, self.pre_conditions_inverted,
                                         lambda a, b: a != b):
                    if self.apply(obj, req, resp, **kwargs):
                        self.set_response(resp,
                                          responses.SuccessResponseJustOk())

        except FkConstraintViolation as e:
            self.set_response(
                resp,
                responses.FkConstraintViolationResponse(
                    data=errors.CommonErrors.get_fk_violation_error(
                        e.table_name, e.column_name, e.value)))
        except IntegrityError as e:
            try:
                msg = str(e).split('\n')[1].split(':')[1]
            except:
                msg = str(e)

            self.set_response(resp, AlreadyExistsResponse(self.entity, msg))
            return False
        except PreConditionFailedException as e:
            self.set_response(
                resp,
                responses.OperationErrorResponse(
                    data=errors.CommonErrors.get_pre_condition_failed_error(
                        e.message)))
        except Exception as e:
            self.set_response(resp, OperationalError(e))
예제 #6
0
    def on_delete(self, req, resp, **kwargs):
        self.init_req(req)
        if not self.has_delete_operation:  # pragma: no cover
            return self.method_not_allowed_response(resp, 'DELETE')
        obj = self.get_object(resp, self.model_class, **kwargs)
        if not check_permission(self, req, 'delete', obj):
            self.set_response(
                resp,
                responses.ForbiddenErrorResponse(
                    data=errors.AuthErrors.Forbidden))
            return

        kw = user_context(self, req, resp, **kwargs)
        try:
            self.before_delete(obj, req, resp, **kwargs)
        except Exception as e:
            self.set_response(resp, OperationalError(e))
            return
        try:
            if self.delete_object(req, resp, self.model_class, **kwargs):
                self.set_response(resp, responses.SuccessResponseJustOk())
        except NoResultFound:
            self.set_response(resp, responses.ObjectNotFoundErrorResponse())
예제 #7
0
    def on_post(self, req, resp, **kwargs):
        obj = self.get_object(resp, ObjectRevisionModel, **kwargs)
        user = self.get_user(req)
        if not check_permission('ObjectRevision', req, 'restore', obj) or (
                hasattr(user, 'can_restore') and not user.can_restore(obj)):
            self.set_response(
                resp,
                responses.ForbiddenErrorResponse(
                    data=errors.AuthErrors.Forbidden))
            return

        try:
            if obj.restore(self.get_user(req)):
                self.set_response(resp, responses.SuccessResponseJustOk())
        except RestoreExceptionToRevisionWhereWasDeleted:
            self.set_response(
                resp,
                responses.OperationErrorResponse(data=CantRestoreToDeleted))
        except RestoreExceptionToCurrentRevision:
            self.set_response(
                resp,
                responses.OperationErrorResponse(
                    data=CantRestoreToCurrentRevision))
예제 #8
0
    def apply(self, obj, req, resp, **kwargs):
        result = []
        updated = 0
        added = 0
        deleted = 0
        try:
            ids = [
                it[self.id_field] for it in req.data['items']
                if self.id_field in it
            ]
            if not self.modify_only:
                for obj in self.model_class.query().all():
                    if not obj.get_object_primary_key_value() in ids:
                        kwargs[self.id_field] = str(
                            obj.get_object_primary_key_value())
                        if self.delete_object(req, resp, self.model_class,
                                              **kwargs):
                            deleted += 1

            for data in req.data['items']:
                req.data = data
                if self.id_field in data and not data[self.id_field] == 'null':
                    kwargs[self.id_field] = data[self.id_field]
                    if self.update_object(req, resp, self.model_class,
                                          self.scheme_class, **kwargs):

                        updated += 1
                else:
                    if 'id' in kwargs:
                        del kwargs[self.id_field]
                    scheme = self.scheme_class(exclude=(self.id_field, ))
                    if not self.modify_only:
                        if self.check_request_data(req, resp, scheme):
                            #resp = self.scheme_class.get_object_created_response(data=self.create_object(req, scheme, **kwargs) )
                            obj = self.create_object(req, scheme, **kwargs)
                            added += 1

            if (updated + deleted + added) > 0:
                self.set_response(
                    resp,
                    responses.SuccessResponse(data={
                        'updated': updated,
                        'deleted': deleted,
                        'added': added,
                    },
                                              scheme=schemes.ObjectScheme))
                return True
            else:
                self.set_response(resp, responses.SuccessResponseJustOk())
                return True

        except IntegrityError as e:
            try:
                msg = str(e).split('\n')[1].split(':')[1]
            except:
                msg = str(e)

            self.set_response(resp, AlreadyExistsResponse(self.entity, msg))
            return False
        except FkConstraintViolation as e:
            self.set_response(
                resp,
                responses.FkConstraintViolationResponse(
                    data=errors.CommonErrors.get_fk_violation_error(
                        e.table_name, e.column_name, e.value)))
            return False
        except Exception as e:
            self.set_response(resp, OperationalError(e))
            return False