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
Select Git revision
  • koma/feature/preference-polling-form
  • main
  • renovate/beautifulsoup4-4.x
  • renovate/django-5.x
  • renovate/django-bootstrap5-25.x
  • renovate/django-debug-toolbar-6.x
  • renovate/django-tex-1.x
  • renovate/djangorestframework-3.x
  • renovate/jsonschema-4.x
9 results

Target

Select target project
No results found
Select Git revision
  • koma/feature/preference-polling-form
  • main
  • renovate/beautifulsoup4-4.x
  • renovate/django-5.x
  • renovate/django-bootstrap5-25.x
  • renovate/django-debug-toolbar-6.x
  • renovate/django-tex-1.x
  • renovate/djangorestframework-3.x
  • renovate/jsonschema-4.x
9 results
Show changes
81 files
+ 2931
838
Compare changes
  • Side-by-side
  • Inline

Files

+44 −5
Original line number Diff line number Diff line
@@ -18,16 +18,16 @@ before_script:
  - python -V  # Print out python version for debugging
  - apt-get -qq update
  - apt-get -qq install -y python3-virtualenv python3 python3-dev python3-pip gettext default-mysql-client default-libmysqlclient-dev
  - export DJANGO_SETTINGS_MODULE=AKPlanning.settings_ci
  - ./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

check:
  script:
    - ./Utils/check.sh --all

check-migrations:
  script:
    - source venv/bin/activate
    - ./manage.py makemigrations --dry-run --check

@@ -48,3 +48,42 @@ test:
        coverage_format: cobertura
        path: coverage.xml
      junit: unit.xml

lint:
  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:
  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
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@ from AKDashboard.models import DashboardButton

@admin.register(DashboardButton)
class DashboardButtonAdmin(admin.ModelAdmin):
    """
    Admin interface for dashboard buttons
    """
    list_display = ['text', 'url', 'event']
    list_filter = ['event']
    search_fields = ['text', 'url']
Original line number Diff line number Diff line
@@ -2,4 +2,7 @@ from django.apps import AppConfig


class AkdashboardConfig(AppConfig):
    """
    App configuration for dashboard (default)
    """
    name = 'AKDashboard'
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-05-15 20:03+0200\n"
"POT-Creation-Date: 2023-08-16 16:30+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -17,47 +17,47 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: AKDashboard/models.py:10
#: AKDashboard/models.py:21
msgid "Dashboard Button"
msgstr "Dashboard-Button"

#: AKDashboard/models.py:11
#: AKDashboard/models.py:22
msgid "Dashboard Buttons"
msgstr "Dashboard-Buttons"

#: AKDashboard/models.py:21
#: AKDashboard/models.py:32
msgid "Text"
msgstr "Text"

#: AKDashboard/models.py:22
#: AKDashboard/models.py:33
msgid "Text that will be shown on the button"
msgstr "Text, der auf dem Button angezeigt wird"

#: AKDashboard/models.py:23
#: AKDashboard/models.py:34
msgid "Link URL"
msgstr "Link-URL"

#: AKDashboard/models.py:23
#: AKDashboard/models.py:34
msgid "URL this button links to"
msgstr "URL auf die der Button verweist"

#: AKDashboard/models.py:24
#: AKDashboard/models.py:35
msgid "Icon"
msgstr "Symbol"

#: AKDashboard/models.py:26
#: AKDashboard/models.py:37
msgid "Button Style"
msgstr "Stil des Buttons"

#: AKDashboard/models.py:26
#: AKDashboard/models.py:37
msgid "Style (Color) of this button (bootstrap class)"
msgstr "Stiel (Farbe) des Buttons (Bootstrap-Klasse)"

#: AKDashboard/models.py:28
#: AKDashboard/models.py:39
msgid "Event"
msgstr "Veranstaltung"

#: AKDashboard/models.py:28
#: AKDashboard/models.py:39
msgid "Event this button belongs to"
msgstr "Veranstaltung, zu der dieser Button gehört"

@@ -105,22 +105,22 @@ msgstr "AK-Einreichung"
msgid "AK History"
msgstr "AK-Verlauf"

#: AKDashboard/views.py:42
#: AKDashboard/views.py:59
#, python-format
msgid "New AK: %(ak)s."
msgstr "Neuer AK: %(ak)s."

#: AKDashboard/views.py:45
#: AKDashboard/views.py:62
#, python-format
msgid "AK \"%(ak)s\" edited."
msgstr "AK \"%(ak)s\" bearbeitet."

#: AKDashboard/views.py:48
#: AKDashboard/views.py:65
#, python-format
msgid "AK \"%(ak)s\" deleted."
msgstr "AK \"%(ak)s\" gelöscht."

#: AKDashboard/views.py:60
#: AKDashboard/views.py:80
#, python-format
msgid "AK \"%(ak)s\" (re-)scheduled."
msgstr "AK \"%(ak)s\" (um-)geplant."
Original line number Diff line number Diff line
@@ -6,6 +6,17 @@ from AKModel.models import Event


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:
        verbose_name = _("Dashboard Button")
        verbose_name_plural = _("Dashboard Buttons")
Original line number Diff line number Diff line
@@ -10,8 +10,14 @@ from AKModel.tests import BasicViewTests


class DashboardTests(TestCase):
    """
    Specific Dashboard Tests
    """
    @classmethod
    def setUpTestData(cls):
        """
        Initialize Test database
        """
        super().setUpTestData()
        cls.event = Event.objects.create(
            name="Dashboard Test Event",
@@ -28,17 +34,30 @@ class DashboardTests(TestCase):
        )

    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})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    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"})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    @override_settings(DASHBOARD_SHOW_RECENT=True)
    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})

        # History should be empty
@@ -57,6 +76,11 @@ class DashboardTests(TestCase):
        self.assertEqual(response.context["recent_changes"][0]['text'], "New AK: Test AK.")

    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_event_dashboard = reverse('dashboard:dashboard_event', kwargs={"slug": self.event.slug})

@@ -79,6 +103,9 @@ class DashboardTests(TestCase):
        self.assertTrue(self.event in response.context["events"])

    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})

        if apps.is_installed('AKSubmission'):
@@ -95,6 +122,9 @@ class DashboardTests(TestCase):
            self.assertContains(response, "AK Submission")

    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})

        if apps.is_installed('AKPlan'):
@@ -114,6 +144,9 @@ class DashboardTests(TestCase):
            self.assertContains(response, "AK Wall")

    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})

        response = self.client.get(url_event_dashboard)
@@ -129,6 +162,9 @@ class DashboardTests(TestCase):


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'
Original line number Diff line number Diff line
from django.apps import apps
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic import TemplateView, DetailView
@@ -10,6 +9,11 @@ from AKPlanning import settings


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'

    @method_decorator(ensure_csrf_cookie)
@@ -23,6 +27,14 @@ class DashboardView(TemplateView):


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'
    context_object_name = 'event'
    model = Event
@@ -32,11 +44,16 @@ class DashboardEventView(DetailView):

        # Show feed of recent changes (if activated)
        if settings.DASHBOARD_SHOW_RECENT:
            # Create a list of chronically sorted events (both AK and plan changes):
            recent_changes = []

            # Newest AKs
            # Newest AKs (if AKSubmission is used)
            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:
                    if s.history_type == '+':
                        text = _('New AK: %(ak)s.') % {'ak': s.name}
