Ejemplo n.º 1
0
 def create(self, data: typing.Dict[str, typing.Any]) -> Model:
     try:
         object_: Model = self.model(**data)
         database.db.session.add(object_)
         database.db.session.commit()
     except exc.SQLAlchemyError:
         logger.exception(f'Create ModelRepository({self.model.__name__})')
         helpers.error_abort(
             code=http.HTTPStatus.UNPROCESSABLE_ENTITY,
             message=f"Creation of '{self.model.__name__}' failed")
     return object_
Ejemplo n.º 2
0
    def get(self, id: uuid.UUID, with_for_update: bool = False) -> Model:
        try:
            query = (database.db.session.query(self.model).filter_by(id=id))

            if with_for_update:
                query = query.with_for_update(of=self.model)

            objects_: Model = query.one()

        except orm_exc.NoResultFound:
            helpers.error_abort(code=http.HTTPStatus.NOT_FOUND,
                                message=f"'{self.model.__name__}' not found")
        return objects_
Ejemplo n.º 3
0
    def create_all(self, data: typing.List[typing.Dict[str,
                                                       typing.Any]]) -> None:
        try:
            for model_definition in data:
                object_: Model = self.model(**model_definition)  # type: ignore
                database.db.session.add(object_)

            database.db.session.commit()
        except exc.SQLAlchemyError:
            logger.exception(f'Create ModelRepository({self.model.__name__})')
            helpers.error_abort(
                code=http.HTTPStatus.UNPROCESSABLE_ENTITY,
                message=f"Creation of '{self.model.__name__}' failed")
Ejemplo n.º 4
0
def check_head_url_exist(url):
    try:
        import httplib2
        h = httplib2.Http()
        resp = h.request(url, 'HEAD')
        reutrn_code = int(resp[0]['status'])

        if reutrn_code >= 400:
            if reutrn_code == 403:
                return
            helpers.error_abort(
                code=http.HTTPStatus.BAD_REQUEST,
                message=
                f'Given url does not exist or some problem occured getting.'
                f' Got code:{reutrn_code} when HEAD {url}')

    except (httplib2.ServerNotFoundError, OSError):
        helpers.error_abort(code=http.HTTPStatus.BAD_REQUEST,
                            message=f'Given url does not exist: {url}')
Ejemplo n.º 5
0
    def update(self, id: uuid.UUID, data: typing.Dict[str,
                                                      typing.Any]) -> Model:
        try:
            object_: Model = self.get(id)

            for key, value in data.items():
                if not self._check_model_property(object_, key):
                    raise RepositoryError
                setattr(object_, key, value)

            database.db.session.add(object_)
            database.db.session.commit()

        except exc.SQLAlchemyError:
            logger.exception(f'Update ModelRepository({self.model.__name__})')
            helpers.error_abort(
                code=http.HTTPStatus.UNPROCESSABLE_ENTITY,
                message=f"Update of '{self.model.__name__}' failed")

        return object_
Ejemplo n.º 6
0
    def post(self) -> Tuple[models.ScrapeTask, http.HTTPStatus]:
        payload = helpers.get_payload(schemas.SCRAPE_URL)

        if not payload['scrape_text'] and not payload['scrape_images']:
            helpers.error_abort(
                code=http.HTTPStatus.NOT_ACCEPTABLE,
                message=
                'No effect of the task - at least one of scrape text or images have to be True'
            )

        check_head_url_exist(payload['url'])

        scrape_repository = repositories.ScrapeTaskRepository()
        scrape_task: models.ScrapeTask = scrape_repository.create(payload)

        result = celery_tasks.scrape_url.delay(id=str(scrape_task.id))

        # helpers.error_abort(
        #     code=http.HTTPStatus.NOT_IMPLEMENTED,
        #     message= result.wait()
        # )

        return scrape_task, http.HTTPStatus.CREATED