Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event #141

Merged
merged 2 commits into from
Aug 24, 2023
Merged

Event #141

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions connectors/fill-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ python3 connectors/synchronization.py \
-c connectors.example.example.ExampleEducationalResourceConnector \
-w /opt/connectors/data/example/educational_resource

python3 connectors/synchronization.py \
-c connectors.example.example.ExampleEventConnector \
-w /opt/connectors/data/example/event

python3 connectors/synchronization.py \
-c connectors.example.example.ExampleExperimentConnector \
-w /opt/connectors/data/example/experiment
Expand Down
7 changes: 7 additions & 0 deletions src/connectors/example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from database.model.computational_asset.computational_asset import ComputationalAsset
from database.model.dataset.dataset import Dataset
from database.model.educational_resource.educational_resource import EducationalResource
from database.model.event.event import Event
from database.model.knowledge_asset.publication import Publication
from database.model.models_and_experiments.experiment import Experiment
from database.model.models_and_experiments.ml_model import MLModel
Expand Down Expand Up @@ -40,6 +41,12 @@ def __init__(self):
super().__init__(json_path, EducationalResource)


class ExampleEventConnector(ExampleConnector[Event]):
def __init__(self):
json_path = _path_example_resources / "events.json"
super().__init__(json_path, Event)


class ExampleExperimentConnector(ExampleConnector[Experiment]):
def __init__(self):
json_path = _path_example_resources / "experiments.json"
Expand Down
86 changes: 86 additions & 0 deletions src/connectors/example/resources/events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
[
{
"platform": "example",
"platform_identifier": "1",
"name": "Name of the Event",
"description": "A description.",
"same_as": "https://www.example.com/resource/this_resource",
"date_published": "2022-01-01T15:15:00.000",
"version": "1.1.0",
"pid": "https://doi.org/10.1000/182",
"aiod_entry": {
"editor": [],
"status": "draft"
},
"alternate_name": [
"alias 1",
"alias 2"
],
"application_area": [
"Fraud Prevention",
"Voice Assistance",
"Disease Classification"
],
"citation": [],
"contact": [],
"creator": [],
"distribution": [
{
"checksum": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"checksum_algorithm": "sha256",
"copyright": "2010-2020 Example Company. All rights reserved.",
"content_url": "https://www.example.com/case_study/file.pdf",
"content_size_kb": 10000,
"date_published": "2022-01-01T15:15:00.000",
"description": "Description of this file.",
"encoding_format": "application/pdf",
"name": "Name of this file.",
"technology_readiness_level": 1
}
],
"has_part": [],
"industrial_sector": [
"Finance",
"eCommerce",
"Healthcare"
],
"is_part_of": [],
"keyword": [
"keyword1",
"keyword2"
],
"license": "https://creativecommons.org/share-your-work/public-domain/cc0/",
"media": [
{
"checksum": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"checksum_algorithm": "sha256",
"copyright": "2010-2020 Example Company. All rights reserved.",
"content_url": "https://www.example.com/image.jpeg",
"content_size_kb": 10000,
"date_published": "2022-01-01T15:15:00.000",
"description": "Description of this file.",
"encoding_format": "image/jpeg",
"name": "Name of this file."
}
],
"note": [
"A brief record of points or ideas about this AI resource."
],
"research_area": [
"Explainable AI",
"Physical AI"
],
"scientific_domain": [
"Anomaly Detection",
"Voice Recognition",
"Computer Vision."
],
"start_date": "2021-02-03T15:15:00",
"end_date": "2022-02-03T15:15:00",
"schedule": "10:00-10:30: Opening. 10:30-11:00 ...",
"registration_link": "https://example.com/registration-form",
"status": "scheduled",
"mode": "offline"
}
]

Empty file.
89 changes: 89 additions & 0 deletions src/database/model/event/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from datetime import datetime
from typing import Optional

from sqlmodel import Field, Relationship

from database.model.agent.agent_table import AgentTable
from database.model.ai_resource.resource import AIResourceBase, AIResource
from database.model.event.event_mode import EventMode
from database.model.event.event_status import EventStatus
from database.model.field_length import NORMAL, DESCRIPTION
from database.model.helper_functions import link_factory
from database.model.relationships import ResourceRelationshipList, ResourceRelationshipSingle
from database.model.serializers import (
AttributeSerializer,
FindByIdentifierDeserializer,
FindByNameDeserializer,
)


class EventBase(AIResourceBase):
start_date: datetime = Field(
description="The start date and time of the event, formatted using the ISO 8601 date-time "
"format.",
default=None,
schema_extra={"example": "2021-02-03T15:15:00"},
)
end_date: datetime | None = Field(
description="The end date and time of the event, formatted using the ISO 8601 date-time "
"format.",
default=None,
schema_extra={"example": "2022-01-01T15:15:00"},
)
schedule: str | None = Field(
description="The agenda of the event.",
max_length=DESCRIPTION,
default=None,
schema_extra={"example": "10:00-10:30: Opening. 10:30-11:00 ..."},
)
registration_link: str | None = Field(
description="The url of the registration form.",
max_length=NORMAL,
default=None,
schema_extra={"example": "https://example.com/registration-form"},
)
# Did not add duration here: it can be described using start_date and end_date