@@ -48,14 +65,17 @@ class DashboardEventView(DetailView):
                        text = _('AK "%(ak)s" deleted.') % {'ak': s.name}
                        icon = ('times', 'fas')

                    recent_changes.append({'icon': icon, 'text': text, 'link': s.instance.detail_url, 'timestamp': s.history_date})
                    # Store representation in change list (still unsorted)
                    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:
                    last_changed_slots = AKSlot.objects.select_related('ak').filter(event=context['event'], start__isnull=False).order_by('-updated')[
                                         :int(settings.DASHBOARD_RECENT_MAX)]
            # Changes in plan (if AKPlan is used and plan is publicly visible)
            if apps.is_installed("AKPlan") and not context['event'].plan_hidden:
                # Get the latest plan changes (again using a threshold, see above)
                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
                for changed_slot in last_changed_slots:
                    # Create textual representation including icons and links and store in list (still unsorted)
                    recent_changes.append({'icon': ('clock', 'far'),
                                           'text': _('AK "%(ak)s" (re-)scheduled.') % {'ak': changed_slot.ak.name},
                                           'link': changed_slot.ak.detail_url,
+227 −91

File changed.

Preview size limit exceeded, changes collapsed.

+7 −0
Original line number Diff line number Diff line
@@ -3,8 +3,15 @@ from django.contrib.admin.apps import AdminConfig


class AkmodelConfig(AppConfig):
    """
    App configuration (default, only specifies name of the app)
    """
    name = 'AKModel'


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'
Original line number Diff line number Diff line
# This part of the code was adapted from pretalx (https://github.com/pretalx/pretalx)
# Copyright 2017-2019, Tobias Kunze
# 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 json

@@ -17,6 +17,10 @@ from AKModel.models import Event


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(
        label=_('Availability'),
        help_text=_(
@@ -28,6 +32,14 @@ class AvailabilitiesFormMixin(forms.Form):
    )

    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:
            availabilities = AvailabilitySerializer(
                instance.availabilities.all(), many=True
@@ -48,20 +60,28 @@ class AvailabilitiesFormMixin(forms.Form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Load event information and populate availabilities text field
        self.event = self.initial.get('event')
        if isinstance(self.event, int):
            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'])
        if not isinstance(self, forms.BaseModelForm):
            kwargs.pop('instance')
        kwargs['initial'] = initial

    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:
            rawdata = json.loads(jsonavailabilities)
        except ValueError:
            raise forms.ValidationError("Submitted availabilities are not valid json.")
        except ValueError as exc:
            raise forms.ValidationError("Submitted availabilities are not valid json.") from exc
        if not isinstance(rawdata, dict):
            raise forms.ValidationError(
                "Submitted json does not comply with expected format, should be object."
@@ -74,17 +94,32 @@ class AvailabilitiesFormMixin(forms.Form):
        return availabilities

    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

        obj = parse_datetime(strdate)
        if not obj:
            raise TypeError
        if obj.tzinfo is None:
            # Adapt to new python timezone interface
            obj = obj.replace(tzinfo=tz)

        return obj

    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.")
        if not isinstance(rawavail, dict):
            raise forms.ValidationError(message)
@@ -96,12 +131,11 @@ class AvailabilitiesFormMixin(forms.Form):
        try:
            rawavail['start'] = self._parse_datetime(rawavail['start'])
            rawavail['end'] = self._parse_datetime(rawavail['end'])
        except (TypeError, ValueError):
        # Adapt: Better error handling
        except (TypeError, ValueError) as exc:
            raise forms.ValidationError(
                _("The submitted availability contains an invalid date.")
            )

        tz = self.event.timezone  # adapt to our event model
            ) from exc

        timeframe_start = self.event.start  # adapt to our event model
        if rawavail['start'] < timeframe_start:
@@ -115,6 +149,10 @@ class AvailabilitiesFormMixin(forms.Form):
            rawavail['end'] = timeframe_end

    def clean_availabilities(self):
        """
        Turn raw availabilities into real availability objects
        :return:
        """
        data = self.cleaned_data.get('availabilities')
        required = (
                'availabilities' in self.fields and self.fields['availabilities'].required
@@ -135,7 +173,8 @@ class AvailabilitiesFormMixin(forms.Form):
        return 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
        case instance of type Room.
        """
@@ -145,10 +184,20 @@ class AvailabilitiesFormMixin(forms.Form):
            setattr(avail, reference_name, instance.id)

    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():
            # 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()
            Availability.objects.bulk_create(availabilities)
            # Adaption:
            # 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
            # violations of the corresponding AK
@@ -156,6 +205,9 @@ class AvailabilitiesFormMixin(forms.Form):
                post_save.send(Availability, instance=availabilities[0], created=True)

    def save(self, *args, **kwargs):
        """
        Override the saving method of the (model) form
        """
        instance = super().save(*args, **kwargs)
        availabilities = self.cleaned_data.get('availabilities')

Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ zero_time = datetime.time(0, 0)
# add meta class
# enable availabilities for AKs and AKCategories
# add verbose names and help texts to model attributes
# adapt or extemd documentation


class Availability(models.Model):
    """The Availability class models when people or rooms are available for.

@@ -31,6 +34,8 @@ class Availability(models.Model):
    span multiple days, but due to our choice of input widget, it will
    usually only span a single day at most.
    """
    # pylint: disable=broad-exception-raised

    event = models.ForeignKey(
        to=Event,
        related_name='availabilities',
@@ -96,10 +101,10 @@ class Availability(models.Model):
        are the same.
        """
        return all(
            [
            (
                getattr(self, attribute, None) == getattr(other, attribute, None)
                for attribute in ['event', 'person', 'room', 'ak', 'ak_category', 'start', 'end']
            ]
            )
        )

    @cached_property
@@ -233,10 +238,28 @@ class Availability(models.Model):

    @property
    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
    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
        # add 1 day, not 24 hours, https://stackoverflow.com/a/25427822/2486196
        timeframe_end = event.end  # adapt to our event model
Original line number Diff line number Diff line
# This part of the code was adapted from pretalx (https://github.com/pretalx/pretalx)
# Copyright 2017-2019, Tobias Kunze
# 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
from django.utils import timezone
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer
@@ -10,19 +10,35 @@ from AKModel.availability.models import Availability


class AvailabilitySerializer(ModelSerializer):
    """
    REST Framework Serializer for Availability
    """
    allDay = SerializerMethodField()
    start = SerializerMethodField()
    end = SerializerMethodField()

    def get_allDay(self, obj):
    def get_allDay(self, obj):  # pylint: disable=invalid-name
        """
        Bridge between naming conventions of python and fullcalendar by providing the all_day field as allDay, too
        """
        return obj.all_day

    # Use already localized strings in serialized field
    # (default would be UTC, but that would require heavy timezone calculation on client side)
    def get_start(self, obj):
        """
        Get start timestamp

        Use already localized strings in serialized field
        (default would be UTC, but that would require heavy timezone calculation on client side)
        """
        return timezone.localtime(obj.start, obj.event.timezone).strftime("%Y-%m-%dT%H:%M:%S")

    def get_end(self, obj):
        """
        Get end timestamp

        Use already localized strings in serialized field
        (default would be UTC, but that would require heavy timezone calculation on client side)
        """
        return timezone.localtime(obj.end, obj.event.timezone).strftime("%Y-%m-%dT%H:%M:%S")

    class Meta:
Original line number Diff line number Diff line
# environment.py
"""
Environment definitions
Needed for tex compilation
"""
import re

from django_tex.environment import environment

# Used to filter all very special UTF-8 chars that are probably not contained in the LaTeX fonts
# and would hence cause compilation errors
utf8_replace_pattern = re.compile(u'[^\u0000-\u206F]', re.UNICODE)
utf8_replace_pattern = re.compile('[^\u0000-\u206F]', re.UNICODE)


def latex_escape_utf8(value):
@@ -17,12 +20,14 @@ def latex_escape_utf8(value):
    :return: escaped string
    :rtype: str
    """
    return utf8_replace_pattern.sub('', value).replace('&', '\&').replace('_', '\_').replace('#', '\#').replace('$',
                                                                                                                '\$').replace(
        '%', '\%').replace('{', '\{').replace('}', '\}')
    return (utf8_replace_pattern.sub('', value).replace('&', r'\&').replace('_', r'\_').replace('#', r'\#').
            replace('$', r'\$').replace('%', r'\%').replace('{', r'\{').replace('}', r'\}'))


def improved_tex_environment(**options):
    """
    Encapsulate our improved latex environment for usage
    """
    env = environment(**options)
    env.filters.update({
        'latex_escape_utf8': latex_escape_utf8,
+79 −4
Original line number Diff line number Diff line
"""
Central and admin forms
"""

import csv
import io

@@ -11,6 +15,17 @@ from AKModel.models import Event, AKCategory, AKRequirement, Room


class NewEventWizardStartForm(forms.ModelForm):
    """
    Initial view of new event wizard

    This form is a model form for Event, but only with a subset of the required fields.
    It is therefore not possible to really create an event using this form, but only to enter
    information, in particular the timezone, that is needed to correctly handle/parse the user
    inputs for further required fields like start and end of the event.

    The form will be used for this partial input, the input of the remaining required fields
    will then be handled by :class:`NewEventWizardSettingsForm` (see below).
    """
    class Meta:
        model = Event
        fields = ['name', 'slug', 'timezone', 'plan_hidden']
@@ -18,13 +33,20 @@ class NewEventWizardStartForm(forms.ModelForm):
            'plan_hidden': forms.HiddenInput(),
        }

    # Special hidden field for wizard state handling
    is_init = forms.BooleanField(initial=True, widget=forms.HiddenInput)


class NewEventWizardSettingsForm(forms.ModelForm):
    """
    Form for second view of the event creation wizard.

    Will handle the input of the remaining required as well as some optional fields.
    See also :class:`NewEventWizardStartForm`.
    """
    class Meta:
        model = Event
        exclude = []
        fields = "__all__"
        widgets = {
            'name': forms.HiddenInput(),
            'slug': forms.HiddenInput(),
@@ -38,6 +60,10 @@ class NewEventWizardSettingsForm(forms.ModelForm):


class NewEventWizardPrepareImportForm(forms.Form):
    """
    Wizard form for choosing an event to import/copy elements (requirements, categories, etc) from.
    Is used to restrict the list of elements to choose from in the next step (see :class:`NewEventWizardImportForm`).
    """
    import_event = forms.ModelChoiceField(
        queryset=Event.objects.all(),
        label=_("Copy ak requirements and ak categories of existing event"),
@@ -46,6 +72,12 @@ class NewEventWizardPrepareImportForm(forms.Form):


class NewEventWizardImportForm(forms.Form):
    """
    Wizard form for excaclty choosing which elemments to copy/import for the newly created event.
    Possible elements are categories, requirements, and dashboard buttons if AKDashboard is active.
    The lists are restricted to elements from the event selected in the previous step
    (see :class:`NewEventWizardPrepareImportForm`).
    """
    import_categories = forms.ModelMultipleChoiceField(
        queryset=AKCategory.objects.all(),
        widget=forms.CheckboxSelectMultiple,
@@ -60,6 +92,7 @@ class NewEventWizardImportForm(forms.Form):
        required=False,
    )

    # pylint: disable=too-many-arguments
    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList,
                 label_suffix=None, empty_permitted=False, field_order=None, use_required_attribute=None,
                 renderer=None):
@@ -70,10 +103,12 @@ class NewEventWizardImportForm(forms.Form):
        self.fields["import_requirements"].queryset = self.fields["import_requirements"].queryset.filter(
            event=self.initial["import_event"])

        # pylint: disable=import-outside-toplevel
        # Local imports used to prevent cyclic imports and to only import when AKDashboard is available
        from django.apps import apps
        if apps.is_installed("AKDashboard"):
            # If AKDashboard is active, allow to copy dashboard buttons, too
            from AKDashboard.models import DashboardButton

            self.fields["import_buttons"] = forms.ModelMultipleChoiceField(
                queryset=DashboardButton.objects.filter(event=self.initial["import_event"]),
                widget=forms.CheckboxSelectMultiple,
@@ -83,20 +118,37 @@ class NewEventWizardImportForm(forms.Form):


class NewEventWizardActivateForm(forms.ModelForm):
    """
    Wizard form to activate the newly created event
    """
    class Meta:
        fields = ["active"]
        model = Event


class AdminIntermediateForm(forms.Form):
    pass
    """
    Base form for admin intermediate views (forms used there should inherit from this,
    by default, the form is empty since it is only needed for the confirmation button)
    """


class AdminIntermediateActionForm(AdminIntermediateForm):
    """
    Form for Admin Action Confirmation views -- has a pks field needed to handle the serialization/deserialization of
    the IDs of the entities the user selected for the admin action to be performed on
    """
    pks = forms.CharField(widget=forms.HiddenInput)


class SlideExportForm(AdminIntermediateForm):
    """
    Form to control the slides generated from the AK list of an event

    The user can select how many upcoming AKs are displayed at the footer to inform people that it is their turn soon,
    whether the AK list should be restricted to those AKs that where marked for presentation, and whether ther should
    be a symbol and empty space to take notes on for wishes
    """
    num_next = forms.IntegerField(
        min_value=0,
        max_value=6,
@@ -121,6 +173,9 @@ class SlideExportForm(AdminIntermediateForm):


class DefaultSlotEditorForm(AdminIntermediateForm):
    """
    Form for default slot editor
    """
    availabilities = forms.CharField(
        label=_('Default Slots'),
        help_text=_(
@@ -133,6 +188,12 @@ class DefaultSlotEditorForm(AdminIntermediateForm):


class RoomBatchCreationForm(AdminIntermediateForm):
    """
    Form for room batch creation

    Allows to input a list of room details and choose whether default availabilities should be generated for these
    rooms. Will check that the input follows a CSV format with at least a name column present.
    """
    rooms = forms.CharField(
        label=_('New rooms'),
        help_text=_('Enter room details in CSV format. Required colum is "name", optional colums are "location", '
@@ -147,6 +208,13 @@ class RoomBatchCreationForm(AdminIntermediateForm):
    )

    def clean_rooms(self):
        """
        Validate and transform the input for the rooms textfield
        Treat the input as CSV and turn it into a dict containing the relevant information.

        :return: a dict containing the raw room information
        :rtype: dict[str, str]
        """
        rooms_raw_text = self.cleaned_data["rooms"]
        rooms_raw_dict = csv.DictReader(io.StringIO(rooms_raw_text), delimiter=";")

@@ -157,6 +225,10 @@ class RoomBatchCreationForm(AdminIntermediateForm):


class RoomForm(forms.ModelForm):
    """
    Room (creation) form (basic), will be extended for handling of availabilities
    (see :class:`RoomFormWithAvailabilities`) and also for creating hybrid rooms in AKOnline (if active)
    """
    class Meta:
        model = Room
        fields = ['name',
@@ -167,6 +239,9 @@ class RoomForm(forms.ModelForm):


class RoomFormWithAvailabilities(AvailabilitiesFormMixin, RoomForm):
    """
    Room (update) form including handling of availabilities, extends :class:`RoomForm`
    """
    class Meta:
        model = Room
        fields = ['name',
@@ -182,7 +257,7 @@ class RoomFormWithAvailabilities(AvailabilitiesFormMixin, RoomForm):

    def __init__(self, *args, **kwargs):
        # Init availability mixin
        kwargs['initial'] = dict()
        kwargs['initial'] = {}
        super().__init__(*args, **kwargs)
        self.initial = {**self.initial, **kwargs['initial']}
        # Filter possible values for m2m when event is specified
Original line number Diff line number Diff line
"""
Ensure PO files are generated using forward slashes in the location comments on all operating systems
"""
import os

from django.core.management.commands.makemessages import Command as MakeMessagesCommand


class Command(MakeMessagesCommand):
    """
    Adapted version of the :class:`MakeMessagesCommand`
    Ensure PO files are generated using forward slashes in the location comments on all operating systems
    """
    def find_files(self, root):
        # Replace backward slashes with forward slashes if necessary in file list
        all_files = super().find_files(root)
        if os.sep != "\\":
            return all_files
@@ -21,12 +23,14 @@ class Command(MakeMessagesCommand):
        return all_files

    def build_potfiles(self):
        # Replace backward slashes with forward slashes if necessary in the files itself
        pot_files = super().build_potfiles()
        if os.sep != "\\":
            return pot_files

        for filename in pot_files:
            lines = open(filename, "r", encoding="utf-8").readlines()
            with open(filename, "r", encoding="utf-8") as f:
                lines = f.readlines()
                fixed_lines = []
                for line in lines:
                    if line.startswith("#: "):
Original line number Diff line number Diff line
from AKModel.metaviews.status import StatusManager

# create on instance of the :class:`AKModel.metaviews.status.StatusManager`
# that can then be accessed everywhere (singleton pattern)
status_manager = StatusManager()
Original line number Diff line number Diff line
@@ -13,36 +13,61 @@ from AKModel.models import Event
class EventSlugMixin:
    """
    Mixin to handle views with event slugs

    This will make the relevant event available as self.event in all kind types of requests
    (generic GET and POST views, list views, dispatching, create views)
    """
    # pylint: disable=no-member
    event = None

    def _load_event(self):
        """
        Perform the real loading of the event data (as specified by event_slug in the URL) into self.event
        """
        # Find event based on event slug
        if self.event is None:
            self.event = get_object_or_404(Event, slug=self.kwargs.get("event_slug", None))

    def get(self, request, *args, **kwargs):
        """
        Override GET request handling to perform loading event first
        """
        self._load_event()
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """
        Override POST request handling to perform loading event first
        """
        self._load_event()
        return super().post(request, *args, **kwargs)

    def list(self, request, *args, **kwargs):
        """
        Override list view request handling to perform loading event first
        """
        self._load_event()
        return super().list(request, *args, **kwargs)

    def create(self, request, *args, **kwargs):
        """
        Override create view request handling to perform loading event first
        """
        self._load_event()
        return super().create(request, *args, **kwargs)

    def dispatch(self, request, *args, **kwargs):
        """
        Override dispatch which is called in many generic views to perform loading event first
        """
        if self.event is None:
            self._load_event()
        return super().dispatch(request, *args, **kwargs)

    def get_context_data(self, *, object_list=None, **kwargs):
        """
        Override `get_context_data` to make the event information available in the rendering context as `event`. too
        """
        context = super().get_context_data(object_list=object_list, **kwargs)
        # Add event to context (to make it accessible in templates)
        context["event"] = self.event
@@ -55,15 +80,29 @@ class FilterByEventSlugMixin(EventSlugMixin):
    """

    def get_queryset(self):
        # Filter current queryset based on url event slug or return 404 if event slug is invalid
        """
        Get adapted queryset:
        Filter current queryset based on url event slug or return 404 if event slug is invalid
        :return: Queryset
        """
        return super().get_queryset().filter(event=self.event)


class AdminViewMixin:
    """
    Mixin to provide context information needed in custom admin views

    Will either use default information for `site_url` and `title` or allows to set own values for that
    """
    # pylint: disable=too-few-public-methods

    site_url = ''
    title = ''

    def get_context_data(self, **kwargs):
        """
        Extend context
        """
        extra = admin.site.each_context(self.request)
        extra.update(super().get_context_data(**kwargs))

@@ -76,10 +115,19 @@ class AdminViewMixin:


class IntermediateAdminView(AdminViewMixin, FormView):
    """
    Metaview: Handle typical "action but with preview and confirmation step before" workflow
    """
    template_name = "admin/AKModel/action_intermediate.html"
    form_class = AdminIntermediateForm

    def get_preview(self):
        """
        Render a preview of the action to be performed.
        Default is empty
        :return: preview (html)
        :rtype: str
        """
        return ""

    def get_context_data(self, **kwargs):
@@ -90,7 +138,18 @@ class IntermediateAdminView(AdminViewMixin, FormView):


class WizardViewMixin:
    """
    Mixin to create wizard-like views.
    This visualizes the progress of the user in the creation process and provides the interlinking to the next step

    In the current implementation, the steps of the wizard are hardcoded here,
    hence this mixin can only be used for the event creation wizard
    """
    # pylint: disable=too-few-public-methods
    def get_context_data(self, **kwargs):
        """
        Extend context
        """
        context = super().get_context_data(**kwargs)
        context["wizard_step"] = self.wizard_step
        context["wizard_steps"] = [
@@ -107,10 +166,23 @@ class WizardViewMixin:


class IntermediateAdminActionView(IntermediateAdminView, ABC):
    """
    Abstract base view: Intermediate action view (preview & confirmation see :class:`IntermediateAdminView`)
    for custom admin actions (marking multiple objects in a django admin model instances list with a checkmark and then
    choosing an action from the dropdown).

    This will automatically handle the decoding of the URL encoding of the list of primary keys django does to select
    which objects the action should be run on, then display a preview, perform the action after confirmation and
    redirect again to the object list including display of a confirmation message
    """
    # pylint: disable=no-member
    form_class = AdminIntermediateActionForm
    entities = None

    def get_queryset(self, pks=None):
        """
        Get the queryset of objects to perform the action on
        """
        if pks is None:
            pks = self.request.GET['pks']
        return self.model.objects.filter(pk__in=pks.split(","))
@@ -130,7 +202,10 @@ class IntermediateAdminActionView(IntermediateAdminView, ABC):

    @abstractmethod
    def action(self, form):
        pass
        """
        The real action to perform
        :param form: form holding the data probably needed for the action
        """

    def form_valid(self, form):
        self.entities = self.get_queryset(pks=form.cleaned_data['pks'])
@@ -140,7 +215,21 @@ class IntermediateAdminActionView(IntermediateAdminView, ABC):


class LoopActionMixin(ABC):
    def action(self, form):
    """
    Mixin for the typical kind of action where one needs to loop over all elements
    and perform a certain function on each of them

    The action is performed by overriding `perform_action(self, entity)`
    further customization can be reached with the two callbacks `pre_action()` and `post_action()`
    that are called before and after performing the action loop
    """
    def action(self, form):  # pylint: disable=unused-argument
        """
        The real action to perform.
        Will perform the loop, perform the action on each aelement and call the callbacks

        :param form: form holding the data probably needed for the action
        """
        self.pre_action()
        for entity in self.entities:
            self.perform_action(entity)
@@ -149,10 +238,18 @@ class LoopActionMixin(ABC):

    @abstractmethod
    def perform_action(self, entity):
        pass
        """
        Action to perform on each entity

        :param entity: entity to perform the action on
        """

    def pre_action(self):
        pass
        """
        Callback for custom action before loop starts
        """

    def post_action(self):
        pass
        """
        Callback for custom action after loop finished
        """
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@ from AKModel.metaviews.admin import AdminViewMixin


class StatusWidget(ABC):
    """
    Abstract parent for status page widgets
    """
    title = "Status Widget"
    actions = []
    status = "primary"
@@ -18,7 +21,6 @@ class StatusWidget(ABC):
        """
        Which model/context is needed to render this widget?
        """
        pass

    def get_context_data(self, context) -> dict:
        """
@@ -32,6 +34,7 @@ class StatusWidget(ABC):
        Render widget based on context

        :param context: Context for rendering
        :param request: HTTP request, needed for rendering
        :return: Dictionary containing the rendered/prepared information
        """
        context = self.get_context_data(context)
@@ -42,7 +45,7 @@ class StatusWidget(ABC):
            "status": self.render_status(context),
        }

    def render_title(self, context: {}) -> str:
    def render_title(self, context: {}) -> str:  # pylint: disable=unused-argument
        """
        Render title for widget based on context

@@ -52,7 +55,7 @@ class StatusWidget(ABC):
        """
        return self.title

    def render_status(self, context: {}) -> str:
    def render_status(self, context: {}) -> str:  # pylint: disable=unused-argument
        """
        Render status for widget based on context

@@ -63,16 +66,16 @@ class StatusWidget(ABC):
        return self.status

    @abstractmethod
    def render_body(self, context: {}, request) -> str:
    def render_body(self, context: {}, request) -> str:  # pylint: disable=unused-argument
        """
        Render body for widget based on context

        :param context: Context for rendering
        :param request: HTTP request (needed for rendering)
        :return: Rendered widget body (HTML)
        """
        pass

    def render_actions(self, context: {}) -> list[dict]:
    def render_actions(self, context: {}) -> list[dict]:  # pylint: disable=unused-argument
        """
        Render actions for widget based on context

@@ -81,16 +84,30 @@ class StatusWidget(ABC):
        :param context: Context for rendering
        :return: List of actions
        """
        return [a for a in self.actions]
        return self.actions


class TemplateStatusWidget(StatusWidget):
    """
    A :class:`StatusWidget` that produces its content by rendering a given html template
    """
    @property
    @abstractmethod
    def template_name(self) -> str:
        pass
        """
        Configure the template to use
        :return: name of the template to use
        """

    def render_body(self, context: {}, request) -> str:
        """
        Render the body of the widget using the template rendering method from django
        (load and render template using the provided context)

        :param context: context to use for rendering
        :param request: HTTP request (needed by django)
        :return: rendered content (HTML)
        """
        template = loader.get_template(self.template_name)
        return template.render(context, request)

@@ -98,6 +115,8 @@ class TemplateStatusWidget(StatusWidget):
class StatusManager:
    """
    Registry for all status widgets

    Allows to register status widgets using the `@status_manager.register(name="xyz")` decorator
    """
    widgets = {}
    widgets_by_context_type = defaultdict(list)
@@ -131,6 +150,9 @@ class StatusManager:


class StatusView(ABC, AdminViewMixin, TemplateView):
    """
    Abstract view: A generic base view to create a status page holding multiple widgets
    """
    template_name = "admin/AKModel/status/status.html"

    @property
@@ -139,12 +161,15 @@ class StatusView(ABC, AdminViewMixin, TemplateView):
        """
        Which model/context is provided by this status view?
        """
        pass

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)

        from AKModel.metaviews import status_manager
        context['widgets'] = [w.render(context, self.request) for w in status_manager.get_by_context_type(self.provided_context_type)]
        # Load status manager (local import to prevent cyclic import)
        from AKModel.metaviews import status_manager  # pylint: disable=import-outside-toplevel

        # Render all widgets and provide them as part of the context
        context['widgets'] = [w.render(context, self.request)
                              for w in status_manager.get_by_context_type(self.provided_context_type)]

        return self.render_to_response(context)
+224 −36
Original line number Diff line number Diff line
@@ -14,7 +14,8 @@ from timezone_field import TimeZoneField


class Event(models.Model):
    """ An event supplies the frame for all Aks.
    """
    An event supplies the frame for all Aks.
    """
    name = models.CharField(max_length=64, unique=True, verbose_name=_('Name'),
                            help_text=_('Name or iteration of the event'))
@@ -50,8 +51,8 @@ class Event(models.Model):
                                       help_text=_('Default length in hours that is assumed for AKs in this event.'))

    contact_email = models.EmailField(verbose_name=_("Contact email address"), blank=True,
                                      help_text=_(
                                          "An email address that is displayed on every page and can be used for all kinds of questions"))
                                        help_text=_("An email address that is displayed on every page "
                                                    "and can be used for all kinds of questions"))

    class Meta:
        verbose_name = _('Event')
@@ -63,25 +64,37 @@ class Event(models.Model):

    @staticmethod
    def get_by_slug(slug):
        """
        Get event by its slug

        :param slug: slug of the event
        :return: event identified by the slug
        :rtype: Event
        """
        return Event.objects.get(slug=slug)

    @staticmethod
    def get_next_active():
        # Get first active event taking place
        """
        Get first active event taking place
        :return: matching event (if any) or None
        :rtype: Event
        """
        event = Event.objects.filter(active=True).order_by('start').first()
        # No active event? Return the next event taking place
        if event is None:
            event = Event.objects.order_by('start').filter(start__gt=datetime.now()).first()
        return event

    def get_categories_with_aks(self, wishes_seperately=False, filter=lambda ak: True, hide_empty_categories=False):
    def get_categories_with_aks(self, wishes_seperately=False,
                                filter_func=lambda ak: True, hide_empty_categories=False):
        """
        Get AKCategories as well as a list of AKs belonging to the category for this event

        :param wishes_seperately: Return wishes as individual list.
        :type wishes_seperately: bool
        :param filter: Optional filter predicate, only include AK in list if filter returns True
        :type filter: (AK)->bool
        :param filter_func: Optional filter predicate, only include AK in list if filter returns True
        :type filter_func: (AK)->bool
        :return: list of category-AK-list-tuples, optionally the additional list of AK wishes
        :rtype: list[(AKCategory, list[AK])] [, list[AK]]
        """
@@ -89,11 +102,26 @@ class Event(models.Model):
        categories_with_aks = []
        ak_wishes = []

        # Fill lists by iterating
        # A different behavior is needed depending on whether wishes should show up inside their categories
        # or as a separate category

        def _get_category_aks(category):
            """
            Get all AKs belonging to a category
            Use joining and prefetching to reduce the number of necessary SQL queries

            :param category: category the AKs should belong to
            :return: QuerySet over AKs
            :return: QuerySet[AK]
            """
            return category.ak_set.select_related('event').prefetch_related('owners', 'akslot_set').all()

        if wishes_seperately:
            for category in categories:
                ak_list = []
                for ak in category.ak_set.select_related('event').prefetch_related('owners', 'akslot_set').all():
                    if filter(ak):
                for ak in _get_category_aks(category):
                    if filter_func(ak):
                        if ak.wish:
                            ak_wishes.append(ak)
                        else:
@@ -101,21 +129,36 @@ class Event(models.Model):
                if not hide_empty_categories or len(ak_list) > 0:
                    categories_with_aks.append((category, ak_list))
            return categories_with_aks, ak_wishes
        else:

        for category in categories:
            ak_list = []
                for ak in category.ak_set.all():
                    if filter(ak):
            for ak in _get_category_aks(category):
                if filter_func(ak):
                    ak_list.append(ak)
            if not hide_empty_categories or len(ak_list) > 0:
                categories_with_aks.append((category, ak_list))
        return categories_with_aks

    def get_unscheduled_wish_slots(self):
        """
        Get all slots of wishes that are currently not scheduled
        :return: queryset of theses slots
        :rtype: QuerySet[AKSlot]
        """
        return self.akslot_set.filter(start__isnull=True).annotate(Count('ak__owners')).filter(ak__owners__count=0)

    def get_aks_without_availabilities(self):
        return self.ak_set.annotate(Count('availabilities', distinct=True)).annotate(Count('owners', distinct=True)).filter(availabilities__count=0, owners__count__gt=0)
        """
        Gt all AKs that don't have any availability at all

        :return: generator over these AKs
        :rtype: Generator[AK]
        """
        return (self.ak_set
                .annotate(Count('availabilities', distinct=True))
                .annotate(Count('owners', distinct=True))
                .filter(availabilities__count=0, owners__count__gt=0)
                )


class AKOwner(models.Model):
@@ -141,21 +184,34 @@ class AKOwner(models.Model):
        return self.name

    def _generate_slug(self):
        """
        Auto-generate a slug for an owner
        This will start with a very simple slug (the name truncated to a maximum length) and then gradually produce
        more complicated slugs when the previous candidates are already used

        :return: the slug
        :rtype: str
        """
        max_length = self._meta.get_field('slug').max_length

        # Try name alone (truncated if necessary)
        slug_candidate = slugify(self.name)[:max_length]
        if not AKOwner.objects.filter(event=self.event, slug=slug_candidate).exists():
            self.slug = slug_candidate
            return

        # Try name and institution separated by '_' (truncated if necessary)
        slug_candidate = slugify(slug_candidate + '_' + self.institution)[:max_length]
        if not AKOwner.objects.filter(event=self.event, slug=slug_candidate).exists():
            self.slug = slug_candidate
            return

        # Try name + institution + an incrementing digit
        for i in itertools.count(1):
            if not AKOwner.objects.filter(event=self.event, slug=slug_candidate).exists():
                break
            digits = len(str(i))
            slug_candidate = '{}-{}'.format(slug_candidate[:-(digits + 1)], i)
            slug_candidate = f'{slug_candidate[:-(digits + 1)]}-{i}'

        self.slug = slug_candidate

@@ -167,6 +223,15 @@ class AKOwner(models.Model):

    @staticmethod
    def get_by_slug(event, slug):
        """
        Get owner by slug
        Will be identified by the combination of event slug and owner slug which is unique

        :param event: event
        :param slug: slug of the owner
        :return: owner identified by slugs
        :rtype: AKOwner
        """
        return AKOwner.objects.get(event=event, slug=slug)


@@ -178,8 +243,8 @@ class AKCategory(models.Model):
    description = models.TextField(blank=True, verbose_name=_("Description"),
                                   help_text=_("Short description of this AK Category"))
    present_by_default = models.BooleanField(blank=True, default=True, verbose_name=_("Present by default"),
                                             help_text=_(
                                                 "Present AKs of this category by default if AK owner did not specify whether this AK should be presented?"))
                                             help_text=_("Present AKs of this category by default "
                                                 "if AK owner did not specify whether this AK should be presented?"))

    event = models.ForeignKey(to=Event, on_delete=models.CASCADE, verbose_name=_('Event'),
                              help_text=_('Associated event'))
@@ -213,6 +278,11 @@ class AKTrack(models.Model):
        return self.name

    def aks_with_category(self):
        """
        Get all AKs that belong to this track with category already joined to prevent additional SQL queries
        :return: queryset over the AKs
        :rtype: QuerySet[AK]
        """
        return self.ak_set.select_related('category').all()


@@ -268,7 +338,8 @@ class AK(models.Model):
                                           help_text=_('AKs that should precede this AK in the schedule'))

    notes = models.TextField(blank=True, verbose_name=_('Organizational Notes'), help_text=_(
        'Notes to organizers. These are public. For private notes, please use the button for private messages on the detail page of this AK (after creation/editing).'))
        'Notes to organizers. These are public. For private notes, please use the button for private messages '
        'on the detail page of this AK (after creation/editing).'))

    interest = models.IntegerField(default=-1, verbose_name=_('Interest'), help_text=_('Expected number of people'))
    interest_counter = models.IntegerField(default=0, verbose_name=_('Interest Counter'),
@@ -295,8 +366,16 @@ class AK(models.Model):

    @property
    def details(self):
        """
        Generate a detailled string representation, e.g., for usage in scheduling
        :return: string representation of that AK with all details
        :rtype: str
        """
        # local import to prevent cyclic import
        # pylint: disable=import-outside-toplevel
        from AKModel.availability.models import Availability
        availabilities = ', \n'.join(f'{a.simplified}' for a in Availability.objects.select_related('event').filter(ak=self))
        availabilities = ', \n'.join(f'{a.simplified}' for a in Availability.objects.select_related('event')
                                     .filter(ak=self))
        return f"""{self.name}{" (R)" if self.reso else ""}:
        
        {self.owners_list}
@@ -309,34 +388,74 @@ class AK(models.Model):

    @property
    def owners_list(self):
        """
        Get a list of stringified representations of all owners

        :return: list of owners
        :rtype: List[str]
        """
        return ", ".join(str(owner) for owner in self.owners.all())

    @property
    def durations_list(self):
        """
        Get a list of stringified representations of all durations of associated slots

        :return: list of durations
        :rtype: List[str]
        """
        return ", ".join(str(slot.duration_simplified) for slot in self.akslot_set.select_related('event').all())

    @property
    def wish(self):
        """
        Is the AK a wish?
        :return: true if wish, false if not
        :rtype: bool
        """
        return self.owners.count() == 0

    def increment_interest(self):
        """
        Increment the interest counter for this AK by one
        without tracking that change to prevent an unreadable and large history
        """
        self.interest_counter += 1
        self.skip_history_when_saving = True
        self.skip_history_when_saving = True  # pylint: disable=attribute-defined-outside-init
        self.save()
        del self.skip_history_when_saving

    @property
    def availabilities(self):
        """
        Get all availabilities associated to this AK
        :return: availabilities
        :rtype: QuerySet[Availability]
        """
        return "Availability".objects.filter(ak=self)

    @property
    def edit_url(self):
        """
        Get edit URL for this AK
        Will link to frontend if AKSubmission is active, otherwise to the edit view for this object in admin interface

        :return: URL
        :rtype: str
        """
        if apps.is_installed("AKSubmission"):
            return reverse_lazy('submit:ak_edit', kwargs={'event_slug': self.event.slug, 'pk': self.id})
        return reverse_lazy('admin:AKModel_ak_change', kwargs={'object_id': self.id})

    @property
    def detail_url(self):
        """
        Get detail URL for this AK
        Will link to frontend if AKSubmission is active, otherwise to the edit view for this object in admin interface

        :return: URL
        :rtype: str
        """
        if apps.is_installed("AKSubmission"):
            return reverse_lazy('submit:ak_detail', kwargs={'event_slug': self.event.slug, 'pk': self.id})
        return self.edit_url
@@ -364,6 +483,12 @@ class Room(models.Model):

    @property
    def title(self):
        """
        Get title of a room, which consists of location and name if location is set, otherwise only the name

        :return: title
        :rtype: str
        """
        if self.location:
            return f"{self.location} {self.name}"
        return self.name
@@ -429,7 +554,8 @@ class AKSlot(models.Model):
        start = self.start.astimezone(self.event.timezone)
        end = self.end.astimezone(self.event.timezone)

        return f"{start.strftime('%a %H:%M')} - {end.strftime('%H:%M') if start.day == end.day else end.strftime('%a %H:%M')}"
        return (f"{start.strftime('%a %H:%M')} - "
                f"{end.strftime('%H:%M') if start.day == end.day else end.strftime('%a %H:%M')}")

    @property
    def end(self):
@@ -448,10 +574,20 @@ class AKSlot(models.Model):
        return (timezone.now() - self.updated).total_seconds()

    def overlaps(self, other: "AKSlot"):
        """
        Check wether two slots overlap

        :param other: second slot to compare with
        :return: true if they overlap, false if not:
        :rtype: bool
        """
        return self.start < other.end <= self.end or self.start <= other.start < self.end


class AKOrgaMessage(models.Model):
    """
    Model representing confidential messages to the organizers/scheduling people, belonging to a certain AK
    """
    ak = models.ForeignKey(to=AK, on_delete=models.CASCADE, verbose_name=_('AK'),
                           help_text=_('AK this message belongs to'))
    text = models.TextField(verbose_name=_("Message text"),
@@ -470,12 +606,23 @@ class AKOrgaMessage(models.Model):


class ConstraintViolation(models.Model):
    """
    Model to represent any kind of constraint violation

    Can have two different severities: violation and warning
    The list of possible types is defined in :class:`ViolationType`
    Depending on the type, different fields (references to other models) will be filled. Each violation should always
    be related to an event and at least on other instance of a causing entity
    """
    class Meta:
        verbose_name = _('Constraint Violation')
        verbose_name_plural = _('Constraint Violations')
        ordering = ['-timestamp']

    class ViolationType(models.TextChoices):
        """
        Possible types of violations with their text representation
        """
        OWNER_TWO_SLOTS = 'ots', _('Owner has two parallel slots')
        SLOT_OUTSIDE_AVAIL = 'soa', _('AK Slot was scheduled outside the AK\'s availabilities')
        ROOM_TWO_SLOTS = 'rts', _('Room has two AK slots scheduled at the same time')
@@ -490,6 +637,9 @@ class ConstraintViolation(models.Model):
        SLOT_OUTSIDE_EVENT = 'soe', _('AK Slot is scheduled outside the event\'s availabilities')

    class ViolationLevel(models.IntegerChoices):
        """
        Possible severities/levels of a CV
        """
        WARNING = 1, _('Warning')
        VIOLATION = 10, _('Violation')

@@ -501,6 +651,7 @@ class ConstraintViolation(models.Model):
    event = models.ForeignKey(to=Event, on_delete=models.CASCADE, verbose_name=_('Event'),
                              help_text=_('Associated event'))

    # Possible "causes":
    aks = models.ManyToManyField(to=AK, blank=True, verbose_name=_('AKs'),
                                 help_text=_('AK(s) belonging to this constraint'))
    ak_slots = models.ManyToManyField(to=AKSlot, blank=True, verbose_name=_('AK Slots'),
@@ -551,22 +702,37 @@ class ConstraintViolation(models.Model):

    @property
    def details(self):
        """
        Property: Details
        """
        return self.get_details()

    @property
    def edit_url(self):
    def edit_url(self) -> str:
        """
        Property: Edit URL for this CV
        """
        return reverse_lazy('admin:AKModel_constraintviolation_change', kwargs={'object_id': self.pk})

    @property
    def level_display(self):
    def level_display(self) -> str:
        """
        Property: Severity as string
        """
        return self.get_level_display()

    @property
    def type_display(self):
    def type_display(self) -> str:
        """
        Property: Type as string
        """
        return self.get_type_display()

    @property
    def timestamp_display(self):
    def timestamp_display(self) -> str:
        """
        Property: Creation timestamp as string
        """
        return self.timestamp.astimezone(self.event.timezone).strftime('%d.%m.%y %H:%M')

    @property
@@ -585,7 +751,10 @@ class ConstraintViolation(models.Model):
        return self.aks_tmp

    @property
    def _aks_str(self):
    def _aks_str(self) -> str:
        """
        Property: AKs as string
        """
        if self.pk and self.pk > 0:
            return ', '.join(str(a) for a in self.aks.all())
        return ', '.join(str(a) for a in self.aks_tmp)
@@ -606,7 +775,10 @@ class ConstraintViolation(models.Model):
        return self.ak_slots_tmp

    @property
    def _ak_slots_str(self):
    def _ak_slots_str(self) -> str:
        """
        Property: Slots as string
        """
        if self.pk and self.pk > 0:
            return ', '.join(str(a) for a in self.ak_slots.select_related('event').all())
        return ', '.join(str(a) for a in self.ak_slots_tmp)
@@ -655,6 +827,10 @@ class ConstraintViolation(models.Model):


class DefaultSlot(models.Model):
    """
    Model representing a default slot,
    i.e., a prefered slot to use for typical AKs in the schedule to guarantee enough breaks etc.
    """
    class Meta:
        verbose_name = _('Default Slot')
        verbose_name_plural = _('Default Slots')
@@ -670,19 +846,31 @@ class DefaultSlot(models.Model):
                                            help_text=_('Categories that should be assigned to this slot primarily'))

    @property
    def start_simplified(self):
    def start_simplified(self) -> str:
        """
        Property: Simplified version of the start timetstamp (weekday, hour, minute) as string
        """
        return self.start.astimezone(self.event.timezone).strftime('%a %H:%M')

    @property
    def start_iso(self):
    def start_iso(self) -> str:
        """
        Property: Start timestamp as ISO timestamp for usage in calendar views
        """
        return timezone.localtime(self.start, self.event.timezone).strftime("%Y-%m-%dT%H:%M:%S")

    @property
    def end_simplified(self):
    def end_simplified(self) -> str:
        """
        Property: Simplified version of the end timetstamp (weekday, hour, minute) as string
        """
        return self.end.astimezone(self.event.timezone).strftime('%a %H:%M')

    @property
    def end_iso(self):
    def end_iso(self) -> str:
        """
        Property: End timestamp as ISO timestamp for usage in calendar views
        """
        return timezone.localtime(self.end, self.event.timezone).strftime("%Y-%m-%dT%H:%M:%S")

    def __str__(self):
Original line number Diff line number Diff line
@@ -4,36 +4,54 @@ from AKModel.models import AK, Room, AKSlot, AKTrack, AKCategory, AKOwner


class AKOwnerSerializer(serializers.ModelSerializer):
    """
    REST Framework Serializer for AKOwner
    """
    class Meta:
        model = AKOwner
        fields = '__all__'


class AKCategorySerializer(serializers.ModelSerializer):
    """
    REST Framework Serializer for AKCategory
    """
    class Meta:
        model = AKCategory
        fields = '__all__'


class AKTrackSerializer(serializers.ModelSerializer):
    """
    REST Framework Serializer for AKTrack
    """
    class Meta:
        model = AKTrack
        fields = '__all__'


class AKSerializer(serializers.ModelSerializer):
    """
    REST Framework Serializer for AK
    """
    class Meta:
        model = AK
        fields = '__all__'


class RoomSerializer(serializers.ModelSerializer):
    """
    REST Framework Serializer for Room
    """
    class Meta:
        model = Room
        fields = '__all__'


class AKSlotSerializer(serializers.ModelSerializer):
    """
    REST Framework Serializer for AKSlot
    """
    class Meta:
        model = AKSlot
        fields = '__all__'
@@ -41,6 +59,9 @@ class AKSlotSerializer(serializers.ModelSerializer):
    treat_as_local = serializers.BooleanField(required=False, default=False, write_only=True)

    def create(self, validated_data:dict):
        # Handle timezone adaption based upon the control field "treat_as_local":
        # If it is set, ignore timezone submitted from the browser (will always be UTC)
        # and treat it as input in the events timezone instead
        if validated_data['treat_as_local']:
            validated_data['start'] = validated_data['start'].replace(tzinfo=None).astimezone(
                validated_data['event'].timezone)
+9 −2
Original line number Diff line number Diff line
from django.contrib.admin import AdminSite
from django.utils.translation import gettext_lazy as _
# from django.urls import path

from AKModel.models import Event


class AKAdminSite(AdminSite):
    """
    Custom admin interface definition (extend the admin functionality of Django)
    """
    index_template = "admin/ak_index.html"
    site_header = f"AKPlanning - {_('Administration')}"
    index_title = _('Administration')

    def get_urls(self):
        from django.urls import path

        """
        Get URLs -- add further views that are not related to a certain model here if needed
        """
        urls = super().get_urls()
        urls += [
            # path('...', self.admin_view(...)),
@@ -19,6 +24,8 @@ class AKAdminSite(AdminSite):
        return urls

    def index(self, request, extra_context=None):
        # Override index page rendering to provide extra context (the list of active events)
        # to be used in the adapted template
        if extra_context is None:
            extra_context = {}
        extra_context["active_events"] = Event.objects.filter(active=True)
Original line number Diff line number Diff line
@@ -8,30 +8,58 @@ from fontawesome_6.app_settings import get_css
register = template.Library()


# Get Footer Info from settings
@register.simple_tag
def footer_info():
    """
    Get Footer Info from settings

    :return: a dict of several strings like the impress URL to use in the footer
    :rtype: Dict[str, str]
    """
    return settings.FOOTER_INFO


@register.filter
def check_app_installed(name):
    """
    Check whether the app with the given name is active in this instance

    :param name: name of the app to check for
    :return: true if app is installed
    :rtype: bool
    """
    return apps.is_installed(name)


@register.filter
def message_bootstrap_class(tag):
    """
    Turn message severity classes into corresponding bootstrap css classes

    :param tag: severity of the message
    :return: matching bootstrap class
    """
    if tag == "error":
        return "alert-danger"
    elif tag == "success":
    if tag == "success":
        return "alert-success"
    elif tag == "warning":
    if tag == "warning":
        return "alert-warning"
    return "alert-info"


@register.filter
def wiki_owners_export(owners, event):
    """
    Preserve owner link information for wiki export by using internal links if possible
    but external links when owner specified a non-wikilink. This is applied to the full list of owners

    :param owners: list of owners
    :param event: event this owner belongs to and that is currently exported
    (specifying this directly prevents unnecesary database lookups)
    :return: linkified owners list in wiki syntax
    :rtype: str
    """
    def to_link(owner):
        if owner.link != '':
            event_link_prefix, _ = event.base_url.rsplit("/", 1)
@@ -44,17 +72,30 @@ def wiki_owners_export(owners, event):
    return ", ".join(to_link(owner) for owner in owners.all())


# get list of relevant css fontawesome css files for this instance
css = get_css()


@register.simple_tag
def fontawesome_6_css():
    """
    Create html code to load all required fontawesome css files

    :return: HTML code to load css
    :rtype: str
    """
    return mark_safe(conditional_escape('\n').join(format_html(
            '<link href="{}" rel="stylesheet" media="all">', stylesheet) for stylesheet in css))


@register.simple_tag
def fontawesome_6_js():
    """
    Create html code to load all required fontawesome javascript files

    :return: HTML code to load js
    :rtype: str
    """
    return mark_safe(format_html(
        '<script type="text/javascript" src="{}"></script>', static('fontawesome_6/js/django-fontawesome.js')
    ))
+101 −33
Original line number Diff line number Diff line
import traceback
from typing import List

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
from django.contrib.messages.storage.base import Message
from django.test import TestCase
@@ -12,21 +12,43 @@ from AKModel.models import Event, AKOwner, AKCategory, AKTrack, AKRequirement, A


class BasicViewTests:
    """
    Parent class for "standard" tests of views

    Provided with a list of views and arguments (if necessary), this will test that views
    - render correctly without errors
    - are only reachable with the correct rights (neither too freely nor too restricted)

    To do this, the test creates sample users, fixtures are loaded automatically by the django test framework.
    It also provides helper functions, e.g., to check for correct messages to the user or more simply generate
    the URLs to test

    In this class, methods from :class:`TestCase` will be called at multiple places event though TestCase is not a
    parent of this class but has to be included as parent in concrete implementations of this class seperately.
    It however still makes sense to treat this class as some kind of mixin and not implement it as a child of TestCase,
    since the test framework does not understand the concept of abstract test definitions and would handle this class
    as real test case otherwise, distorting the test results.
    """
    # pylint: disable=no-member
    VIEWS = []
    APP_NAME = ''
    VIEWS_STAFF_ONLY = []
    EDIT_TESTCASES = []

    def setUp(self):
        self.staff_user = User.objects.create(
    def setUp(self):  # pylint: disable=invalid-name
        """
        Setup testing by creating sample users
        """
        user_model = get_user_model()
        self.staff_user = user_model.objects.create(
            username='Test Staff User', email='teststaff@example.com', password='staffpw',
            is_staff=True, is_active=True
        )
        self.admin_user = User.objects.create(
        self.admin_user = user_model.objects.create(
            username='Test Admin User', email='testadmin@example.com', password='adminpw',
            is_staff=True, is_superuser=True, is_active=True
        )
        self.deactivated_user = User.objects.create(
        self.deactivated_user = user_model.objects.create(
            username='Test Deactivated User', email='testdeactivated@example.com', password='deactivatedpw',
            is_staff=True, is_active=False
        )
@@ -45,6 +67,13 @@ class BasicViewTests:
        return view_name_with_prefix, url

    def _assert_message(self, response, expected_message, msg_prefix=""):
        """
        Assert that the correct message is shown and cause test to fail if not

        :param response: response to check
        :param expected_message: message that should be shown
        :param msg_prefix: prefix for the error message when test fails
        """
        messages:List[Message] = list(get_messages(response.wsgi_request))

        msg_count = "No message shown to user"
@@ -59,60 +88,83 @@ class BasicViewTests:
        self.assertEqual(messages[-1].message, expected_message, msg=msg_content)

    def test_views_for_200(self):
        """
        Test the list of public views (as specified in "VIEWS") for error-free rendering
        """
        for view_name in self.VIEWS:
            view_name_with_prefix, url = self._name_and_url(view_name)
            try:
                response = self.client.get(url)
                self.assertEqual(response.status_code, 200, msg=f"{view_name_with_prefix} ({url}) broken")
            except Exception as e:
                self.fail(f"An error occurred during rendering of {view_name_with_prefix} ({url}):\n\n{traceback.format_exc()}")
            except Exception: # pylint: disable=broad-exception-caught
                self.fail(f"An error occurred during rendering of {view_name_with_prefix} ({url}):"
                          f"\n\n{traceback.format_exc()}")

    def test_access_control_staff_only(self):
        """
        Test whether internal views (as specified in "VIEWS_STAFF_ONLY" are visible to staff users and staff users only
        """
        # Not logged in? Views should not be visible
        self.client.logout()
        for view_name in self.VIEWS_STAFF_ONLY:
            view_name_with_prefix, url = self._name_and_url(view_name)
        for view_name_info in self.VIEWS_STAFF_ONLY:
            expected_response_code = 302 if len(view_name_info) == 2 else view_name_info[2]
            view_name_with_prefix, url = self._name_and_url(view_name_info)
            response = self.client.get(url)
            self.assertEqual(response.status_code, 302, msg=f"{view_name_with_prefix} ({url}) accessible by non-staff")
            self.assertEqual(response.status_code, expected_response_code,
                             msg=f"{view_name_with_prefix} ({url}) accessible by non-staff")

        # Logged in? Views should be visible
        self.client.force_login(self.staff_user)
        for view_name in self.VIEWS_STAFF_ONLY:
            view_name_with_prefix, url = self._name_and_url(view_name)
        for view_name_info in self.VIEWS_STAFF_ONLY:
            view_name_with_prefix, url = self._name_and_url(view_name_info)
            try:
                response = self.client.get(url)
                self.assertEqual(response.status_code, 200,
                                 msg=f"{view_name_with_prefix} ({url}) should be accessible for staff (but isn't)")
            except Exception as e:
                self.fail(f"An error occurred during rendering of {view_name_with_prefix} ({url}):\n\n{traceback.format_exc()}")
            except Exception:  # pylint: disable=broad-exception-caught
                self.fail(f"An error occurred during rendering of {view_name_with_prefix} ({url}):"
                          f"\n\n{traceback.format_exc()}")

        # Disabled user? Views should not be visible
        self.client.force_login(self.deactivated_user)
        for view_name in self.VIEWS_STAFF_ONLY:
            view_name_with_prefix, url = self._name_and_url(view_name)
        for view_name_info in self.VIEWS_STAFF_ONLY:
            expected_response_code = 302 if len(view_name_info) == 2 else view_name_info[2]
            view_name_with_prefix, url = self._name_and_url(view_name_info)
            response = self.client.get(url)
            self.assertEqual(response.status_code, 302,
            self.assertEqual(response.status_code, expected_response_code,
                             msg=f"{view_name_with_prefix} ({url}) still accessible for deactivated user")

    def _to_sendable_value(self, v):
    def _to_sendable_value(self, val):
        """
        Create representation sendable via POST from form data

        :param v: value to prepare
        :type v: any
        Needed to automatically check create, update and delete views

        :param val: value to prepare
        :type val: any
        :return: prepared value (normally either raw value or primary key of complex object)
        """
        if type(v) == list:
            return [e.pk for e in v]
        if type(v) == "RelatedManager":
            return [e.pk for e in v.all()]
        return v
        if isinstance(val, list):
            return [e.pk for e in val]
        if type(val) == "RelatedManager":  # pylint: disable=unidiomatic-typecheck
            return [e.pk for e in val.all()]
        return val

    def test_submit_edit_form(self):
        """
        Test edit forms in the most simple way (sending them again unchanged)
        Test edit forms (as specified in "EDIT_TESTCASES") in the most simple way (sending them again unchanged)
        """
        for testcase in self.EDIT_TESTCASES:
            self._test_submit_edit_form(testcase)

    def _test_submit_edit_form(self, testcase):
        """
        Test a single edit form by rendering and sending it again unchanged

        This will test for correct rendering, dispatching/redirecting, messages and access control handling

        :param testcase: details of the form to test
        """
        name, url = self._name_and_url((testcase["view"], testcase["kwargs"]))
        form_name = testcase.get("form_name", "form")
        expected_code = testcase.get("expected_code", 302)
@@ -145,6 +197,9 @@ class BasicViewTests:


class ModelViewTests(BasicViewTests, TestCase):
    """
    Basic view test cases for views from AKModel plus some custom tests
    """
    fixtures = ['model.json']

    ADMIN_MODELS = [
@@ -172,35 +227,48 @@ class ModelViewTests(BasicViewTests, TestCase):
    ]

    def test_admin(self):
        """
        Test basic admin functionality (displaying and interacting with model instances)
        """
        self.client.force_login(self.admin_user)
        for model in self.ADMIN_MODELS:
            # Special treatment for a subset of views (where we exchanged default functionality, e.g., create views)
            if model[1] == "event":
                view_name_with_prefix, url = self._name_and_url((f'admin:new_event_wizard_start', {}))
                _, url = self._name_and_url(('admin:new_event_wizard_start', {}))
            elif model[1] == "room":
                view_name_with_prefix, url = self._name_and_url((f'admin:room-new', {}))
                _, url = self._name_and_url(('admin:room-new', {}))
            # Otherwise, just call the creation form view
            else:
                view_name_with_prefix, url = self._name_and_url((f'admin:AKModel_{model[1]}_add', {}))
                _, url = self._name_and_url((f'admin:AKModel_{model[1]}_add', {}))
            response = self.client.get(url)
            self.assertEqual(response.status_code, 200, msg=f"Add form for model {model[1]} ({url}) broken")

        for model in self.ADMIN_MODELS:
            # Test the update view using the first existing instance of each model
            m = model[0].objects.first()
            if m is not None:
                view_name_with_prefix, url = self._name_and_url((f'admin:AKModel_{model[1]}_change', {'object_id': m.pk}))
                _, url = self._name_and_url(
                    (f'admin:AKModel_{model[1]}_change', {'object_id': m.pk})
                )
                response = self.client.get(url)
                self.assertEqual(response.status_code, 200, msg=f"Edit form for model {model[1]} ({url}) broken")

    def test_wiki_export(self):
        """
        Test wiki export
        This will test whether the view renders at all and whether the export list contains the correct AKs
        """
        self.client.force_login(self.admin_user)

        export_url = reverse_lazy(f"admin:ak_wiki_export", kwargs={'slug': 'kif42'})
        export_url = reverse_lazy("admin:ak_wiki_export", kwargs={'slug': 'kif42'})
        response = self.client.get(export_url)
        self.assertEqual(response.status_code, 200, "Export not working at all")

        export_count = 0
        for category, aks in response.context["categories_with_aks"]:
        for _, aks in response.context["categories_with_aks"]:
            for ak in aks:
                self.assertEqual(ak.include_in_export, True, f"AK with export flag set to False (pk={ak.pk}) included in export")
                self.assertEqual(ak.include_in_export, True,
                                 f"AK with export flag set to False (pk={ak.pk}) included in export")
                self.assertNotEqual(ak.pk, 1, "AK known to be excluded from export (PK 1) included in export")
                export_count += 1

+20 −2
Original line number Diff line number Diff line
@@ -4,12 +4,14 @@ from django.urls import include, path
from rest_framework.routers import DefaultRouter

import AKModel.views.api
from AKModel.views.manage import ExportSlidesView
from AKModel.views.manage import ExportSlidesView, PlanPublishView, PlanUnpublishView, DefaultSlotEditorView
from AKModel.views.ak import AKRequirementOverview, AKCSVExportView, AKWikiExportView, AKMessageDeleteView
from AKModel.views.event_wizard import NewEventWizardStartView, NewEventWizardPrepareImportView, \
    NewEventWizardImportView, NewEventWizardActivateView, NewEventWizardFinishView, NewEventWizardSettingsView
from AKModel.views.room import RoomBatchCreationView
from AKModel.views.status import EventStatusView

# Register basic API views/endpoints
api_router = DefaultRouter()
api_router.register('akowner', AKModel.views.api.AKOwnerViewSet, basename='AKOwner')
api_router.register('akcategory', AKModel.views.api.AKCategoryViewSet, basename='AKCategory')
@@ -18,7 +20,9 @@ api_router.register('ak', AKModel.views.api.AKViewSet, basename='AK')
api_router.register('room', AKModel.views.api.RoomViewSet, basename='Room')
api_router.register('akslot', AKModel.views.api.AKSlotViewSet, basename='AKSlot')

# TODO Can we move this functionality to the individual apps instead?
extra_paths = []
# If AKScheduling is active, register additional API endpoints
if apps.is_installed("AKScheduling"):
    from AKScheduling.api import ResourcesViewSet, RoomAvailabilitiesView, EventsView, EventsViewSet, \
        ConstraintViolationsViewSet, DefaultSlotsView
@@ -33,9 +37,10 @@ if apps.is_installed("AKScheduling"):
             name='scheduling-room-availabilities')),
    extra_paths.append(path('api/scheduling-default-slots/', DefaultSlotsView.as_view(),
             name='scheduling-default-slots'))

#If AKSubmission is active, register an additional API endpoint for increasing the interest counter
if apps.is_installed("AKSubmission"):
    from AKSubmission.api import increment_interest_counter

    extra_paths.append(path('api/ak/<pk>/indicate-interest/', increment_interest_counter, name='submission-ak-indicate-interest'))

event_specific_paths = [
@@ -45,6 +50,7 @@ event_specific_paths.extend(extra_paths)

app_name = 'model'

# Included all these extra view paths at a path starting with the event slug
urlpatterns = [
    path(
        '<slug:event_slug>/',
@@ -55,6 +61,9 @@ urlpatterns = [


def get_admin_urls_event_wizard(admin_site):
    """
    Defines all additional URLs for the event creation wizard
    """
    return [
        path('add/wizard/start/', admin_site.admin_view(NewEventWizardStartView.as_view()),
             name="new_event_wizard_start"),
@@ -75,6 +84,9 @@ def get_admin_urls_event_wizard(admin_site):


def get_admin_urls_event(admin_site):
    """
    Defines all additional event-related view URLs that will be included in the event admin interface
    """
    return [
        path('<slug:event_slug>/status/', admin_site.admin_view(EventStatusView.as_view()), name="event_status"),
        path('<slug:event_slug>/requirements/', admin_site.admin_view(AKRequirementOverview.as_view()),
@@ -86,4 +98,10 @@ def get_admin_urls_event(admin_site):
        path('<slug:event_slug>/delete-orga-messages/', admin_site.admin_view(AKMessageDeleteView.as_view()),
             name="ak_delete_orga_messages"),
        path('<slug:event_slug>/ak-slide-export/', admin_site.admin_view(ExportSlidesView.as_view()), name="ak_slide_export"),
        path('plan/publish/', admin_site.admin_view(PlanPublishView.as_view()), name="plan-publish"),
        path('plan/unpublish/', admin_site.admin_view(PlanUnpublishView.as_view()), name="plan-unpublish"),
        path('<slug:event_slug>/defaultSlots/', admin_site.admin_view(DefaultSlotEditorView.as_view()),
             name="default-slots-editor"),
        path('<slug:event_slug>/importRooms/', admin_site.admin_view(RoomBatchCreationView.as_view()),
             name="room-import"),
    ]
Original line number Diff line number Diff line
@@ -9,6 +9,9 @@ from AKModel.models import AKRequirement, AKSlot, Event, AKOrgaMessage, AK


class AKRequirementOverview(AdminViewMixin, FilterByEventSlugMixin, ListView):
    """
    View: Display requirements for the given event
    """
    model = AKRequirement
    context_object_name = "requirements"
    title = _("Requirements for Event")
@@ -22,6 +25,9 @@ class AKRequirementOverview(AdminViewMixin, FilterByEventSlugMixin, ListView):


class AKCSVExportView(AdminViewMixin, FilterByEventSlugMixin, ListView):
    """
    View: Export all AK slots of this event in CSV format ordered by tracks
    """
    template_name = "admin/AKModel/ak_csv_export.html"
    model = AKSlot
    context_object_name = "slots"
@@ -30,12 +36,12 @@ class AKCSVExportView(AdminViewMixin, FilterByEventSlugMixin, ListView):
    def get_queryset(self):
        return super().get_queryset().order_by("ak__track")

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context


class AKWikiExportView(AdminViewMixin, DetailView):
    """
    View: Export AKs of this event in wiki syntax
    This will show one text field per category, with a separate category/field for wishes
    """
    template_name = "admin/AKModel/wiki_export.html"
    model = Event
    context_object_name = "event"
@@ -46,7 +52,7 @@ class AKWikiExportView(AdminViewMixin, DetailView):

        categories_with_aks, ak_wishes = context["event"].get_categories_with_aks(
            wishes_seperately=True,
            filter=lambda ak: ak.include_in_export
            filter_func=lambda ak: ak.include_in_export
        )

        context["categories_with_aks"] = [(category.name, ak_list) for category, ak_list in categories_with_aks]
@@ -56,10 +62,18 @@ class AKWikiExportView(AdminViewMixin, DetailView):


class AKMessageDeleteView(EventSlugMixin, IntermediateAdminView):
    """
    View: Confirmation page to delete confidential AK-related messages to orga

    Confirmation functionality provided by :class:`AKModel.metaviews.admin.IntermediateAdminView`
    """
    template_name = "admin/AKModel/message_delete.html"
    title = _("Delete AK Orga Messages")

    def get_orga_messages_for_event(self, event):
        """
        Get all orga messages for the given event
        """
        return AKOrgaMessage.objects.filter(ak__event=event)

    def get_success_url(self):
@@ -77,6 +91,11 @@ class AKMessageDeleteView(EventSlugMixin, IntermediateAdminView):


class AKResetInterestView(IntermediateAdminActionView):
    """
    View: Confirmation page to reset all manually specified interest values

    Confirmation functionality provided by :class:`AKModel.metaviews.admin.IntermediateAdminView`
    """
    title = _("Reset interest in AKs")
    model = AK
    confirmation_message = _("Interest of the following AKs will be set to not filled (-1):")
@@ -87,6 +106,11 @@ class AKResetInterestView(IntermediateAdminActionView):


class AKResetInterestCounterView(IntermediateAdminActionView):
    """
    View: Confirmation page to reset all interest counters (online interest indication)

    Confirmation functionality provided by :class:`AKModel.metaviews.admin.IntermediateAdminView`
    """
    title = _("Reset AKs' interest counters")
    model = AK
    confirmation_message = _("Interest counter of the following AKs will be set to 0:")
Original line number Diff line number Diff line
@@ -7,6 +7,10 @@ from AKModel.serializers import AKOwnerSerializer, AKCategorySerializer, AKTrack


class AKOwnerViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    API View: Owners (restricted to those of the given event)
    Read-only
    """
    permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly,)
    serializer_class = AKOwnerSerializer

@@ -15,6 +19,10 @@ class AKOwnerViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.ListModel


class AKCategoryViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    API View: Categories (restricted to those of the given event)
    Read-only
    """
    permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly,)
    serializer_class = AKCategorySerializer

@@ -24,6 +32,10 @@ class AKCategoryViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.ListMo

class AKTrackViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin,
                     mixins.DestroyModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    API View: Tracks (restricted to those of the given event)
    Read, Write, Delete
    """
    permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly,)
    serializer_class = AKTrackSerializer

