Blame view

src/pyrosapp/admin.py 5.23 KB
509509a8   haribo   Replacing admin.p...
1
from django import forms
64501c9d   Etienne Pallier   first full projec...
2
from django.contrib import admin
509509a8   haribo   Replacing admin.p...
3
from pyrosapp.models import *
fde1bc96   haribo   Link User (now Py...
4
from django.contrib.auth.models import User
c0c999f3   Etienne Pallier   admin : view or e...
5
6
7
8
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


# 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...
48
# Many To Many interface adapter
c0c999f3   Etienne Pallier   admin : view or e...
49

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


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


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


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

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


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


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


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


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


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


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


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


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


# Admin model classes

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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


509509a8   haribo   Replacing admin.p...
223
# Link the models to the admin interface
509509a8   haribo   Replacing admin.p...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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...
243
admin.site.register(PyrosUser, PyrosUserAdmin)
509509a8   haribo   Replacing admin.p...
244
245
246
admin.site.register(UserLevel, UserLevelAdmin)
admin.site.register(WeatherWatch)
admin.site.register(WeatherWatchHistory)