Skip to content

Commit

Permalink
backoffice: add data app
Browse files Browse the repository at this point in the history
  • Loading branch information
DonHaul committed Nov 27, 2024
1 parent 2a81f19 commit 84a0793
Show file tree
Hide file tree
Showing 43 changed files with 1,446 additions and 441 deletions.
78 changes: 4 additions & 74 deletions backoffice/backoffice/authors/admin.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,16 @@
from django.contrib import admin
from django.db.models import JSONField
from django_json_widget.widgets import JSONEditorWidget

from backoffice.management.permissions import IsAdminOrCuratorUser
from backoffice.authors.models import (
AuthorDecision,
AuthorWorkflow,
AuthorWorkflowTicket,
)


class AuthorWorkflowsAdminSite(admin.AdminSite):
"""
Custom admin site for managing workflows.
This admin site extends the default Django admin site to include additional
functionality for managing workflows. It checks whether the user has the
necessary permissions to access the admin site by verifying that they are
an active user and either a superuser or a member of the 'curator' group.
Attributes:
None
Methods:
has_permission(request): Checks whether the user has permission to access
the admin site.
"""

def has_permission(self, request):
return request.user.is_active and (
request.user.is_superuser
or request.user.groups.filter(name="curator").exists()
)
from backoffice.common.admin import WorkflowsAdminSite, WorkflowAdmin, BaseModelAdmin


class BaseModelAdmin(admin.ModelAdmin):
def has_view_permission(self, request, obj=None):
"""
Returns True if the user has permission to view the Workflow model.
"""
permission_check = IsAdminOrCuratorUser()
return request.user.is_superuser or permission_check.has_permission(
request, self
)

def has_change_permission(self, request, obj=None):
"""
Returns True if the user has permission to change the Workflow model.
"""
permission_check = IsAdminOrCuratorUser()
return request.user.is_superuser or permission_check.has_permission(
request, self
)

def has_delete_permission(self, request, obj=None):
"""
Returns True if the user has permission to delete the Workflow model.
"""
permission_check = IsAdminOrCuratorUser()
return request.user.is_superuser or permission_check.has_permission(
request, self
)

formfield_overrides = {
JSONField: {"widget": JSONEditorWidget},
}
class AuthorWorkflowsAdminSite(WorkflowsAdminSite):
pass


class AuthorWorkflowsDecisionsInline(admin.StackedInline):
Expand Down Expand Up @@ -94,27 +40,11 @@ def has_change_permission(self, request, obj=None):


@admin.register(AuthorWorkflow)
class WorkflowAdmin(BaseModelAdmin):
class AuthorWorkflowAdmin(WorkflowAdmin):
"""
Admin class for Workflow model. Define get, update and delete permissions.
"""

ordering = ("-_updated_at",)
search_fields = ["id", "data"]
list_display = (
"id",
"workflow_type",
"status",
"_created_at",
"_updated_at",
)
list_filter = [
"workflow_type",
"status",
"_created_at",
"_updated_at",
]

inlines = [AuthorWorkflowsDecisionsInline, AuthorWorkflowTicketsInline]


Expand Down
19 changes: 6 additions & 13 deletions backoffice/backoffice/authors/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
from drf_spectacular.utils import OpenApiExample, extend_schema_serializer
from inspire_schemas.utils import get_validation_errors
from rest_framework import serializers
from backoffice.authors.api import utils
from backoffice.authors.constants import DECISION_CHOICES, StatusChoices, WorkflowType
from backoffice.authors.constants import DECISION_CHOICES, WorkflowType
from backoffice.common.constants import StatusChoices
from backoffice.authors.documents import AuthorWorkflowDocument
from backoffice.authors.models import (
AuthorDecision,
AuthorWorkflow,
AuthorWorkflowTicket,
)

from backoffice.common.serializers import AbstractWorkflowSerializer


class AuthorWorkflowTicketSerializer(serializers.ModelSerializer):
ticket_url = serializers.SerializerMethodField()
Expand Down Expand Up @@ -57,11 +58,9 @@ class Meta:
),
],
)
class AuthorWorkflowSerializer(serializers.ModelSerializer):
class AuthorWorkflowSerializer(AbstractWorkflowSerializer):
tickets = AuthorWorkflowTicketSerializer(many=True, read_only=True)
decisions = AuthorDecisionSerializer(many=True, read_only=True)
validation_errors = serializers.JSONField(required=False)
data = serializers.JSONField(required=True)
workflow_type = serializers.ChoiceField(
choices=[
WorkflowType.AUTHOR_CREATE,
Expand All @@ -71,13 +70,7 @@ class AuthorWorkflowSerializer(serializers.ModelSerializer):
)

def validate_data(self, value):
validation_errors = list(get_validation_errors(value, schema="authors"))
if validation_errors:
validation_errors_msg = utils.render_validation_error_response(
validation_errors
)
raise serializers.ValidationError(validation_errors_msg)
return value
return super().validate_data(value, schema="authors")

class Meta:
model = AuthorWorkflow
Expand Down
Loading

0 comments on commit 84a0793

Please sign in to comment.