Ejemplo n.º 1
0
class TeamMemberForm(forms.Form):
    name = fields.CharField()
    job_title_en = fields.CharField(label=_('Job Title'))
    job_title_fr = fields.CharField(label=_('Job Title (French)'),
                                    required=False)
    photo = forms.ImageField(required=False)
    phone = fields.CharField(label=_('Phone Number'), required=False)
    email = fields.EmailField(required=False)

    def __init__(self, club, *args, **kwargs):
        self.club = club
        self.team_member = None

        if 'team_member' in kwargs:
            self.team_member = kwargs.pop('team_member')

        if self.team_member:
            kw_initial = kwargs.get('initial', {})
            kwargs['initial'] = {
                'name': self.team_member.name,
                'job_title_en': self.team_member.job_title_en,
                'job_title_fr': self.team_member.job_title_fr,
                'phone': self.team_member.phone,
                'email': self.team_member.email,
            }
            kwargs['initial'].update(kw_initial)

        super().__init__(*args, **kwargs)
Ejemplo n.º 2
0
class MyAccountForm(forms.Form):
    first_name = fields.CharField()
    middle_name = fields.CharField(required=False)
    last_name = fields.CharField()
    email = fields.EmailField()
    password = fields.CharField(required=False, widget=forms.PasswordInput())
    preferred_language = fields.ChoiceField(choices=settings.LANGUAGES,
                                            initial=settings.LANGUAGE_CODE)

    def __init__(self, *args, **kwargs):
        self.user = None

        if 'user' in kwargs:
            self.user = kwargs.pop('user')
            kw_initial = kwargs.get('initial', {})
            kwargs['initial'] = {
                'first_name': self.user.first_name,
                'last_name': self.user.last_name,
                'middle_name': self.user.middle_name,
                'email': self.user.email,
                'preferred_language': self.user.preferred_language,
            }
            kwargs['initial'].update(kw_initial)

        super().__init__(*args, **kwargs)
Ejemplo n.º 3
0
class CalendarMessageForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    from_name = fields.CharField(required=True, initial='ClubLink')
    reply_to = fields.EmailField(required=True, initial='*****@*****.**')
    subject = fields.CharField(required=True)
    message = fields.TextareaField(required=True)
