admin.py 3.19 KB
Newer Older
1
"""The admin interfaces registered by the pushnotifications package"""
2
3
4
5
6
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from pushnotifications import models
from pushnotifications.models import Message
7
from utils.translation import TranslatedModelAdmin
8
9
10
11


@admin.register(models.Device)
class DeviceAdmin(admin.ModelAdmin):
12
    """Manage the devices"""
13
    list_display = ('name', 'type', 'active', 'date_created')
14
15
    list_filter = ('active', 'type')
    actions = ('enable', 'disable')
16
    ordering = ('user__first_name', )
17
18
    search_fields = ('registration_id', 'user__username',
                     'user__first_name', 'user__last_name')
19
20
21
22
23
24
25
26
27

    def enable(self, request, queryset):
        queryset.update(active=True)
    enable.short_description = _('Enable selected devices')

    def disable(self, request, queryset):
        queryset.update(active=False)
    disable.short_description = _('Disable selected devices')

28
29
30
    def name(self, obj):
        return '{} ({})'.format(obj.user.get_full_name(), obj.user.username)
    name.short_description = _('Name')
31
    name.admin_order_field = 'user__first_name'
32

33
34

@admin.register(models.Message)
35
class MessageAdmin(TranslatedModelAdmin):
36
    """Manage normal messages"""
37
38
    list_display = ('title', 'body', 'category', 'url',
                    'sent', 'success', 'failure')
39
    filter_horizontal = ('users',)
40
    list_filter = ('sent', 'category')
41
42
43

    def get_fields(self, request, obj=None):
        if obj and obj.sent:
44
            return ('users', 'title_nl', 'title_en', 'body_nl', 'body_en',
45
                    'url', 'category', 'success', 'failure')
46
        return ('users', 'title_nl', 'title_en', 'body_nl', 'body_en',
47
                'url', 'category')
48
49
50

    def get_readonly_fields(self, request, obj=None):
        if obj and obj.sent:
51
            return ('users', 'title_nl', 'title_en', 'body_nl', 'body_en',
52
                    'url', 'category', 'success', 'failure')
53
54
55
56
57
58
        return super().get_readonly_fields(request, obj)

    def change_view(self, request, object_id, form_url='', **kwargs):
        obj = Message.objects.filter(id=object_id)[0]
        return super(MessageAdmin, self).change_view(
            request, object_id, form_url, {'message': obj})
59
60
61
62


@admin.register(models.ScheduledMessage)
class ScheduledMessageAdmin(TranslatedModelAdmin):
63
    """Manage scheduled messages"""
64
65
66
67
68
69
70
71
72
    list_display = ('title', 'body', 'time', 'category', 'sent', 'success',
                    'failure')
    date_hierarchy = 'time'
    filter_horizontal = ('users',)
    list_filter = ('sent', 'category')

    def get_fields(self, request, obj=None):
        if obj and obj.sent:
            return ('users', 'title_nl', 'title_en', 'body_nl', 'body_en',
73
74
                    'url', 'category', 'success', 'failure', 'time',
                    'executed')
75
        return ('users', 'title_nl', 'title_en', 'body_nl', 'body_en',
76
                'url', 'category', 'time')
77
78
79
80

    def get_readonly_fields(self, request, obj=None):
        if obj and obj.sent:
            return ('users', 'title_nl', 'title_en', 'body_nl', 'body_en',
81
82
83
                    'url', 'category', 'success', 'failure', 'time',
                    'executed')
        return 'executed',