from django.contrib import admin

from events import models
from pizzas.models import FoodEvent
from promotion.models import PromotionRequest

from .forms import RegistrationInformationFieldForm


class RegistrationInformationFieldInline(admin.TabularInline):
    """The inline for registration information fields in the Event admin."""

    form = RegistrationInformationFieldForm
    extra = 0
    model = models.RegistrationInformationField
    ordering = ("_order",)

    radio_fields = {"type": admin.VERTICAL}

    def get_formset(self, request, obj=None, **kwargs):
        formset = super().get_formset(request, obj, **kwargs)
        if obj is not None:
            count = obj.registrationinformationfield_set.count()
            formset.form.declared_fields["order"].initial = count
        return formset


class PizzaEventInline(admin.StackedInline):
    """The inline for pizza events in the Event admin."""

    model = FoodEvent
    extra = 0
    max_num = 1


class PromotionRequestInline(admin.StackedInline):
    model = PromotionRequest
    readonly_fields = (
        "assigned_to",
        "status",
        "drive_folder",
    )
    extra = 0

website/events/admin/inlines.py
from django.contrib import admin

from events import models
from pizzas.models import FoodEvent
from promotion.models import PromotionRequest

from .forms import RegistrationInformationFieldForm


class RegistrationInformationFieldInline(admin.TabularInline):
    """The inline for registration information fields in the Event admin."""

    form = RegistrationInformationFieldForm
    extra = 0
    model = models.RegistrationInformationField
    ordering = ("_order",)

    radio_fields = {"type": admin.VERTICAL}

    def get_formset(self, request, obj=None, **kwargs):
        formset = super().get_formset(request, obj, **kwargs)
        if obj is not None:
            count = obj.registrationinformationfield_set.count()
            formset.form.declared_fields["order"].initial = count
        return formset


class PizzaEventInline(admin.StackedInline):
    """The inline for pizza events in the Event admin."""

    model = FoodEvent
    extra = 0
    max_num = 1


class PromotionRequestInline(admin.StackedInline):
    model = PromotionRequest
    readonly_fields = (
        "assigned_to",
        "status",
        "drive_folder",
        "status_updated",
    )

    def has_add_permission(self, request, obj=None):
        return True

    def has_view_permission(self, request, obj=None):
        return True

    def has_change_permission(self, request, obj=None):
        return True

    def has_delete_permission(self, request, obj=None):
        return True

    extra = 0

website/events/admin/inlines.py
diff --git a/website/events/admin/inlines.py b/website/events/admin/inlines.py
index 2d3f80210..b47b3a473 100644
--- a/website/events/admin/inlines.py
+++ b/website/events/admin/inlines.py
@@ -39,5 +39,19 @@ class PromotionRequestInline(admin.StackedInline):
         "assigned_to",
         "status",
         "drive_folder",
+        "status_updated",
     )
+
+    def has_add_permission(self, request, obj=None):
+        return True
+
+    def has_view_permission(self, request, obj=None):
+        return True
+
+    def has_change_permission(self, request, obj=None):
+        return True
+
+    def has_delete_permission(self, request, obj=None):
+        return True
+
     extra = 0

Separate promotion permissions in eventadmin inline from the main promotion perms
### What?
Currently, people need add/change_promotionrequest permission to make promotionrequests for their events. But with this permission they also get full access to all other promotionrequests. So we should make the inline in the eventadmin bypass that check. For all I care, anyone who can change an event can make a promorquest from the eventadmin (by virtue of their 'change_event' permission and being an organizer of the event), without having the add/change_promotionrequest permission, and thus without seeing the main Promotion Requests changelist page.

### Why?
<!-- A clear and concise motivation why we should consider implementing this. -->
Least privilege principle: many people should be allowed to request promotion for their own events, but don't need to be able to edit unrelated requests. And this way we can have promocie be able to bypass the requirements in #3529, without normal organizers being able to do the same.

### How?
Override has_xxx_permission() on the inline class. Read the inlinemodeladmin docs for guidance.