@@ -33,6 +45,10 @@ class AKTrackViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.CreateMod

class AKViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.ListModelMixin,
                viewsets.GenericViewSet):
    """
    API View: AKs (restricted to those of the given event)
    Read, Write
    """
    permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly,)
    serializer_class = AKSerializer

@@ -41,6 +57,10 @@ class AKViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMix


class RoomViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    API View: Rooms (restricted to those of the given event)
    Read-only
    """
    permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly,)
    serializer_class = RoomSerializer

@@ -50,6 +70,10 @@ class RoomViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.ListModelMix

class AKSlotViewSet(EventSlugMixin, mixins.RetrieveModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin,
                    mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    API View: AK slots (restricted to those of the given event)
    Read, Write
    """
    permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly,)
    serializer_class = AKSlotSerializer

Original line number Diff line number Diff line
@@ -12,6 +12,12 @@ from AKModel.models import Event


class NewEventWizardStartView(AdminViewMixin, WizardViewMixin, CreateView):
    """
    Wizard view: Entry/Start

    Specify basic settings, especially the timezone for correct time treatment in the next view
    (:class:`NewEventWizardSettingsView`) where this view will redirect to without saving the new event already
    """
    model = Event
    form_class = NewEventWizardStartForm
    template_name = "admin/AKModel/event_wizard/start.html"
