Пример #1
0
    def commit_transaction(response):
        """Commit any open transaction if the request was successful."""
        if 200 <= response.status_code < 400:
            Session.commit()
        else:
            Session.rollback()

        return response
Пример #2
0
async def db_middleware(request: Request, call_next):
    try:
        response: Response = await call_next(request)
        if 200 <= response.status_code < 400:
            Session.commit()
        else:
            Session.rollback()
    finally:
        Session.remove()

    return response
Пример #3
0
def test_db_setup():
    migrate.systemdata.init_system_scopes()
    Session.commit()
    result = Session.execute(select(Scope)).scalars()
    assert [row.id for row in result] == [s.value for s in ODPScope]

    # create a batch of arbitrary scopes, which should not be assigned to the sysadmin
    ScopeFactory.create_batch(5)

    migrate.systemdata.init_admin_role()
    Session.commit()
    result = Session.execute(select(Role)).scalar_one()
    assert (result.id,
            result.collection_id) == (migrate.systemdata.ODP_ADMIN_ROLE, None)

    result = Session.execute(select(RoleScope)).scalars()
    assert [(row.role_id, row.scope_id, row.scope_type) for row in result] \
           == [(migrate.systemdata.ODP_ADMIN_ROLE, s.value, ScopeType.odp) for s in ODPScope]
Пример #4
0
        publication_schema = schema_catalog.get_schema(URI(catalog.schema.uri))

        if (result := publication_schema.evaluate(record_json)).valid:
            catalog_record.validity = result.output('flag')
            catalog_record.published = True
            catalog_record.published_record = self._create_published_record(record_model).dict()
            self._save_published_doi(record_model)
        else:
            catalog_record.validity = result.output('detailed')
            catalog_record.published = False
            catalog_record.published_record = None

        catalog_record.timestamp = timestamp
        catalog_record.save()
        Session.commit()

        return catalog_record.published

    def _create_published_record(self, record_model: RecordModel) -> PublishedRecordModel:
        """Create the published form of a record."""
        return PublishedRecordModel(
            id=record_model.id,
            doi=record_model.doi,
            sid=record_model.sid,
            collection_id=record_model.collection_id,
            metadata=self._create_published_metadata(record_model),
            tags=self._create_published_tags(record_model),
            timestamp=record_model.timestamp,
        )
Пример #5
0
 def scopes(obj, create, scopes):
     if scopes:
         for scope in scopes:
             obj.scopes.append(scope)
         if create:
             Session.commit()
Пример #6
0
 def collections(obj, create, collections):
     if collections:
         for collection in collections:
             obj.collections.append(collection)
         if create:
             Session.commit()
Пример #7
0
 def roles(obj, create, roles):
     if roles:
         for role in roles:
             obj.roles.append(role)
         if create:
             Session.commit()