-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
284 lines (238 loc) · 9.29 KB
/
admin.py
1
2
3
4
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""Submit the sequence by user and name of the protein is predicted."""
from django.contrib import admin
from django.contrib.admin.checks import BaseModelAdminChecks
from django.contrib.admin.models import LogEntry
from django.contrib.admin.options import get_content_type_for_model
from django.contrib.contenttypes.admin import GenericStackedInline
from django.utils.html import format_html, mark_safe
from import_export import resources
from import_export.admin import ImportExportModelAdmin
from database.models import (
PesticidalProteinDatabase,
PesticidalProteinPrivateDatabase,
)
from namingalgorithm.models import AuditEntry
from .models import Archive, SendEmail, UserSubmission
@admin.register(AuditEntry)
class AuditEntryLogAdmin(admin.ModelAdmin):
list_display = [
"action",
"username",
"ip",
]
list_filter = [
"action",
]
class LogEntryAdmin(admin.ModelAdmin):
readonly_fields = (
"content_type",
"user",
"action_time",
"object_id",
"object_repr",
"action_flag",
"change_message",
)
def has_delete_permission(self, request, obj=None):
return False
def get_actions(self, request):
actions = super(LogEntryAdmin, self).get_actions(request)
del actions["delete_selected"]
return actions
class ModelAdminLog(GenericStackedInline):
model = LogEntry
# All fields are read-only, obviously
readonly_fields = fields = ["action_time", "user", "change_message"]
# No extra fields so noone can add new logs
extra = 0
# No one can delete logs
can_delete = False
# This is a hackity hack! See below
checks_class = BaseModelAdminChecks
def change_message(self, obj):
message = obj.get_change_message()
return message
class ArchiveResource(resources.ModelResource):
class Meta:
model = Archive
class ArchiveAdmin(ImportExportModelAdmin):
resource_class = ArchiveResource
search_fields = ["submittersemail", "submittersname"]
list_display = (
"submittersname",
"accession",
"uploaded",
# 'admin_user',
"admin_comments",
)
inlines = [ModelAdminLog]
def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
if change:
change_message = "{} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {}".format(
obj.submittersname,
obj.submittersemail,
obj.name,
obj.year,
obj.sequence,
obj.bacterium,
obj.bacterium_textbox,
obj.taxonid,
obj.accession,
obj.partnerprotein,
obj.partnerprotein_textbox,
obj.toxicto,
obj.nontoxic,
obj.dnasequence,
obj.pdbcode,
obj.publication,
obj.admin_user,
obj.admin_comments,
obj.comment,
obj.uploaded,
obj.predict_name,
obj.user_provided_proteinname,
)
LogEntry.objects.create(
user=request.user,
content_type=get_content_type_for_model(obj),
object_id=obj.id,
action_flag=2,
change_message=change_message,
object_repr=obj.__str__()[:200],
)
class UserSubmissionResource(resources.ModelResource):
class Meta:
model = UserSubmission
class UserSubmissionAdmin(ImportExportModelAdmin):
resource_class = UserSubmissionResource
"""Submit the sequence by user and name of the protein is predicted."""
search_fields = ["submittersemail", "submittersname", "accession"]
list_editable = ("contact_status",)
list_display = (
"submittersname",
"accession_url",
"naming_algorithm",
"availability",
"refresh",
"send_email",
"uploaded",
"contact_status"
)
exclude = ["alignresults"]
inlines = [ModelAdminLog]
def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
if change:
change_message = "{} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {}".format(
obj.submittersname,
obj.submittersemail,
obj.name,
obj.year,
obj.sequence,
obj.bacterium,
obj.bacterium_textbox,
obj.taxonid,
obj.accession,
obj.partnerprotein,
obj.partnerprotein_textbox,
obj.toxicto,
obj.nontoxic,
obj.dnasequence,
obj.pdbcode,
obj.publication,
obj.admin_user,
obj.admin_comments,
obj.comment,
obj.uploaded,
obj.predict_name,
obj.user_provided_proteinname,
)
LogEntry.objects.create(
user=request.user,
content_type=get_content_type_for_model(obj),
object_id=obj.id,
action_flag=2,
change_message=change_message,
object_repr=obj.__str__()[:200],
)
def accession_url(self, obj):
return format_html(
'<a href="%s%s" target="_blank">%s</a>'
% (
"https://www.ncbi.nlm.nih.gov/protein/",
obj.accession,
obj.accession,
)
)
def naming_algorithm(self, obj):
if ">" in str(obj.sequence).split("\n")[0]:
obj.sequence = "\n".join(str(obj.sequence).split("\n")[1:])
"""Submit the sequence by user and name of the protein is predicted."""
return format_html(
mark_safe(
'<a href="/run_naming_algorithm/?fulltextarea={0}&submission_id={1}" target="_blank">Naming Algorithm</a>'.format(
obj.sequence, obj.id
)
)
)
def align_results(self, obj):
"""Submit the sequence by user and name of the protein is predicted."""
if obj.alignresults:
return format_html(
'<a href="/align_results/?submission_id={0}" target="_blank">View Result</a>'.format(
obj.id)
)
return ""
# def create_public(self, obj):
# """Submit the sequence by user and name of the protein is predicted."""
# return format_html('<a href="/admin/database/pesticidalproteindatabase/add/?name={0}&sequence={1}" target="_blank">Create Public</a>'.format(obj.predict_name or '', obj.sequence))
def refresh(self, obj):
"""Submit the sequence by user and name of the protein is predicted."""
return format_html('<a href="/admin/namingalgorithm/usersubmission/">refresh</a>')
def send_email(self, obj):
return format_html(
'<a href="/admin/contact/?submittersname={0}&submittersemail={1}&accession={2}" target="_blank">Send Email</a>'.format(
obj.submittersname, obj.submittersemail, obj.accession
)
)
def availability(self, obj):
accession_number = obj.accession
if not accession_number:
return format_html("<body> <p>No accession number</p> </body>")
public = PesticidalProteinDatabase.objects.filter(
accession=accession_number)
private = PesticidalProteinPrivateDatabase.objects.filter(
accession=accession_number)
if public:
print(accession_number)
return format_html('<body> <p style="color:#FF0000";>Available in Public</p></body>')
if private:
return format_html('<body> <p style="color:#FF0000";>Available in Private</p></body>')
def Pfam(self, obj):
return format_html()
naming_algorithm.allow_tags = True
naming_algorithm.description = "Run the align link for the submission"
align_results.allow_tags = True
align_results.description = "View the align results"
align_results.allow_tags = True
align_results.description = "Create new data in the database"
class SendEmailAdmin(admin.ModelAdmin):
list_display = (
"submittersname",
"submittersemail",
"accession",
"message",
)
inlines = [ModelAdminLog]
# class ArchiveAdmin(admin.ModelAdmin):
# search_fields = ('name', 'year', 'submittersname',
# 'submittersemail')
# fields = ('submittersname', 'submittersemail', 'name', 'year', 'sequence', 'bacterium', 'bacterium_textbox', 'taxonid', 'accession', 'partnerprotein', 'partnerprotein_textbox', 'toxicto', 'nontoxic', 'dnasequence', 'pdbcode', 'publication', 'comment', 'uploaded', 'predict_name', 'alignresults', 'terms_conditions')
# list_display = ('name', 'submittersname',
# 'accession', 'uploaded',)
# ordering = ('-uploaded',)
admin.site.register(LogEntry, LogEntryAdmin)
admin.site.register(UserSubmission, UserSubmissionAdmin)
admin.site.register(Archive, ArchiveAdmin)
admin.site.register(SendEmail, SendEmailAdmin)