Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Issue #744
- Loading branch information
1 parent
591d1d2
commit 0189142
Showing
30 changed files
with
1,303 additions
and
242 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
atramhasis/alembic/versions/b2a7d7614973_add_provider_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Add provider table | ||
Revision ID: b2a7d7614973 | ||
Revises: 0978347c16f9 | ||
Create Date: 2023-03-22 15:10:27.781804 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "b2a7d7614973" | ||
down_revision = "0978347c16f9" | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"provider", | ||
sa.Column("id", sa.String(), nullable=False), | ||
sa.Column("conceptscheme_id", sa.Integer(), nullable=False), | ||
sa.Column("uri_pattern", sa.Text(), nullable=False), | ||
sa.Column("metadata", sa.JSON(), nullable=False), | ||
sa.Column( | ||
"expand_strategy", | ||
sa.Enum("RECURSE", "VISIT", name="expandstrategy"), | ||
nullable=True, | ||
), | ||
sa.ForeignKeyConstraint( | ||
["conceptscheme_id"], | ||
["conceptscheme.id"], | ||
ondelete="CASCADE", | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("provider") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import Mapping | ||
|
||
from skosprovider.registry import Registry | ||
from sqlalchemy.orm import Session | ||
|
||
from atramhasis import mappers | ||
from atramhasis.data.datamanagers import ProviderDataManager | ||
from atramhasis.data.models import Provider | ||
from atramhasis.errors import ValidationError | ||
|
||
|
||
def create_provider(json_data: Mapping, session: Session, skos_registry: Registry) -> Provider: | ||
"""Process a provider JSON into a newly stored Provider.""" | ||
for provider in skos_registry.get_providers(): | ||
if provider.get_vocabulary_uri() == json_data["conceptscheme_uri"]: | ||
raise ValidationError( | ||
"Provider could not be validated.", | ||
[{"conceptscheme_uri": "Collides with existing provider."}], | ||
) | ||
db_provider = mappers.map_provider(json_data) | ||
if not db_provider.id: | ||
# Store conceptscheme first so we can copy its id | ||
session.add(db_provider.conceptscheme) | ||
session.flush() | ||
db_provider.id = str(db_provider.conceptscheme.id) | ||
|
||
session.add(db_provider) | ||
session.flush() | ||
|
||
return db_provider | ||
|
||
|
||
def update_provider(provider_id: str, json_data: Mapping, session: Session) -> Provider: | ||
"""Process a JSON into to update an existing provider.""" | ||
manager = ProviderDataManager(session) | ||
db_provider = manager.get_provider_by_id(provider_id) | ||
db_provider = mappers.map_provider(json_data, provider=db_provider) | ||
session.flush() | ||
return db_provider | ||
|
||
|
||
def delete_provider(provider_id, session: Session) -> None: | ||
manager = ProviderDataManager(session) | ||
db_provider = manager.get_provider_by_id(provider_id) | ||
session.delete(db_provider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.