-
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
616 additions
and
380 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 ..auth import can_write_foirequest | ||
|
||
|
||
class OwnsFoiRequestPermission(permissions.BasePermission): | ||
def __init__(self, foirequest_field="request"): | ||
self.foirequest_field = foirequest_field | ||
super().__init__() | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if request.method in permissions.SAFE_METHODS and obj.is_draft is False: | ||
return True | ||
|
||
foirequest = getattr(obj, self.foirequest_field) | ||
return can_write_foirequest(foirequest, 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,48 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.core.exceptions import BadRequest | ||
from django.utils.translation import gettext as _ | ||
|
||
from django_filters import rest_framework as filters | ||
from rest_framework import permissions, viewsets | ||
|
||
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_queryset(self): | ||
qs = get_read_foimessage_queryset(self.request).order_by() | ||
return self.optimize_query(qs) | ||
|
||
def get_permissions(self): | ||
return [ | ||
permissions.IsAuthenticated(), | ||
OwnsFoiRequestPermission("belongs_to__request"), | ||
] | ||
|
||
def optimize_query(self, qs): | ||
return optimize_message_queryset(self.request, qs) | ||
|
||
def destroy(self, request, *args, **kwargs): | ||
if not self.get_object().is_draft: | ||
raise BadRequest(_("Cannot delete non-draft messages")) | ||
|
||
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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .test_admin import * # noqa | ||
from .test_api import * # noqa | ||
from .test_api_request import * # noqa | ||
from .test_mail import * # noqa | ||
from .test_request import * # noqa | ||
from .test_web import * # noqa |
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 |
---|---|---|
|
@@ -120,6 +120,26 @@ def test_search_similar(self): | |
self.assertContains(response, "title") | ||
self.assertContains(response, "description") | ||
|
||
def test_create_message(self): | ||
self.client.login(email="[email protected]", password="froide") | ||
req = factories.FoiRequestFactory.create() | ||
|
||
response = self.client.post( | ||
"/api/v1/message/", | ||
data={ | ||
"request": reverse("api:request-detail", kwargs={"pk": req.pk}), | ||
}, | ||
) | ||
self.assertEqual(response.status_code, 201) | ||
|
||
message_id = json.loads(response.content)["id"] | ||
resource_uri = reverse("api:message-detail", kwargs={"pk": message_id}) | ||
|
||
response = self.client.delete(resource_uri) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
response = self.client.patch(resource_uri, data={}) | ||
|
||
|
||
class OAuthAPIMixin: | ||
def setUp(self): | ||
|
Oops, something went wrong.