Skip to content

Commit

Permalink
Merge pull request #1433 from maykinmedia/swr/hackathon-update-berich…
Browse files Browse the repository at this point in the history
…t-read-status

Sync message read statuses
  • Loading branch information
alextreme authored Oct 9, 2024
2 parents 6117076 + 5b7c7da commit 0f97264
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
25 changes: 25 additions & 0 deletions src/open_inwoner/berichten/services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
from typing import Literal

from objectsapiclient.client import Client as ObjectenClient
Expand Down Expand Up @@ -39,3 +40,27 @@ def fetch_bericht(self, uuid: str):
obj = self.client.get_object(uuid)

return Bericht.model_validate(obj.record["data"] | {"object_uuid": obj.uuid})

def update_object(self, uuid: str, updated_data: dict):

# TODO: the PATCH method in the Objects API does not appear to work as
# expected. It validates the partial against the JSON Schema, so in this
# case you have to supply an object that is valid according to the
# schema. We thus have to do our own merging.

# Also: we are usign the underlying API directly to avoid going back and
# forth between camel and snake case.
existing_obj = self.client.objects_api.retrieve("object", uuid=uuid)
existing_data = existing_obj["record"]["data"]
self.client.objects_api.partial_update(
"object",
{
"record": {
"startAt": datetime.date.today().isoformat(),
"data": existing_data | updated_data,
}
},
uuid=uuid,
)
# Refresh the object and build the Bericht model
return self.client.get_object(uuid)
9 changes: 6 additions & 3 deletions src/open_inwoner/berichten/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from django.urls import path

from open_inwoner.berichten.views.bericht_detail import BerichtDetailView

from .views import BerichtListView
from .views import BerichtDetailView, BerichtListView, mark_bericht_as_unread

app_name = "berichten"

urlpatterns = [
path("<uuid:object_uuid>/", BerichtDetailView.as_view(), name="detail"),
path(
"<uuid:object_uuid>/mark-unread",
mark_bericht_as_unread,
name="mark-bericht-unread",
),
path("", BerichtListView.as_view(), name="list"),
]
4 changes: 2 additions & 2 deletions src/open_inwoner/berichten/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .bericht_detail import BerichtDetailView
from .bericht_detail import BerichtDetailView, mark_bericht_as_unread
from .bericht_list import BerichtListView

__all__ = ["BerichtDetailView", "BerichtListView"]
__all__ = ["BerichtDetailView", "BerichtListView", "mark_bericht_as_unread"]
20 changes: 18 additions & 2 deletions src/open_inwoner/berichten/views/bericht_detail.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging

from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
Expand All @@ -13,7 +16,9 @@
logger = logging.getLogger(__name__)


class BerichtDetailView(CommonPageMixin, BaseBreadcrumbMixin, TemplateView):
class BerichtDetailView(
CommonPageMixin, BaseBreadcrumbMixin, TemplateView, LoginRequiredMixin
):

template_name = "pages/berichten/detail.html"

Expand All @@ -30,5 +35,16 @@ def page_title(self):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
service = BerichtenService()
context["bericht"] = service.fetch_bericht(self.kwargs["object_uuid"])
bericht = service.fetch_bericht(self.kwargs["object_uuid"])
context["bericht"] = bericht
if not bericht.geopend:
service.update_object(self.kwargs["object_uuid"], {"geopend": True})

return context


@login_required
def mark_bericht_as_unread(request, object_uuid):
service = BerichtenService()
service.update_object(object_uuid, {"geopend": False})
return HttpResponseRedirect(reverse("berichten:list"))

0 comments on commit 0f97264

Please sign in to comment.