Skip to content

Commit

Permalink
add created_at and extras columns
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-iv committed Oct 31, 2024
1 parent 2638155 commit 7e9e0e9
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 7 deletions.
3 changes: 3 additions & 0 deletions ckanext/relationship/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def relationship_relation_create(
subject_id = data_dict["subject_id"]
object_id = data_dict["object_id"]
relation_type = data_dict.get("relation_type")
extras = data_dict.get("extras", {})

if Relationship.by_object_id(subject_id, object_id, relation_type):
return []
Expand All @@ -52,12 +53,14 @@ def relationship_relation_create(
subject_id=subject_id,
object_id=object_id,
relation_type=relation_type,
extras=extras,
)

reverse_relation = Relationship(
subject_id=object_id,
object_id=subject_id,
relation_type=Relationship.reverse_relation_type[relation_type],
extras=extras,
)

context["session"].add(relation)
Expand Down
11 changes: 9 additions & 2 deletions ckanext/relationship/logic/schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from ckan.logic.schema import validator_args
from ckan.types import Schema, Validator
from ckan.types import Schema, Validator, ValidatorFactory


@validator_args
def relation_create(not_empty: Validator, one_of: Validator) -> Schema:
def relation_create(
not_empty: Validator,
one_of: Validator,
default: ValidatorFactory,
convert_to_json_if_string: Validator,
dict_only: Validator,
) -> Schema:
return {
"subject_id": [
not_empty,
Expand All @@ -14,6 +20,7 @@ def relation_create(not_empty: Validator, one_of: Validator) -> Schema:
"relation_type": [
one_of(["related_to", "child_of", "parent_of"]),
],
"extras": [default("{}"), convert_to_json_if_string, dict_only],
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Add extras and created_at columns.
Revision ID: dd010e8e0680
Revises: aca2ff1d3ce4
Create Date: 2024-10-31 01:30:49.251175
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB

# revision identifiers, used by Alembic.
revision = "dd010e8e0680"
down_revision = "aca2ff1d3ce4"
branch_labels = None
depends_on = None


def upgrade():
op.add_column(
"relationship_relationship",
sa.Column("extras", JSONB, nullable=False, server_default="{}"),
)
op.add_column(
"relationship_relationship",
sa.Column(
"created_at", sa.DateTime, nullable=False, server_default=sa.text("now()")
),
)


def downgrade():
op.drop_column("relationship_relationship", "created_at")
op.drop_column("relationship_relationship", "extras")
13 changes: 11 additions & 2 deletions ckanext/relationship/model/relationship.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

from sqlalchemy import Column, Text, or_
from datetime import datetime

from sqlalchemy import Column, DateTime, Text, or_
from sqlalchemy.dialects.postgresql import JSONB

from ckan import logic, model
from ckan.model.types import make_uuid
Expand All @@ -14,6 +17,8 @@ class Relationship(Base):
subject_id: str = Column(Text, nullable=False)
object_id: str = Column(Text, nullable=False)
relation_type: str = Column(Text, nullable=False)
created_at: datetime = Column(DateTime, nullable=False, default=datetime.utcnow)
extras: dict = Column(JSONB, nullable=False, default=dict)

reverse_relation_type = {
"related_to": "related_to",
Expand All @@ -27,7 +32,9 @@ def __repr__(self):
f"id={self.id!r}, "
f"subject_id={self.subject_id!r}, "
f"object_id={self.object_id!r}, "
f"relation_type={self.relation_type!r})"
f"relation_type={self.relation_type!r}, "
f"created_at={self.created_at!r}, "
f"extras={self.extras!r})"
)

def as_dict(self):
Expand All @@ -36,6 +43,8 @@ def as_dict(self):
"subject_id": self.subject_id,
"object_id": self.object_id,
"relation_type": self.relation_type,
"created_at": self.created_at.isoformat() if self.created_at else None,
"extras": self.extras,
}

@classmethod
Expand Down
40 changes: 40 additions & 0 deletions ckanext/relationship/tests/logic/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,46 @@ def test_no_relation_type(self):
object_id=object_id,
)

def test_created_at(self):
subject_dataset = factories.Dataset()
object_dataset = factories.Dataset()

subject_id = subject_dataset["id"]
object_id = object_dataset["id"]
relation_type = "related_to"

result = call_action(
"relationship_relation_create",
{"ignore_auth": True},
subject_id=subject_id,
object_id=object_id,
relation_type=relation_type,
)

assert "created_at" in result[0]
assert "created_at" in result[1]

def test_extras(self):
subject_dataset = factories.Dataset()
object_dataset = factories.Dataset()

subject_id = subject_dataset["id"]
object_id = object_dataset["id"]
relation_type = "related_to"
extras = {"key": "value"}

result = call_action(
"relationship_relation_create",
{"ignore_auth": True},
subject_id=subject_id,
object_id=object_id,
relation_type=relation_type,
extras=extras,
)

assert result[0]["extras"] == extras
assert result[1]["extras"] == extras


@pytest.mark.usefixtures("clean_db")
class TestRelationDelete:
Expand Down
20 changes: 17 additions & 3 deletions ckanext/relationship/tests/package_with_relationship.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
scheming_version: 1
dataset_type: package_with_relationship
dataset_type: package-with-relationship
about_url: http://github.com/ckan/ckanext-relationship

dataset_fields:
- field_name: title
label: Title
preset: title

- field_name: name
label: URL
preset: dataset_slug

- field_name: owner_org
label: Organization
preset: dataset_organization

- field_name: related_packages
preset: related_entity
label: Related Packages
validators: relationship_related_entity
current_entity: package
current_entity_type: package_with_relationship
current_entity_type: package-with-relationship
related_entity: package
related_entity_type: package_with_relationship
related_entity_type: package-with-relationship
relation_type: related_to

resource_fields:
Expand Down
4 changes: 4 additions & 0 deletions test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ ckan.plugins = scheming_datasets
scheming.dataset_schemas =
ckanext.relationship.tests:package_with_relationship.yaml

scheming.presets =
ckanext.relationship:presets.yaml
ckanext.scheming:presets.json

# Logging configuration
[loggers]
keys = root, ckan, sqlalchemy
Expand Down

0 comments on commit 7e9e0e9

Please sign in to comment.