-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add create + update endpoints to message api, refactor api code
- Loading branch information
Showing
29 changed files
with
629 additions
and
407 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from rest_framework import permissions | ||
from rest_framework.views import Request | ||
|
||
from ..auth import can_write_foirequest | ||
|
||
|
||
class OwnsFoiRequestPermission(permissions.BasePermission): | ||
def __init__(self, request_field: str = "request") -> None: | ||
self.request_field = request_field | ||
super().__init__() | ||
|
||
def has_object_permission(self, request: Request, view, obj): | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return can_write_foirequest(getattr(obj, self.request_field), request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.utils.translation import gettext as _ | ||
|
||
from django_filters import rest_framework as filters | ||
from rest_framework import permissions, viewsets | ||
from rest_framework.exceptions import PermissionDenied | ||
from rest_framework.views import Request | ||
|
||
from froide.foirequest.api.permissions import OwnsFoiRequestPermission | ||
|
||
from ...auth import ( | ||
get_read_foimessage_queryset, | ||
) | ||
from ...models import FoiMessage | ||
from ..serializers import FoiMessageSerializer, optimize_message_queryset | ||
|
||
User = get_user_model() | ||
|
||
|
||
class FoiMessageFilter(filters.FilterSet): | ||
class Meta: | ||
model = FoiMessage | ||
fields = ("request", "kind", "is_response", "is_draft") | ||
|
||
|
||
class FoiMessageViewSet(viewsets.ModelViewSet): | ||
serializer_class = FoiMessageSerializer | ||
filter_backends = (filters.DjangoFilterBackend,) | ||
filterset_class = FoiMessageFilter | ||
required_scopes = ["make:message"] | ||
|
||
def get_permissions(self): | ||
if self.action in ["list", "retrieve"]: | ||
return [] | ||
return [permissions.IsAuthenticated(), OwnsFoiRequestPermission()] | ||
|
||
def get_queryset(self): | ||
qs = get_read_foimessage_queryset(self.request).order_by() | ||
return self.optimize_query(qs) | ||
|
||
def optimize_query(self, qs): | ||
return optimize_message_queryset(self.request, qs) | ||
|
||
def ensure_draft(self): | ||
# only drafts can be edited | ||
if not self.get_object().is_draft: | ||
raise PermissionDenied(_("Cannot alter non-draft messages")) | ||
|
||
def update(self, request: Request, *args, **kwargs): | ||
self.ensure_draft() | ||
return super().update(request, *args, **kwargs) | ||
|
||
def destroy(self, request: Request, *args, **kwargs): | ||
self.ensure_draft() | ||
return super().destroy(request, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.