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?:
bb45cd4a   haribo   Date: 25/05/2016
9
# from pyrosapp import models as pyrosapp_models
480c0c02   Etienne Pallier   bugfix
10
11
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
bb45cd4a   haribo   Date: 25/05/2016
42
# DEBUG = False
c0c999f3   Etienne Pallier   admin : view or e...
43
# View only mode
bb45cd4a   haribo   Date: 25/05/2016
44
# DEBUG = True
c0c999f3   Etienne Pallier   admin : view or e...
45

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


7a79e25b   haribo   Date: 19/05/2016
66
67
class SequenceAndScheduleInline(admin.TabularInline):
    model = Schedule.sequences.through
c0c999f3   Etienne Pallier   admin : view or e...
68
69


7a79e25b   haribo   Date: 19/05/2016
70
class ScheduleAdmin(admin.ModelAdmin):
509509a8   haribo   Replacing admin.p...
71
    inlines = [
7a79e25b   haribo   Date: 19/05/2016
72
        SequenceAndScheduleInline,
509509a8   haribo   Replacing admin.p...
73
74
    ]
    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


c0c999f3   Etienne Pallier   admin : view or e...
150
class SequenceAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
151
152
    inlines = [
        AlbumInline,
7a79e25b   haribo   Date: 19/05/2016
153
        SequenceAndScheduleInline,  # 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
    inlines = [
509509a8   haribo   Replacing admin.p...
206
207
        AlbumInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
208
209
210


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


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


94336356   haribo   Added starting fi...
222
# class AlbumAdmin(admin.ModelAdmin):
c0c999f3   Etienne Pallier   admin : view or e...
223
class AlbumAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
224
225
226
    inlines = [
        PlanInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
227
228


509509a8   haribo   Replacing admin.p...
229
# Link the models to the admin interface
94336356   haribo   Added starting fi...
230
231
# TODO?:
# admin.site.register(pyrosapp_models.*)
480c0c02   Etienne Pallier   bugfix
232

509509a8   haribo   Replacing admin.p...
233
234
235
236
237
238
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)
ec372f36   haribo   Date: 28/06/2016
239
admin.site.register(FilterWheel)
509509a8   haribo   Replacing admin.p...
240
admin.site.register(Image)
bb45cd4a   haribo   Date: 25/05/2016
241
admin.site.register(Log)
509509a8   haribo   Replacing admin.p...
242
243
244
245
admin.site.register(NrtAnalysis, NrtAnalysisAdmin)
admin.site.register(Plan, PlanAdmin)
admin.site.register(Request, RequestAdmin)
admin.site.register(Schedule, ScheduleAdmin)
7c87794b   haribo   Date: 20/05/2016
246
admin.site.register(ScheduleHasSequences)
509509a8   haribo   Replacing admin.p...
247
248
admin.site.register(ScientificProgram, ScientificProgramAdmin)
admin.site.register(Sequence, SequenceAdmin)
509509a8   haribo   Replacing admin.p...
249
250
251
admin.site.register(SiteWatch)
admin.site.register(SiteWatchHistory)
admin.site.register(StrategyObs, StrategyObsAdmin)
bb45cd4a   haribo   Date: 25/05/2016
252
admin.site.register(TaskId)
509509a8   haribo   Replacing admin.p...
253
admin.site.register(Telescope, TelescopeAdmin)
fde1bc96   haribo   Link User (now Py...
254
admin.site.register(PyrosUser, PyrosUserAdmin)
509509a8   haribo   Replacing admin.p...
255
admin.site.register(UserLevel, UserLevelAdmin)
9f81217f   haribo   Date: 22/06/2016
256
admin.site.register(Version)
509509a8   haribo   Replacing admin.p...
257
258
admin.site.register(WeatherWatch)
admin.site.register(WeatherWatchHistory)