Fakultas Ilmu Komputer UI

admin.py 3.52 KB
Newer Older
1
from django.contrib import admin
2
from django.conf import settings
3
4
5
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _

6
from .models import User, Profile
7

8

9
10
@admin.register(User)
class UserAdmin(DjangoUserAdmin):
11
    """Define admin model for custom User model with no username field."""
12

13
14
15
16
17
18
    readonly_fields = ('email',
                       'first_name',
                       'last_name',
                       'last_login',
                       'date_joined')

19
20
21
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
22
23
        (_('Permissions'), {'fields': ('is_active', 'is_staff',
                                       'is_superuser', 'is_verified',
24
25
26
27
28
29
30
31
32
33
34
35
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
        }),
    )
    list_display = ('email', 'first_name', 'last_name', 'is_staff')
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)
36

37
38
    class Media:
        js = [settings.MEDIA_URL+'/js/user_custom_admin.js', ]
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56


class InputFilter(admin.SimpleListFilter):
    template = 'admin/input_filter.html'

    def lookups(self, request, model_admin):
        # Dummy, required to show the filter.
        return ((),)

    def choices(self, changelist):
        # Grab only the "all" option.
        all_choice = next(super().choices(changelist))
        all_choice['query_parts'] = (
            (k, v)
            for k, v in changelist.get_filters_params().items()
            if k != self.parameter_name
        )
        yield all_choice
57
58


59
60
@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    list_display = ('user', 'district', 'address', 'age', 'sex', 'blood_type')
    list_filter = ('blood_type', 'sex')
    search_fields = ('address', 'district')

    readonly_fields = ('user',
                       'body_weight',
                       'id_card_no',
                       'birthplace',
                       'birthdate',
                       'sex',
                       'profession',
                       'blood_type',
                       'married_status',
                       'address',
                       'city',
                       'district',
                       'village',
                       'phone_no',
                       'work_address',
                       'work_email',
                       'work_phone_no',
                       'age')

    fieldsets = (
        (None, {'fields': ('user',)}),
        ('INFORMASI PRIBADI', {
            'fields': ('id_card_no',
                       'birthplace',
                       'birthdate',
                       'age',
                       'body_weight',
                       'sex',
                       'profession',
                       'blood_type',
                       'married_status')
        }),
        ('ALAMAT', {
            'fields': ('address',
                       'village',
                       'district',
                       'city',
                       'work_address')
        }),
        ('KONTAK', {
            'fields': ('phone_no',
                       'work_email',
                       'work_phone_no')
        }),
    )

    class Media:
        js = [settings.MEDIA_URL+'/js/profile_custom_admin.js', ]