Blame view

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

480c0c02   Etienne Pallier   bugfix
5
6
7
8
#TODO?:
#from pyrosapp import models as pyrosapp_models
from pyrosapp.models import *

c0c999f3   Etienne Pallier   admin : view or e...
9
10
11
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
42
43
44
45
46
47
48
49
50

# 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


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

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


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


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


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

509509a8   haribo   Replacing admin.p...
70
71
72
73
class SequenceInline(admin.TabularInline):
    model = Sequence
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
74
75


509509a8   haribo   Replacing admin.p...
76
77
78
79
class RequestInline(admin.TabularInline):
    model = Request
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
80
81


509509a8   haribo   Replacing admin.p...
82
83
84
85
class AlbumInline(admin.TabularInline):
    model = Album
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
86
87


509509a8   haribo   Replacing admin.p...
88
89
90
91
class PlanInline(admin.TabularInline):
    model = Plan
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
92
93


509509a8   haribo   Replacing admin.p...
94
95
96
97
class ImageInline(admin.TabularInline):
    model = Image
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
98
99


509509a8   haribo   Replacing admin.p...
100
101
102
103
class DetectorInline(admin.TabularInline):
    model = Detector
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
104
105


fde1bc96   haribo   Link User (now Py...
106
107
class PyrosUserInline(admin.TabularInline):
    model = PyrosUser
c0c999f3   Etienne Pallier   admin : view or e...
108
109
    #fields = ("name",)
    fields = ("user.username",)
509509a8   haribo   Replacing admin.p...
110
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
111
112


509509a8   haribo   Replacing admin.p...
113
114
115
116
class FilterInline(admin.TabularInline):
    model = Filter
    fields = ("name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
117
118


509509a8   haribo   Replacing admin.p...
119
120
121
122
class AlertInline(admin.TabularInline):
    model = Alert
    fields = ("request.name",)
    show_change_link = True
c0c999f3   Etienne Pallier   admin : view or e...
123
124
125
126
127


# Admin model classes

class RequestAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
128
129
130
    inlines = [
        SequenceInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
131
132
133


class ScheduleAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
134
135
136
    inlines = [
        SequenceInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
137
138
139


class SequenceAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
140
141
    inlines = [
        AlbumInline,
c0c999f3   Etienne Pallier   admin : view or e...
142
        SequenceAndSHInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
143
    ]
c0c999f3   Etienne Pallier   admin : view or e...
144
145
146


class PyrosUserAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
147
148
    inlines = [
        RequestInline,
c0c999f3   Etienne Pallier   admin : view or e...
149
150
        # A user has many SPs
        PyrosUserAndSPInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
151
    ]
c0c999f3   Etienne Pallier   admin : view or e...
152
153
154


class StrategyObsAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
155
156
157
    inlines = [
        AlertInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
158
159
160


class ScientificProgramAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
161
162
    inlines = [
        RequestInline,
c0c999f3   Etienne Pallier   admin : view or e...
163
164
        # A SP has many users:
        PyrosUserAndSPInline,  # for M2M interface
509509a8   haribo   Replacing admin.p...
165
    ]
c0c999f3   Etienne Pallier   admin : view or e...
166
167
168
169
    exclude = ('pyros_users',)  # for M2M interface


class CountryAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
170
    inlines = [
fde1bc96   haribo   Link User (now Py...
171
        PyrosUserInline,
509509a8   haribo   Replacing admin.p...
172
    ]
c0c999f3   Etienne Pallier   admin : view or e...
173
174
175


class UserLevelAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
176
    inlines = [
fde1bc96   haribo   Link User (now Py...
177
        PyrosUserInline,
509509a8   haribo   Replacing admin.p...
178
    ]
c0c999f3   Etienne Pallier   admin : view or e...
179
180
181


class FilterAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
182
183
184
    inlines = [
        PlanInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
185
186
187


class NrtAnalysisAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
188
189
190
    inlines = [
        ImageInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
191
192
193


class DetectorAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
194
195
196
197
    inlines = [
        FilterInline,
        AlbumInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
198
199
200


class TelescopeAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
201
202
203
    inlines = [
        DetectorInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
204
205
206


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


#class AlbumAdmin(admin.ModelAdmin):
class AlbumAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
214
215
216
    inlines = [
        PlanInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
217
218
219
220


#class SequenceTypeAdmin(admin.ModelAdmin):
class SequenceTypeAdmin(PyrosModelAdmin):
509509a8   haribo   Replacing admin.p...
221
222
223
    inlines = [
        SequenceInline,
    ]
c0c999f3   Etienne Pallier   admin : view or e...
224
225


509509a8   haribo   Replacing admin.p...
226
# Link the models to the admin interface
480c0c02   Etienne Pallier   bugfix
227
228
229
#TODO?:
#admin.site.register(pyrosapp_models.*)

509509a8   haribo   Replacing admin.p...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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...
249
admin.site.register(PyrosUser, PyrosUserAdmin)
509509a8   haribo   Replacing admin.p...
250
251
252
admin.site.register(UserLevel, UserLevelAdmin)
admin.site.register(WeatherWatch)
admin.site.register(WeatherWatchHistory)