admin.py 7.06 KB

# Django imports
from django import forms
from django.contrib import admin
from django.contrib.auth.models import User
# EP
from django.conf import settings

# Project imports
from user_mgmt.models import Country, Institute, PyrosUser, UserLevel, ScientificProgram
from common.models import *
from majordome.models import *
from env_monitor.models import *
from devices.models import Detector, Filter, AgentDeviceStatus, FilterWheel, Telescope, PlcDevice, PlcDeviceStatus
from seq_submit.models import Image, Schedule, Sequence, Album, Plan, ScheduleHasSequences #, StrategyObs, NrtAnalysis
#from seq_submit.models import Image, StrategyObs, Schedule, Request, Alert, Sequence, Album, Plan, NrtAnalysis, ScheduleHasSequences


# 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

''' Uncomment for production '''

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


class PyrosModelAdmin(admin.ModelAdmin):
    pass

# Many To Many interface adapter


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


class SequenceAndScheduleInline(admin.TabularInline):
    model = Schedule.sequences.through

class PyrosUserAndUserLevelInline(admin.TabularInline):
    # add admin representation for m2m relation between PyrosUser and UserLevel
    model = UserLevel.pyros_users.through

class ScheduleAdmin(admin.ModelAdmin):
    inlines = [
        SequenceAndScheduleInline,
    ]
    exclude = ('sequences',)


# One To Many interface adapters

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


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


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


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


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


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


class PyrosUserInline(admin.TabularInline):
    model = PyrosUser
    readonly_fields = ("user_username",)
    fields = ("user_username",)
    show_change_link = True


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


# class AlertInline(admin.TabularInline):
#     model = Alert
#     readonly_fields = ("request_name",)
#     fields = ("request_name",)
#     show_change_link = True


# Admin model classes

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


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


class PyrosUserAdmin(PyrosModelAdmin):
    list_display = ("user_username","is_active","laboratory")
    list_filter = ("is_active",)
    list_editable = ("is_active",)
    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,
        PyrosUserAndUserLevelInline,
    ]
    list_display = ("name","priority",)
    # we need to exclude pyros_users which represents the m2m relation between UserLevel and PyrosUser
    exclude = ("pyros_users",)


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


class FilterWheelAdmin(PyrosModelAdmin):
    inlines = [
        FilterInline,
    ]


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


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


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


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


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


# Link the models to the admin interface

# (EP added 10/7/19)
# #admin.site.register(AgentCmd)
# admin.site.register(AgentLogs)
# admin.site.register(AgentSurvey)
# admin.site.register(AgentDeviceStatus)


# admin.site.register(Album, AlbumAdmin)
# #admin.site.register(Alert)
# admin.site.register(Country, CountryAdmin)
# admin.site.register(Detector, DetectorAdmin)
# admin.site.register(Filter, FilterAdmin)
# admin.site.register(FilterWheel, FilterWheelAdmin)
# admin.site.register(Image)
# admin.site.register(Log)
# #admin.site.register(NrtAnalysis, NrtAnalysisAdmin)
# admin.site.register(Plan, PlanAdmin)
# #admin.site.register(Request, RequestAdmin)
# admin.site.register(Schedule, ScheduleAdmin)
# admin.site.register(ScheduleHasSequences)
# admin.site.register(ScientificProgram, ScientificProgramAdmin)
# admin.site.register(Sequence, SequenceAdmin)
# admin.site.register(SiteWatch)
# admin.site.register(SiteWatchHistory)
# #admin.site.register(StrategyObs, StrategyObsAdmin)
# ##admin.site.register(TaskId)
# admin.site.register(Telescope, TelescopeAdmin)
# admin.site.register(PyrosUser, PyrosUserAdmin)
# admin.site.register(UserLevel, UserLevelAdmin)
# admin.site.register(Version)
# admin.site.register(WeatherWatch)
# admin.site.register(WeatherWatchHistory)
# admin.site.register(PlcDeviceStatus)
# admin.site.register(PlcDevice)
# admin.site.register(Config)
# admin.site.register(Institute)