@@ -19,6 +25,16 @@ class NewEventWizardStartView(AdminViewMixin, WizardViewMixin, CreateView):


class NewEventWizardSettingsView(AdminViewMixin, WizardViewMixin, CreateView):
    """
    Wizard view: Event settings

    Specify most of the event settings. The user will see that certain fields are required since they were lead here
    from another form in :class:`NewEventWizardStartView` that did not contain these fields even though they are
    mandatory for the event model

    Next step will then be :class:`NewEventWizardPrepareImportView` to prepare copy configuration elements
    from an existing event
    """
    model = Event
    form_class = NewEventWizardSettingsForm
    template_name = "admin/AKModel/event_wizard/settings.html"
@@ -34,6 +50,14 @@ class NewEventWizardSettingsView(AdminViewMixin, WizardViewMixin, CreateView):


class NewEventWizardPrepareImportView(WizardViewMixin, EventSlugMixin, FormView):
    """
    Wizard view: Choose event to copy configuration elements from

    The user can here select an existing event to copy elements like requirements, categories and dashboard buttons from
    The exact subset of elements to copy from can then be selected in the next view (:class:`NewEventWizardImportView`)

    Instead, this step can be skipped by directly continuing with :class:`NewEventWizardActivateView`
    """
    form_class = NewEventWizardPrepareImportForm
    template_name = "admin/AKModel/event_wizard/created_prepare_import.html"
    wizard_step = 3
