aboutsummaryrefslogtreecommitdiffstats
path: root/account/admin.py
blob: fe6fca12850f694d78e5b7f0463252baa5026057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-

from django.contrib import admin
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _

from account.models import UserProfile, UserFile

import os


class UserProfileAdmin(admin.ModelAdmin):
    """
    customize the admin interface for UserProfile
    """
    actions = [
        'approve_users',
        'sponsor_users',
        'cancel_approve_users',
        'cancel_sponsor_users',
        'reset_approve_users',
        'reset_sponsor_users',
    ]
    list_display = (
        'user',
        'realname',
        'gender',
        'institute',
        'identify',
        'reason',
        'transcript_url',
        'supplement',
        'attachments',
        'is_approved',
        'is_sponsored',
    )

    ## custom admin actions
    def approve_users(self, request, queryset):
        """
        Approve the selected users.
        """
        profiles_updated = queryset.update(is_approved='Y')
        if profiles_updated == 1:
            msg = _("1 user was successfully approved.")
        else:
            msg = _("%(num)s users were successfully approved." % {'num': profiles_updated})
        self.message_user(request, msg)
    approve_users.short_description = _("Approve users")

    def sponsor_users(self, request, queryset):
        """
        Sponsor the selected users.
        """
        profiles_updated = queryset.update(is_sponsored='Y')
        if profiles_updated == 1:
            msg = _("1 user was successfully sponsored.")
        else:
            msg = _("%(num)s users were successfully sponsored." % {'num': profiles_updated})
        self.message_user(request, msg)
    sponsor_users.short_description = _("Sponsor users")

    def cancel_approve_users(self, request, queryset):
        """
        Cancel the approval of the selected users.
        """
        profiles_updated = queryset.update(is_approved='N')
        if profiles_updated == 1:
            msg = _("1 user was successfully cancelled approval.")
        else:
            msg = _("%(num)s users were successfully cancelled approval." % {'num': profiles_updated})
        self.message_user(request, msg)
    cancel_approve_users.short_description = _("Cancel approve users")

    def cancel_sponsor_users(self, request, queryset):
        """
        Cancel the sponsor of the selected users.
        """
        profiles_updated = queryset.update(is_sponsored='N')
        if profiles_updated == 1:
            msg = _("1 user was successfully cancelled sponsor.")
        else:
            msg = _("%(num)s users were successfully cancelled sponsor." % {'num': profiles_updated})
        self.message_user(request, msg)
    cancel_sponsor_users.short_description = _("Cancel sponsor users")

    def reset_approve_users(self, request, queryset):
        """
        Reset the approval of the selected users.
        """
        profiles_updated = queryset.update(is_approved='C')
        if profiles_updated == 1:
            msg = _("1 user was successfully reset approval.")
        else:
            msg = _("%(num)s users were successfully reset approval." % {'num': profiles_updated})
        self.message_user(request, msg)
    reset_approve_users.short_description = _("Reset approve users")

    def reset_sponsor_users(self, request, queryset):
        """
        Reset the sponsor of the selected users.
        """
        profiles_updated = queryset.update(is_sponsored='C')
        if profiles_updated == 1:
            msg = _("1 user was successfully reset sponsor.")
        else:
            msg = _("%(num)s users were successfully reset sponsor." % {'num': profiles_updated})
        self.message_user(request, msg)
    reset_sponsor_users.short_description = _("Reset sponsor users")

    ## custom fields
    def transcript_url(self, obj):
        """
        return the html code of transcript with url link
        """
        transcript = obj.transcript
        if transcript:
            html = '<a href="%(url)s">%(name)s</a>' % {
                'url': transcript.url,
                'name': os.path.basename(transcript.name),
            }
        else:
            html = _("Null")
        return format_html(html)
    transcript_url.short_description = _("Transcript")

    def attachments(self, obj):
        """
        return the html code of attachments with url
        """
        user = obj.user
        files = user.userfile_set.all()
        if files:
            attachments = ['<a href="%(url)s">%(name)s</a>' % {
                    'url': userfile.file.url,
                    'name': os.path.basename(userfile.file.name),
                }
                for userfile in files
            ]
            html = '<br>'.join(attachments)
        else:
            html = _("Null")
        return format_html(html)
    attachments.short_description = _("Attachments")


admin.site.register(UserProfile, UserProfileAdmin)
admin.site.register(UserFile)