Blame view

src/pyrosapp/admin.py 5.68 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

480c0c02   Etienne Pallier   bugfix
8
9
10
11
#TODO?:
#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

ef84dfb8   Etienne Pallier   use global settin...
46
if settings.DEBUG:
c0c999f3   Etienne Pallier   admin : view or e...
47
48
49
50
51
52
53
    class PyrosModelAdmin(ReadOnlyModelAdmin):
        pass
else:
    class PyrosModelAdmin(admin.ModelAdmin):
        pass


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

fde1bc96   haribo   Link User (now Py...
56
57
class PyrosUserAndSPInline(admin.TabularInline):
    model = ScientificProgram.pyros_users.through
c0c999f3   Etienne Pallier   admin : view or e...
58
59


509509a8   haribo   Replacing admin.p...
60
61
class SequenceAndSHInline(admin.TabularInline):
    model = ScheduleHistory.sequences.through
c0c999f3   Etienne Pallier   admin : view or e...
62
63


509509a8   haribo   Replacing admin.p...
64
65
66
67
68
class ScheduleHistoryAdmin(admin.ModelAdmin):
    inlines = [
        SequenceAndSHInline,
    ]
    exclude = ('sequences',)
c0c999f3   Etienne Pallier   admin : view or e...
69
70


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

509509a8   haribo   Replacing admin.p...
73
74
class SequenceInline(admin.TabularInline):
    model = Sequence
6d86da1b   haribo   fix #3400, #3401,...
75
    readonly_fields = ("name",)
509509a8   haribo   Replacing admin.p...
76
77
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
78
79


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


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


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


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


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


fde1bc96   haribo   Link User (now Py...
115
116
class PyrosUserInline(admin.TabularInline):
    model = PyrosUser
6d86da1b   haribo   fix #3400, #3401,...
117
118
    readonly_fields = ("user_username",)
    fields = ("user_username",)
509509a8   haribo   Replacing admin.p...
119
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
120
121


509509a8   haribo   Replacing admin.p...
122
123
class FilterInline(admin.TabularInline):
    model = Filter
6d86da1b   haribo   fix #3400, #3401,...
124
125
    readonly_fields = ("device_name",)
    fields = ("device_name",)
509509a8   haribo   Replacing admin.p...
126
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
127
128


509509a8   haribo   Replacing admin.p...
129
130
class AlertInline(admin.TabularInline):
    model = Alert
6d86da1b   haribo   fix #3400, #3401,...
131
132
    readonly_fields = ("request_name",)
    fields = ("request_name",)
509509a8   haribo   Replacing admin.p...
133
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
134
135
136
137
138


# Admin model classes

class RequestAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
139
140
141
    inlines = [
        SequenceInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
142
143
144


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


class SequenceAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
151
152
    inlines = [
        AlbumInline,
c0c999f3   Etienne Pallier   admin : view or e...
153
        SequenceAndSHInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
154
    ]
c0c999f3   Etienne Pallier   admin : view or e...
155
156
157


class PyrosUserAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
158
159
    inlines = [
        RequestInline,
c0c999f3   Etienne Pallier   admin : view or e...
160
161
        # A user has many SPs
        PyrosUserAndSPInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
162
    ]
c0c999f3   Etienne Pallier   admin : view or e...
163
164
165


class StrategyObsAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
166
167
168
    inlines = [
        AlertInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
169
170
171


class ScientificProgramAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
172
173
    inlines = [
        RequestInline,
c0c999f3   Etienne Pallier   admin : view or e...
174
175
        # A SP has many users:
        PyrosUserAndSPInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
176
    ]
c0c999f3   Etienne Pallier   admin : view or e...
177
178
179
180
    exclude = ('pyros_users',)  # for M2M interface


class CountryAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
181
    inlines = [
fde1bc96   haribo   Link User (now Py...
182
        PyrosUserInline,
509509a8   haribo   Replacing admin.p...
183
    ]
c0c999f3   Etienne Pallier   admin : view or e...
184
185
186


class UserLevelAdmin(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 FilterAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
193
194
195
    inlines = [
        PlanInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
196
197
198


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


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


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


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


#class AlbumAdmin(admin.ModelAdmin):
class AlbumAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
225
226
227
    inlines = [
        PlanInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
228
229
230
231


#class SequenceTypeAdmin(admin.ModelAdmin):
class SequenceTypeAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
232
233
234
    inlines = [
        SequenceInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
235
236


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

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