@@ -45,29 +69,40 @@ class NewEventWizardPrepareImportView(WizardViewMixin, EventSlugMixin, FormView)


class NewEventWizardImportView(EventSlugMixin, WizardViewMixin, FormView):
    """
    Wizard view: Select configuration elements to copy

    Displays lists of requirements, categories and dashboard buttons that the user can select entries to be copied from

    Afterwards, the event can be activated in :class:`NewEventWizardActivateView`
    """
    form_class = NewEventWizardImportForm
    template_name = "admin/AKModel/event_wizard/import.html"
    wizard_step = 4

    def get_initial(self):
        initial = super().get_initial()
        # Remember which event was selected and send it again when submitting the form for validation
        initial["import_event"] = Event.objects.get(slug=self.kwargs["import_slug"])
        return initial

    def form_valid(self, form):
        # pylint: disable=consider-using-f-string
        import_types = ["import_categories", "import_requirements"]
        if apps.is_installed("AKDashboard"):
            import_types.append("import_buttons")

        # Loop over all kinds of configuration elements and then over all selected elements of each type
        # and try to clone them by requesting a new primary key, adapting the event and then storing the
        # object in the database
        for import_type in import_types:
            for import_obj in form.cleaned_data.get(import_type):
                # clone existing entry
                try:
                    import_obj.event = self.event
                    import_obj.pk = None
                    import_obj.save()
                    messages.add_message(self.request, messages.SUCCESS, _("Copied '%(obj)s'" % {'obj': import_obj}))
                except BaseException as e:
                except BaseException as e:  # pylint: disable=broad-exception-caught
                    messages.add_message(self.request, messages.ERROR,
                                         _("Could not copy '%(obj)s' (%(error)s)" % {'obj': import_obj,
                                                                                     "error": str(e)}))
