Skip to content
Snippets Groups Projects
Select Git revision
  • 1fbfaa72c6754cbed68b3556818cf512a33c1ca8
  • master default protected
  • datepicker-non-cdn
  • dev-and-graphics
  • readable-ak-times
  • feature-constraint-checking-wip
  • feature-constraint-checking
7 results

forms.py

Blame
  • Forked from KIF / AKPlanning
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    forms.py 1.54 KiB
    from betterforms.multiform import MultiModelForm
    from django.forms import ModelForm
    
    from AKModel.forms import RoomForm
    from AKOnline.models import VirtualRoom
    
    
    class VirtualRoomForm(ModelForm):
        """
        Form to create a virtual room
    
        Should be used as part of a multi form (see :class:`RoomWithVirtualForm` below)
        """
        class Meta:
            model = VirtualRoom
            # Show all fields except for room
            exclude = ['room'] #pylint: disable=modelform-uses-exclude
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            # Make the URL field optional to allow submitting the multi form without creating a virtual room
            self.fields['url'].required = False
    
    
    class RoomWithVirtualForm(MultiModelForm):
        """
        Combined form to create rooms and optionally virtual rooms
    
        Multi-Form that combines a :class:`RoomForm` (from AKModel) and a :class:`VirtualRoomForm` (see above).
    
        The form will always create a room on valid input
        and may additionally create a virtual room if the url field of the virtual room form part is set.
        """
        form_classes = {
            'room': RoomForm,
            'virtual': VirtualRoomForm
        }
    
        def save(self, commit=True):
            objects = super().save(commit=False)
    
            if commit:
                room = objects['room']
                room.save()
    
                virtual = objects['virtual']
                if virtual.url != "":
                    virtual.room = room
                    virtual.save()
                else:
                    objects['virtual'] = None
    
            return objects