Ejemplo n.º 4
0
class CalendarForm(forms.Form):
    # extra_fields
    recurrence_set = forms.BooleanField(label=_('Set recurrence parameters'),
                                        required=False,
                                        initial=False,
                                        widget=forms.CheckboxInput)
    recurrence_pattern = forms.ChoiceField(label=_('Pattern'),
                                           choices=RECURRENCE_PATTERN,
                                           required=False)
    recurrence_every = forms.IntegerField(label=_('Event happens every'),
                                          required=False)
    recurrence_repetition_types = forms.ChoiceField(
        label=_('Recurrence type'),
        required=False,
        choices=RECURRENCE_REPETITION_TYPES)
    recurrence_until = forms.DateField(label=_('Until'),
                                       required=False,
                                       initial=None,
                                       widget=forms.SelectDateWidget())
    recurrence_repetitions = forms.IntegerField(label=_('Repetitions'),
                                                required=False)

    type = fields.ChoiceField(choices=ClubEvent.TYPE_CHOICES)
    name = fields.CharField()
    email = fields.EmailField(required=False)
    description = fields.TextareaField(widget=forms.Textarea(
        attrs={'data-tinymce': True}))
    start_date = forms.DateField(widget=forms.SelectDateWidget())
    start_time = forms.TimeField(initial=DEFAULT_START_TIME,
                                 widget=SelectTimeWidget(required=False,
                                                         twelve_hr=True))
    end_date = forms.DateField(required=False,
                               initial=None,
                               widget=forms.SelectDateWidget())
    end_time = forms.TimeField(required=False,
                               initial=DEFAULT_END_TIME,
                               widget=SelectTimeWidget(required=False,
                                                       twelve_hr=True))
    max_attendees = fields.IntegerField(initial=0)
    max_guests_per_rsvp = fields.IntegerField(initial=1, min_value=1)
    photo = forms.ImageField(required=False)
    custom_question_1 = fields.CharField(required=False)
    custom_question_2 = fields.CharField(required=False)
    custom_question_3 = fields.CharField(required=False)
    custom_question_4 = fields.CharField(required=False)
    custom_question_5 = fields.CharField(required=False)
    online_registration = fields.TypedChoiceField(
        coerce=lambda x: x == 'True',
        choices=fields.BOOLEAN_CHOICES,
        initial=True,
        required=False)
    registration_open_date = forms.DateField(required=False,
                                             initial=None,
                                             widget=forms.SelectDateWidget())
    registration_open_time = forms.TimeField(
        required=False,
        initial=DEFAULT_REGISTRATION_OPEN_TIME,
        widget=SelectTimeWidget(required=False, twelve_hr=True))
    registration_close_date = forms.DateField(required=False,
                                              initial=None,
                                              widget=forms.SelectDateWidget())
    registration_close_time = forms.TimeField(
        required=False,
        initial=DEFAULT_REGISTRATION_CLOSE_TIME,
        widget=SelectTimeWidget(required=False, twelve_hr=True))

    def __init__(self, *args, **kwargs):
        self.event = None

        if 'event' in kwargs:
            self.event = kwargs.pop('event')

        if self.event:
            kw_initial = kwargs.get('initial', {})
            kwargs['initial'] = {
                'start_date': self.event.start_date,
                'start_time': self.event.start_time,
                'end_date': self.event.end_date,
                'end_time': self.event.end_time,
                'name': self.event.name,
                'email': self.event.email,
                'description': self.event.description,
                'type': self.event.type,
                'max_attendees': self.event.max_attendees,
                'max_guests_per_rsvp': self.event.max_guests_per_rsvp,
                'custom_question_1': self.event.custom_question_1,
                'custom_question_2': self.event.custom_question_2,
                'custom_question_3': self.event.custom_question_3,
                'custom_question_4': self.event.custom_question_4,
                'custom_question_5': self.event.custom_question_5,
                'online_registration': self.event.online_registration,
                'registration_open_date': self.event.registration_open_date,
                'registration_open_time': self.event.registration_open_time,
                'registration_close_date': self.event.registration_close_date,
                'registration_close_time': self.event.registration_close_time
            }
            kwargs['initial'].update(kw_initial)

        super().__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = self.cleaned_data

        start_date = self.cleaned_data['start_date']
        start_time = self.cleaned_data['start_time']

        if self.cleaned_data.get('recurrence_set'):
            if not self.cleaned_data.get('recurrence_pattern'):
                self.add_error(
                    'recurrence_pattern',
                    forms.ValidationError(_('This is a required field.')))

            if not self.cleaned_data.get('recurrence_every'):
                self.add_error(
                    'recurrence_every',
                    forms.ValidationError(_('This is a required field.')))

            if self.cleaned_data.get('recurrence_repetition_types') == REPETITION_UNTIL \
                    and not self.cleaned_data.get('recurrence_until'):
                self.add_error(
                    'recurrence_until',
                    forms.ValidationError(_('This is a required field.')))

            if self.cleaned_data.get('recurrence_repetition_types') == NB_REPETITIONS \
                    and not self.cleaned_data.get('recurrence_repetitions'):
                self.add_error(
                    'recurrence_repetitions',
                    forms.ValidationError(_('This is a required field.')))

        if not self.cleaned_data.get('end_date'):
            self.cleaned_data['end_date'] = start_date

        if not self.cleaned_data.get('end_time'):
            self.cleaned_data['end_time'] = start_time

        end_date = self.cleaned_data['end_date']
        end_time = self.cleaned_data['end_time']
        email = self.cleaned_data['email']

        if end_date < start_date:
            self.add_error(
                'end_date',
                forms.ValidationError(
                    _('End date must not be earlier than start date.')))

        if start_date == end_date and end_time < start_time:
            self.add_error(
                'end_time',
                forms.ValidationError(
                    _('End time must not be earlier than start time.')))

        open_date = self.cleaned_data.get('registration_open_date')
        open_time = self.cleaned_data.get('registration_open_time')
        close_date = self.cleaned_data.get('registration_close_date')
        close_time = self.cleaned_data.get('registration_close_time')

        online_registration = self.cleaned_data.get('online_registration')

        if online_registration:
            if not open_date:
                self.add_error(
                    'registration_open_date',
                    forms.ValidationError(_('This field is required.')))
            if not open_time:
                self.add_error(
                    'registration_open_time',
                    forms.ValidationError(_('This field is required.')))
            if not close_date:
                self.add_error(
                    'registration_close_date',
                    forms.ValidationError(_('This field is required.')))
            if not close_time:
                self.add_error(
                    'registration_close_time',
                    forms.ValidationError(_('This field is required.')))

            if open_date and open_time and close_date and close_time:
                if open_date > end_date:
                    self.add_error(
                        'registration_open_date',
                        forms.ValidationError(
                            _('Open date must not be later than end date.')))

                if close_date > end_date:
                    self.add_error(
                        'registration_close_date',
                        forms.ValidationError(
                            _('Close date must not be later than end date.')))

                if close_date < open_date:
                    self.add_error(
                        'registration_close_date',
                        forms.ValidationError(
                            _('Close date must not be earlier than open date.')
                        ))

                if open_date == close_date and close_time < open_time:
                    self.add_error(
                        'registration_close_time',
                        forms.ValidationError(
                            _('Close time must not be earlier than open time.')
                        ))

        photo = self.cleaned_data.get('photo', False)
        if photo and photo._size > 4 * 1024 * 1024:
            self.add_error(
                'photo', forms.ValidationError(_('Image too large( > 4MB )')))

        return cleaned_data
