Skip to content

Commit

Permalink
Move reports api into reports app
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Feb 5, 2024
1 parent 196bd61 commit 352afd2
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 72 deletions.
3 changes: 0 additions & 3 deletions radis/api/admin.py

This file was deleted.

5 changes: 0 additions & 5 deletions radis/api/apps.py

This file was deleted.

Empty file removed radis/api/migrations/__init__.py
Empty file.
3 changes: 0 additions & 3 deletions radis/api/models.py

This file was deleted.

39 changes: 0 additions & 39 deletions radis/api/site.py

This file was deleted.

3 changes: 0 additions & 3 deletions radis/api/tests.py

This file was deleted.

6 changes: 3 additions & 3 deletions radis/collections/apps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate

from radis.core.site import register_main_menu_item
from radis.reports.site import register_report_panel_button


class CollectionsConfig(AppConfig):
name = "radis.collections"
Expand All @@ -16,6 +13,9 @@ def ready(self):


def register_app():
from radis.core.site import register_main_menu_item
from radis.reports.site import register_report_panel_button

register_main_menu_item(
url_name="collection_list",
label="Collections",
Expand Down
6 changes: 3 additions & 3 deletions radis/notes/apps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate

from radis.core.site import register_main_menu_item
from radis.reports.site import register_report_panel_button


class NotesConfig(AppConfig):
name = "radis.notes"
Expand All @@ -16,6 +13,9 @@ def ready(self):


def register_app():
from radis.core.site import register_main_menu_item
from radis.reports.site import register_report_panel_button

register_main_menu_item(
url_name="note_list",
label="Notes",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from radis.reports.models import Report
from ..models import Report


class ReportSerializer(serializers.ModelSerializer):
Expand Down
4 changes: 2 additions & 2 deletions radis/api/urls.py → radis/reports/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.urls import include, path
from rest_framework.routers import DefaultRouter

from .views import ReportViewSet
from .viewsets import ReportViewSet

router = DefaultRouter()
router.register(r"reports", ReportViewSet)
router.register(r"", ReportViewSet)

urlpatterns = [
path("", include(router.urls)),
Expand Down
5 changes: 2 additions & 3 deletions radis/api/views.py → radis/reports/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from rest_framework.response import Response
from rest_framework.serializers import BaseSerializer

from radis.reports.models import Report

from ..models import Report
from ..site import document_fetchers, report_event_handlers
from .serializers import ReportSerializer
from .site import document_fetchers, report_event_handlers


class ReportViewSet(
Expand Down
41 changes: 40 additions & 1 deletion radis/reports/site.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
from typing import Any, NamedTuple
from typing import Any, Callable, Literal, NamedTuple

from django.http import HttpRequest

from .models import Report

ReportEventType = Literal["created", "updated", "deleted"]
ReportEventHandler = Callable[[ReportEventType, Report], None]

report_event_handlers: list[ReportEventHandler] = []


def register_report_handler(handler: ReportEventHandler) -> None:
"""Register a report event handler.
The report handler gets notified a report is created, updated, or deleted in
PostgreSQL database. It can be used to sync report documents in other
databases like Vespa.
"""
report_event_handlers.append(handler)


FetchDocument = Callable[[Report], dict[str, Any] | None]


class DocumentFetcher(NamedTuple):
source: str
fetch: FetchDocument


document_fetchers: list[DocumentFetcher] = []


def register_document_fetcher(source: str, fetch: FetchDocument) -> None:
"""Register a document fetcher.
A document fetcher is a function that takes a report from the PostgreSQL
database and returns a document in the form of a dictionary from another
database (like Vespa).
"""
document_fetchers.append(DocumentFetcher(source, fetch))


class ReportPanelButton(NamedTuple):
order: int
Expand All @@ -12,6 +50,7 @@ class ReportPanelButton(NamedTuple):


def register_report_panel_button(order: int, template_name: str) -> None:
"""Register an additional button for the panel below each report."""
global report_panel_buttons
report_panel_buttons.append(ReportPanelButton(order, template_name))
report_panel_buttons = sorted(report_panel_buttons, key=lambda x: x.order)
Expand Down
5 changes: 2 additions & 3 deletions radis/search/apps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate

from radis.core.site import register_main_menu_item


class SearchConfig(AppConfig):
name = "radis.search"
Expand All @@ -15,7 +13,8 @@ def ready(self):


def register_app():
from radis.api.site import register_document_fetcher, register_report_handler
from radis.core.site import register_main_menu_item
from radis.reports.site import register_document_fetcher, register_report_handler

from .models import fetch_document, handle_report

Expand Down
2 changes: 1 addition & 1 deletion radis/search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from rest_framework.status import HTTP_200_OK
from vespa.io import VespaQueryResponse

from radis.api.site import ReportEventType
from radis.core.models import AppSettings
from radis.reports.models import Report
from radis.reports.site import ReportEventType

from .utils.search_utils import extract_document_id
from .vespa_app import REPORT_SCHEMA_NAME, vespa_app
Expand Down
1 change: 0 additions & 1 deletion radis/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"radis.core.apps.CoreConfig",
"radis.accounts.apps.AccountsConfig",
"radis.token_authentication.apps.TokenAuthenticationConfig",
"radis.api.apps.ApiConfig",
"radis.reports.apps.ReportsConfig",
"radis.search.apps.SearchConfig",
"radis.collections.apps.CollectionsConfig",
Expand Down
2 changes: 1 addition & 1 deletion radis/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
path("admin-the-great/", admin.site.urls),
path("api-auth/", include("rest_framework.urls")),
path("accounts/", include("radis.accounts.urls")),
path("api/", include("radis.api.urls")),
path("", include("radis.core.urls")),
path("token-authentication/", include("radis.token_authentication.urls")),
path("sandbox/", include("radis.sandbox.urls")),
path("reports/", include("radis.reports.urls")),
path("api/reports/", include("radis.reports.api.urls")),
path("search/", include("radis.search.urls")),
path("collections/", include("radis.collections.urls")),
path("notes/", include("radis.notes.urls")),
Expand Down

0 comments on commit 352afd2

Please sign in to comment.