Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • konstantin/akplanning
  • matedealer/akplanning
  • kif/akplanning
  • mirco/akplanning
  • lordofthevoid/akplanning
  • voidptr/akplanning
  • xayomer/akplanning-fork
  • mollux/akplanning
  • neumantm/akplanning
  • mmarx/akplanning
  • nerf/akplanning
  • felix_bonn/akplanning
  • sebastian.uschmann/akplanning
13 results
Show changes
Commits on Source (253)
Showing
with 799 additions and 252 deletions
uwsgi==2.0.19.1 uwsgi==2.0.28
image: python:3.9 image: python:3.11
services: services:
- mysql:5.7 - mysql
variables: variables:
MYSQL_DATABASE: "test" MYSQL_DATABASE: "test"
...@@ -14,14 +14,80 @@ cache: ...@@ -14,14 +14,80 @@ cache:
paths: paths:
- ~/.cache/pip/ - ~/.cache/pip/
before_script: .before_script_template:
- python -V # Print out python version for debugging before_script:
- apt-get -qq update - python -V # Print out python version for debugging
- apt-get -qq install -y python3-virtualenv python3 python3-dev python3-pip gettext default-libmysqlclient-dev - apt-get -qq update
- export DJANGO_SETTINGS_MODULE=AKPlanning.settings_ci - apt-get -qq install -y python3-virtualenv python3 python3-dev python3-pip gettext default-mysql-client default-libmysqlclient-dev
- ./Utils/setup.sh --prod - ./Utils/setup.sh --ci
- mkdir -p public/badges public/lint
- echo undefined > public/badges/$CI_JOB_NAME.score
- source venv/bin/activate
- pip install pylint-gitlab pylint-django
- mysql --version
migrations:
extends: .before_script_template
script:
- source venv/bin/activate
- ./manage.py makemigrations --dry-run --check
test: test:
extends: .before_script_template
script: script:
- source venv/bin/activate - source venv/bin/activate
- python manage.py test --settings AKPlanning.settings_ci --keepdb - echo "GRANT ALL on *.* to '${MYSQL_USER}';"| mysql -u root --password="${MYSQL_ROOT_PASSWORD}" -h mysql
- pip install pytest-cov unittest-xml-reporting
- coverage run --source='.' manage.py test --settings AKPlanning.settings_ci
after_script:
- source venv/bin/activate
- coverage report
- coverage xml
coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
junit: unit.xml
lint:
extends: .before_script_template
stage: test
script:
- pylint --load-plugins pylint_django --django-settings-module=AKPlanning.settings_ci --rcfile pylintrc --exit-zero --output-format=text AK* | tee /tmp/pylint.txt
- sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' /tmp/pylint.txt > public/badges/$CI_JOB_NAME.score
- pylint --load-plugins pylint_django --django-settings-module=AKPlanning.settings_ci --rcfile pylintrc --exit-zero --output-format=pylint_gitlab.GitlabCodeClimateReporter AK* > codeclimate.json
- pylint --load-plugins pylint_django --django-settings-module=AKPlanning.settings_ci --rcfile pylintrc --exit-zero --output-format=pylint_gitlab.GitlabPagesHtmlReporter AK* > public/lint/index.html
after_script:
- |
echo "Linting score: $(cat public/badges/$CI_JOB_NAME.score)"
artifacts:
paths:
- public
reports:
codequality: codeclimate.json
when: always
doc:
extends: .before_script_template
stage: test
script:
- cd docs
- make html
- cd ..
artifacts:
paths:
- docs/_build/html
pages:
stage: deploy
image: alpine:latest
script:
- echo
artifacts:
paths:
- public
only:
refs:
- main
...@@ -4,6 +4,9 @@ from AKDashboard.models import DashboardButton ...@@ -4,6 +4,9 @@ from AKDashboard.models import DashboardButton
@admin.register(DashboardButton) @admin.register(DashboardButton)
class DashboardButtonAdmin(admin.ModelAdmin): class DashboardButtonAdmin(admin.ModelAdmin):
"""
Admin interface for dashboard buttons
"""
list_display = ['text', 'url', 'event'] list_display = ['text', 'url', 'event']
list_filter = ['event'] list_filter = ['event']
search_fields = ['text', 'url'] search_fields = ['text', 'url']
......
...@@ -2,4 +2,7 @@ from django.apps import AppConfig ...@@ -2,4 +2,7 @@ from django.apps import AppConfig
class AkdashboardConfig(AppConfig): class AkdashboardConfig(AppConfig):
"""
App configuration for dashboard (default)
"""
name = 'AKDashboard' name = 'AKDashboard'
[
{
"model": "AKDashboard.dashboardbutton",
"pk": 1,
"fields": {
"text": "Wiki",
"url": "http://wiki.kif.rocks",
"icon": "fab,wikipedia-w",
"color": 2,
"event": 2
}
}
]
...@@ -8,7 +8,7 @@ msgid "" ...@@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-10-29 13:36+0000\n" "POT-Creation-Date: 2025-01-01 17:28+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
...@@ -17,106 +17,118 @@ msgstr "" ...@@ -17,106 +17,118 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
#: AKDashboard/models.py:10 #: AKDashboard/models.py:21
msgid "Dashboard Button" msgid "Dashboard Button"
msgstr "Dashboard-Button" msgstr "Dashboard-Button"
#: AKDashboard/models.py:11 #: AKDashboard/models.py:22
msgid "Dashboard Buttons" msgid "Dashboard Buttons"
msgstr "Dashboard-Buttons" msgstr "Dashboard-Buttons"
#: AKDashboard/models.py:21 #: AKDashboard/models.py:32
msgid "Text" msgid "Text"
msgstr "Text" msgstr "Text"
#: AKDashboard/models.py:22 #: AKDashboard/models.py:33
msgid "Text that will be shown on the button" msgid "Text that will be shown on the button"
msgstr "Text, der auf dem Button angezeigt wird" msgstr "Text, der auf dem Button angezeigt wird"
#: AKDashboard/models.py:23 #: AKDashboard/models.py:34
msgid "Link URL" msgid "Link URL"
msgstr "Link-URL" msgstr "Link-URL"
#: AKDashboard/models.py:23 #: AKDashboard/models.py:34
msgid "URL this button links to" msgid "URL this button links to"
msgstr "URL auf die der Button verweist" msgstr "URL auf die der Button verweist"
#: AKDashboard/models.py:24 #: AKDashboard/models.py:35
msgid "Icon" msgid "Icon"
msgstr "Symbol" msgstr "Symbol"
#: AKDashboard/models.py:26 #: AKDashboard/models.py:37
msgid "Button Style" msgid "Button Style"
msgstr "Stil des Buttons" msgstr "Stil des Buttons"
#: AKDashboard/models.py:26 #: AKDashboard/models.py:37
msgid "Style (Color) of this button (bootstrap class)" msgid "Style (Color) of this button (bootstrap class)"
msgstr "Stiel (Farbe) des Buttons (Bootstrap-Klasse)" msgstr "Stiel (Farbe) des Buttons (Bootstrap-Klasse)"
#: AKDashboard/models.py:28 #: AKDashboard/models.py:39
msgid "Event" msgid "Event"
msgstr "Veranstaltung" msgstr "Veranstaltung"
#: AKDashboard/models.py:28 #: AKDashboard/models.py:39
msgid "Event this button belongs to" msgid "Event this button belongs to"
msgstr "Veranstaltung, zu der dieser Button gehört" msgstr "Veranstaltung, zu der dieser Button gehört"
#: AKDashboard/templates/AKDashboard/dashboard.html:25 #: AKDashboard/templates/AKDashboard/dashboard.html:18
#: AKDashboard/templates/AKDashboard/dashboard_event.html:37 #: AKDashboard/templates/AKDashboard/dashboard_event.html:29
#: AKDashboard/templates/AKDashboard/dashboard_row_old_event.html:53
msgid "Write to organizers of this event for questions and comments" msgid "Write to organizers of this event for questions and comments"
msgstr "" msgstr ""
"Kontaktiere die Organisator*innen des Events bei Fragen oder Kommentaren" "Kontaktiere die Organisator*innen des Events bei Fragen oder Kommentaren"
#: AKDashboard/templates/AKDashboard/dashboard.html:32 #: AKDashboard/templates/AKDashboard/dashboard.html:24
msgid "Old events"
msgstr "Frühere Veranstaltungen"
#: AKDashboard/templates/AKDashboard/dashboard.html:34
msgid "Currently, there are no Events!" msgid "Currently, there are no Events!"
msgstr "Aktuell gibt es keine Events!" msgstr "Aktuell gibt es keine Events!"
#: AKDashboard/templates/AKDashboard/dashboard.html:35 #: AKDashboard/templates/AKDashboard/dashboard.html:37
msgid "Please contact an administrator if you want to use AKPlanning." msgid "Please contact an administrator if you want to use AKPlanning."
msgstr "" msgstr ""
"Bitte kontaktiere eine*n Administrator*in, falls du AKPlanning verwenden " "Bitte kontaktiere eine*n Administrator*in, falls du AKPlanning verwenden "
"möchtest." "möchtest."
#: AKDashboard/templates/AKDashboard/dashboard_event.html:27 #: AKDashboard/templates/AKDashboard/dashboard_event.html:19
msgid "Recent" msgid "Recent"
msgstr "Kürzlich" msgstr "Kürzlich"
#: AKDashboard/templates/AKDashboard/dashboard_row.html:12 #: AKDashboard/templates/AKDashboard/dashboard_row.html:18
#: AKDashboard/templates/AKDashboard/dashboard_row_old_event.html:20
msgid "AK List" msgid "AK List"
msgstr "AK-Liste" msgstr "AK-Liste"
#: AKDashboard/templates/AKDashboard/dashboard_row.html:23 #: AKDashboard/templates/AKDashboard/dashboard_row.html:29
msgid "Current AKs" msgid "Current AKs"
msgstr "Aktuelle AKs" msgstr "Aktuelle AKs"
#: AKDashboard/templates/AKDashboard/dashboard_row.html:30 #: AKDashboard/templates/AKDashboard/dashboard_row.html:36
msgid "AK Wall" msgid "AK Wall"
msgstr "AK-Wall" msgstr "AK-Wall"
#: AKDashboard/templates/AKDashboard/dashboard_row.html:38 #: AKDashboard/templates/AKDashboard/dashboard_row.html:44
#: AKDashboard/templates/AKDashboard/dashboard_row_old_event.html:30
msgid "Schedule" msgid "Schedule"
msgstr "AK-Plan" msgstr "AK-Plan"
#: AKDashboard/templates/AKDashboard/dashboard_row.html:49 #: AKDashboard/templates/AKDashboard/dashboard_row.html:55
msgid "AK Submission" msgid "AK Submission"
msgstr "AK-Einreichung" msgstr "AK-Einreichung"
#: AKDashboard/views.py:42 #: AKDashboard/templates/AKDashboard/dashboard_row.html:63
#: AKDashboard/templates/AKDashboard/dashboard_row_old_event.html:39
msgid "AK History"
msgstr "AK-Verlauf"
#: AKDashboard/views.py:69
#, python-format #, python-format
msgid "New AK: %(ak)s." msgid "New AK: %(ak)s."
msgstr "Neuer AK: %(ak)s." msgstr "Neuer AK: %(ak)s."
#: AKDashboard/views.py:45 #: AKDashboard/views.py:72
#, python-format #, python-format
msgid "AK \"%(ak)s\" edited." msgid "AK \"%(ak)s\" edited."
msgstr "AK \"%(ak)s\" bearbeitet." msgstr "AK \"%(ak)s\" bearbeitet."
#: AKDashboard/views.py:48 #: AKDashboard/views.py:75
#, python-format #, python-format
msgid "AK \"%(ak)s\" deleted." msgid "AK \"%(ak)s\" deleted."
msgstr "AK \"%(ak)s\" gelöscht." msgstr "AK \"%(ak)s\" gelöscht."
#: AKDashboard/views.py:61 #: AKDashboard/views.py:90
#, python-format #, python-format
msgid "AK \"%(ak)s\" (re-)scheduled." msgid "AK \"%(ak)s\" (re-)scheduled."
msgstr "AK \"%(ak)s\" (um-)geplant." msgstr "AK \"%(ak)s\" (um-)geplant."
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import fontawesome_5.fields import fontawesome_6.fields
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -20,7 +20,7 @@ class Migration(migrations.Migration): ...@@ -20,7 +20,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.CharField(help_text='Text that will be shown on the button', max_length=50, verbose_name='Text')), ('text', models.CharField(help_text='Text that will be shown on the button', max_length=50, verbose_name='Text')),
('url', models.URLField(help_text='URL this button links to', verbose_name='Link URL')), ('url', models.URLField(help_text='URL this button links to', verbose_name='Link URL')),
('icon', fontawesome_5.fields.IconField(blank=True, default='external-link-alt', help_text='Symbol represeting this button.', max_length=60, verbose_name='Icon')), ('icon', fontawesome_6.fields.IconField(blank=True, default='external-link-alt', help_text='Symbol represeting this button.', max_length=60, verbose_name='Icon')),
('color', models.PositiveSmallIntegerField(choices=[(0, 'primary'), (1, 'success'), (2, 'info'), (3, 'warning'), (4, 'danger')], default=0, help_text='Style (Color) of this button (bootstrap class)', verbose_name='Button Style')), ('color', models.PositiveSmallIntegerField(choices=[(0, 'primary'), (1, 'success'), (2, 'info'), (3, 'warning'), (4, 'danger')], default=0, help_text='Style (Color) of this button (bootstrap class)', verbose_name='Button Style')),
('event', models.ForeignKey(help_text='Event this button belongs to', on_delete=django.db.models.deletion.CASCADE, to='AKModel.Event', verbose_name='Event')), ('event', models.ForeignKey(help_text='Event this button belongs to', on_delete=django.db.models.deletion.CASCADE, to='AKModel.Event', verbose_name='Event')),
], ],
......
# Generated by Django 3.2.16 on 2023-01-03 16:50
from django.db import migrations
import fontawesome_6.fields
class Migration(migrations.Migration):
dependencies = [
('AKDashboard', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='dashboardbutton',
name='icon',
field=fontawesome_6.fields.IconField(blank=True, default='external-link-alt', help_text='Symbol represeting this button.', max_length=60, verbose_name='Icon'),
),
]
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from fontawesome_5.fields import IconField from fontawesome_6.fields import IconField
from AKModel.models import Event from AKModel.models import Event
class DashboardButton(models.Model): class DashboardButton(models.Model):
"""
Model for a single dashboard button
Allows to specify
* a text (currently without possibility to translate),
* a color (based on predefined design colors)
* a url the button should point to (internal or external)
* an icon (from the collection of fontawesome)
Each button is associated with a single event and will be deleted when the event is deleted.
"""
class Meta: class Meta:
verbose_name = _("Dashboard Button") verbose_name = _("Dashboard Button")
verbose_name_plural = _("Dashboard Buttons") verbose_name_plural = _("Dashboard Buttons")
......
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
margin-bottom: 5em; margin-bottom: 5em;
} }
.dashboard-row-small {
margin-bottom: 3em;
}
.dashboard-row > .row { .dashboard-row > .row {
margin-left: 0; margin-left: 0;
padding-bottom: 1em; padding-bottom: 1em;
...@@ -18,7 +22,6 @@ ...@@ -18,7 +22,6 @@
} }
.dashboard-box { .dashboard-box {
display: block;
padding: 2em; padding: 2em;
margin-right: 1em; margin-right: 1em;
min-width: 15em; min-width: 15em;
......
{% extends 'base.html' %} {% extends 'base.html' %}
{% load fontawesome_5 %} {% load fontawesome_6 %}
{% load i18n %} {% load i18n %}
{% load static %} {% load static %}
{% block imports %}
{{ block.super }}
<link rel="stylesheet" href="{% static 'AKDashboard/style.css' %}">
{% endblock %}
{% block breadcrumbs %} {% block breadcrumbs %}
<li class="breadcrumb-item">AKPlanning</li> <li class="breadcrumb-item">AKPlanning</li>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
{% for event in events %} {% if total_event_count > 0 %}
<div class="dashboard-row"> {% for event in active_and_current_events %}
{% include "AKDashboard/dashboard_row.html" %} <div class="dashboard-row">
{% if event.contact_email %} {% include "AKDashboard/dashboard_row.html" %}
<p> {% if event.contact_email %}
<a href="mailto:{{ event.contact_email }}">{% fa5_icon "envelope" "fas" %} {% trans "Write to organizers of this event for questions and comments" %}</a> <p>
</p> <a href="mailto:{{ event.contact_email }}">{% fa6_icon "envelope" "fas" %} {% trans "Write to organizers of this event for questions and comments" %}</a>
{% endif %} </p>
</div> {% endif %}
{% empty %} </div>
{% endfor %}
{% if old_event_count > 0 %}
<h2 class="mb-3">{% trans "Old events" %}</h2>
{% for event in old_events %}
<div class="dashboard-row-small">
{% include "AKDashboard/dashboard_row_old_event.html" %}
</div>
{% endfor %}
{% endif %}
{% else %}
<div class="jumbotron"> <div class="jumbotron">
<h2 class="display-4"> <h2 class="display-4">
{% trans 'Currently, there are no Events!' %} {% trans 'Currently, there are no Events!' %}
...@@ -35,5 +37,5 @@ ...@@ -35,5 +37,5 @@
{% trans 'Please contact an administrator if you want to use AKPlanning.' %} {% trans 'Please contact an administrator if you want to use AKPlanning.' %}
</p> </p>
</div> </div>
{% endfor %} {% endif %}
{% endblock %} {% endblock %}
{% extends 'base.html' %} {% extends 'base.html' %}
{% load fontawesome_5 %} {% load fontawesome_6 %}
{% load i18n %} {% load i18n %}
{% load static %} {% load static %}
{% load tags_AKModel %} {% load tags_AKModel %}
{% load tz %} {% load tz %}
{% block imports %}
{{ block.super }}
<link rel="stylesheet" href="{% static 'AKDashboard/style.css' %}">
{% endblock %}
{% block breadcrumbs %} {% block breadcrumbs %}
<li class="breadcrumb-item"><a href="{% url 'dashboard:dashboard' %}">AKPlanning</a></li> <li class="breadcrumb-item"><a href="{% url 'dashboard:dashboard' %}">AKPlanning</a></li>
<li class="breadcrumb-item active">{{ event }}</li> <li class="breadcrumb-item active">{{ event }}</li>
...@@ -24,17 +16,17 @@ ...@@ -24,17 +16,17 @@
{% include "AKDashboard/dashboard_row.html" %} {% include "AKDashboard/dashboard_row.html" %}
{% if recent_changes|length > 0 %} {% if recent_changes|length > 0 %}
<h3 class="mt-1">{% trans "Recent" %}:</h3> <h3 class="mt-1" id="history">{% trans "Recent" %}:</h3>
<ul id="recent-changes-list"> <ul id="recent-changes-list">
{% for recent in recent_changes %} {% for recent in recent_changes %}
<li><a href="{{ recent.link }}">{% fa5_icon recent.icon.0 recent.icon.1 %} {{ recent.text }}</a> <span style="color: #999999;">{{ recent.timestamp | timezone:event.timezone | date:"d.m. H:i" }}</span></li> <li><a href="{{ recent.link }}">{% fa6_icon recent.icon.0 recent.icon.1 %} {{ recent.text }}</a> <span style="color: #999999;">{{ recent.timestamp | timezone:event.timezone | date:"d.m. H:i" }}</span></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% if event.contact_email %} {% if event.contact_email %}
<p> <p>
<a href="mailto:{{ event.contact_email }}">{% fa5_icon "envelope" "fas" %} {% trans "Write to organizers of this event for questions and comments" %}</a> <a href="mailto:{{ event.contact_email }}">{% fa6_icon "envelope" "fas" %} {% trans "Write to organizers of this event for questions and comments" %}</a>
</p> </p>
{% endif %} {% endif %}
</div> </div>
......
{% load i18n %} {% load i18n %}
{% load tags_AKModel %} {% load tags_AKModel %}
{% load fontawesome_5 %} {% load fontawesome_6 %}
<h2><a href="{% url 'dashboard:dashboard_event' slug=event.slug %}">{{ event.name }}</a></h2> <h2><a href="{% url 'dashboard:dashboard_event' slug=event.slug %}">{{ event.name }}</a></h2>
<div class="row"> <h4 class="text-muted">
{% if event.place %}
<b>{{ event.place }} &middot;</b>
{% endif %}
{{ event | event_month_year }}
</h4>
<div class="mt-2">
{% if 'AKSubmission'|check_app_installed %} {% if 'AKSubmission'|check_app_installed %}
<a class="dashboard-box btn btn-primary" <a class="dashboard-box btn btn-primary"
href="{% url 'submit:ak_list' event_slug=event.slug %}"> href="{% url 'submit:ak_list' event_slug=event.slug %}">
...@@ -50,6 +56,13 @@ ...@@ -50,6 +56,13 @@
</div> </div>
</a> </a>
{% endif %} {% endif %}
<a class="dashboard-box btn btn-primary"
href="{% url 'dashboard:dashboard_event' slug=event.slug %}#history">
<div class="col-sm-12 col-md-3 col-lg-2 dashboard-button">
<span class="fa fa-history"></span>
<span class='text'>{% trans 'AK History' %}</span>
</div>
</a>
{% for button in event.dashboardbutton_set.all %} {% for button in event.dashboardbutton_set.all %}
<a class="dashboard-box btn btn-{{ button.get_color_display }}" <a class="dashboard-box btn btn-{{ button.get_color_display }}"
href="{{ button.url }}"> href="{{ button.url }}">
...@@ -60,4 +73,3 @@ ...@@ -60,4 +73,3 @@
</a> </a>
{% endfor %} {% endfor %}
</div> </div>
{% load i18n %}
{% load tags_AKModel %}
{% load fontawesome_6 %}
<h3><a href="{% url 'dashboard:dashboard_event' slug=event.slug %}">{{ event.name }}</a>
<span class="text-muted">
&middot;
{% if event.place %}
{{ event.place }} &middot;
{% endif %}
{{ event | event_month_year }}
</span>
</h3>
<div class="mt-2">
{% if 'AKSubmission'|check_app_installed %}
<a class="btn btn-primary"
href="{% url 'submit:ak_list' event_slug=event.slug %}">
<div class="col-sm-12 col-md-3 col-lg-2 dashboard-button">
<span class="fa fa-list-ul"></span>
<span class='text'>{% trans 'AK List' %}</span>
</div>
</a>
{% endif %}
{% if 'AKPlan'|check_app_installed %}
{% if not event.plan_hidden or user.is_staff %}
<a class="btn btn-primary"
href="{% url 'plan:plan_overview' event_slug=event.slug %}">
<div class="col-sm-12 col-md-3 col-lg-2 dashboard-button">
<span class="fa fa-calendar-alt"></span>
<span class='text'>{% trans 'Schedule' %}</span>
</div>
</a>
{% endif %}
{% endif %}
<a class="btn btn-primary"
href="{% url 'dashboard:dashboard_event' slug=event.slug %}#history">
<div class="col-sm-12 col-md-3 col-lg-2 dashboard-button">
<span class="fa fa-history"></span>
<span class='text'>{% trans 'AK History' %}</span>
</div>
</a>
{% for button in event.dashboardbutton_set.all %}
<a class="btn btn-{{ button.get_color_display }}"
href="{{ button.url }}">
<div class="col-sm-12 col-md-3 col-lg-2 dashboard-button">
{% if button.icon %}<span class="fa">{{ button.icon.as_html }}</span>{% endif %}
<span class='text'>{{ button.text }}</span>
</div>
</a>
{% endfor %}
<a class="btn btn-info"
href=mailto:{{ event.contact_email }}"
title="{% trans 'Write to organizers of this event for questions and comments' %}">
{% fa6_icon "envelope" "fas" %}
</a>
</div>
import pytz import zoneinfo
from django.apps import apps from django.apps import apps
from django.test import TestCase, override_settings from django.test import override_settings, TestCase
from django.urls import reverse from django.urls import reverse
from django.utils.timezone import now from django.utils.timezone import now
from AKDashboard.models import DashboardButton from AKDashboard.models import DashboardButton
from AKModel.models import Event, AK, AKCategory from AKModel.models import AK, AKCategory, Event
from AKModel.tests import BasicViewTests
class DashboardTests(TestCase): class DashboardTests(TestCase):
"""
Specific Dashboard Tests
"""
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
"""
Initialize Test database
"""
super().setUpTestData() super().setUpTestData()
cls.event = Event.objects.create( cls.event = Event.objects.create(
name="Dashboard Test Event", name="Dashboard Test Event",
slug="dashboardtest", slug="dashboardtest",
timezone=pytz.utc, timezone=zoneinfo.ZoneInfo("Europe/Berlin"),
start=now(), start=now(),
end=now(), end=now(),
active=True, active=True,
plan_hidden=False, plan_hidden=False,
) )
cls.default_category = AKCategory.objects.create( cls.default_category = AKCategory.objects.create(
name="Test Category", name="Test Category",
event=cls.event, event=cls.event,
) )
def test_dashboard_view(self): def test_dashboard_view(self):
"""
Check that the main dashboard is reachable
(would also be covered by generic view testcase below)
"""
url = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug}) url = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug})
response = self.client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_nonexistent_dashboard_view(self): def test_nonexistent_dashboard_view(self):
"""
Make sure there is no dashboard for an non-existing event
"""
url = reverse('dashboard:dashboard_event', kwargs={"slug": "nonexistent-event"}) url = reverse('dashboard:dashboard_event', kwargs={"slug": "nonexistent-event"})
response = self.client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
@override_settings(DASHBOARD_SHOW_RECENT=True) @override_settings(DASHBOARD_SHOW_RECENT=True)
def test_history(self): def test_history(self):
"""
Test displaying of history
For the sake of that test, the setting to show recent events in dashboard is enforced to be true
regardless of the default configuration currently in place
"""
url = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug}) url = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug})
# History should be empty # History should be empty
response = self.client.get(url) response = self.client.get(url)
self.assertQuerysetEqual(response.context["recent_changes"], []) self.assertQuerySetEqual(response.context["recent_changes"], [])
AK.objects.create( AK.objects.create(
name="Test AK", name="Test AK",
category=self.default_category, category=self.default_category,
event=self.event, event=self.event,
) )
# History should now contain one AK (Test AK) # History should now contain one AK (Test AK)
...@@ -56,6 +78,11 @@ class DashboardTests(TestCase): ...@@ -56,6 +78,11 @@ class DashboardTests(TestCase):
self.assertEqual(response.context["recent_changes"][0]['text'], "New AK: Test AK.") self.assertEqual(response.context["recent_changes"][0]['text'], "New AK: Test AK.")
def test_public(self): def test_public(self):
"""
Test handling of public and private events
(only public events should be part of the standard dashboard,
but there should be an individual dashboard for both public and private events)
"""
url_dashboard_index = reverse('dashboard:dashboard') url_dashboard_index = reverse('dashboard:dashboard')
url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug}) url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug})
...@@ -64,9 +91,9 @@ class DashboardTests(TestCase): ...@@ -64,9 +91,9 @@ class DashboardTests(TestCase):
self.event.public = False self.event.public = False
self.event.save() self.event.save()
response = self.client.get(url_dashboard_index) response = self.client.get(url_dashboard_index)
print(response)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertFalse(self.event in response.context["events"]) self.assertFalse(self.event in response.context["active_and_current_events"])
self.assertFalse(self.event in response.context["old_events"])
response = self.client.get(url_event_dashboard) response = self.client.get(url_event_dashboard)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["event"], self.event) self.assertEqual(response.context["event"], self.event)
...@@ -76,9 +103,12 @@ class DashboardTests(TestCase): ...@@ -76,9 +103,12 @@ class DashboardTests(TestCase):
self.event.save() self.event.save()
response = self.client.get(url_dashboard_index) response = self.client.get(url_dashboard_index)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue(self.event in response.context["events"]) self.assertTrue(self.event in response.context["active_and_current_events"])
def test_active(self): def test_active(self):
"""
Test existence of buttons with regard to activity status of the given event
"""
url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug}) url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug})
if apps.is_installed('AKSubmission'): if apps.is_installed('AKSubmission'):
...@@ -95,6 +125,9 @@ class DashboardTests(TestCase): ...@@ -95,6 +125,9 @@ class DashboardTests(TestCase):
self.assertContains(response, "AK Submission") self.assertContains(response, "AK Submission")
def test_plan_hidden(self): def test_plan_hidden(self):
"""
Test visibility of plan buttons with regard to plan visibility status for a given event
"""
url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug}) url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug})
if apps.is_installed('AKPlan'): if apps.is_installed('AKPlan'):
...@@ -114,15 +147,32 @@ class DashboardTests(TestCase): ...@@ -114,15 +147,32 @@ class DashboardTests(TestCase):
self.assertContains(response, "AK Wall") self.assertContains(response, "AK Wall")
def test_dashboard_buttons(self): def test_dashboard_buttons(self):
"""
Make sure manually added buttons are displayed correctly
"""
url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug}) url_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug})
response = self.client.get(url_event_dashboard) response = self.client.get(url_event_dashboard)
self.assertNotContains(response, "Dashboard Button Test") self.assertNotContains(response, "Dashboard Button Test")
DashboardButton.objects.create( DashboardButton.objects.create(
text="Dashboard Button Test", text="Dashboard Button Test",
event=self.event event=self.event
) )
response = self.client.get(url_event_dashboard) response = self.client.get(url_event_dashboard)
self.assertContains(response, "Dashboard Button Test") self.assertContains(response, "Dashboard Button Test")
class DashboardViewTests(BasicViewTests, TestCase):
"""
Generic view tests, based on :class:`AKModel.BasicViewTests` as specified in this class in VIEWS
"""
fixtures = ['model.json', 'dashboard.json']
APP_NAME = 'dashboard'
VIEWS = [
('dashboard', {}),
('dashboard_event', {'slug': 'kif42'}),
]
from django.apps import apps from django.apps import apps
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic import TemplateView, DetailView from django.views.generic import TemplateView, DetailView
...@@ -10,6 +9,11 @@ from AKPlanning import settings ...@@ -10,6 +9,11 @@ from AKPlanning import settings
class DashboardView(TemplateView): class DashboardView(TemplateView):
"""
Index view of dashboard and therefore the main entry point for AKPlanning
Displays information and buttons for all public events
"""
template_name = 'AKDashboard/dashboard.html' template_name = 'AKDashboard/dashboard.html'
@method_decorator(ensure_csrf_cookie) @method_decorator(ensure_csrf_cookie)
...@@ -18,11 +22,30 @@ class DashboardView(TemplateView): ...@@ -18,11 +22,30 @@ class DashboardView(TemplateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['events'] = Event.objects.filter(public=True) # Load events and split between active and current/featured events and those that should show smaller below
context["active_and_current_events"] = []
context["old_events"] = []
events = Event.objects.filter(public=True).order_by("-active", "-pk").prefetch_related('dashboardbutton_set')
for event in events:
if event.active or len(context["active_and_current_events"]) < settings.DASHBOARD_MAX_FEATURED_EVENTS:
context["active_and_current_events"].append(event)
else:
context["old_events"].append(event)
context["active_event_count"] = len(context["active_and_current_events"])
context["old_event_count"] = len(context["old_events"])
context["total_event_count"] = context["active_event_count"] + context["old_event_count"]
return context return context
class DashboardEventView(DetailView): class DashboardEventView(DetailView):
"""
Dashboard view for a single event
In addition to the basic information and the buttons,
an overview over recent events (new and changed AKs, moved AKSlots) for the given event is shown.
The event dashboard also exists for non-public events (one only needs to know the URL/slug of the event).
"""
template_name = 'AKDashboard/dashboard_event.html' template_name = 'AKDashboard/dashboard_event.html'
context_object_name = 'event' context_object_name = 'event'
model = Event model = Event
...@@ -32,11 +55,16 @@ class DashboardEventView(DetailView): ...@@ -32,11 +55,16 @@ class DashboardEventView(DetailView):
# Show feed of recent changes (if activated) # Show feed of recent changes (if activated)
if settings.DASHBOARD_SHOW_RECENT: if settings.DASHBOARD_SHOW_RECENT:
# Create a list of chronically sorted events (both AK and plan changes):
recent_changes = [] recent_changes = []
# Newest AKs # Newest AKs (if AKSubmission is used)
if apps.is_installed("AKSubmission"): if apps.is_installed("AKSubmission"):
submission_changes = AK.history.filter(event=context['event'])[:int(settings.DASHBOARD_RECENT_MAX)] # Get the latest x changes (if there are that many),
# where x corresponds to the entry threshold configured in the settings
# (such that the list will be completely filled even if there are no (newer) plan changes)
submission_changes = AK.history.filter(event=context['event'])[:int(settings.DASHBOARD_RECENT_MAX)] # pylint: disable=no-member, line-too-long
# Create textual representation including icons
for s in submission_changes: for s in submission_changes:
if s.history_type == '+': if s.history_type == '+':
text = _('New AK: %(ak)s.') % {'ak': s.name} text = _('New AK: %(ak)s.') % {'ak': s.name}
...@@ -48,20 +76,21 @@ class DashboardEventView(DetailView): ...@@ -48,20 +76,21 @@ class DashboardEventView(DetailView):
text = _('AK "%(ak)s" deleted.') % {'ak': s.name} text = _('AK "%(ak)s" deleted.') % {'ak': s.name}
icon = ('times', 'fas') icon = ('times', 'fas')
recent_changes.append({'icon': icon, 'text': text, 'link': reverse_lazy('submit:ak_detail', kwargs={ # Store representation in change list (still unsorted)
'event_slug': context['event'].slug, 'pk': s.id}), 'timestamp': s.history_date}) recent_changes.append(
{'icon': icon, 'text': text, 'link': s.instance.detail_url, 'timestamp': s.history_date}
# Changes in plan )
if apps.is_installed("AKPlan"):
if not context['event'].plan_hidden: # Changes in plan (if AKPlan is used and plan is publicly visible)
last_changed_slots = AKSlot.objects.filter(event=context['event'], start__isnull=False).order_by('-updated')[ if apps.is_installed("AKPlan") and not context['event'].plan_hidden:
:int(settings.DASHBOARD_RECENT_MAX)] # Get the latest plan changes (again using a threshold, see above)
for changed_slot in last_changed_slots: last_changed_slots = AKSlot.objects.select_related('ak').filter(event=context['event'], start__isnull=False).order_by('-updated')[:int(settings.DASHBOARD_RECENT_MAX)] #pylint: disable=line-too-long
recent_changes.append({'icon': ('clock', 'far'), for changed_slot in last_changed_slots:
'text': _('AK "%(ak)s" (re-)scheduled.') % {'ak': changed_slot.ak.name}, # Create textual representation including icons and links and store in list (still unsorted)
'link': reverse_lazy('submit:ak_detail', kwargs={ recent_changes.append({'icon': ('clock', 'far'),
'event_slug': context['event'].slug, 'pk': changed_slot.ak.id}), 'text': _('AK "%(ak)s" (re-)scheduled.') % {'ak': changed_slot.ak.name},
'timestamp': changed_slot.updated}) 'link': changed_slot.ak.detail_url,
'timestamp': changed_slot.updated})
# Sort by change date... # Sort by change date...
recent_changes.sort(key=lambda x: x['timestamp'], reverse=True) recent_changes.sort(key=lambda x: x['timestamp'], reverse=True)
......
...@@ -2,6 +2,8 @@ from django import forms ...@@ -2,6 +2,8 @@ from django import forms
from django.apps import apps 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.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User # pylint: disable=E5142
from django.db.models import Count, F from django.db.models import Count, F
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
...@@ -10,19 +12,23 @@ from django.utils import timezone ...@@ -10,19 +12,23 @@ from django.utils import timezone
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from rest_framework.reverse import reverse
from simple_history.admin import SimpleHistoryAdmin from simple_history.admin import SimpleHistoryAdmin
from AKModel.availability.forms import AvailabilitiesFormMixin
from AKModel.availability.models import Availability from AKModel.availability.models import Availability
from AKModel.models import Event, AKOwner, AKCategory, AKTrack, AKTag, AKRequirement, AK, AKSlot, Room, AKOrgaMessage, \ from AKModel.forms import RoomFormWithAvailabilities
ConstraintViolation, DefaultSlot from AKModel.models import Event, AKOwner, AKCategory, AKTrack, AKRequirement, AK, AKSlot, Room, AKOrgaMessage, \
ConstraintViolation, DefaultSlot, AKType
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.ak import AKResetInterestView, AKResetInterestCounterView
AKResetInterestCounterView, PlanPublishView, PlanUnpublishView, DefaultSlotEditorView, RoomBatchCreationView from AKModel.views.manage import CVMarkResolvedView, CVSetLevelViolationView, CVSetLevelWarningView
class EventRelatedFieldListFilter(RelatedFieldListFilter): class EventRelatedFieldListFilter(RelatedFieldListFilter):
"""
Reusable filter to restrict the possible choices of a field to those belonging to a certain event
as specified in the event__id__exact GET parameter.
The choices are only restricted if this parameter is present, otherwise all choices are used/returned
"""
def field_choices(self, field, request, model_admin): def field_choices(self, field, request, model_admin):
ordering = self.field_admin_ordering(field, request, model_admin) ordering = self.field_admin_ordering(field, request, model_admin)
limit_choices = {} limit_choices = {}
...@@ -33,6 +39,17 @@ class EventRelatedFieldListFilter(RelatedFieldListFilter): ...@@ -33,6 +39,17 @@ class EventRelatedFieldListFilter(RelatedFieldListFilter):
@admin.register(Event) @admin.register(Event)
class EventAdmin(admin.ModelAdmin): class EventAdmin(admin.ModelAdmin):
"""
Admin interface for Event
This allows to edit most fields of an event, some can only be changed by admin actions, since they have side effects
This admin interface registers additional views as defined in urls.py, the wizard, and the full scheduling
functionality if the AKScheduling app is active.
The interface overrides the built-in creation interface for a new event and replaces it with the event creation
wizard.
"""
model = Event model = Event
list_display = ['name', 'status_url', 'place', 'start', 'end', 'active', 'plan_hidden'] list_display = ['name', 'status_url', 'place', 'start', 'end', 'active', 'plan_hidden']
list_filter = ['active'] list_filter = ['active']
...@@ -42,32 +59,54 @@ class EventAdmin(admin.ModelAdmin): ...@@ -42,32 +59,54 @@ class EventAdmin(admin.ModelAdmin):
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):
# Always use wizard to create new events (the built-in form wouldn't work anyways since the timezone cannot # Override
# Always use wizard to create new events (the built-in form wouldn't work anyway since the timezone cannot
# be specified before starting to fill the form) # be specified before starting to fill the form)
return redirect("admin:new_event_wizard_start") return redirect("admin:new_event_wizard_start")
def get_urls(self): def get_urls(self):
"""
Get all event-related URLs
This will be both the built-in URLs and additional views providing additional functionality
:return: list of all relevant URLs
:rtype: List[path]
"""
# Load wizard URLs and the additional URLs defined in urls.py
# (first, to have the highest priority when overriding views)
urls = get_admin_urls_event_wizard(self.admin_site) urls = get_admin_urls_event_wizard(self.admin_site)
urls.extend(get_admin_urls_event(self.admin_site)) urls.extend(get_admin_urls_event(self.admin_site))
# Make scheduling admin views available if app is active
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 # pylint: disable=import-outside-toplevel
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"), # Make sure built-in URLs are available as well
path('plan/unpublish/', PlanUnpublishView.as_view(), name="plan-unpublish"),
path('<slug:event_slug>/defaultSlots/', DefaultSlotEditorView.as_view(), name="default-slots-editor"),
path('<slug:event_slug>/importRooms/', RoomBatchCreationView.as_view(), name="room-import"),
])
urls.extend(super().get_urls()) urls.extend(super().get_urls())
return urls return urls
@display(description=_("Status")) @display(description=_("Status"))
def status_url(self, obj): def status_url(self, obj):
"""
Define a read-only field to go to the status page of the event
:param obj: the event to link
:return: status page link (HTML)
:rtype: str
"""
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={'event_slug': obj.slug}), text=_("Status"))
@display(description=_("Toggle plan visibility")) @display(description=_("Toggle plan visibility"))
def toggle_plan_visibility(self, obj): def toggle_plan_visibility(self, obj):
"""
Define a read-only field to toggle the visibility of the plan of this event
This will choose from two different link targets/views depending on the current visibility status
:param obj: event to change the visibility of the plan for
:return: toggling link (HTML)
:rtype: str
"""
if obj.plan_hidden: if obj.plan_hidden:
url = f"{reverse_lazy('admin:plan-publish')}?pks={obj.pk}" url = f"{reverse_lazy('admin:plan-publish')}?pks={obj.pk}"
text = _('Publish plan') text = _('Publish plan')
...@@ -77,87 +116,123 @@ class EventAdmin(admin.ModelAdmin): ...@@ -77,87 +116,123 @@ class EventAdmin(admin.ModelAdmin):
return format_html("<a href='{url}'>{text}</a>", url=url, text=text) 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 # Override (update) form rendering to make sure the timezone of the event is used
timezone.activate(obj.timezone) timezone.activate(obj.timezone)
return super().get_form(request, obj, change, **kwargs) return super().get_form(request, obj, change, **kwargs)
@action(description=_('Publish plan')) @action(description=_('Publish plan'))
def publish(self, request, queryset): def publish(self, request, queryset):
"""
Admin action to publish the plan
"""
selected = queryset.values_list('pk', flat=True) selected = queryset.values_list('pk', flat=True)
return HttpResponseRedirect(f"{reverse_lazy('admin:plan-publish')}?pks={','.join(str(pk) for pk in selected)}") 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):
"""
Admin action to hide the plan
"""
selected = queryset.values_list('pk', flat=True) selected = queryset.values_list('pk', flat=True)
return HttpResponseRedirect(f"{reverse_lazy('admin:plan-unpublish')}?pks={','.join(str(pk) for pk in selected)}") return HttpResponseRedirect(
f"{reverse_lazy('admin:plan-unpublish')}?pks={','.join(str(pk) for pk in selected)}")
class PrepopulateWithNextActiveEventMixin:
"""
Mixin for automated pre-population of the event field
"""
# pylint: disable=too-few-public-methods
def formfield_for_foreignkey(self, db_field, request, **kwargs):
"""
Override field generation for foreign key fields to introduce special handling for event fields:
Pre-populate the event field with the next active event (since that is the most likeliest event to be worked
on in the admin interface) to make creation of new owners easier
"""
if db_field.name == 'event':
kwargs['initial'] = Event.get_next_active()
return super().formfield_for_foreignkey(db_field, request, **kwargs)
@admin.register(AKOwner) @admin.register(AKOwner)
class AKOwnerAdmin(admin.ModelAdmin): class AKOwnerAdmin(PrepopulateWithNextActiveEventMixin, admin.ModelAdmin):
"""
Admin interface for AKOwner
"""
model = AKOwner model = AKOwner
list_display = ['name', 'institution', 'event'] list_display = ['name', 'institution', 'event', 'aks_url']
list_filter = ['event', 'institution'] list_filter = ['event', 'institution']
list_editable = [] list_editable = []
ordering = ['name'] ordering = ['name']
readonly_fields = ['aks_url']
def formfield_for_foreignkey(self, db_field, request, **kwargs): @display(description=_("AKs"))
if db_field.name == 'event': def aks_url(self, obj):
kwargs['initial'] = Event.get_next_active() """
return super(AKOwnerAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) Define a read-only field to go to the list of all AKs by this user
:param obj: user
:return: AK list page link (HTML)
:rtype: str
"""
return format_html("<a href='{url}'>{text}</a>",
url=reverse_lazy('admin:aks_by_owner', kwargs={'event_slug': obj.event.slug, 'pk': obj.pk}),
text=obj.ak_set.count())
@admin.register(AKCategory) @admin.register(AKCategory)
class AKCategoryAdmin(admin.ModelAdmin): class AKCategoryAdmin(PrepopulateWithNextActiveEventMixin, admin.ModelAdmin):
"""
Admin interface for AKCategory
"""
model = AKCategory model = AKCategory
list_display = ['name', 'color', 'event'] list_display = ['name', 'color', 'event']
list_filter = ['event'] list_filter = ['event']
list_editable = ['color'] list_editable = ['color']
ordering = ['name'] ordering = ['name']
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'event':
kwargs['initial'] = Event.get_next_active()
return super(AKCategoryAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
@admin.register(AKTrack) @admin.register(AKTrack)
class AKTrackAdmin(admin.ModelAdmin): class AKTrackAdmin(PrepopulateWithNextActiveEventMixin, admin.ModelAdmin):
"""
Admin interface for AKTrack
"""
model = AKTrack model = AKTrack
list_display = ['name', 'color', 'event'] list_display = ['name', 'color', 'event']
list_filter = ['event'] list_filter = ['event']
list_editable = ['color'] list_editable = ['color']
ordering = ['name'] ordering = ['name']
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'event':
kwargs['initial'] = Event.get_next_active()
return super(AKTrackAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
@admin.register(AKTag) @admin.register(AKRequirement)
class AKTagAdmin(admin.ModelAdmin): class AKRequirementAdmin(PrepopulateWithNextActiveEventMixin, admin.ModelAdmin):
model = AKTag """
list_display = ['name'] Admin interface for AKRequirements
list_filter = [] """
model = AKRequirement
list_display = ['name', 'event']
list_filter = ['event']
list_editable = [] list_editable = []
ordering = ['name'] ordering = ['name']
@admin.register(AKRequirement) @admin.register(AKType)
class AKRequirementAdmin(admin.ModelAdmin): class AKTypeAdmin(PrepopulateWithNextActiveEventMixin, admin.ModelAdmin):
model = AKRequirement """
Admin interface for AKRequirements
"""
model = AKType
list_display = ['name', 'event'] list_display = ['name', 'event']
list_filter = ['event'] list_filter = ['event']
list_editable = [] list_editable = []
ordering = ['name'] ordering = ['name']
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'event':
kwargs['initial'] = Event.get_next_active()
return super(AKRequirementAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
class WishFilter(SimpleListFilter): class WishFilter(SimpleListFilter):
"""
Re-usable filter for wishes
"""
title = _("Wish") # a label for our filter title = _("Wish") # a label for our filter
parameter_name = 'wishes' # you can put anything here parameter_name = 'wishes' # you can put anything here
...@@ -178,6 +253,9 @@ class WishFilter(SimpleListFilter): ...@@ -178,6 +253,9 @@ class WishFilter(SimpleListFilter):
class AKAdminForm(forms.ModelForm): class AKAdminForm(forms.ModelForm):
"""
Modified admin form for AKs, to be used in :class:`AKAdmin`
"""
class Meta: class Meta:
widgets = { widgets = {
'requirements': forms.CheckboxSelectMultiple, 'requirements': forms.CheckboxSelectMultiple,
...@@ -193,13 +271,23 @@ class AKAdminForm(forms.ModelForm): ...@@ -193,13 +271,23 @@ class AKAdminForm(forms.ModelForm):
self.fields["requirements"].queryset = AKRequirement.objects.filter(event=self.instance.event) self.fields["requirements"].queryset = AKRequirement.objects.filter(event=self.instance.event)
self.fields["conflicts"].queryset = AK.objects.filter(event=self.instance.event) self.fields["conflicts"].queryset = AK.objects.filter(event=self.instance.event)
self.fields["prerequisites"].queryset = AK.objects.filter(event=self.instance.event) self.fields["prerequisites"].queryset = AK.objects.filter(event=self.instance.event)
self.fields["types"].queryset = AKType.objects.filter(event=self.instance.event)
@admin.register(AK) @admin.register(AK)
class AKAdmin(SimpleHistoryAdmin): class AKAdmin(PrepopulateWithNextActiveEventMixin, SimpleHistoryAdmin):
"""
Admin interface for AKs
Uses a modified form (see :class:`AKAdminForm`)
"""
model = AK model = AK
list_display = ['name', 'short_name', 'category', 'track', 'is_wish', 'interest', 'interest_counter', 'event'] list_display = ['name', 'short_name', 'category', 'track', 'is_wish', 'interest', 'interest_counter', 'event']
list_filter = ['event', WishFilter, ('category', EventRelatedFieldListFilter), ('requirements', EventRelatedFieldListFilter)] list_filter = ['event',
WishFilter,
('category', EventRelatedFieldListFilter),
('requirements', EventRelatedFieldListFilter)
]
list_editable = ['short_name', 'track', 'interest_counter'] list_editable = ['short_name', 'track', 'interest_counter']
ordering = ['pk'] ordering = ['pk']
actions = ['wiki_export', 'reset_interest', 'reset_interest_counter'] actions = ['wiki_export', 'reset_interest', 'reset_interest_counter']
...@@ -207,25 +295,36 @@ class AKAdmin(SimpleHistoryAdmin): ...@@ -207,25 +295,36 @@ class AKAdmin(SimpleHistoryAdmin):
@display(boolean=True) @display(boolean=True)
def is_wish(self, obj): def is_wish(self, obj):
"""
Property: Is this AK a wish?
"""
return obj.wish return obj.wish
@action(description=_("Export to wiki syntax")) @action(description=_("Export to wiki syntax"))
def wiki_export(self, request, queryset): def wiki_export(self, request, queryset):
"""
Action: Export to wiki syntax
This will use the wiki export view (therefore, all AKs have to have the same event to correclty handle the
categories and to prevent accidentially merging AKs from different events in the wiki)
but restrict the AKs to the ones explicitly selected here.
"""
# Only export when all AKs belong to the same event # Only export when all AKs belong to the same event
if queryset.values("event").distinct().count() == 1: if queryset.values("event").distinct().count() == 1:
event = queryset.first().event event = queryset.first().event
pks = set(ak.pk for ak in queryset.all()) pks = set(ak.pk for ak in queryset.all())
categories_with_aks = event.get_categories_with_aks(wishes_seperately=False, filter=lambda ak: ak.pk in pks, categories_with_aks = event.get_categories_with_aks(wishes_seperately=False,
filter_func=lambda ak: ak.pk in pks,
hide_empty_categories=True) hide_empty_categories=True)
return render(request, 'admin/AKModel/wiki_export.html', context={"categories_with_aks": categories_with_aks}) return render(request, 'admin/AKModel/wiki_export.html',
context={"categories_with_aks": categories_with_aks})
self.message_user(request, _("Cannot export AKs from more than one event at the same time."), messages.ERROR) self.message_user(request, _("Cannot export AKs from more than one event at the same time."), messages.ERROR)
return redirect('admin:AKModel_ak_changelist')
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'event':
kwargs['initial'] = Event.get_next_active()
return super(AKAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
def get_urls(self): def get_urls(self):
"""
Add additional URLs/views
Currently used to reset the interest field and interest counter field
"""
urls = [ urls = [
path('reset-interest/', AKResetInterestView.as_view(), name="ak-reset-interest"), path('reset-interest/', AKResetInterestView.as_view(), name="ak-reset-interest"),
path('reset-interest-counter/', AKResetInterestCounterView.as_view(), name="ak-reset-interest-counter"), path('reset-interest-counter/', AKResetInterestCounterView.as_view(), name="ak-reset-interest-counter"),
...@@ -235,41 +334,30 @@ class AKAdmin(SimpleHistoryAdmin): ...@@ -235,41 +334,30 @@ class AKAdmin(SimpleHistoryAdmin):
@action(description=_("Reset interest in AKs")) @action(description=_("Reset interest in AKs"))
def reset_interest(self, request, queryset): def reset_interest(self, request, queryset):
"""
Action: Reset interest field for the given AKs
Will use a typical admin confirmation view flow
"""
selected = queryset.values_list('pk', flat=True) selected = queryset.values_list('pk', flat=True)
return HttpResponseRedirect(f"{reverse_lazy('admin:ak-reset-interest')}?pks={','.join(str(pk) for pk in selected)}") return HttpResponseRedirect(
f"{reverse_lazy('admin:ak-reset-interest')}?pks={','.join(str(pk) for pk in selected)}")
@action(description=_("Reset AKs' interest counters")) @action(description=_("Reset AKs' interest counters"))
def reset_interest_counter(self, request, queryset): def reset_interest_counter(self, request, queryset):
"""
Action: Reset interest counter field for the given AKs
Will use a typical admin confirmation view flow
"""
selected = queryset.values_list('pk', flat=True) selected = queryset.values_list('pk', flat=True)
return HttpResponseRedirect(f"{reverse_lazy('admin:ak-reset-interest-counter')}?pks={','.join(str(pk) for pk in selected)}") return HttpResponseRedirect(
f"{reverse_lazy('admin:ak-reset-interest-counter')}?pks={','.join(str(pk) for pk in selected)}")
class RoomForm(AvailabilitiesFormMixin, forms.ModelForm):
class Meta:
model = Room
fields = ['name',
'location',
'capacity',
'properties',
'event',
]
widgets = {
'properties': forms.CheckboxSelectMultiple,
}
def __init__(self, *args, **kwargs):
# Init availability mixin
kwargs['initial'] = dict()
super().__init__(*args, **kwargs)
self.initial = {**self.initial, **kwargs['initial']}
# Filter possible values for m2m when event is specified
if hasattr(self.instance, "event") and self.instance.event is not None:
self.fields["properties"].queryset = AKRequirement.objects.filter(event=self.instance.event)
@admin.register(Room) @admin.register(Room)
class RoomAdmin(admin.ModelAdmin): class RoomAdmin(PrepopulateWithNextActiveEventMixin, admin.ModelAdmin):
"""
Admin interface for Rooms
"""
model = Room model = Room
list_display = ['name', 'location', 'capacity', 'event'] list_display = ['name', 'location', 'capacity', 'event']
list_filter = ['event', ('properties', EventRelatedFieldListFilter), 'location'] list_filter = ['event', ('properties', EventRelatedFieldListFilter), 'location']
...@@ -277,20 +365,60 @@ class RoomAdmin(admin.ModelAdmin): ...@@ -277,20 +365,60 @@ class RoomAdmin(admin.ModelAdmin):
ordering = ['location', 'name'] ordering = ['location', 'name']
change_form_template = "admin/AKModel/room_change_form.html" change_form_template = "admin/AKModel/room_change_form.html"
def add_view(self, request, form_url='', extra_context=None):
# Override creation view
# Use custom view for room creation (either room form or combined form if virtual rooms are supported)
return redirect("admin:room-new")
def get_form(self, request, obj=None, change=False, **kwargs): def get_form(self, request, obj=None, change=False, **kwargs):
# Override form creation to use a form that allows to specify availabilites of the room once this room is
# associated with an event (so not before the first saving) since the timezone information and event start
# and end are needed to correclty render the calendar
if obj is not None: if obj is not None:
return RoomForm return RoomFormWithAvailabilities
return super().get_form(request, obj, change, **kwargs) return super().get_form(request, obj, change, **kwargs)
def formfield_for_foreignkey(self, db_field, request, **kwargs): def get_urls(self):
if db_field.name == 'event': """
kwargs['initial'] = Event.get_next_active() Add additional URLs/views
return super(RoomAdmin, self).formfield_for_foreignkey( This is currently used to adapt the creation form behavior, to allow the creation of virtual rooms in-place
db_field, request, **kwargs when the support for virtual rooms is turned on (AKOnline app active)
) """
# pylint: disable=import-outside-toplevel
if apps.is_installed("AKOnline"):
from AKOnline.views import RoomCreationWithVirtualView as RoomCreationView
else:
from .views.room import RoomCreationView
urls = [
path('new/', self.admin_site.admin_view(RoomCreationView.as_view()), name="room-new"),
]
urls.extend(super().get_urls())
return urls
class EventTimezoneFormMixin:
"""
Mixin to enforce the usage of the timezone of the associated event in forms
"""
# pylint: disable=too-few-public-methods
def get_form(self, request, obj=None, change=False, **kwargs):
"""
Override form creation, use timezone of associated event
"""
if obj is not None and obj.event.timezone:
timezone.activate(obj.event.timezone)
# No timezone available? Use UTC
else:
timezone.activate("UTC")
return super().get_form(request, obj, change, **kwargs)
class AKSlotAdminForm(forms.ModelForm): class AKSlotAdminForm(forms.ModelForm):
"""
Modified admin form for AKSlots, to be used in :class:`AKSlotAdmin`
"""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Filter possible values for foreign keys when event is specified # Filter possible values for foreign keys when event is specified
...@@ -300,7 +428,12 @@ class AKSlotAdminForm(forms.ModelForm): ...@@ -300,7 +428,12 @@ class AKSlotAdminForm(forms.ModelForm):
@admin.register(AKSlot) @admin.register(AKSlot)
class AKSlotAdmin(admin.ModelAdmin): class AKSlotAdmin(EventTimezoneFormMixin, PrepopulateWithNextActiveEventMixin, admin.ModelAdmin):
"""
Admin interface for AKSlots
Uses a modified form (see :class:`AKSlotAdminForm`)
"""
model = AKSlot model = AKSlot
list_display = ['id', 'ak', 'room', 'start', 'duration', 'event'] list_display = ['id', 'ak', 'room', 'start', 'duration', 'event']
list_filter = ['event', ('room', EventRelatedFieldListFilter)] list_filter = ['event', ('room', EventRelatedFieldListFilter)]
...@@ -308,50 +441,46 @@ class AKSlotAdmin(admin.ModelAdmin): ...@@ -308,50 +441,46 @@ class AKSlotAdmin(admin.ModelAdmin):
readonly_fields = ['ak_details_link', 'updated'] readonly_fields = ['ak_details_link', 'updated']
form = AKSlotAdminForm form = AKSlotAdminForm
def get_form(self, request, obj=None, change=False, **kwargs):
# Use timezone of associated event
if obj is not None and obj.event.timezone:
timezone.activate(obj.event.timezone)
# No timezone available? Use UTC
else:
timezone.activate("UTC")
return super().get_form(request, obj, change, **kwargs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'event':
kwargs['initial'] = Event.get_next_active()
return super(AKSlotAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
@display(description=_('AK Details')) @display(description=_('AK Details'))
def ak_details_link(self, akslot): def ak_details_link(self, akslot):
"""
Define a read-only field to link the details of the associated AK
:param obj: the AK to link
:return: AK detail page page link (HTML)
:rtype: str
"""
if apps.is_installed("AKSubmission") and akslot.ak is not None: if apps.is_installed("AKSubmission") and akslot.ak is not None:
link = f"<a href={reverse('submit:ak_detail', args=[akslot.event.slug, akslot.ak.pk])}>{str(akslot.ak)}</a>" link = f"<a href='{ akslot.ak.detail_url }'>{str(akslot.ak)}</a>"
return mark_safe(link) return mark_safe(str(link))
return "-" return "-"
ak_details_link.short_description = _('AK Details') ak_details_link.short_description = _('AK Details')
@admin.register(Availability) @admin.register(Availability)
class AvailabilityAdmin(admin.ModelAdmin): class AvailabilityAdmin(EventTimezoneFormMixin, admin.ModelAdmin):
def get_form(self, request, obj=None, change=False, **kwargs): """
# Use timezone of associated event Admin interface for Availabilities
if obj is not None and obj.event.timezone: """
timezone.activate(obj.event.timezone) list_display = ['__str__', 'event']
# No timezone available? Use UTC list_filter = ['event']
else:
timezone.activate("UTC")
return super().get_form(request, obj, change, **kwargs)
@admin.register(AKOrgaMessage) @admin.register(AKOrgaMessage)
class AKOrgaMessageAdmin(admin.ModelAdmin): class AKOrgaMessageAdmin(admin.ModelAdmin):
list_display = ['timestamp', 'ak', 'text'] """
Admin interface for AKOrgaMessages
"""
list_display = ['timestamp', 'ak', 'text', 'resolved']
list_filter = ['ak__event'] list_filter = ['ak__event']
readonly_fields = ['timestamp', 'ak', 'text'] readonly_fields = ['timestamp', 'ak', 'text']
class ConstraintViolationAdminForm(forms.ModelForm): class ConstraintViolationAdminForm(forms.ModelForm):
"""
Adapted admin form for constraint violations for usage in :class:`ConstraintViolationAdmin`)
"""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Filter possible values for foreign keys & m2m when event is specified # Filter possible values for foreign keys & m2m when event is specified
...@@ -366,6 +495,10 @@ class ConstraintViolationAdminForm(forms.ModelForm): ...@@ -366,6 +495,10 @@ class ConstraintViolationAdminForm(forms.ModelForm):
@admin.register(ConstraintViolation) @admin.register(ConstraintViolation)
class ConstraintViolationAdmin(admin.ModelAdmin): class ConstraintViolationAdmin(admin.ModelAdmin):
"""
Admin interface for constraint violations
Uses an adapted form (see :class:`ConstraintViolationAdminForm`)
"""
list_display = ['type', 'level', 'get_details', 'manually_resolved'] list_display = ['type', 'level', 'get_details', 'manually_resolved']
list_filter = ['event'] list_filter = ['event']
readonly_fields = ['timestamp'] readonly_fields = ['timestamp']
...@@ -373,6 +506,9 @@ class ConstraintViolationAdmin(admin.ModelAdmin): ...@@ -373,6 +506,9 @@ class ConstraintViolationAdmin(admin.ModelAdmin):
actions = ['mark_resolved', 'set_violation', 'set_warning'] actions = ['mark_resolved', 'set_violation', 'set_warning']
def get_urls(self): def get_urls(self):
"""
Add additional URLs/views to change status and severity of CVs
"""
urls = [ urls = [
path('mark-resolved/', CVMarkResolvedView.as_view(), name="cv-mark-resolved"), path('mark-resolved/', CVMarkResolvedView.as_view(), name="cv-mark-resolved"),
path('set-violation/', CVSetLevelViolationView.as_view(), name="cv-set-violation"), path('set-violation/', CVSetLevelViolationView.as_view(), name="cv-set-violation"),
...@@ -383,21 +519,36 @@ class ConstraintViolationAdmin(admin.ModelAdmin): ...@@ -383,21 +519,36 @@ class ConstraintViolationAdmin(admin.ModelAdmin):
@action(description=_("Mark Constraint Violations as manually resolved")) @action(description=_("Mark Constraint Violations as manually resolved"))
def mark_resolved(self, request, queryset): def mark_resolved(self, request, queryset):
"""
Action: Mark CV as resolved
"""
selected = queryset.values_list('pk', flat=True) selected = queryset.values_list('pk', flat=True)
return HttpResponseRedirect(f"{reverse_lazy('admin:cv-mark-resolved')}?pks={','.join(str(pk) for pk in selected)}") return HttpResponseRedirect(
f"{reverse_lazy('admin:cv-mark-resolved')}?pks={','.join(str(pk) for pk in selected)}")
@action(description=_('Set Constraint Violations to level "violation"')) @action(description=_('Set Constraint Violations to level "violation"'))
def set_violation(self, request, queryset): def set_violation(self, request, queryset):
"""
Action: Promote CV to level violation
"""
selected = queryset.values_list('pk', flat=True) selected = queryset.values_list('pk', flat=True)
return HttpResponseRedirect(f"{reverse_lazy('admin:cv-set-violation')}?pks={','.join(str(pk) for pk in selected)}") return HttpResponseRedirect(
f"{reverse_lazy('admin:cv-set-violation')}?pks={','.join(str(pk) for pk in selected)}")
@action(description=_('Set Constraint Violations to level "warning"')) @action(description=_('Set Constraint Violations to level "warning"'))
def set_warning(self, request, queryset): def set_warning(self, request, queryset):
"""
Action: Set CV to level warning
"""
selected = queryset.values_list('pk', flat=True) selected = queryset.values_list('pk', flat=True)
return HttpResponseRedirect(f"{reverse_lazy('admin:cv-set-warning')}?pks={','.join(str(pk) for pk in selected)}") return HttpResponseRedirect(
f"{reverse_lazy('admin:cv-set-warning')}?pks={','.join(str(pk) for pk in selected)}")
class DefaultSlotAdminForm(forms.ModelForm): class DefaultSlotAdminForm(forms.ModelForm):
"""
Adapted admin form for DefaultSlot for usage in :class:`DefaultSlotAdmin`
"""
class Meta: class Meta:
widgets = { widgets = {
'primary_categories': forms.CheckboxSelectMultiple 'primary_categories': forms.CheckboxSelectMultiple
...@@ -411,7 +562,49 @@ class DefaultSlotAdminForm(forms.ModelForm): ...@@ -411,7 +562,49 @@ class DefaultSlotAdminForm(forms.ModelForm):
@admin.register(DefaultSlot) @admin.register(DefaultSlot)
class DefaultSlotAdmin(admin.ModelAdmin): class DefaultSlotAdmin(EventTimezoneFormMixin, admin.ModelAdmin):
list_display = ['start', 'end', 'event'] """
Admin interface for default slots
Uses an adapted form (see :class:`DefaultSlotAdminForm`)
"""
list_display = ['start_simplified', 'end_simplified', 'event']
list_filter = ['event'] list_filter = ['event']
form = DefaultSlotAdminForm form = DefaultSlotAdminForm
# Define a new User admin
class UserAdmin(BaseUserAdmin):
"""
Admin interface for Users
Enhances the built-in UserAdmin with additional actions to activate and deactivate users and a custom selection
of displayed properties in overview list
"""
list_display = ["username", "email", "is_active", "is_staff", "is_superuser"]
actions = ['activate', 'deactivate']
@admin.action(description=_("Activate selected users"))
def activate(self, request, queryset):
"""
Bulk activate users
:param request: HTTP request
:param queryset: queryset containing all users that should be activated
"""
queryset.update(is_active=True)
self.message_user(request, _("The selected users have been activated."))
@admin.action(description=_("Deactivate selected users"))
def deactivate(self, request, queryset):
"""
Bulk deactivate users
:param request: HTTP request
:param queryset: queryset containing all users that should be deactivated
"""
queryset.update(is_active=False)
self.message_user(request, _("The selected users have been deactivated."))
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
...@@ -3,8 +3,15 @@ from django.contrib.admin.apps import AdminConfig ...@@ -3,8 +3,15 @@ from django.contrib.admin.apps import AdminConfig
class AkmodelConfig(AppConfig): class AkmodelConfig(AppConfig):
"""
App configuration (default, only specifies name of the app)
"""
name = 'AKModel' name = 'AKModel'
class AKAdminConfig(AdminConfig): class AKAdminConfig(AdminConfig):
"""
App configuration for admin
Loading a custom version here allows to add additional contex and further adapt the behavior of the admin interface
"""
default_site = 'AKModel.site.AKAdminSite' default_site = 'AKModel.site.AKAdminSite'
# This part of the code was adapted from pretalx (https://github.com/pretalx/pretalx) # This part of the code was adapted from pretalx (https://github.com/pretalx/pretalx)
# Copyright 2017-2019, Tobias Kunze # Copyright 2017-2019, Tobias Kunze
# Original Copyrights licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 # Original Copyrights licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
# Changes are marked in the code # Documentation was mainly added by us, other changes are marked in the code
import datetime import datetime
import json import json
...@@ -17,16 +17,29 @@ from AKModel.models import Event ...@@ -17,16 +17,29 @@ from AKModel.models import Event
class AvailabilitiesFormMixin(forms.Form): class AvailabilitiesFormMixin(forms.Form):
"""
Mixin for forms to add availabilities functionality to it
Will handle the rendering and population of an availabilities field
"""
availabilities = forms.CharField( availabilities = forms.CharField(
label=_('Availability'), label=_('Availability'),
help_text=_( help_text=_(
'Click and drag to mark the availability during the event, double-click to delete.' # Adapted help text 'Click and drag to mark the availability during the event, double-click to delete. '
'Or use the start and end inputs to add entries to the calendar view.' # Adapted help text
), ),
widget=forms.TextInput(attrs={'class': 'availabilities-editor-data'}), widget=forms.TextInput(attrs={'class': 'availabilities-editor-data'}),
required=False, required=False,
) )
def _serialize(self, event, instance): def _serialize(self, event, instance):
"""
Serialize relevant availabilities into a JSON format to populate the text field in the form
:param event: event the availabilities belong to (relevant for start and end times)
:param instance: the entity availabilities in this form should belong to (e.g., an AK, or a Room)
:return: JSON serializiation of the relevant availabilities
:rtype: str
"""
if instance: if instance:
availabilities = AvailabilitySerializer( availabilities = AvailabilitySerializer(
instance.availabilities.all(), many=True instance.availabilities.all(), many=True
...@@ -47,20 +60,28 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -47,20 +60,28 @@ class AvailabilitiesFormMixin(forms.Form):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Load event information and populate availabilities text field
self.event = self.initial.get('event') self.event = self.initial.get('event')
if isinstance(self.event, int): if isinstance(self.event, int):
self.event = Event.objects.get(pk=self.event) self.event = Event.objects.get(pk=self.event)
initial = kwargs.pop('initial', dict()) initial = kwargs.pop('initial', {})
initial['availabilities'] = self._serialize(self.event, kwargs['instance']) initial['availabilities'] = self._serialize(self.event, kwargs['instance'])
if not isinstance(self, forms.BaseModelForm): if not isinstance(self, forms.BaseModelForm):
kwargs.pop('instance') kwargs.pop('instance')
kwargs['initial'] = initial kwargs['initial'] = initial
def _parse_availabilities_json(self, jsonavailabilities): def _parse_availabilities_json(self, jsonavailabilities):
"""
Turn raw JSON availabilities into a list of model instances
:param jsonavailabilities: raw json input
:return: a list of availability objects corresponding to the raw input
:rtype: List[Availability]
"""
try: try:
rawdata = json.loads(jsonavailabilities) rawdata = json.loads(jsonavailabilities)
except ValueError: except ValueError as exc:
raise forms.ValidationError("Submitted availabilities are not valid json.") raise forms.ValidationError("Submitted availabilities are not valid json.") from exc
if not isinstance(rawdata, dict): if not isinstance(rawdata, dict):
raise forms.ValidationError( raise forms.ValidationError(
"Submitted json does not comply with expected format, should be object." "Submitted json does not comply with expected format, should be object."
...@@ -73,17 +94,32 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -73,17 +94,32 @@ class AvailabilitiesFormMixin(forms.Form):
return availabilities return availabilities
def _parse_datetime(self, strdate): def _parse_datetime(self, strdate):
"""
Parse input date string
This will try to correct timezone information if needed
:param strdate: string representing a timestamp
:return: a timestamp object
"""
tz = self.event.timezone # adapt to our event model tz = self.event.timezone # adapt to our event model
obj = parse_datetime(strdate) obj = parse_datetime(strdate)
if not obj: if not obj:
raise TypeError raise TypeError
if obj.tzinfo is None: if obj.tzinfo is None:
obj = tz.localize(obj) # Adapt to new python timezone interface
obj = obj.replace(tzinfo=tz)
return obj return obj
def _validate_availability(self, rawavail): def _validate_availability(self, rawavail):
"""
Validate a raw availability instance input by making sure the relevant fields are present and can be parsed
The cleaned up values that are produced to test the validity of the input are stored in-place in the input
object for later usage in cleaning/parsing to availability objects
:param rawavail: object to validate/clean
"""
message = _("The submitted availability does not comply with the required format.") message = _("The submitted availability does not comply with the required format.")
if not isinstance(rawavail, dict): if not isinstance(rawavail, dict):
raise forms.ValidationError(message) raise forms.ValidationError(message)
...@@ -95,12 +131,11 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -95,12 +131,11 @@ class AvailabilitiesFormMixin(forms.Form):
try: try:
rawavail['start'] = self._parse_datetime(rawavail['start']) rawavail['start'] = self._parse_datetime(rawavail['start'])
rawavail['end'] = self._parse_datetime(rawavail['end']) rawavail['end'] = self._parse_datetime(rawavail['end'])
except (TypeError, ValueError): # Adapt: Better error handling
except (TypeError, ValueError) as exc:
raise forms.ValidationError( raise forms.ValidationError(
_("The submitted availability contains an invalid date.") _("The submitted availability contains an invalid date.")
) ) from exc
tz = self.event.timezone # adapt to our event model
timeframe_start = self.event.start # adapt to our event model timeframe_start = self.event.start # adapt to our event model
if rawavail['start'] < timeframe_start: if rawavail['start'] < timeframe_start:
...@@ -114,6 +149,10 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -114,6 +149,10 @@ class AvailabilitiesFormMixin(forms.Form):
rawavail['end'] = timeframe_end rawavail['end'] = timeframe_end
def clean_availabilities(self): def clean_availabilities(self):
"""
Turn raw availabilities into real availability objects
:return:
"""
data = self.cleaned_data.get('availabilities') data = self.cleaned_data.get('availabilities')
required = ( required = (
'availabilities' in self.fields and self.fields['availabilities'].required 'availabilities' in self.fields and self.fields['availabilities'].required
...@@ -134,7 +173,8 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -134,7 +173,8 @@ class AvailabilitiesFormMixin(forms.Form):
return availabilities return availabilities
def _set_foreignkeys(self, instance, availabilities): def _set_foreignkeys(self, instance, availabilities):
"""Set the reference to `instance` in each given availability. """
Set the reference to `instance` in each given availability.
For example, set the availabilitiy.room_id to instance.id, in For example, set the availabilitiy.room_id to instance.id, in
case instance of type Room. case instance of type Room.
""" """
...@@ -144,10 +184,20 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -144,10 +184,20 @@ class AvailabilitiesFormMixin(forms.Form):
setattr(avail, reference_name, instance.id) setattr(avail, reference_name, instance.id)
def _replace_availabilities(self, instance, availabilities: [Availability]): def _replace_availabilities(self, instance, availabilities: [Availability]):
"""
Replace the existing list of availabilities belonging to an entity with a new, updated one
This will trigger a post_save signal for usage in constraint violation checking
:param instance: entity the availabilities belong to
:param availabilities: list of new availabilities
"""
with transaction.atomic(): with transaction.atomic():
# TODO: do not recreate objects unnecessarily, give the client the IDs, so we can track modifications and leave unchanged objects alone # TODO: do not recreate objects unnecessarily, give the client the IDs, so we can track modifications and
# leave unchanged objects alone
instance.availabilities.all().delete() instance.availabilities.all().delete()
Availability.objects.bulk_create(availabilities) Availability.objects.bulk_create(availabilities)
# Adaption:
# Trigger post save signal manually to make sure constraints are updated accordingly # Trigger post save signal manually to make sure constraints are updated accordingly
# Doing this one time is sufficient, since this will nevertheless update all availability constraint # Doing this one time is sufficient, since this will nevertheless update all availability constraint
# violations of the corresponding AK # violations of the corresponding AK
...@@ -155,6 +205,9 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -155,6 +205,9 @@ class AvailabilitiesFormMixin(forms.Form):
post_save.send(Availability, instance=availabilities[0], created=True) post_save.send(Availability, instance=availabilities[0], created=True)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
"""
Override the saving method of the (model) form
"""
instance = super().save(*args, **kwargs) instance = super().save(*args, **kwargs)
availabilities = self.cleaned_data.get('availabilities') availabilities = self.cleaned_data.get('availabilities')
......
...@@ -23,6 +23,9 @@ zero_time = datetime.time(0, 0) ...@@ -23,6 +23,9 @@ zero_time = datetime.time(0, 0)
# add meta class # add meta class
# enable availabilities for AKs and AKCategories # enable availabilities for AKs and AKCategories
# add verbose names and help texts to model attributes # add verbose names and help texts to model attributes
# adapt or extemd documentation
class Availability(models.Model): class Availability(models.Model):
"""The Availability class models when people or rooms are available for. """The Availability class models when people or rooms are available for.
...@@ -31,6 +34,8 @@ class Availability(models.Model): ...@@ -31,6 +34,8 @@ class Availability(models.Model):
span multiple days, but due to our choice of input widget, it will span multiple days, but due to our choice of input widget, it will
usually only span a single day at most. usually only span a single day at most.
""" """
# pylint: disable=broad-exception-raised
event = models.ForeignKey( event = models.ForeignKey(
to=Event, to=Event,
related_name='availabilities', related_name='availabilities',
...@@ -96,10 +101,10 @@ class Availability(models.Model): ...@@ -96,10 +101,10 @@ class Availability(models.Model):
are the same. are the same.
""" """
return all( return all(
[ (
getattr(self, attribute, None) == getattr(other, attribute, None) getattr(self, attribute, None) == getattr(other, attribute, None)
for attribute in ['event', 'person', 'room', 'ak', 'ak_category', 'start', 'end'] for attribute in ['event', 'person', 'room', 'ak', 'ak_category', 'start', 'end']
] )
) )
@cached_property @cached_property
...@@ -233,10 +238,28 @@ class Availability(models.Model): ...@@ -233,10 +238,28 @@ class Availability(models.Model):
@property @property
def simplified(self): def simplified(self):
return f'{self.start.astimezone(self.event.timezone).strftime("%a %H:%M")}-{self.end.astimezone(self.event.timezone).strftime("%a %H:%M")}' """
Get a simplified (only Weekday, hour and minute) string representation of an availability
:return: simplified string version
:rtype: str
"""
return (f'{self.start.astimezone(self.event.timezone).strftime("%a %H:%M")}-'
f'{self.end.astimezone(self.event.timezone).strftime("%a %H:%M")}')
@classmethod @classmethod
def with_event_length(cls, event, person=None, room=None, ak=None, ak_category=None): def with_event_length(cls, event, person=None, room=None, ak=None, ak_category=None):
"""
Create an availability covering exactly the time between event start and event end.
Can e.g., be used to create default availabilities.
:param event: relevant event
:param person: person, if availability should be connected to a person
:param room: room, if availability should be connected to a room
:param ak: ak, if availability should be connected to a ak
:param ak_category: ak_category, if availability should be connected to a ak_category
:return: availability associated to the entity oder entities selected
:rtype: Availability
"""
timeframe_start = event.start # adapt to our event model timeframe_start = event.start # adapt to our event model
# add 1 day, not 24 hours, https://stackoverflow.com/a/25427822/2486196 # add 1 day, not 24 hours, https://stackoverflow.com/a/25427822/2486196
timeframe_end = event.end # adapt to our event model timeframe_end = event.end # adapt to our event model
......