summaryrefslogtreecommitdiff
path: root/pgweb/util/admin.py
blob: 31cbaf8ebc84b53e69e9631ba2e44371a38750a3 (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
from django.contrib import admin


class PgwebAdmin(admin.ModelAdmin):
    """
    ModelAdmin wrapper that will enable a few pg specific things:
    * Markdown preview for markdown capable textfields (specified by
      including them in a class variable named markdown_capable that is a tuple
      of field names)
    """

    change_form_template = 'admin/change_form_pgweb.html'

    def formfield_for_dbfield(self, db_field, **kwargs):
        fld = admin.ModelAdmin.formfield_for_dbfield(self, db_field, **kwargs)

        if hasattr(self.model, 'markdown_fields'):
            if db_field.name in self.model.markdown_fields:
                fld.widget.attrs['class'] = fld.widget.attrs['class'] + ' markdown_preview'
        return fld

    # Remove the builtin delete_selected action, so it doesn't
    # conflict with the custom one.
    def get_actions(self, request):
        actions = super(PgwebAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions

    # Define a custom delete_selected action. This is required because the
    # default one uses the delete functionality in QuerySet, which bypasses
    # the delete() operation on the model, and thus won't send out our
    # notifications. Manually calling delete() on each one will be slightly
    # slower, but will send proper notifications - and it's not like this
    # is something that happens often enough that we care about performance.
    def custom_delete_selected(self, request, queryset):
        for x in queryset:
            x.delete()
    custom_delete_selected.short_description = "Delete selected items"
    actions = ['custom_delete_selected']


def register_pgwebadmin(model):
    admin.site.register(model, PgwebAdmin)