@@ -75,6 +110,17 @@ class NewEventWizardImportView(EventSlugMixin, WizardViewMixin, FormView):


class NewEventWizardActivateView(WizardViewMixin, UpdateView):
    """
    Wizard view: Allow activating the event

    The user is asked to make the created event active. This is done in this step and not already during the creation
    in the second step of the wizard to prevent users seeing an unconfigured submission.
    The event will nevertheless already be visible in the dashboard before, when a public event was created in
    :class:`NewEventWizardSettingsView`.

    In the following last step (:class:`NewEventWizardFinishView`), a confirmation of the full process and some
    details of the created event are shown
    """
    model = Event
    template_name = "admin/AKModel/event_wizard/activate.html"
    form_class = NewEventWizardActivateForm
@@ -85,6 +131,11 @@ class NewEventWizardActivateView(WizardViewMixin, UpdateView):


class NewEventWizardFinishView(WizardViewMixin, DetailView):
    """
    Wizard view: Confirmation and summary

    Show a confirmation and a summary of the created event
    """
    model = Event
    template_name = "admin/AKModel/event_wizard/finish.html"
    wizard_step = 6
Original line number Diff line number Diff line
@@ -18,16 +18,28 @@ from AKModel.models import ConstraintViolation, Event, DefaultSlot


