admin.py 2.47 KB
Newer Older
1
2
3
4
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django import forms
5
from funkwhale_api.common import admin
6
7
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
8
from django.utils.translation import ugettext_lazy as _
9

10
from . import models
11
12
13
14


class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
15
        model = models.User
16
17
18
19


class MyUserCreationForm(UserCreationForm):

Eliot Berriot's avatar
Eliot Berriot committed
20
21
22
    error_message = UserCreationForm.error_messages.update(
        {"duplicate_username": "This username has already been taken."}
    )
23
24

    class Meta(UserCreationForm.Meta):
25
        model = models.User
26
27
28
29

    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
Eliot Berriot's avatar
Eliot Berriot committed
30
31
            models.User.objects.get(username=username)
        except models.User.DoesNotExist:
32
            return username
Eliot Berriot's avatar
Eliot Berriot committed
33
        raise forms.ValidationError(self.error_messages["duplicate_username"])
34
35


36
@admin.register(models.User)
37
38
39
class UserAdmin(AuthUserAdmin):
    form = MyUserChangeForm
    add_form = MyUserCreationForm
40
    list_display = [
Eliot Berriot's avatar
Eliot Berriot committed
41
42
43
44
45
46
        "username",
        "email",
        "date_joined",
        "last_login",
        "is_staff",
        "is_superuser",
47
48
    ]
    list_filter = [
Eliot Berriot's avatar
Eliot Berriot committed
49
50
51
52
53
54
        "is_superuser",
        "is_staff",
        "privacy_level",
        "permission_settings",
        "permission_library",
        "permission_federation",
55
    ]
56
57

    fieldsets = (
Eliot Berriot's avatar
Eliot Berriot committed
58
        (None, {"fields": ("username", "password", "privacy_level")}),
Eliot Berriot's avatar
Eliot Berriot committed
59
60
61
62
        (
            _("Personal info"),
            {"fields": ("first_name", "last_name", "email", "avatar")},
        ),
Eliot Berriot's avatar
Eliot Berriot committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "permission_upload",
                    "permission_library",
                    "permission_settings",
                    "permission_federation",
                )
            },
        ),
        (_("Important dates"), {"fields": ("last_login", "date_joined")}),
        (_("Useless fields"), {"fields": ("user_permissions", "groups")}),
    )
80
81
82
83
84
85
86
87


@admin.register(models.Invitation)
class InvitationAdmin(admin.ModelAdmin):
    list_select_related = True
    list_display = ["owner", "code", "creation_date", "expiration_date"]
    search_fields = ["owner__username", "code"]
    readonly_fields = ["expiration_date", "code"]