2024-01-09 15:30:36 +01:00
|
|
|
"""API endpoints"""
|
2024-08-20 16:42:27 +02:00
|
|
|
|
2024-08-19 22:38:41 +02:00
|
|
|
import re
|
2024-08-19 22:35:48 +02:00
|
|
|
import uuid
|
2024-08-19 22:38:41 +02:00
|
|
|
from urllib.parse import urlparse
|
2024-08-19 22:35:48 +02:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
2024-03-03 08:49:27 +01:00
|
|
|
from django.contrib.postgres.aggregates import ArrayAgg
|
2024-11-06 08:09:05 +01:00
|
|
|
from django.contrib.postgres.search import TrigramSimilarity
|
2024-09-08 23:37:49 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2024-08-19 22:35:48 +02:00
|
|
|
from django.core.files.storage import default_storage
|
2024-01-09 15:30:36 +01:00
|
|
|
from django.db.models import (
|
2024-09-16 19:27:48 +02:00
|
|
|
Min,
|
2024-01-09 15:30:36 +01:00
|
|
|
OuterRef,
|
|
|
|
|
Q,
|
|
|
|
|
Subquery,
|
|
|
|
|
)
|
2024-08-07 14:44:18 +02:00
|
|
|
from django.http import Http404
|
2024-01-09 15:30:36 +01:00
|
|
|
|
2024-04-08 23:37:15 +02:00
|
|
|
from botocore.exceptions import ClientError
|
2024-01-09 15:30:36 +01:00
|
|
|
from rest_framework import (
|
|
|
|
|
decorators,
|
|
|
|
|
exceptions,
|
2024-04-16 09:56:18 +02:00
|
|
|
filters,
|
2024-09-20 22:42:46 +02:00
|
|
|
metadata,
|
2024-01-09 15:30:36 +01:00
|
|
|
mixins,
|
|
|
|
|
pagination,
|
2024-02-09 19:32:12 +01:00
|
|
|
status,
|
2024-11-15 09:29:07 +01:00
|
|
|
views,
|
2024-01-09 15:30:36 +01:00
|
|
|
viewsets,
|
|
|
|
|
)
|
2024-02-09 19:32:12 +01:00
|
|
|
from rest_framework import (
|
|
|
|
|
response as drf_response,
|
|
|
|
|
)
|
2024-11-15 09:29:07 +01:00
|
|
|
from rest_framework.permissions import AllowAny
|
2024-01-09 15:30:36 +01:00
|
|
|
|
2024-09-20 22:42:46 +02:00
|
|
|
from core import enums, models
|
|
|
|
|
from core.services.ai_services import AIService
|
2024-01-09 15:30:36 +01:00
|
|
|
|
2024-08-19 22:38:41 +02:00
|
|
|
from . import permissions, serializers, utils
|
|
|
|
|
|
|
|
|
|
ATTACHMENTS_FOLDER = "attachments"
|
|
|
|
|
UUID_REGEX = (
|
|
|
|
|
r"[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
|
|
|
|
|
)
|
|
|
|
|
FILE_EXT_REGEX = r"\.[a-zA-Z]{3,4}"
|
|
|
|
|
MEDIA_URL_PATTERN = re.compile(
|
|
|
|
|
f"{settings.MEDIA_URL:s}({UUID_REGEX:s})/"
|
|
|
|
|
f"({ATTACHMENTS_FOLDER:s}/{UUID_REGEX:s}{FILE_EXT_REGEX:s})$"
|
|
|
|
|
)
|
2024-01-09 15:30:36 +01:00
|
|
|
|
2024-02-09 19:32:12 +01:00
|
|
|
# pylint: disable=too-many-ancestors
|
|
|
|
|
|
2024-08-19 22:35:48 +02:00
|
|
|
ATTACHMENTS_FOLDER = "attachments"
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
|
|
|
|
|
class NestedGenericViewSet(viewsets.GenericViewSet):
|
|
|
|
|
"""
|
|
|
|
|
A generic Viewset aims to be used in a nested route context.
|
|
|
|
|
e.g: `/api/v1.0/resource_1/<resource_1_pk>/resource_2/<resource_2_pk>/`
|
|
|
|
|
|
|
|
|
|
It allows to define all url kwargs and lookup fields to perform the lookup.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
lookup_fields: list[str] = ["pk"]
|
|
|
|
|
lookup_url_kwargs: list[str] = []
|
|
|
|
|
|
|
|
|
|
def __getattribute__(self, item):
|
|
|
|
|
"""
|
|
|
|
|
This method is overridden to allow to get the last lookup field or lookup url kwarg
|
|
|
|
|
when accessing the `lookup_field` or `lookup_url_kwarg` attribute. This is useful
|
|
|
|
|
to keep compatibility with all methods used by the parent class `GenericViewSet`.
|
|
|
|
|
"""
|
|
|
|
|
if item in ["lookup_field", "lookup_url_kwarg"]:
|
|
|
|
|
return getattr(self, item + "s", [None])[-1]
|
|
|
|
|
|
|
|
|
|
return super().__getattribute__(item)
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
"""
|
|
|
|
|
Get the list of items for this view.
|
|
|
|
|
|
|
|
|
|
`lookup_fields` attribute is enumerated here to perform the nested lookup.
|
|
|
|
|
"""
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
|
|
|
|
|
|
# The last lookup field is removed to perform the nested lookup as it corresponds
|
|
|
|
|
# to the object pk, it is used within get_object method.
|
|
|
|
|
lookup_url_kwargs = (
|
|
|
|
|
self.lookup_url_kwargs[:-1]
|
|
|
|
|
if self.lookup_url_kwargs
|
|
|
|
|
else self.lookup_fields[:-1]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
filter_kwargs = {}
|
|
|
|
|
for index, lookup_url_kwarg in enumerate(lookup_url_kwargs):
|
|
|
|
|
if lookup_url_kwarg not in self.kwargs:
|
|
|
|
|
raise KeyError(
|
|
|
|
|
f"Expected view {self.__class__.__name__} to be called with a URL "
|
|
|
|
|
f'keyword argument named "{lookup_url_kwarg}". Fix your URL conf, or '
|
|
|
|
|
"set the `.lookup_fields` attribute on the view correctly."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
filter_kwargs.update(
|
|
|
|
|
{self.lookup_fields[index]: self.kwargs[lookup_url_kwarg]}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return queryset.filter(**filter_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SerializerPerActionMixin:
|
|
|
|
|
"""
|
|
|
|
|
A mixin to allow to define serializer classes for each action.
|
|
|
|
|
|
|
|
|
|
This mixin is useful to avoid to define a serializer class for each action in the
|
|
|
|
|
`get_serializer_class` method.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
serializer_classes: dict[str, type] = {}
|
|
|
|
|
default_serializer_class: type = None
|
|
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
|
"""
|
|
|
|
|
Return the serializer class to use depending on the action.
|
|
|
|
|
"""
|
|
|
|
|
return self.serializer_classes.get(self.action, self.default_serializer_class)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Pagination(pagination.PageNumberPagination):
|
|
|
|
|
"""Pagination to display no more than 100 objects per page sorted by creation date."""
|
|
|
|
|
|
|
|
|
|
ordering = "-created_on"
|
|
|
|
|
max_page_size = 100
|
|
|
|
|
page_size_query_param = "page_size"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserViewSet(
|
2024-05-29 14:48:12 +02:00
|
|
|
mixins.UpdateModelMixin, viewsets.GenericViewSet, mixins.ListModelMixin
|
2024-01-09 15:30:36 +01:00
|
|
|
):
|
|
|
|
|
"""User ViewSet"""
|
|
|
|
|
|
|
|
|
|
permission_classes = [permissions.IsSelf]
|
|
|
|
|
queryset = models.User.objects.all()
|
|
|
|
|
serializer_class = serializers.UserSerializer
|
|
|
|
|
|
2024-05-29 14:48:12 +02:00
|
|
|
def get_queryset(self):
|
|
|
|
|
"""
|
|
|
|
|
Limit listed users by querying the email field with a trigram similarity
|
|
|
|
|
search if a query is provided.
|
|
|
|
|
Limit listed users by excluding users already in the document if a document_id
|
|
|
|
|
is provided.
|
|
|
|
|
"""
|
|
|
|
|
queryset = self.queryset
|
|
|
|
|
|
|
|
|
|
if self.action == "list":
|
|
|
|
|
# Exclude all users already in the given document
|
|
|
|
|
if document_id := self.request.GET.get("document_id", ""):
|
|
|
|
|
queryset = queryset.exclude(documentaccess__document_id=document_id)
|
|
|
|
|
|
|
|
|
|
# Filter users by email similarity
|
|
|
|
|
if query := self.request.GET.get("q", ""):
|
2024-11-06 08:09:05 +01:00
|
|
|
# For performance reasons we filter first by similarity, which relies on an index,
|
|
|
|
|
# then only calculate precise similarity scores for sorting purposes
|
2024-05-29 14:48:12 +02:00
|
|
|
queryset = queryset.filter(email__trigram_word_similar=query)
|
|
|
|
|
|
2024-11-06 08:09:05 +01:00
|
|
|
queryset = queryset.annotate(
|
|
|
|
|
similarity=TrigramSimilarity("email", query)
|
|
|
|
|
)
|
|
|
|
|
# When the query only is on the name part, we should try to make many proposals
|
|
|
|
|
# But when the query looks like an email we should only propose serious matches
|
|
|
|
|
threshold = 0.6 if "@" in query else 0.1
|
|
|
|
|
|
|
|
|
|
queryset = queryset.filter(similarity__gt=threshold).order_by(
|
|
|
|
|
"-similarity"
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-29 14:48:12 +02:00
|
|
|
return queryset
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
@decorators.action(
|
|
|
|
|
detail=False,
|
|
|
|
|
methods=["get"],
|
|
|
|
|
url_name="me",
|
|
|
|
|
url_path="me",
|
|
|
|
|
permission_classes=[permissions.IsAuthenticated],
|
|
|
|
|
)
|
|
|
|
|
def get_me(self, request):
|
|
|
|
|
"""
|
|
|
|
|
Return information on currently logged user
|
|
|
|
|
"""
|
|
|
|
|
context = {"request": request}
|
2024-02-09 19:32:12 +01:00
|
|
|
return drf_response.Response(
|
2024-01-09 15:30:36 +01:00
|
|
|
self.serializer_class(request.user, context=context).data
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
class ResourceViewsetMixin:
|
|
|
|
|
"""Mixin with methods common to all resource viewsets that are managed with accesses."""
|
2024-01-09 15:30:36 +01:00
|
|
|
|
2024-04-16 09:56:18 +02:00
|
|
|
filter_backends = [filters.OrderingFilter]
|
2024-07-04 12:02:31 +02:00
|
|
|
ordering_fields = ["created_at", "updated_at", "title"]
|
2024-04-16 09:56:18 +02:00
|
|
|
ordering = ["-created_at"]
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
def get_queryset(self):
|
2024-04-03 18:50:28 +02:00
|
|
|
"""Custom queryset to get user related resources."""
|
|
|
|
|
queryset = super().get_queryset()
|
2024-03-03 08:49:27 +01:00
|
|
|
user = self.request.user
|
2024-09-08 23:37:49 +02:00
|
|
|
|
|
|
|
|
if not user.is_authenticated:
|
|
|
|
|
return queryset
|
|
|
|
|
|
2024-03-03 08:49:27 +01:00
|
|
|
user_roles_query = (
|
2024-04-03 18:50:28 +02:00
|
|
|
self.access_model_class.objects.filter(
|
2024-09-06 16:12:02 +02:00
|
|
|
Q(user=user) | Q(team__in=user.teams),
|
2024-04-03 18:50:28 +02:00
|
|
|
**{self.resource_field_name: OuterRef("pk")},
|
2024-03-03 08:49:27 +01:00
|
|
|
)
|
2024-04-03 18:50:28 +02:00
|
|
|
.values(self.resource_field_name)
|
2024-03-03 08:49:27 +01:00
|
|
|
.annotate(roles_array=ArrayAgg("role"))
|
|
|
|
|
.values("roles_array")
|
|
|
|
|
)
|
2024-09-06 16:12:02 +02:00
|
|
|
return queryset.annotate(user_roles=Subquery(user_roles_query)).distinct()
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
def perform_create(self, serializer):
|
2024-04-03 18:50:28 +02:00
|
|
|
"""Set the current user as owner of the newly created object."""
|
|
|
|
|
obj = serializer.save()
|
|
|
|
|
self.access_model_class.objects.create(
|
2024-01-09 15:30:36 +01:00
|
|
|
user=self.request.user,
|
|
|
|
|
role=models.RoleChoices.OWNER,
|
2024-04-03 18:50:28 +02:00
|
|
|
**{self.resource_field_name: obj},
|
2024-01-09 15:30:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
class ResourceAccessViewsetMixin:
|
|
|
|
|
"""Mixin with methods common to all access viewsets."""
|
2024-01-09 15:30:36 +01:00
|
|
|
|
|
|
|
|
def get_permissions(self):
|
2024-04-03 18:50:28 +02:00
|
|
|
"""User only needs to be authenticated to list resource accesses"""
|
2024-01-09 15:30:36 +01:00
|
|
|
if self.action == "list":
|
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
else:
|
|
|
|
|
return super().get_permissions()
|
|
|
|
|
|
|
|
|
|
return [permission() for permission in permission_classes]
|
|
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
|
"""Extra context provided to the serializer class."""
|
|
|
|
|
context = super().get_serializer_context()
|
2024-04-03 18:50:28 +02:00
|
|
|
context["resource_id"] = self.kwargs["resource_id"]
|
2024-01-09 15:30:36 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
"""Return the queryset according to the action."""
|
|
|
|
|
queryset = super().get_queryset()
|
2024-04-03 18:50:28 +02:00
|
|
|
queryset = queryset.filter(
|
|
|
|
|
**{self.resource_field_name: self.kwargs["resource_id"]}
|
|
|
|
|
)
|
2024-01-09 15:30:36 +01:00
|
|
|
|
|
|
|
|
if self.action == "list":
|
2024-03-03 08:49:27 +01:00
|
|
|
user = self.request.user
|
2024-09-06 16:12:02 +02:00
|
|
|
teams = user.teams
|
2024-03-03 08:49:27 +01:00
|
|
|
user_roles_query = (
|
2024-04-03 18:50:28 +02:00
|
|
|
queryset.filter(
|
2024-03-03 08:49:27 +01:00
|
|
|
Q(user=user) | Q(team__in=teams),
|
2024-04-03 18:50:28 +02:00
|
|
|
**{self.resource_field_name: self.kwargs["resource_id"]},
|
2024-03-03 08:49:27 +01:00
|
|
|
)
|
2024-04-03 18:50:28 +02:00
|
|
|
.values(self.resource_field_name)
|
2024-03-03 08:49:27 +01:00
|
|
|
.annotate(roles_array=ArrayAgg("role"))
|
|
|
|
|
.values("roles_array")
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
# Limit to resource access instances related to a resource THAT also has
|
|
|
|
|
# a resource access
|
|
|
|
|
# instance for the logged-in user (we don't want to list only the resource
|
2024-02-09 19:32:12 +01:00
|
|
|
# access instances pointing to the logged-in user)
|
2024-01-09 15:30:36 +01:00
|
|
|
queryset = (
|
|
|
|
|
queryset.filter(
|
2024-04-03 18:50:28 +02:00
|
|
|
Q(**{f"{self.resource_field_name}__accesses__user": user})
|
|
|
|
|
| Q(**{f"{self.resource_field_name}__accesses__team__in": teams}),
|
|
|
|
|
**{self.resource_field_name: self.kwargs["resource_id"]},
|
2024-01-09 15:30:36 +01:00
|
|
|
)
|
2024-03-03 08:49:27 +01:00
|
|
|
.annotate(user_roles=Subquery(user_roles_query))
|
2024-01-09 15:30:36 +01:00
|
|
|
.distinct()
|
|
|
|
|
)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
|
"""Forbid deleting the last owner access"""
|
|
|
|
|
instance = self.get_object()
|
2024-04-03 18:50:28 +02:00
|
|
|
resource = getattr(instance, self.resource_field_name)
|
2024-01-09 15:30:36 +01:00
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
# Check if the access being deleted is the last owner access for the resource
|
2024-02-09 19:32:12 +01:00
|
|
|
if (
|
|
|
|
|
instance.role == "owner"
|
2024-04-03 18:50:28 +02:00
|
|
|
and resource.accesses.filter(role="owner").count() == 1
|
2024-02-09 19:32:12 +01:00
|
|
|
):
|
|
|
|
|
return drf_response.Response(
|
2024-04-03 18:50:28 +02:00
|
|
|
{"detail": "Cannot delete the last owner access for the resource."},
|
2024-08-19 22:35:48 +02:00
|
|
|
status=status.HTTP_403_FORBIDDEN,
|
2024-01-09 15:30:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return super().destroy(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
|
"""Check that we don't change the role if it leads to losing the last owner."""
|
|
|
|
|
instance = serializer.instance
|
|
|
|
|
|
|
|
|
|
# Check if the role is being updated and the new role is not "owner"
|
|
|
|
|
if (
|
|
|
|
|
"role" in self.request.data
|
|
|
|
|
and self.request.data["role"] != models.RoleChoices.OWNER
|
|
|
|
|
):
|
2024-04-03 18:50:28 +02:00
|
|
|
resource = getattr(instance, self.resource_field_name)
|
|
|
|
|
# Check if the access being updated is the last owner access for the resource
|
2024-01-09 15:30:36 +01:00
|
|
|
if (
|
|
|
|
|
instance.role == models.RoleChoices.OWNER
|
2024-04-03 18:50:28 +02:00
|
|
|
and resource.accesses.filter(role=models.RoleChoices.OWNER).count() == 1
|
2024-01-09 15:30:36 +01:00
|
|
|
):
|
|
|
|
|
message = "Cannot change the role to a non-owner role for the last owner access."
|
2024-02-09 19:32:12 +01:00
|
|
|
raise exceptions.PermissionDenied({"detail": message})
|
2024-01-09 15:30:36 +01:00
|
|
|
|
|
|
|
|
serializer.save()
|
2024-04-03 18:50:28 +02:00
|
|
|
|
|
|
|
|
|
2024-09-20 22:42:46 +02:00
|
|
|
class DocumentMetadata(metadata.SimpleMetadata):
|
|
|
|
|
"""Custom metadata class to add information"""
|
|
|
|
|
|
|
|
|
|
def determine_metadata(self, request, view):
|
|
|
|
|
"""Add language choices only for the list endpoint."""
|
|
|
|
|
simple_metadata = super().determine_metadata(request, view)
|
|
|
|
|
|
|
|
|
|
if request.path.endswith("/documents/"):
|
|
|
|
|
simple_metadata["actions"]["POST"]["language"] = {
|
|
|
|
|
"choices": [
|
|
|
|
|
{"value": code, "display_name": name}
|
|
|
|
|
for code, name in enums.ALL_LANGUAGES.items()
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
return simple_metadata
|
|
|
|
|
|
|
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
class DocumentViewSet(
|
|
|
|
|
ResourceViewsetMixin,
|
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
|
):
|
|
|
|
|
"""Document ViewSet"""
|
|
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
|
permissions.AccessPermission,
|
|
|
|
|
]
|
|
|
|
|
serializer_class = serializers.DocumentSerializer
|
|
|
|
|
access_model_class = models.DocumentAccess
|
|
|
|
|
resource_field_name = "document"
|
|
|
|
|
queryset = models.Document.objects.all()
|
2024-08-23 13:20:19 +02:00
|
|
|
ordering = ["-updated_at"]
|
2024-09-20 22:42:46 +02:00
|
|
|
metadata_class = DocumentMetadata
|
2024-04-03 18:50:28 +02:00
|
|
|
|
2024-09-08 23:37:49 +02:00
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
|
"""Restrict resources returned by the list endpoint"""
|
|
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
|
|
|
|
user = self.request.user
|
|
|
|
|
if user.is_authenticated:
|
|
|
|
|
queryset = queryset.filter(
|
|
|
|
|
Q(accesses__user=user)
|
|
|
|
|
| Q(accesses__team__in=user.teams)
|
|
|
|
|
| (
|
|
|
|
|
Q(link_traces__user=user)
|
|
|
|
|
& ~Q(link_reach=models.LinkReachChoices.RESTRICTED)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
queryset = queryset.none()
|
|
|
|
|
|
|
|
|
|
page = self.paginate_queryset(queryset)
|
|
|
|
|
if page is not None:
|
|
|
|
|
serializer = self.get_serializer(page, many=True)
|
|
|
|
|
return self.get_paginated_response(serializer.data)
|
|
|
|
|
|
|
|
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
|
|
|
return drf_response.Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Add a trace that the document was accessed by a user. This is used to list documents
|
|
|
|
|
on a user's list view even though the user has no specific role in the document (link
|
|
|
|
|
access when the link reach configuration of the document allows it).
|
|
|
|
|
"""
|
|
|
|
|
instance = self.get_object()
|
|
|
|
|
serializer = self.get_serializer(instance)
|
|
|
|
|
|
|
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
|
try:
|
|
|
|
|
# Add a trace that the user visited the document (this is needed to include
|
|
|
|
|
# the document in the user's list view)
|
|
|
|
|
models.LinkTrace.objects.create(
|
|
|
|
|
document=instance,
|
|
|
|
|
user=self.request.user,
|
|
|
|
|
)
|
|
|
|
|
except ValidationError:
|
|
|
|
|
# The trace already exists, so we just pass without doing anything
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
return drf_response.Response(serializer.data)
|
|
|
|
|
|
2024-04-08 23:37:15 +02:00
|
|
|
@decorators.action(detail=True, methods=["get"], url_path="versions")
|
|
|
|
|
def versions_list(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Return the document's versions but only those created after the user got access
|
|
|
|
|
to the document
|
|
|
|
|
"""
|
2024-09-16 19:27:48 +02:00
|
|
|
user = request.user
|
|
|
|
|
if not user.is_authenticated:
|
2024-09-08 23:37:49 +02:00
|
|
|
raise exceptions.PermissionDenied("Authentication required.")
|
|
|
|
|
|
2024-09-16 19:27:48 +02:00
|
|
|
# Validate query parameters using dedicated serializer
|
|
|
|
|
serializer = serializers.VersionFilterSerializer(data=request.query_params)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
2024-04-08 23:37:15 +02:00
|
|
|
document = self.get_object()
|
2024-09-16 19:27:48 +02:00
|
|
|
|
|
|
|
|
# Users should not see version history dating from before they gained access to the
|
|
|
|
|
# document. Filter to get the minimum access date for the logged-in user
|
|
|
|
|
access_queryset = document.accesses.filter(
|
|
|
|
|
Q(user=user) | Q(team__in=user.teams)
|
|
|
|
|
).aggregate(min_date=Min("created_at"))
|
|
|
|
|
|
|
|
|
|
# Handle the case where the user has no accesses
|
|
|
|
|
min_datetime = access_queryset["min_date"]
|
|
|
|
|
if not min_datetime:
|
|
|
|
|
return exceptions.PermissionDenied(
|
|
|
|
|
"Only users with specific access can see version history"
|
2024-04-08 23:37:15 +02:00
|
|
|
)
|
2024-07-17 09:10:05 +02:00
|
|
|
|
2024-09-16 19:27:48 +02:00
|
|
|
versions_data = document.get_versions_slice(
|
|
|
|
|
from_version_id=serializer.validated_data.get("version_id"),
|
|
|
|
|
min_datetime=min_datetime,
|
|
|
|
|
page_size=serializer.validated_data.get("page_size"),
|
2024-04-08 23:37:15 +02:00
|
|
|
)
|
|
|
|
|
|
2024-09-16 19:27:48 +02:00
|
|
|
return drf_response.Response(versions_data)
|
2024-07-17 09:10:05 +02:00
|
|
|
|
2024-04-08 23:37:15 +02:00
|
|
|
@decorators.action(
|
|
|
|
|
detail=True,
|
|
|
|
|
methods=["get", "delete"],
|
|
|
|
|
url_path="versions/(?P<version_id>[0-9a-f-]{36})",
|
|
|
|
|
)
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
def versions_detail(self, request, pk, version_id, *args, **kwargs):
|
|
|
|
|
"""Custom action to retrieve a specific version of a document"""
|
|
|
|
|
document = self.get_object()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = document.get_content_response(version_id=version_id)
|
|
|
|
|
except (FileNotFoundError, ClientError) as err:
|
|
|
|
|
raise Http404 from err
|
|
|
|
|
|
|
|
|
|
# Don't let users access versions that were created before they were given access
|
|
|
|
|
# to the document
|
2024-09-06 16:12:02 +02:00
|
|
|
user = request.user
|
2024-09-16 19:27:48 +02:00
|
|
|
min_datetime = min(
|
2024-04-08 23:37:15 +02:00
|
|
|
access.created_at
|
|
|
|
|
for access in document.accesses.filter(
|
2024-09-06 16:12:02 +02:00
|
|
|
Q(user=user) | Q(team__in=user.teams),
|
2024-04-08 23:37:15 +02:00
|
|
|
)
|
|
|
|
|
)
|
2024-09-16 19:27:48 +02:00
|
|
|
if response["LastModified"] < min_datetime:
|
2024-04-08 23:37:15 +02:00
|
|
|
raise Http404
|
|
|
|
|
|
|
|
|
|
if request.method == "DELETE":
|
|
|
|
|
response = document.delete_version(version_id)
|
|
|
|
|
return drf_response.Response(
|
|
|
|
|
status=response["ResponseMetadata"]["HTTPStatusCode"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return drf_response.Response(
|
|
|
|
|
{
|
2024-05-21 14:46:23 +02:00
|
|
|
"content": response["Body"].read().decode("utf-8"),
|
2024-04-08 23:37:15 +02:00
|
|
|
"last_modified": response["LastModified"],
|
2024-07-17 09:10:05 +02:00
|
|
|
"id": version_id,
|
2024-04-08 23:37:15 +02:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-08 23:07:47 +02:00
|
|
|
@decorators.action(detail=True, methods=["put"], url_path="link-configuration")
|
|
|
|
|
def link_configuration(self, request, *args, **kwargs):
|
|
|
|
|
"""Update link configuration with specific rights (cf get_abilities)."""
|
|
|
|
|
# Check permissions first
|
|
|
|
|
document = self.get_object()
|
|
|
|
|
|
|
|
|
|
# Deserialize and validate the data
|
|
|
|
|
serializer = serializers.LinkDocumentSerializer(
|
|
|
|
|
document, data=request.data, partial=True
|
|
|
|
|
)
|
2024-09-20 22:42:46 +02:00
|
|
|
serializer.is_valid(raise_exception=True)
|
2024-09-08 23:07:47 +02:00
|
|
|
|
|
|
|
|
serializer.save()
|
|
|
|
|
return drf_response.Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
|
|
2024-08-19 22:35:48 +02:00
|
|
|
@decorators.action(detail=True, methods=["post"], url_path="attachment-upload")
|
|
|
|
|
def attachment_upload(self, request, *args, **kwargs):
|
|
|
|
|
"""Upload a file related to a given document"""
|
|
|
|
|
# Check permissions first
|
|
|
|
|
document = self.get_object()
|
|
|
|
|
|
|
|
|
|
# Validate metadata in payload
|
|
|
|
|
serializer = serializers.FileUploadSerializer(data=request.data)
|
2024-09-20 22:42:46 +02:00
|
|
|
serializer.is_valid(raise_exception=True)
|
2024-08-19 22:35:48 +02:00
|
|
|
|
|
|
|
|
# Generate a generic yet unique filename to store the image in object storage
|
|
|
|
|
file_id = uuid.uuid4()
|
2024-10-07 20:10:42 +02:00
|
|
|
extension = serializer.validated_data["expected_extension"]
|
|
|
|
|
key = f"{document.key_base}/{ATTACHMENTS_FOLDER:s}/{file_id!s}.{extension:s}"
|
|
|
|
|
|
|
|
|
|
# Prepare metadata for storage
|
2024-09-20 22:42:46 +02:00
|
|
|
extra_args = {"Metadata": {"owner": str(request.user.id)}}
|
2024-10-07 20:10:42 +02:00
|
|
|
if serializer.validated_data["is_unsafe"]:
|
2024-09-20 22:42:46 +02:00
|
|
|
extra_args["Metadata"]["is_unsafe"] = "true"
|
2024-10-07 20:10:42 +02:00
|
|
|
|
|
|
|
|
file = serializer.validated_data["file"]
|
|
|
|
|
default_storage.connection.meta.client.upload_fileobj(
|
2024-09-20 22:42:46 +02:00
|
|
|
file, default_storage.bucket_name, key, ExtraArgs=extra_args
|
2024-10-07 20:10:42 +02:00
|
|
|
)
|
2024-08-19 22:35:48 +02:00
|
|
|
|
|
|
|
|
return drf_response.Response(
|
|
|
|
|
{"file": f"{settings.MEDIA_URL:s}{key:s}"}, status=status.HTTP_201_CREATED
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-19 22:38:41 +02:00
|
|
|
@decorators.action(detail=False, methods=["get"], url_path="retrieve-auth")
|
|
|
|
|
def retrieve_auth(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
This view is used by an Nginx subrequest to control access to a document's
|
|
|
|
|
attachment file.
|
|
|
|
|
|
|
|
|
|
The original url is passed by nginx in the "HTTP_X_ORIGINAL_URL" header.
|
|
|
|
|
See corresponding ingress configuration in Helm chart and read about the
|
|
|
|
|
nginx.ingress.kubernetes.io/auth-url annotation to understand how the Nginx ingress
|
|
|
|
|
is configured to do this.
|
|
|
|
|
|
|
|
|
|
Based on the original url and the logged in user, we must decide if we authorize Nginx
|
|
|
|
|
to let this request go through (by returning a 200 code) or if we block it (by returning
|
|
|
|
|
a 403 error). Note that we return 403 errors without any further details for security
|
|
|
|
|
reasons.
|
|
|
|
|
|
|
|
|
|
When we let the request go through, we compute authorization headers that will be added to
|
|
|
|
|
the request going through thanks to the nginx.ingress.kubernetes.io/auth-response-headers
|
|
|
|
|
annotation. The request will then be proxied to the object storage backend who will
|
|
|
|
|
respond with the file after checking the signature included in headers.
|
|
|
|
|
"""
|
|
|
|
|
original_url = urlparse(request.META.get("HTTP_X_ORIGINAL_URL"))
|
|
|
|
|
match = MEDIA_URL_PATTERN.search(original_url.path)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
pk, attachment_key = match.groups()
|
|
|
|
|
except AttributeError as excpt:
|
|
|
|
|
raise exceptions.PermissionDenied() from excpt
|
|
|
|
|
|
|
|
|
|
# Check permission
|
|
|
|
|
try:
|
|
|
|
|
document = models.Document.objects.get(pk=pk)
|
|
|
|
|
except models.Document.DoesNotExist as excpt:
|
|
|
|
|
raise exceptions.PermissionDenied() from excpt
|
|
|
|
|
|
|
|
|
|
if not document.get_abilities(request.user).get("retrieve", False):
|
|
|
|
|
raise exceptions.PermissionDenied()
|
|
|
|
|
|
|
|
|
|
# Generate authorization headers and return an authorization to proceed with the request
|
|
|
|
|
request = utils.generate_s3_authorization_headers(f"{pk:s}/{attachment_key:s}")
|
|
|
|
|
return drf_response.Response("authorized", headers=request.headers, status=200)
|
|
|
|
|
|
2024-09-20 22:42:46 +02:00
|
|
|
@decorators.action(
|
|
|
|
|
detail=True,
|
|
|
|
|
methods=["post"],
|
|
|
|
|
name="Apply a transformation action on a piece of text with AI",
|
|
|
|
|
url_path="ai-transform",
|
|
|
|
|
throttle_classes=[utils.AIDocumentRateThrottle, utils.AIUserRateThrottle],
|
|
|
|
|
)
|
|
|
|
|
def ai_transform(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
POST /api/v1.0/documents/<resource_id>/ai-transform
|
|
|
|
|
with expected data:
|
|
|
|
|
- text: str
|
|
|
|
|
- action: str [prompt, correct, rephrase, summarize]
|
|
|
|
|
Return JSON response with the processed text.
|
|
|
|
|
"""
|
|
|
|
|
# Check permissions first
|
|
|
|
|
self.get_object()
|
|
|
|
|
|
|
|
|
|
serializer = serializers.AITransformSerializer(data=request.data)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
|
|
text = serializer.validated_data["text"]
|
|
|
|
|
action = serializer.validated_data["action"]
|
|
|
|
|
|
|
|
|
|
response = AIService().transform(text, action)
|
|
|
|
|
|
|
|
|
|
return drf_response.Response(response, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
@decorators.action(
|
|
|
|
|
detail=True,
|
|
|
|
|
methods=["post"],
|
|
|
|
|
name="Translate a piece of text with AI",
|
|
|
|
|
serializer_class=serializers.AITranslateSerializer,
|
|
|
|
|
url_path="ai-translate",
|
|
|
|
|
throttle_classes=[utils.AIDocumentRateThrottle, utils.AIUserRateThrottle],
|
|
|
|
|
)
|
|
|
|
|
def ai_translate(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
POST /api/v1.0/documents/<resource_id>/ai-translate
|
|
|
|
|
with expected data:
|
|
|
|
|
- text: str
|
|
|
|
|
- language: str [settings.LANGUAGES]
|
|
|
|
|
Return JSON response with the translated text.
|
|
|
|
|
"""
|
|
|
|
|
# Check permissions first
|
|
|
|
|
self.get_object()
|
|
|
|
|
|
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
|
|
text = serializer.validated_data["text"]
|
|
|
|
|
language = serializer.validated_data["language"]
|
|
|
|
|
|
|
|
|
|
response = AIService().translate(text, language)
|
|
|
|
|
|
|
|
|
|
return drf_response.Response(response, status=status.HTTP_200_OK)
|
|
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
|
|
|
|
|
class DocumentAccessViewSet(
|
|
|
|
|
ResourceAccessViewsetMixin,
|
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
API ViewSet for all interactions with document accesses.
|
|
|
|
|
|
|
|
|
|
GET /api/v1.0/documents/<resource_id>/accesses/:<document_access_id>
|
|
|
|
|
Return list of all document accesses related to the logged-in user or one
|
|
|
|
|
document access if an id is provided.
|
|
|
|
|
|
|
|
|
|
POST /api/v1.0/documents/<resource_id>/accesses/ with expected data:
|
|
|
|
|
- user: str
|
2024-05-25 08:15:34 +02:00
|
|
|
- role: str [administrator|editor|reader]
|
2024-04-03 18:50:28 +02:00
|
|
|
Return newly created document access
|
|
|
|
|
|
|
|
|
|
PUT /api/v1.0/documents/<resource_id>/accesses/<document_access_id>/ with expected data:
|
2024-05-25 08:15:34 +02:00
|
|
|
- role: str [owner|admin|editor|reader]
|
2024-04-03 18:50:28 +02:00
|
|
|
Return updated document access
|
|
|
|
|
|
|
|
|
|
PATCH /api/v1.0/documents/<resource_id>/accesses/<document_access_id>/ with expected data:
|
2024-05-25 08:15:34 +02:00
|
|
|
- role: str [owner|admin|editor|reader]
|
2024-04-03 18:50:28 +02:00
|
|
|
Return partially updated document access
|
|
|
|
|
|
|
|
|
|
DELETE /api/v1.0/documents/<resource_id>/accesses/<document_access_id>/
|
|
|
|
|
Delete targeted document access
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
lookup_field = "pk"
|
|
|
|
|
pagination_class = Pagination
|
|
|
|
|
permission_classes = [permissions.IsAuthenticated, permissions.AccessPermission]
|
|
|
|
|
queryset = models.DocumentAccess.objects.select_related("user").all()
|
|
|
|
|
resource_field_name = "document"
|
|
|
|
|
serializer_class = serializers.DocumentAccessSerializer
|
|
|
|
|
|
2024-08-15 15:40:56 +02:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
"""Add a new access to the document and send an email to the new added user."""
|
|
|
|
|
access = serializer.save()
|
|
|
|
|
language = self.request.headers.get("Content-Language", "en-us")
|
2024-10-15 15:53:18 +02:00
|
|
|
|
2024-09-25 12:43:02 +02:00
|
|
|
access.document.email_invitation(
|
2024-10-15 15:53:18 +02:00
|
|
|
language,
|
|
|
|
|
access.user.email,
|
|
|
|
|
access.role,
|
|
|
|
|
self.request.user,
|
2024-09-25 12:43:02 +02:00
|
|
|
)
|
2024-08-15 15:40:56 +02:00
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
|
|
|
|
|
class TemplateViewSet(
|
|
|
|
|
ResourceViewsetMixin,
|
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
|
):
|
|
|
|
|
"""Template ViewSet"""
|
|
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
|
permissions.IsAuthenticatedOrSafe,
|
|
|
|
|
permissions.AccessPermission,
|
|
|
|
|
]
|
|
|
|
|
serializer_class = serializers.TemplateSerializer
|
|
|
|
|
access_model_class = models.TemplateAccess
|
|
|
|
|
resource_field_name = "template"
|
|
|
|
|
queryset = models.Template.objects.all()
|
|
|
|
|
|
2024-09-08 23:37:49 +02:00
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
|
"""Restrict templates returned by the list endpoint"""
|
|
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
|
|
|
|
user = self.request.user
|
|
|
|
|
if user.is_authenticated:
|
|
|
|
|
queryset = queryset.filter(
|
|
|
|
|
Q(accesses__user=user)
|
|
|
|
|
| Q(accesses__team__in=user.teams)
|
|
|
|
|
| Q(is_public=True)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
queryset = queryset.filter(is_public=True)
|
|
|
|
|
|
|
|
|
|
page = self.paginate_queryset(queryset)
|
|
|
|
|
if page is not None:
|
|
|
|
|
serializer = self.get_serializer(page, many=True)
|
|
|
|
|
return self.get_paginated_response(serializer.data)
|
|
|
|
|
|
|
|
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
|
|
|
return drf_response.Response(serializer.data)
|
|
|
|
|
|
2024-04-03 18:50:28 +02:00
|
|
|
@decorators.action(
|
|
|
|
|
detail=True,
|
|
|
|
|
methods=["post"],
|
|
|
|
|
url_path="generate-document",
|
|
|
|
|
permission_classes=[permissions.AccessPermission],
|
|
|
|
|
)
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
def generate_document(self, request, pk=None):
|
|
|
|
|
"""
|
2024-08-07 14:44:18 +02:00
|
|
|
Generate and return a document for this template around the
|
|
|
|
|
body passed as argument.
|
|
|
|
|
|
|
|
|
|
2 types of body are accepted:
|
|
|
|
|
- HTML: body_type = "html"
|
|
|
|
|
- Markdown: body_type = "markdown"
|
|
|
|
|
|
|
|
|
|
2 types of documents can be generated:
|
|
|
|
|
- PDF: format = "pdf"
|
|
|
|
|
- Docx: format = "docx"
|
2024-04-03 18:50:28 +02:00
|
|
|
"""
|
|
|
|
|
serializer = serializers.DocumentGenerationSerializer(data=request.data)
|
|
|
|
|
|
|
|
|
|
if not serializer.is_valid():
|
|
|
|
|
return drf_response.Response(
|
|
|
|
|
serializer.errors, status=status.HTTP_400_BAD_REQUEST
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
body = serializer.validated_data["body"]
|
2024-04-16 10:30:10 +02:00
|
|
|
body_type = serializer.validated_data["body_type"]
|
2024-08-07 14:44:18 +02:00
|
|
|
export_format = serializer.validated_data["format"]
|
2024-04-03 18:50:28 +02:00
|
|
|
|
|
|
|
|
template = self.get_object()
|
2024-08-07 14:44:18 +02:00
|
|
|
return template.generate_document(body, body_type, export_format)
|
2024-04-03 18:50:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateAccessViewSet(
|
|
|
|
|
ResourceAccessViewsetMixin,
|
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
API ViewSet for all interactions with template accesses.
|
|
|
|
|
|
|
|
|
|
GET /api/v1.0/templates/<template_id>/accesses/:<template_access_id>
|
|
|
|
|
Return list of all template accesses related to the logged-in user or one
|
|
|
|
|
template access if an id is provided.
|
|
|
|
|
|
|
|
|
|
POST /api/v1.0/templates/<template_id>/accesses/ with expected data:
|
|
|
|
|
- user: str
|
2024-05-25 08:15:34 +02:00
|
|
|
- role: str [administrator|editor|reader]
|
2024-04-03 18:50:28 +02:00
|
|
|
Return newly created template access
|
|
|
|
|
|
|
|
|
|
PUT /api/v1.0/templates/<template_id>/accesses/<template_access_id>/ with expected data:
|
2024-05-25 08:15:34 +02:00
|
|
|
- role: str [owner|admin|editor|reader]
|
2024-04-03 18:50:28 +02:00
|
|
|
Return updated template access
|
|
|
|
|
|
|
|
|
|
PATCH /api/v1.0/templates/<template_id>/accesses/<template_access_id>/ with expected data:
|
2024-05-25 08:15:34 +02:00
|
|
|
- role: str [owner|admin|editor|reader]
|
2024-04-03 18:50:28 +02:00
|
|
|
Return partially updated template access
|
|
|
|
|
|
|
|
|
|
DELETE /api/v1.0/templates/<template_id>/accesses/<template_access_id>/
|
|
|
|
|
Delete targeted template access
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
lookup_field = "pk"
|
|
|
|
|
pagination_class = Pagination
|
|
|
|
|
permission_classes = [permissions.IsAuthenticated, permissions.AccessPermission]
|
|
|
|
|
queryset = models.TemplateAccess.objects.select_related("user").all()
|
|
|
|
|
resource_field_name = "template"
|
|
|
|
|
serializer_class = serializers.TemplateAccessSerializer
|
2024-05-13 23:31:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvitationViewset(
|
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
|
mixins.DestroyModelMixin,
|
2024-08-16 16:55:00 +02:00
|
|
|
mixins.UpdateModelMixin,
|
2024-05-13 23:31:00 +02:00
|
|
|
viewsets.GenericViewSet,
|
|
|
|
|
):
|
|
|
|
|
"""API ViewSet for user invitations to document.
|
|
|
|
|
|
|
|
|
|
GET /api/v1.0/documents/<document_id>/invitations/:<invitation_id>/
|
|
|
|
|
Return list of invitations related to that document or one
|
|
|
|
|
document access if an id is provided.
|
|
|
|
|
|
|
|
|
|
POST /api/v1.0/documents/<document_id>/invitations/ with expected data:
|
|
|
|
|
- email: str
|
2024-05-25 08:15:34 +02:00
|
|
|
- role: str [administrator|editor|reader]
|
2024-05-13 23:31:00 +02:00
|
|
|
Return newly created invitation (issuer and document are automatically set)
|
|
|
|
|
|
2024-08-16 16:55:00 +02:00
|
|
|
PATCH /api/v1.0/documents/<document_id>/invitations/:<invitation_id>/ with expected data:
|
|
|
|
|
- role: str [owner|admin|editor|reader]
|
|
|
|
|
Return partially updated document invitation
|
2024-05-13 23:31:00 +02:00
|
|
|
|
|
|
|
|
DELETE /api/v1.0/documents/<document_id>/invitations/<invitation_id>/
|
|
|
|
|
Delete targeted invitation
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
lookup_field = "id"
|
|
|
|
|
pagination_class = Pagination
|
2024-10-22 00:28:16 +02:00
|
|
|
permission_classes = [
|
|
|
|
|
permissions.CanCreateInvitationPermission,
|
|
|
|
|
permissions.AccessPermission,
|
|
|
|
|
]
|
2024-05-13 23:31:00 +02:00
|
|
|
queryset = (
|
|
|
|
|
models.Invitation.objects.all()
|
|
|
|
|
.select_related("document")
|
|
|
|
|
.order_by("-created_at")
|
|
|
|
|
)
|
|
|
|
|
serializer_class = serializers.InvitationSerializer
|
|
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
|
"""Extra context provided to the serializer class."""
|
|
|
|
|
context = super().get_serializer_context()
|
|
|
|
|
context["resource_id"] = self.kwargs["resource_id"]
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
"""Return the queryset according to the action."""
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
|
queryset = queryset.filter(document=self.kwargs["resource_id"])
|
|
|
|
|
|
|
|
|
|
if self.action == "list":
|
|
|
|
|
user = self.request.user
|
2024-09-06 16:12:02 +02:00
|
|
|
teams = user.teams
|
2024-05-13 23:31:00 +02:00
|
|
|
|
|
|
|
|
# Determine which role the logged-in user has in the document
|
|
|
|
|
user_roles_query = (
|
|
|
|
|
models.DocumentAccess.objects.filter(
|
|
|
|
|
Q(user=user) | Q(team__in=teams),
|
|
|
|
|
document=self.kwargs["resource_id"],
|
|
|
|
|
)
|
|
|
|
|
.values("document")
|
|
|
|
|
.annotate(roles_array=ArrayAgg("role"))
|
|
|
|
|
.values("roles_array")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
queryset = (
|
2024-10-22 00:28:16 +02:00
|
|
|
# The logged-in user should be administrator or owner to see its accesses
|
2024-05-13 23:31:00 +02:00
|
|
|
queryset.filter(
|
2024-10-22 00:28:16 +02:00
|
|
|
Q(
|
|
|
|
|
document__accesses__user=user,
|
|
|
|
|
document__accesses__role__in=models.PRIVILEGED_ROLES,
|
|
|
|
|
)
|
|
|
|
|
| Q(
|
|
|
|
|
document__accesses__team__in=teams,
|
|
|
|
|
document__accesses__role__in=models.PRIVILEGED_ROLES,
|
|
|
|
|
),
|
2024-05-13 23:31:00 +02:00
|
|
|
)
|
|
|
|
|
# Abilities are computed based on logged-in user's role and
|
|
|
|
|
# the user role on each document access
|
|
|
|
|
.annotate(user_roles=Subquery(user_roles_query))
|
|
|
|
|
.distinct()
|
|
|
|
|
)
|
|
|
|
|
return queryset
|
2024-08-15 15:40:56 +02:00
|
|
|
|
2024-08-15 15:38:38 +02:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
"""Save invitation to a document then send an email to the invited user."""
|
|
|
|
|
invitation = serializer.save()
|
|
|
|
|
|
|
|
|
|
language = self.request.headers.get("Content-Language", "en-us")
|
2024-10-15 15:53:18 +02:00
|
|
|
|
2024-09-25 12:43:02 +02:00
|
|
|
invitation.document.email_invitation(
|
2024-10-15 15:53:18 +02:00
|
|
|
language, invitation.email, invitation.role, self.request.user
|
2024-09-25 12:43:02 +02:00
|
|
|
)
|
2024-11-15 09:29:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigView(views.APIView):
|
|
|
|
|
"""API ViewSet for sharing some public settings."""
|
|
|
|
|
|
|
|
|
|
permission_classes = [AllowAny]
|
|
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
"""
|
|
|
|
|
GET /api/v1.0/config/
|
|
|
|
|
Return a dictionary of public settings.
|
|
|
|
|
"""
|
|
|
|
|
array_settings = [
|
2024-11-15 11:36:40 +01:00
|
|
|
"COLLABORATION_SERVER_URL",
|
2024-11-15 11:43:10 +01:00
|
|
|
"ENVIRONMENT",
|
|
|
|
|
"FRONTEND_THEME",
|
2024-11-15 11:31:09 +01:00
|
|
|
"MEDIA_BASE_URL",
|
2024-11-15 09:29:07 +01:00
|
|
|
"LANGUAGES",
|
|
|
|
|
"LANGUAGE_CODE",
|
|
|
|
|
"SENTRY_DSN",
|
|
|
|
|
]
|
|
|
|
|
dict_settings = {}
|
|
|
|
|
for setting in array_settings:
|
|
|
|
|
if hasattr(settings, setting):
|
|
|
|
|
dict_settings[setting] = getattr(settings, setting)
|
|
|
|
|
|
|
|
|
|
return drf_response.Response(dict_settings)
|