implemented events and publications endpoints
split endpoints by entity removed credentials
This commit is contained in:
parent
f35f3c78a9
commit
a1af790677
|
@ -10,7 +10,7 @@ from mobilizon_reshare.event.event_selection_strategies import select_unpublishe
|
||||||
from mobilizon_reshare.storage.query.read import (
|
from mobilizon_reshare.storage.query.read import (
|
||||||
get_published_events,
|
get_published_events,
|
||||||
events_with_status,
|
events_with_status,
|
||||||
get_all_events,
|
get_all_mobilizon_events,
|
||||||
events_without_publications,
|
events_without_publications,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ async def list_events(
|
||||||
frm = Arrow.fromdatetime(frm) if frm else None
|
frm = Arrow.fromdatetime(frm) if frm else None
|
||||||
to = Arrow.fromdatetime(to) if to else None
|
to = Arrow.fromdatetime(to) if to else None
|
||||||
if status is None:
|
if status is None:
|
||||||
events = await get_all_events(from_date=frm, to_date=to)
|
events = await get_all_mobilizon_events(from_date=frm, to_date=to)
|
||||||
elif status == EventPublicationStatus.WAITING:
|
elif status == EventPublicationStatus.WAITING:
|
||||||
events = await list_unpublished_events(frm=frm, to=to)
|
events = await list_unpublished_events(frm=frm, to=to)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
from tortoise.contrib.pydantic import pydantic_model_creator
|
||||||
|
|
||||||
|
|
||||||
|
class WithPydantic:
|
||||||
|
@classmethod
|
||||||
|
def to_pydantic(cls):
|
||||||
|
return pydantic_model_creator(cls)
|
|
@ -1,11 +1,12 @@
|
||||||
from tortoise import fields
|
from tortoise import fields
|
||||||
from tortoise.models import Model
|
from tortoise.models import Model
|
||||||
|
|
||||||
|
from mobilizon_reshare.models import WithPydantic
|
||||||
from mobilizon_reshare.models.publication import PublicationStatus, Publication
|
from mobilizon_reshare.models.publication import PublicationStatus, Publication
|
||||||
from mobilizon_reshare.models.publisher import Publisher
|
from mobilizon_reshare.models.publisher import Publisher
|
||||||
|
|
||||||
|
|
||||||
class Event(Model):
|
class Event(Model, WithPydantic):
|
||||||
id = fields.UUIDField(pk=True)
|
id = fields.UUIDField(pk=True)
|
||||||
name = fields.TextField()
|
name = fields.TextField()
|
||||||
description = fields.TextField(null=True)
|
description = fields.TextField(null=True)
|
||||||
|
|
|
@ -3,13 +3,15 @@ from enum import IntEnum
|
||||||
from tortoise import fields
|
from tortoise import fields
|
||||||
from tortoise.models import Model
|
from tortoise.models import Model
|
||||||
|
|
||||||
|
from mobilizon_reshare.models import WithPydantic
|
||||||
|
|
||||||
|
|
||||||
class PublicationStatus(IntEnum):
|
class PublicationStatus(IntEnum):
|
||||||
FAILED = 0
|
FAILED = 0
|
||||||
COMPLETED = 1
|
COMPLETED = 1
|
||||||
|
|
||||||
|
|
||||||
class Publication(Model):
|
class Publication(Model, WithPydantic):
|
||||||
id = fields.UUIDField(pk=True)
|
id = fields.UUIDField(pk=True)
|
||||||
status = fields.IntEnumField(PublicationStatus)
|
status = fields.IntEnumField(PublicationStatus)
|
||||||
|
|
||||||
|
|
|
@ -65,8 +65,12 @@ class MoReDB:
|
||||||
logging.info("Updated database to latest version")
|
logging.info("Updated database to latest version")
|
||||||
|
|
||||||
async def setup(self):
|
async def setup(self):
|
||||||
|
tortoise_config = get_tortoise_orm()
|
||||||
|
Tortoise.init_models(
|
||||||
|
tortoise_config["apps"]["models"]["models"], "models", _init_relations=True
|
||||||
|
)
|
||||||
await self._implement_db_changes()
|
await self._implement_db_changes()
|
||||||
await Tortoise.init(config=get_tortoise_orm(),)
|
await Tortoise.init(config=tortoise_config)
|
||||||
await Tortoise.generate_schemas()
|
await Tortoise.generate_schemas()
|
||||||
await update_publishers(publisher_names)
|
await update_publishers(publisher_names)
|
||||||
|
|
||||||
|
|
|
@ -70,15 +70,18 @@ async def get_all_publications(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def get_all_events(
|
async def get_all_mobilizon_events(
|
||||||
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
|
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
|
||||||
) -> list[MobilizonEvent]:
|
) -> list[MobilizonEvent]:
|
||||||
return [
|
return [event_from_model(event) for event in await get_all_events()]
|
||||||
event_from_model(event)
|
|
||||||
for event in await prefetch_event_relations(
|
|
||||||
|
async def get_all_events(
|
||||||
|
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None
|
||||||
|
):
|
||||||
|
return await prefetch_event_relations(
|
||||||
_add_date_window(Event.all(), "begin_datetime", from_date, to_date)
|
_add_date_window(Event.all(), "begin_datetime", from_date, to_date)
|
||||||
)
|
)
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
async def get_all_publishers() -> list[Publisher]:
|
async def get_all_publishers() -> list[Publisher]:
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
from mobilizon_reshare.models.event import Event
|
||||||
|
from mobilizon_reshare.storage.query.read import get_all_events
|
||||||
|
|
||||||
|
|
||||||
|
def register_endpoints(app):
|
||||||
|
@app.get("/events", status_code=200)
|
||||||
|
async def get_events():
|
||||||
|
all_events = await get_all_events()
|
||||||
|
return [await Event.to_pydantic().from_tortoise_orm(x) for x in all_events]
|
|
@ -1,14 +1,16 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from tortoise.contrib.pydantic import pydantic_model_creator
|
|
||||||
|
|
||||||
from mobilizon_reshare.models.event import Event
|
|
||||||
from mobilizon_reshare.storage.db import init as init_db, get_db_url
|
from mobilizon_reshare.storage.db import init as init_db, get_db_url
|
||||||
|
from mobilizon_reshare.web.backend.events.endpoints import (
|
||||||
|
register_endpoints as register_event_endpoints,
|
||||||
|
)
|
||||||
|
from mobilizon_reshare.web.backend.publications.endpoints import (
|
||||||
|
register_endpoints as register_publication_endpoints,
|
||||||
|
)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
event_pydantic = pydantic_model_creator(Event)
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -23,10 +25,9 @@ def check_database():
|
||||||
|
|
||||||
|
|
||||||
def register_endpoints(app):
|
def register_endpoints(app):
|
||||||
@app.get("/events", status_code=200)
|
|
||||||
async def get_event():
|
|
||||||
|
|
||||||
return await event_pydantic.from_queryset(Event.all())
|
register_event_endpoints(app)
|
||||||
|
register_publication_endpoints(app)
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
from mobilizon_reshare.models.publication import Publication
|
||||||
|
|
||||||
|
|
||||||
|
def register_endpoints(app):
|
||||||
|
@app.get("/publications", status_code=200)
|
||||||
|
async def get_publications():
|
||||||
|
return await Publication.to_pydantic().from_queryset(Publication.all())
|
|
@ -3,7 +3,7 @@ from logging import DEBUG, INFO
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mobilizon_reshare.storage.query.read import (
|
from mobilizon_reshare.storage.query.read import (
|
||||||
get_all_events,
|
get_all_mobilizon_events,
|
||||||
events_without_publications,
|
events_without_publications,
|
||||||
)
|
)
|
||||||
from tests.commands.conftest import (
|
from tests.commands.conftest import (
|
||||||
|
@ -40,7 +40,7 @@ async def test_pull_no_event(
|
||||||
assert "Pulled 0 events from Mobilizon." in caplog.text
|
assert "Pulled 0 events from Mobilizon." in caplog.text
|
||||||
assert "There are now 0 unpublished events." in caplog.text
|
assert "There are now 0 unpublished events." in caplog.text
|
||||||
|
|
||||||
assert expected_result == await get_all_events()
|
assert expected_result == await get_all_mobilizon_events()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -69,7 +69,7 @@ async def test_pull(
|
||||||
with caplog.at_level(DEBUG):
|
with caplog.at_level(DEBUG):
|
||||||
assert await pull() == expected_result
|
assert await pull() == expected_result
|
||||||
assert f"Pulled {len(elements)} events from Mobilizon." in caplog.text
|
assert f"Pulled {len(elements)} events from Mobilizon." in caplog.text
|
||||||
assert expected_result == await get_all_events()
|
assert expected_result == await get_all_mobilizon_events()
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
f"There are now {len(expected_result)} unpublished events." in caplog.text
|
f"There are now {len(expected_result)} unpublished events." in caplog.text
|
||||||
|
@ -112,7 +112,7 @@ async def test_pull_start(
|
||||||
|
|
||||||
with caplog.at_level(INFO):
|
with caplog.at_level(INFO):
|
||||||
assert await pull() == expected_pull
|
assert await pull() == expected_pull
|
||||||
assert expected_pull == await get_all_events()
|
assert expected_pull == await get_all_mobilizon_events()
|
||||||
assert expected_pull == await events_without_publications()
|
assert expected_pull == await events_without_publications()
|
||||||
|
|
||||||
report = await start(command_config)
|
report = await start(command_config)
|
||||||
|
@ -123,7 +123,9 @@ async def test_pull_start(
|
||||||
pull_ids = set(event.mobilizon_id for event in expected_pull)
|
pull_ids = set(event.mobilizon_id for event in expected_pull)
|
||||||
publish_ids = {expected_publish.mobilizon_id}
|
publish_ids = {expected_publish.mobilizon_id}
|
||||||
|
|
||||||
assert pull_ids == set(event.mobilizon_id for event in await get_all_events())
|
assert pull_ids == set(
|
||||||
|
event.mobilizon_id for event in await get_all_mobilizon_events()
|
||||||
|
)
|
||||||
assert (pull_ids - publish_ids) == set(
|
assert (pull_ids - publish_ids) == set(
|
||||||
event.mobilizon_id for event in await events_without_publications()
|
event.mobilizon_id for event in await events_without_publications()
|
||||||
)
|
)
|
||||||
|
@ -188,8 +190,8 @@ async def test_multiple_pull(
|
||||||
with caplog.at_level(DEBUG):
|
with caplog.at_level(DEBUG):
|
||||||
assert await pull()
|
assert await pull()
|
||||||
assert f"There are now {len(expected_first)} unpublished events." in caplog.text
|
assert f"There are now {len(expected_first)} unpublished events." in caplog.text
|
||||||
assert expected_first == await get_all_events()
|
assert expected_first == await get_all_mobilizon_events()
|
||||||
assert await events_without_publications() == await get_all_events()
|
assert await events_without_publications() == await get_all_mobilizon_events()
|
||||||
|
|
||||||
# I clean the message collector
|
# I clean the message collector
|
||||||
message_collector.data = []
|
message_collector.data = []
|
||||||
|
@ -200,6 +202,6 @@ async def test_multiple_pull(
|
||||||
assert f"There are now {len(expected_last)} unpublished events." in caplog.text
|
assert f"There are now {len(expected_last)} unpublished events." in caplog.text
|
||||||
|
|
||||||
assert set(event.mobilizon_id for event in expected_last) == set(
|
assert set(event.mobilizon_id for event in expected_last) == set(
|
||||||
event.mobilizon_id for event in await get_all_events()
|
event.mobilizon_id for event in await get_all_mobilizon_events()
|
||||||
)
|
)
|
||||||
assert await events_without_publications() == await get_all_events()
|
assert await events_without_publications() == await get_all_mobilizon_events()
|
||||||
|
|
|
@ -4,7 +4,7 @@ import pytest
|
||||||
|
|
||||||
from mobilizon_reshare.config.command import CommandConfig
|
from mobilizon_reshare.config.command import CommandConfig
|
||||||
from mobilizon_reshare.storage.query.converter import event_from_model, event_to_model
|
from mobilizon_reshare.storage.query.converter import event_from_model, event_to_model
|
||||||
from mobilizon_reshare.storage.query.read import get_all_events
|
from mobilizon_reshare.storage.query.read import get_all_mobilizon_events
|
||||||
from tests.commands.conftest import simple_event_element, second_event_element
|
from tests.commands.conftest import simple_event_element, second_event_element
|
||||||
from mobilizon_reshare.event.event import EventPublicationStatus
|
from mobilizon_reshare.event.event import EventPublicationStatus
|
||||||
from mobilizon_reshare.main.start import start
|
from mobilizon_reshare.main.start import start
|
||||||
|
@ -222,7 +222,7 @@ async def test_start_second_execution(
|
||||||
"event_1|desc_1",
|
"event_1|desc_1",
|
||||||
]
|
]
|
||||||
# I verify that the db event and the new event coming from mobilizon are both in the db
|
# I verify that the db event and the new event coming from mobilizon are both in the db
|
||||||
assert len(list(await get_all_events())) == 2
|
assert len(list(await get_all_mobilizon_events())) == 2
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
|
@ -3,7 +3,7 @@ from uuid import UUID
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mobilizon_reshare.storage.query.converter import event_to_model
|
from mobilizon_reshare.storage.query.converter import event_to_model
|
||||||
from mobilizon_reshare.storage.query.read import get_all_events
|
from mobilizon_reshare.storage.query.read import get_all_mobilizon_events
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
@ -14,4 +14,4 @@ async def test_get_all_events(event_generator):
|
||||||
for e in all_events:
|
for e in all_events:
|
||||||
await event_to_model(e).save()
|
await event_to_model(e).save()
|
||||||
|
|
||||||
assert list(await get_all_events()) == all_events
|
assert list(await get_all_mobilizon_events()) == all_events
|
||||||
|
|
|
@ -3,7 +3,7 @@ import json
|
||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from mobilizon_reshare.web.backend.main import event_pydantic
|
from mobilizon_reshare.models.event import Event
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
|
@ -13,4 +13,5 @@ async def test_events(client: AsyncClient, event_model_generator):
|
||||||
|
|
||||||
response = await client.get("/events")
|
response = await client.get("/events")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json()[0] == [json.loads(event_pydantic.from_orm(event).json())][0]
|
expected = await Event.to_pydantic().from_tortoise_orm(event)
|
||||||
|
assert response.json()[0] == json.loads(expected.json())
|
||||||
|
|
Loading…
Reference in New Issue