Ejemplo n.º 5
0
class UserForm(forms.Form):
    username = fields.CharField(max_length=48)
    password = fields.CharField(min_length=6,
                                required=False,
                                widget=forms.PasswordInput())
    first_name = fields.CharField(required=False)
    last_name = fields.CharField(required=False)
    middle_name = fields.CharField(required=False)
    email = fields.EmailField()
    membership_number = fields.CharField(max_length=15, required=False)
    employee_number = fields.CharField(max_length=15, required=False)
    is_superuser = fields.TypedChoiceField(coerce=lambda x: x == 'True',
                                           choices=fields.BOOLEAN_CHOICES,
                                           initial=False,
                                           required=False)
    is_staff = fields.TypedChoiceField(coerce=lambda x: x == 'True',
                                       choices=fields.BOOLEAN_CHOICES,
                                       initial=False,
                                       required=False)
    preferred_language = fields.ChoiceField(choices=settings.LANGUAGES,
                                            initial=settings.LANGUAGE_CODE)
    status = fields.ChoiceField(choices=User.STATUSES)
    clubcorp = fields.ChoiceField(choices=[(None, '')], required=False)
    clubcorp_number = fields.CharField(max_length=5, required=False)
    type = fields.ChoiceField(choices=[(None, '')], required=False)
    category = fields.ChoiceField(choices=[(None, '')], required=False)
    home_club = fields.ChoiceField(choices=[(None, '')], required=False)
    option_club = fields.ChoiceField(choices=[(None, '')], required=False)
    home_club_alternate_1 = fields.ChoiceField(choices=[(None, '')],
                                               required=False)
    home_club_alternate_2 = fields.ChoiceField(choices=[(None, '')],
                                               required=False)

    def __init__(self, *args, **kwargs):
        self.user = None

        if kwargs.get('user'):
            self.user = kwargs.pop('user')
            kw_initial = kwargs.get('initial', {})
            kwargs['initial'] = {
                'username':
                self.user.username,
                'first_name':
                self.user.first_name,
                'last_name':
                self.user.last_name,
                'middle_name':
                self.user.middle_name,
                'email':
                self.user.email,
                'membership_number':
                self.user.membership_number,
                'employee_number':
                self.user.employee_number,
                'is_superuser':
                self.user.is_superuser,
                'is_staff':
                self.user.is_staff,
                'preferred_language':
                self.user.preferred_language,
                'status':
                self.user.status,
                'clubcorp':
                getattr(self.user.home_club, 'id', None),
                'clubcorp_number':
                self.user.clubcorp_number,
                'category':
                getattr(self.user.category, 'id', None),
                'type':
                getattr(self.user.type, 'id', None),
                'home_club':
                getattr(self.user.home_club, 'pk', None),
                'option_club':
                getattr(self.user.option_club, 'pk', None),
                'home_club_alternate_1':
                getattr(self.user.home_club_alternate_1, 'pk', None),
                'home_club_alternate_2':
                getattr(self.user.home_club_alternate_2, 'pk', None),
            }
            kwargs['initial'].update(kw_initial)

        super().__init__(*args, **kwargs)

        self.fields['clubcorp'].choices += [(c.id, c.name)
                                            for c in ClubCorp.objects.all()]
        self.fields['type'].choices += [(t.id, t.name)
                                        for t in UserType.objects.all()]
        self.fields['clubcorp'].choices += [
            (c.id, c.name) for c in UserCategory.objects.all()
        ]

        self.fields['home_club'].choices += [(c.pk, c.name)
                                             for c in Club.objects.all()]
        self.fields['option_club'].choices += [(c.pk, c.name)
                                               for c in Club.objects.all()]
        self.fields['home_club_alternate_1'].choices += [
            (c.pk, c.name) for c in Club.objects.all()
        ]
        self.fields['home_club_alternate_2'].choices += [
            (c.pk, c.name) for c in Club.objects.all()
        ]

    def clean_username(self):
        data = self.cleaned_data['username']

        users = User.objects.all()

        if self.user:
            users = users.exclude(pk=self.user.pk)

        try:
            users.get(username=data)
        except User.DoesNotExist:
            pass
        else:
            raise forms.ValidationError(_('Username is already in use.'))

        return data

    def clean_password(self):
        data = self.cleaned_data['password']
        return data if data else None

    def clean_membership_number(self):
        data = self.cleaned_data['membership_number']
        return data if data else None

    def clean_employee_number(self):
        data = self.cleaned_data['employee_number']
        return data if data else None

    def clean_is_staff(self):
        is_superuser = self.cleaned_data['is_superuser']
        is_staff = self.cleaned_data['is_staff']
        return is_staff or is_superuser

    def validate_fk(self, data, klass):
        if data:
            try:
                data = klass.objects.get(pk=data)
            except klass.DoesNotExist:
                raise forms.ValidationError(_('Invalid entry.'))
        else:
            data = None
        return data

    def clean_clubcorp(self):
        data = self.cleaned_data['clubcorp']
        return self.validate_fk(data, ClubCorp)

    def clean_category(self):
        data = self.cleaned_data['category']
        return self.validate_fk(data, UserCategory)

    def clean_type(self):
        data = self.cleaned_data['type']
        return self.validate_fk(data, UserType)

    def clean_home_club(self):
        data = self.cleaned_data['home_club']
        return self.validate_fk(data, Club)

    def clean_option_club(self):
        data = self.cleaned_data['option_club']
        return self.validate_fk(data, Club)

    def clean_home_club_alternate_1(self):
        data = self.cleaned_data['home_club_alternate_1']
        return self.validate_fk(data, Club)

    def clean_home_club_alternate_2(self):
        data = self.cleaned_data['home_club_alternate_2']
        return self.validate_fk(data, Club)