class UserView(TemplateView):
    """
    View: Start page for logged in user

    Will over a link to backend or inform the user that their account still needs to be confirmed
    """
    template_name = "AKModel/user.html"


class ExportSlidesView(EventSlugMixin, IntermediateAdminView):
    """
    View: Export slides to present AKs

    Over a form to choose some settings for the export and then generate the PDF
    """
    title = _('Export AK Slides')
    form_class = SlideExportForm

    def form_valid(self, form):
        # pylint: disable=invalid-name
        template_name = 'admin/AKModel/export/slides.tex'

        # Settings
        NEXT_AK_LIST_LENGTH = form.cleaned_data['num_next']
        RESULT_PRESENTATION_MODE = form.cleaned_data["presentation_mode"]
        SPACE_FOR_NOTES_IN_WISHES = form.cleaned_data["wish_notes"]
@@ -42,12 +54,18 @@ class ExportSlidesView(EventSlugMixin, IntermediateAdminView):
        }

        def build_ak_list_with_next_aks(ak_list):
            """
            Create a list of tuples cosisting of an AK and a list of upcoming AKs (list length depending on setting)
            """
            next_aks_list = zip_longest(*[ak_list[i + 1:] for i in range(NEXT_AK_LIST_LENGTH)], fillvalue=None)
            return [(ak, next_aks) for ak, next_aks in zip_longest(ak_list, next_aks_list, fillvalue=list())]
            return [(ak, next_aks) for ak, next_aks in zip_longest(ak_list, next_aks_list, fillvalue=[])]

        categories_with_aks, ak_wishes = self.event.get_categories_with_aks(wishes_seperately=True, filter=lambda
        # Get all relevant AKs (wishes separately, and either all AKs or only those who should directly or indirectly
        # be presented when restriction setting was chosen)
        categories_with_aks, ak_wishes = self.event.get_categories_with_aks(wishes_seperately=True, filter_func=lambda
            ak: not RESULT_PRESENTATION_MODE or (ak.present or (ak.present is None and ak.category.present_by_default)))

        # Create context for LaTeX rendering
        context = {
            'title': self.event.name,
            'categories_with_aks': [(category, build_ak_list_with_next_aks(ak_list)) for category, ak_list in
@@ -67,11 +85,17 @@ class ExportSlidesView(EventSlugMixin, IntermediateAdminView):
            os.remove(f'{tempdir}/texput.tex')
            pdf = run_tex_in_directory(source, tempdir, template_name=self.template_name)

        # Show PDF file to the user (with a filename containing a timestamp to prevent confusions about the right
        # version to use when generating multiple versions of the slides, e.g., because owners did last-minute changes
        # to their AKs
        timestamp = datetime.datetime.now(tz=self.event.timezone).strftime("%Y-%m-%d_%H_%M")
        return PDFResponse(pdf, filename=f'{self.event.slug}_ak_slides_{timestamp}.pdf')


class CVMarkResolvedView(IntermediateAdminActionView):
    """
    Admin action view: Mark one or multitple constraint violation(s) as resolved
    """
    title = _('Mark Constraint Violations as manually resolved')
    model = ConstraintViolation
    confirmation_message = _("The following Constraint Violations will be marked as manually resolved")
@@ -82,6 +106,9 @@ class CVMarkResolvedView(IntermediateAdminActionView):


class CVSetLevelViolationView(IntermediateAdminActionView):
    """
    Admin action view: Set one or multitple constraint violation(s) as to level "violation"
    """
    title = _('Set Constraint Violations to level "violation"')
    model = ConstraintViolation
    confirmation_message = _("The following Constraint Violations will be set to level 'violation'")
@@ -92,6 +119,9 @@ class CVSetLevelViolationView(IntermediateAdminActionView):


class CVSetLevelWarningView(IntermediateAdminActionView):
    """
    Admin action view: Set one or multitple constraint violation(s) as to level "warning"
    """