class Event(EventBase, AIResource, table=True): # type: ignore [call-arg]
__tablename__ = "event"

performer: list["AgentTable"] = Relationship(
sa_relationship_kwargs={"cascade": "all, delete"},
link_model=link_factory("event", AgentTable.__tablename__, table_prefix="performer"),
)
organiser_identifier: int | None = Field(foreign_key=AgentTable.__tablename__ + ".identifier")
organiser: Optional[AgentTable] = Relationship()
status_identifier: int | None = Field(foreign_key=EventStatus.__tablename__ + ".identifier")
status: Optional[EventStatus] = Relationship()
mode_identifier: int | None = Field(foreign_key=EventMode.__tablename__ + ".identifier")
mode: Optional[EventMode] = Relationship()

class RelationshipConfig(AIResource.RelationshipConfig):
performer: list[int] = ResourceRelationshipList(
description="Links to identifiers of the agents (person or organization) that is "
"contributing to this event ",
serializer=AttributeSerializer("identifier"),
deserializer=FindByIdentifierDeserializer(AgentTable),
default_factory_pydantic=list,
example=[],
)
organiser: Optional[int] = ResourceRelationshipSingle(
identifier_name="organiser_identifier",
description="The person or organisation responsible for organising the event.",
serializer=AttributeSerializer("identifier"),
)
status: Optional[str] = ResourceRelationshipSingle(
description="The status of the event.",
identifier_name="status_identifier",
serializer=AttributeSerializer("name"),
deserializer=FindByNameDeserializer(EventStatus),
example="scheduled",
)
mode: Optional[str] = ResourceRelationshipSingle(
description="The attendance mode of event.",
identifier_name="mode_identifier",
serializer=AttributeSerializer("name"),
deserializer=FindByNameDeserializer(EventMode),
example="offline",
)
5 changes: 5 additions & 0 deletions src/database/model/event/event_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from database.model.named_relation import NamedRelation


class EventMode(NamedRelation, table=True): # type: ignore [call-arg]
__tablename__ = "event_mode"
5 changes: 5 additions & 0 deletions src/database/model/event/event_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from database.model.named_relation import NamedRelation


class EventStatus(NamedRelation, table=True): # type: ignore [call-arg]
__tablename__ = "event_status"
3 changes: 2 additions & 1 deletion src/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .computational_asset_router import ComputationalAssetRouter
from .dataset_router import DatasetRouter
from .educational_resource_router import EducationalResourceRouter
from .event_router import EventRouter
from .experiment_router import ExperimentRouter
from .ml_model_router import MLModelRouter
from .organisation_router import OrganisationRouter
Expand All @@ -19,7 +20,7 @@
ComputationalAssetRouter(),
DatasetRouter(),
EducationalResourceRouter(),
# EventRouter(),
EventRouter(),
ExperimentRouter(),
MLModelRouter(),
# NewsRouter(),
Expand Down
20 changes: 20 additions & 0 deletions src/routers/event_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from database.model.event.event import Event
from routers.resource_router import ResourceRouter


class EventRouter(ResourceRouter):
@property
def version(self) -> int:
return 1

@property
def resource_name(self) -> str:
return "event"

@property
def resource_name_plural(self) -> str:
return "events"

@property
def resource_class(self) -> type[Event]:
return Event
49 changes: 49 additions & 0 deletions src/tests/routers/test_router_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import copy
from unittest.mock import Mock

from sqlalchemy.engine import Engine
from sqlmodel import Session
from starlette.testclient import TestClient

from authentication import keycloak_openid
from database.model.agent.person import Person


def test_happy_path(
client: TestClient,
engine: Engine,
mocked_privileged_token: Mock,
body_resource: dict,
person: Person,
):

with Session(engine) as session:
session.add(person)
session.commit()

keycloak_openid.userinfo = mocked_privileged_token
body = copy.copy(body_resource)
body["start_date"] = "2021-02-03T15:15:00"
body["end_date"] = "2022-02-03T15:15:00"
body["schedule"] = "Some text"
body["registration_link"] = "https://example.com/registration-form"
body["performer"] = [1]
body["organiser"] = 1
body["status"] = "scheduled"
body["mode"] = "offline"

response = client.post("/events/v1", json=body, headers={"Authorization": "Fake token"})
assert response.status_code == 200, response.json()

response = client.get("/events/v1/1")
assert response.status_code == 200, response.json()

response_json = response.json()
assert response_json["start_date"] == "2021-02-03T15:15:00"
assert response_json["end_date"] == "2022-02-03T15:15:00"
assert response_json["schedule"] == "Some text"
assert response_json["registration_link"] == "https://example.com/registration-form"
assert response_json["performer"] == [1]
assert response_json["organiser"] == 1
assert response_json["status"] == "scheduled"
assert response_json["mode"] == "offline"