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.conf import settings
25from django.contrib import messages
26from django.contrib.auth.decorators import login_required
27from django.http import HttpResponseRedirect
28from django.shortcuts import redirect, render
29from django.urls import reverse
30from django.utils.translation import gettext as _
31
32from allauth.account.models import EmailAddress
33
34from django_mailman3.forms import UserProfileForm
35from django_mailman3.lib.mailman import get_mailman_user
36from django_mailman3.models import Profile
37
38
39@login_required
40def user_profile(request):
41    try:
42        profile = Profile.objects.get(user=request.user)
43    except Profile.DoesNotExist:
44        # Create the profile if it does not exist. There's a signal receiver
45        # that creates it for new users, but this app may be added to an
46        # existing Django project with existing users.
47        profile = Profile.objects.create(user=request.user)
48    mm_user = get_mailman_user(request.user)
49    initial_data = {
50        "username": request.user.username,
51        "first_name": request.user.first_name,
52        "last_name": request.user.last_name,
53        "timezone": profile.timezone,
54        }
55
56    if request.method == 'POST':
57        form = UserProfileForm(request.POST, initial=initial_data)
58        if form.is_valid():
59            if form.has_changed():
60                request.user.username = form.cleaned_data["username"]
61                request.user.first_name = form.cleaned_data["first_name"]
62                request.user.last_name = form.cleaned_data["last_name"]
63                profile.timezone = form.cleaned_data["timezone"]
64                request.user.save()
65                profile.save()
66                # Now update the display name in Mailman
67                if mm_user is not None:
68                    mm_user.display_name = "%s %s" % (
69                            request.user.first_name, request.user.last_name)
70                    mm_user.save()
71                messages.success(
72                    request, _("The profile was successfully updated."))
73            else:
74                messages.success(request, _("No change detected."))
75            return redirect(reverse('mm_user_profile'))
76    else:
77        form = UserProfileForm(initial=initial_data)
78
79    # Emails
80    other_addresses = EmailAddress.objects.filter(
81        user=request.user).exclude(
82        email=request.user.email).order_by("email").values_list(
83        'email', flat=True)
84
85    # Extract the gravatar_url used by django_gravatar2.  The site
86    # administrator could alternatively set this to http://cdn.libravatar.org/
87    if getattr(settings, 'HYPERKITTY_ENABLE_GRAVATAR', True):
88        gravatar_url = getattr(settings, 'GRAVATAR_URL',
89                               'http://www.gravatar.com')
90        gravatar_shortname = '.'.join(gravatar_url.split('.')[-2:]).strip('/')
91    else:
92        gravatar_url = gravatar_shortname = None
93    context = {
94        'user_profile': profile,
95        'form': form,
96        'other_addresses': other_addresses,
97        'gravatar_url': gravatar_url,
98        'gravatar_shortname': gravatar_shortname,
99    }
100    return render(request, "django_mailman3/profile/profile.html", context)
101
102
103@login_required
104def delete_account(request):
105    if request.method == 'POST':
106        mm_user = get_mailman_user(request.user)
107        if mm_user:
108            mm_user.delete()
109        request.user.delete()
110        messages.success(request, _("Successfully deleted account"))
111        return HttpResponseRedirect('/')
112    return render(request, 'django_mailman3/profile/delete_profile.html',
113                  {'delete_page': True})
114