summaryrefslogtreecommitdiff
path: root/sublab_project/calendarium/admin.py
blob: 60e9d7ab1428b7a69ea14a57273cb265ffcab854 (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
from django.contrib import admin

from calendarium.models import Event


class EventAdmin(admin.ModelAdmin):
    list_display = ('name', 'start', 'end', 'source')
    list_filter = ('source',)
    search_fields = ['name', 'description']
    date_hierachy = 'start'
    actions = None
    delete_confirmation_template = 'calendarium/delete_confirmation.html'

    def save_model(self, request, obj, form, change):
        """Sets the source to "admin".
        """
        if not obj.id:
            obj.source = Event.SOURCE_ADMIN
        obj.save()

    def get_readonly_fields(self, request, obj=None):
        """Sets all fields read only if source is "import".
        """
        if obj and obj.source == obj.SOURCE_IMPORT:
            return ['name', 'description', 'start', 'end']
        return []


admin.site.register(Event, EventAdmin)