admin.py 5.23 KB
from django import forms
from django.contrib import admin
from pyrosapp.models import *
from django.contrib.auth.models import User


# EP added
class ReadOnlyModelAdmin(admin.ModelAdmin):
    """ModelAdmin class that prevents modifications through the admin.
    The changelist and the detail view work, but a 403 is returned
    if one actually tries to edit an object.
    Source: https://gist.github.com/aaugustin/1388243
    """

    actions = None

    def get_readonly_fields(self, request, obj=None):
        return self.fields or [f.name for f in self.model._meta.fields]

    def has_add_permission(self, request):
        return False

    # Allow viewing objects but not actually changing them
    def has_change_permission(self, request, obj=None):
        if request.method not in ('GET', 'HEAD'):
            return False
        return super(ReadOnlyModelAdmin, self).has_change_permission(request, obj)

    def has_delete_permission(self, request, obj=None):
        return False


# EP added

# Edit mode
DEBUG = False
# View only mode
#DEBUG = True

if DEBUG:
    class PyrosModelAdmin(ReadOnlyModelAdmin):
        pass
else:
    class PyrosModelAdmin(admin.ModelAdmin):
        pass


# Many To Many interface adapter

class PyrosUserAndSPInline(admin.TabularInline):
    model = ScientificProgram.pyros_users.through


class SequenceAndSHInline(admin.TabularInline):
    model = ScheduleHistory.sequences.through


class ScheduleHistoryAdmin(admin.ModelAdmin):
    inlines = [
        SequenceAndSHInline,
    ]
    exclude = ('sequences',)


# One To Many interface adapters

class SequenceInline(admin.TabularInline):
    model = Sequence
    fields = ("name",)
    show_change_link = True


class RequestInline(admin.TabularInline):
    model = Request
    fields = ("name",)
    show_change_link = True


class AlbumInline(admin.TabularInline):
    model = Album
    fields = ("name",)
    show_change_link = True


class PlanInline(admin.TabularInline):
    model = Plan
    fields = ("name",)
    show_change_link = True


class ImageInline(admin.TabularInline):
    model = Image
    fields = ("name",)
    show_change_link = True


class DetectorInline(admin.TabularInline):
    model = Detector
    fields = ("name",)
    show_change_link = True


class PyrosUserInline(admin.TabularInline):
    model = PyrosUser
    #fields = ("name",)
    fields = ("user.username",)
    show_change_link = True


class FilterInline(admin.TabularInline):
    model = Filter
    fields = ("name",)
    show_change_link = True


class AlertInline(admin.TabularInline):
    model = Alert
    fields = ("request.name",)
    show_change_link = True


# Admin model classes

class RequestAdmin(PyrosModelAdmin):
    inlines = [
        SequenceInline,
    ]


class ScheduleAdmin(PyrosModelAdmin):
    inlines = [
        SequenceInline,
    ]


class SequenceAdmin(PyrosModelAdmin):
    inlines = [
        AlbumInline,
        SequenceAndSHInline,  # for M2M interface
    ]


class PyrosUserAdmin(PyrosModelAdmin):
    inlines = [
        RequestInline,
        # A user has many SPs
        PyrosUserAndSPInline,  # for M2M interface
    ]


class StrategyObsAdmin(PyrosModelAdmin):
    inlines = [
        AlertInline,
    ]


class ScientificProgramAdmin(PyrosModelAdmin):
    inlines = [
        RequestInline,
        # A SP has many users:
        PyrosUserAndSPInline,  # for M2M interface
    ]
    exclude = ('pyros_users',)  # for M2M interface


class CountryAdmin(PyrosModelAdmin):
    inlines = [
        PyrosUserInline,
    ]


class UserLevelAdmin(PyrosModelAdmin):
    inlines = [
        PyrosUserInline,
    ]


class FilterAdmin(PyrosModelAdmin):
    inlines = [
        PlanInline,
    ]


class NrtAnalysisAdmin(PyrosModelAdmin):
    inlines = [
        ImageInline,
    ]


class DetectorAdmin(PyrosModelAdmin):
    inlines = [
        FilterInline,
        AlbumInline,
    ]


class TelescopeAdmin(PyrosModelAdmin):
    inlines = [
        DetectorInline,
    ]


class PlanAdmin(PyrosModelAdmin):
    inlines = [
        ImageInline,
    ]


#class AlbumAdmin(admin.ModelAdmin):
class AlbumAdmin(PyrosModelAdmin):
    inlines = [
        PlanInline,
    ]


#class SequenceTypeAdmin(admin.ModelAdmin):
class SequenceTypeAdmin(PyrosModelAdmin):
    inlines = [
        SequenceInline,
    ]


# Link the models to the admin interface
admin.site.register(Album, AlbumAdmin)
admin.site.register(Alert)
admin.site.register(Country, CountryAdmin)
admin.site.register(Detector, DetectorAdmin)
admin.site.register(Device)
admin.site.register(Filter, FilterAdmin)
admin.site.register(Image)
admin.site.register(NrtAnalysis, NrtAnalysisAdmin)
admin.site.register(Plan, PlanAdmin)
admin.site.register(Request, RequestAdmin)
admin.site.register(Schedule, ScheduleAdmin)
admin.site.register(ScheduleHistory, ScheduleHistoryAdmin)
admin.site.register(ScientificProgram, ScientificProgramAdmin)
admin.site.register(Sequence, SequenceAdmin)
admin.site.register(SequenceType, SequenceTypeAdmin)
admin.site.register(SiteWatch)
admin.site.register(SiteWatchHistory)
admin.site.register(StrategyObs, StrategyObsAdmin)
admin.site.register(Telescope, TelescopeAdmin)
admin.site.register(PyrosUser, PyrosUserAdmin)
admin.site.register(UserLevel, UserLevelAdmin)
admin.site.register(WeatherWatch)
admin.site.register(WeatherWatchHistory)