1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2016-2021 by the Free Software Foundation, Inc.
4#
5# This file is part of Django-Mailman.
6#
7# Django-Mailman is free software: you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by the Free
9# Software Foundation, either version 3 of the License, or (at your option)
10# any later version.
11#
12# Django-Mailman is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15# more details.
16#
17# You should have received a copy of the GNU General Public License along with
18# Django-Mailman.  If not, see <http://www.gnu.org/licenses/>.
19#
20# Author: Aurelien Bompard <abompard@fedoraproject.org>
21#
22
23
24from django import forms
25from django.contrib.auth.models import User
26from django.utils.translation import gettext_lazy as _
27
28from django_mailman3.models import TIMEZONES
29
30
31class UserProfileForm(forms.Form):
32    username = forms.CharField(required=True, label=_('Username'))
33    first_name = forms.CharField(label=_('First name'))
34    last_name = forms.CharField(label=_('Last name'))
35    timezone = forms.ChoiceField(
36        label=_('Time zone'), choices=TIMEZONES)
37
38    def clean_username(self):
39        username = self.cleaned_data.get("username")
40        if username != self.initial.get("username"):
41            if User.objects.filter(username=username).exists():
42                raise forms.ValidationError(
43                    _("A user with that username already exists."))
44        return username
45