Skip to content
Snippets Groups Projects
Commit f444f3a6 authored by Benjamin Hättasch's avatar Benjamin Hättasch
Browse files

Introduce intermediate page for plan publishing

Use action with intermediate page instead of direct action for publishing and unpublishing of plans
This allows to create a link on the events detail page and on the status page to change the plans visibility
Add link to detail view
Visualize plan visibility on status page and allow to toggle it from there
This implements the final function of #159
parent 0bc73445
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,6 @@ from django.apps import apps ...@@ -3,7 +3,6 @@ from django.apps import apps
from django.contrib import admin, messages from django.contrib import admin, messages
from django.contrib.admin import SimpleListFilter, RelatedFieldListFilter, action, display from django.contrib.admin import SimpleListFilter, RelatedFieldListFilter, action, display
from django.db.models import Count, F from django.db.models import Count, F
from django.db.models.functions import Now
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.urls import reverse_lazy, path from django.urls import reverse_lazy, path
...@@ -20,7 +19,7 @@ from AKModel.models import Event, AKOwner, AKCategory, AKTrack, AKTag, AKRequire ...@@ -20,7 +19,7 @@ from AKModel.models import Event, AKOwner, AKCategory, AKTrack, AKTag, AKRequire
ConstraintViolation ConstraintViolation
from AKModel.urls import get_admin_urls_event_wizard, get_admin_urls_event from AKModel.urls import get_admin_urls_event_wizard, get_admin_urls_event
from AKModel.views import CVMarkResolvedView, CVSetLevelViolationView, CVSetLevelWarningView, AKResetInterestView, \ from AKModel.views import CVMarkResolvedView, CVSetLevelViolationView, CVSetLevelWarningView, AKResetInterestView, \
AKResetInterestCounterView AKResetInterestCounterView, PlanPublishView, PlanUnpublishView
class EventRelatedFieldListFilter(RelatedFieldListFilter): class EventRelatedFieldListFilter(RelatedFieldListFilter):
...@@ -39,7 +38,7 @@ class EventAdmin(admin.ModelAdmin): ...@@ -39,7 +38,7 @@ class EventAdmin(admin.ModelAdmin):
list_filter = ['active'] list_filter = ['active']
list_editable = ['active'] list_editable = ['active']
ordering = ['-start'] ordering = ['-start']
readonly_fields = ['status_url', 'plan_hidden', 'plan_published_at'] readonly_fields = ['status_url', 'plan_hidden', 'plan_published_at', 'toggle_plan_visibility']
actions = ['publish', 'unpublish'] actions = ['publish', 'unpublish']
def add_view(self, request, form_url='', extra_context=None): def add_view(self, request, form_url='', extra_context=None):
...@@ -53,6 +52,10 @@ class EventAdmin(admin.ModelAdmin): ...@@ -53,6 +52,10 @@ class EventAdmin(admin.ModelAdmin):
if apps.is_installed("AKScheduling"): if apps.is_installed("AKScheduling"):
from AKScheduling.urls import get_admin_urls_scheduling from AKScheduling.urls import get_admin_urls_scheduling
urls.extend(get_admin_urls_scheduling(self.admin_site)) urls.extend(get_admin_urls_scheduling(self.admin_site))
urls.extend([
path('plan/publish/', PlanPublishView.as_view(), name="plan-publish"),
path('plan/unpublish/', PlanUnpublishView.as_view(), name="plan-unpublish"),
])
urls.extend(super().get_urls()) urls.extend(super().get_urls())
return urls return urls
...@@ -61,6 +64,16 @@ class EventAdmin(admin.ModelAdmin): ...@@ -61,6 +64,16 @@ class EventAdmin(admin.ModelAdmin):
return format_html("<a href='{url}'>{text}</a>", return format_html("<a href='{url}'>{text}</a>",
url=reverse_lazy('admin:event_status', kwargs={'slug': obj.slug}), text=_("Status")) url=reverse_lazy('admin:event_status', kwargs={'slug': obj.slug}), text=_("Status"))
@display(description=_("Toggle plan visibility"))
def toggle_plan_visibility(self, obj):
if obj.plan_hidden:
url = f"{reverse_lazy('admin:plan-publish')}?pks={obj.pk}"
text = _('Publish plan')
else:
url = f"{reverse_lazy('admin:plan-unpublish')}?pks={obj.pk}"
text = _('Unpublish plan')
return format_html("<a href='{url}'>{text}</a>", url=url, text=text)
def get_form(self, request, obj=None, change=False, **kwargs): def get_form(self, request, obj=None, change=False, **kwargs):
# Use timezone of event # Use timezone of event
timezone.activate(obj.timezone) timezone.activate(obj.timezone)
...@@ -68,13 +81,13 @@ class EventAdmin(admin.ModelAdmin): ...@@ -68,13 +81,13 @@ class EventAdmin(admin.ModelAdmin):
@action(description=_('Publish plan')) @action(description=_('Publish plan'))
def publish(self, request, queryset): def publish(self, request, queryset):
queryset.update(plan_published_at=Now(), plan_hidden=False) selected = queryset.values_list('pk', flat=True)
self.message_user(request, _('Plan published'), messages.SUCCESS) return HttpResponseRedirect(f"{reverse_lazy('admin:plan-publish')}?pks={','.join(str(pk) for pk in selected)}")
@action(description=_('Unpublish plan')) @action(description=_('Unpublish plan'))
def unpublish(self, request, queryset): def unpublish(self, request, queryset):
queryset.update(plan_published_at=None, plan_hidden=True) selected = queryset.values_list('pk', flat=True)
self.message_user(request, _('Plan unpublished'), messages.SUCCESS) return HttpResponseRedirect(f"{reverse_lazy('admin:plan-unpublish')}?pks={','.join(str(pk) for pk in selected)}")
@admin.register(AKOwner) @admin.register(AKOwner)
......
This diff is collapsed.
...@@ -11,6 +11,13 @@ ...@@ -11,6 +11,13 @@
<h2><a href="{% url 'admin:AKModel_event_change' event.pk %}">{{event}}</a></h2> <h2><a href="{% url 'admin:AKModel_event_change' event.pk %}">{{event}}</a></h2>
<h5>{{ event.start }} - {{ event.end }}</h5> <h5>{{ event.start }} - {{ event.end }}</h5>
<div class="custom-control custom-switch mt-2 mb-2">
<input type="checkbox" class="custom-control-input" id="planPublishedSwitch"
{% if not event.plan_hidden %}checked{% endif %}
onclick="location.href='{% if event.plan_hidden %}{% url 'admin:plan-publish' %}{% else %}{% url 'admin:plan-unpublish' %}{% endif %}?pks={{event.pk}}';">
<label class="custom-control-label" for="planPublishedSwitch">{% trans "Plan published?" %}</label>
</div>
<div class="row"> <div class="row">
<div class="col-md-8"> <div class="col-md-8">
<h3 class="block-header">{% trans "Categories" %}</h3> <h3 class="block-header">{% trans "Categories" %}</h3>
......
...@@ -4,6 +4,7 @@ from abc import ABC, abstractmethod ...@@ -4,6 +4,7 @@ from abc import ABC, abstractmethod
from itertools import zip_longest from itertools import zip_longest
from django.contrib import admin, messages from django.contrib import admin, messages
from django.db.models.functions import Now
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy, reverse from django.urls import reverse_lazy, reverse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
...@@ -474,3 +475,23 @@ class AKResetInterestCounterView(IntermediateAdminActionView): ...@@ -474,3 +475,23 @@ class AKResetInterestCounterView(IntermediateAdminActionView):
def action(self, form): def action(self, form):
self.entities.update(interest_counter=0) self.entities.update(interest_counter=0)
class PlanPublishView(IntermediateAdminActionView):
title = _('Publish plan')
model = Event
confirmation_message = _('Publish the plan(s) of:')
success_message = _('Plan published')
def action(self, form):
self.entities.update(plan_published_at=Now(), plan_hidden=False)
class PlanUnpublishView(IntermediateAdminActionView):
title = _('Unpublish plan')
model = Event
confirmation_message = _('Unpublish the plan(s) of:')
success_message = _('Plan unpublished')
def action(self, form):
self.entities.update(plan_published_at=None, plan_hidden=True)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment