Blame view

src/pyrosapp/admin.py 5.6 KB
509509a8   haribo   Replacing admin.p...
1
from django import forms
64501c9d   Etienne Pallier   first full projec...
2
from django.contrib import admin
fde1bc96   haribo   Link User (now Py...
3
from django.contrib.auth.models import User
c0c999f3   Etienne Pallier   admin : view or e...
4

ef84dfb8   Etienne Pallier   use global settin...
5
6
7
# EP
from django.conf import settings

94336356   haribo   Added starting fi...
8
# TODO?:
480c0c02   Etienne Pallier   bugfix
9
10
11
#from pyrosapp import models as pyrosapp_models
from pyrosapp.models import *

c0c999f3   Etienne Pallier   admin : view or e...
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

# 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
ef84dfb8   Etienne Pallier   use global settin...
42
#DEBUG = False
c0c999f3   Etienne Pallier   admin : view or e...
43
44
45
# View only mode
#DEBUG = True

94336356   haribo   Added starting fi...
46
''' Uncomment for production '''
c0c999f3   Etienne Pallier   admin : view or e...
47

94336356   haribo   Added starting fi...
48
49
50
51
52
53
54
55
56
57
# if settings.DEBUG:
#     class PyrosModelAdmin(ReadOnlyModelAdmin):
#         pass
# else:
#     class PyrosModelAdmin(admin.ModelAdmin):
#         pass


class PyrosModelAdmin(admin.ModelAdmin):
    pass
c0c999f3   Etienne Pallier   admin : view or e...
58

509509a8   haribo   Replacing admin.p...
59
# Many To Many interface adapter
c0c999f3   Etienne Pallier   admin : view or e...
60

94336356   haribo   Added starting fi...
61

fde1bc96   haribo   Link User (now Py...
62
63
class PyrosUserAndSPInline(admin.TabularInline):
    model = ScientificProgram.pyros_users.through
c0c999f3   Etienne Pallier   admin : view or e...
64
65


509509a8   haribo   Replacing admin.p...
66
67
class SequenceAndSHInline(admin.TabularInline):
    model = ScheduleHistory.sequences.through
c0c999f3   Etienne Pallier   admin : view or e...
68
69


509509a8   haribo   Replacing admin.p...
70
71
72
73
74
class ScheduleHistoryAdmin(admin.ModelAdmin):
    inlines = [
        SequenceAndSHInline,
    ]
    exclude = ('sequences',)
c0c999f3   Etienne Pallier   admin : view or e...
75
76


509509a8   haribo   Replacing admin.p...
77
# One To Many interface adapters
c0c999f3   Etienne Pallier   admin : view or e...
78

509509a8   haribo   Replacing admin.p...
79
80
class SequenceInline(admin.TabularInline):
    model = Sequence
6d86da1b   haribo   fix #3400, #3401,...
81
    readonly_fields = ("name",)
509509a8   haribo   Replacing admin.p...
82
83
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
84
85


509509a8   haribo   Replacing admin.p...
86
87
class RequestInline(admin.TabularInline):
    model = Request
6d86da1b   haribo   fix #3400, #3401,...
88
    readonly_fields = ("name",)
509509a8   haribo   Replacing admin.p...
89
90
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
91
92


509509a8   haribo   Replacing admin.p...
93
94
class AlbumInline(admin.TabularInline):
    model = Album
6d86da1b   haribo   fix #3400, #3401,...
95
    readonly_fields = ("name",)
509509a8   haribo   Replacing admin.p...
96
97
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
98
99


509509a8   haribo   Replacing admin.p...
100
101
class PlanInline(admin.TabularInline):
    model = Plan
6d86da1b   haribo   fix #3400, #3401,...
102
    readonly_fields = ("name",)
509509a8   haribo   Replacing admin.p...
103
104
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
105
106


509509a8   haribo   Replacing admin.p...
107
108
class ImageInline(admin.TabularInline):
    model = Image
6d86da1b   haribo   fix #3400, #3401,...
109
    readonly_fields = ("name",)
509509a8   haribo   Replacing admin.p...
110
111
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
112
113


509509a8   haribo   Replacing admin.p...
114
115
class DetectorInline(admin.TabularInline):
    model = Detector
6d86da1b   haribo   fix #3400, #3401,...
116
117
    readonly_fields = ("device_name",)
    fields = ("device_name",)
509509a8   haribo   Replacing admin.p...
118
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
119
120


fde1bc96   haribo   Link User (now Py...
121
122
class PyrosUserInline(admin.TabularInline):
    model = PyrosUser
6d86da1b   haribo   fix #3400, #3401,...
123
124
    readonly_fields = ("user_username",)
    fields = ("user_username",)
509509a8   haribo   Replacing admin.p...
125
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
126
127


509509a8   haribo   Replacing admin.p...
128
129
class FilterInline(admin.TabularInline):
    model = Filter
6d86da1b   haribo   fix #3400, #3401,...
130
131
    readonly_fields = ("device_name",)
    fields = ("device_name",)
509509a8   haribo   Replacing admin.p...
132
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
133
134


509509a8   haribo   Replacing admin.p...
135
136
class AlertInline(admin.TabularInline):
    model = Alert
6d86da1b   haribo   fix #3400, #3401,...
137
138
    readonly_fields = ("request_name",)
    fields = ("request_name",)
509509a8   haribo   Replacing admin.p...
139
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
140
141
142
143
144


# Admin model classes

class RequestAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
145
146
147
    inlines = [
        SequenceInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
148
149
150


class ScheduleAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
151
152
153
    inlines = [
        SequenceInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
154
155
156


class SequenceAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
157
158
    inlines = [
        AlbumInline,
c0c999f3   Etienne Pallier   admin : view or e...
159
        SequenceAndSHInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
160
    ]
c0c999f3   Etienne Pallier   admin : view or e...
161
162
163


class PyrosUserAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
164
165
    inlines = [
        RequestInline,
c0c999f3   Etienne Pallier   admin : view or e...
166
167
        # A user has many SPs
        PyrosUserAndSPInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
168
    ]
c0c999f3   Etienne Pallier   admin : view or e...
169
170
171


class StrategyObsAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
172
173
174
    inlines = [
        AlertInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
175
176
177


class ScientificProgramAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
178
179
    inlines = [
        RequestInline,
c0c999f3   Etienne Pallier   admin : view or e...
180
181
        # A SP has many users:
        PyrosUserAndSPInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
182
    ]
c0c999f3   Etienne Pallier   admin : view or e...
183
184
185
186
    exclude = ('pyros_users',)  # for M2M interface


class CountryAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
187
    inlines = [
fde1bc96   haribo   Link User (now Py...
188
        PyrosUserInline,
509509a8   haribo   Replacing admin.p...
189
    ]
c0c999f3   Etienne Pallier   admin : view or e...
190
191
192


class UserLevelAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
193
    inlines = [
fde1bc96   haribo   Link User (now Py...
194
        PyrosUserInline,
509509a8   haribo   Replacing admin.p...
195
    ]
c0c999f3   Etienne Pallier   admin : view or e...
196
197
198


class FilterAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
199
200
201
    inlines = [
        PlanInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
202
203
204


class NrtAnalysisAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
205
206
207
    inlines = [
        ImageInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
208
209
210


class DetectorAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
211
212
213
214
    inlines = [
        FilterInline,
        AlbumInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
215
216
217


class TelescopeAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
218
219
220
    inlines = [
        DetectorInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
221
222
223


class PlanAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
224
225
226
    inlines = [
        ImageInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
227
228


94336356   haribo   Added starting fi...
229
# class AlbumAdmin(admin.ModelAdmin):
c0c999f3   Etienne Pallier   admin : view or e...
230
class AlbumAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
231
232
233
    inlines = [
        PlanInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
234
235


509509a8   haribo   Replacing admin.p...
236
# Link the models to the admin interface
94336356   haribo   Added starting fi...
237
238
# TODO?:
# admin.site.register(pyrosapp_models.*)
480c0c02   Etienne Pallier   bugfix
239

509509a8   haribo   Replacing admin.p...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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)
509509a8   haribo   Replacing admin.p...
254
255
256
257
admin.site.register(SiteWatch)
admin.site.register(SiteWatchHistory)
admin.site.register(StrategyObs, StrategyObsAdmin)
admin.site.register(Telescope, TelescopeAdmin)
fde1bc96   haribo   Link User (now Py...
258
admin.site.register(PyrosUser, PyrosUserAdmin)
509509a8   haribo   Replacing admin.p...
259
260
261
admin.site.register(UserLevel, UserLevelAdmin)
admin.site.register(WeatherWatch)
admin.site.register(WeatherWatchHistory)