Completed
Push — master ( 4f7ee6...646424 )
by Paolo
08:30 queued 06:53
created

uid.admin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 173
dl 0
loc 285
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A DictBreedAdmin.get_term() 0 2 1
A DictBreedAdmin.get_label() 0 2 1
A UserAdmin.get_inline_instances() 0 4 2
A SampleInLineFormSet.__init__() 0 13 1
A PersonAdmin.user_name() 0 2 1
A PersonAdmin.full_name() 0 2 1
A UserAdmin.get_role() 0 2 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Wed Nov  7 11:54:15 2018
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from django import forms
10
from django.contrib import admin
11
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
12
from django.contrib.auth.models import User
13
14
from .models import (Animal, DictBreed, DictCountry, DictRole, DictSpecie,
15
                     Ontology, Organization, Person, Publication, Sample,
16
                     Submission, DictSex, DictUberon, DictDevelStage,
17
                     DictPhysioStage)
18
19
20
class DictBreedAdmin(admin.ModelAdmin):
21
    search_fields = ['supplied_breed']
22
    list_per_page = 25
23
    list_display = (
24
        'supplied_breed', 'get_label', 'get_term', 'mapped_breed',
25
        'mapped_breed_term', 'confidence', 'country', 'specie')
26
27
    # override label and term verbose names
28
    def get_label(self, instance):
29
        return instance.label
30
    get_label.short_description = "label"
31
32
    def get_term(self, instance):
33
        return instance.term
34
    get_term.short_description = "term"
35
36
    list_filter = ('country', 'specie')
37
38
39
# inspired from here:
40
# https://github.com/geex-arts/django-jet/issues/244#issuecomment-325001298
41
class SampleInLineFormSet(forms.BaseInlineFormSet):
42
    ''' Base Inline formset for Sample Model'''
43
44
    def __init__(self, *args, **kwargs):
45
        super(SampleInLineFormSet, self).__init__(*args, **kwargs)
46
47
        # Animal will be a Animal object called when editing animal
48
        animal = kwargs["instance"]
49
50
        # get all Sample names from a certain animal with a unique query
51
        # HINT: all problems related to subquery when displaying names arise
52
        # from my __str__s methods, which display Sample and Animal name
53
        # and the relation between table Name. In order to have all sample
54
        # names for each animal, I need a join with three tables
55
        self.queryset = Sample.objects.select_related(
56
            'animal').filter(animal=animal)
57
58
59
class SampleInline(admin.StackedInline):
60
    formset = SampleInLineFormSet
61
62
    fields = (
63
        ('name', 'alternative_id', 'biosample_id'),
64
        ('submission', 'owner', 'status'),
65
        ('description', 'publication'),
66
        ('animal', 'protocol', 'organism_part'),
67
        ('collection_date', 'collection_place', 'collection_place_latitude',
68
         'collection_place_longitude', 'collection_place_accuracy'),
69
        ('developmental_stage',
70
         'physiological_stage', 'animal_age_at_collection',
71
         'animal_age_at_collection_units', 'availability'),
72
        ('storage', 'storage_processing', 'preparation_interval',
73
         'preparation_interval_units'),
74
        'last_submitted',
75
    )
76
77
    # manage a fields with many FK keys
78
    # https://books.agiliq.com/projects/django-admin-cookbook/en/latest/many_fks.html
79
    raw_id_fields = ("animal", "submission", "publication")
80
81
    model = Sample
82
    extra = 0
83
84
85
class SampleAdmin(admin.ModelAdmin):
86
    # exclude = ('author',)
87
    # prepopulated_fields = {'name': ['description']}
88
    search_fields = ['name', 'biosample_id']
89
    list_per_page = 25
90
91
    list_display = (
92
        'name', 'biosample_id', 'submission', 'status', 'last_changed',
93
        'last_submitted', 'alternative_id', 'animal', 'collection_date',
94
        'collection_place', 'collection_place_latitude',
95
        'collection_place_longitude', 'collection_place_accuracy',
96
        'organism_part', 'developmental_stage', 'physiological_stage',
97
        'animal_age_at_collection', 'animal_age_at_collection_units',
98
        'availability', 'storage', 'storage_processing',
99
        'preparation_interval', 'preparation_interval_units',
100
        'description', 'publication', 'owner'
101
        )
102
103
    # To tell Django we want to perform a join instead of fetching the names of
104
    # the categories one by one
105
    # https://medium.com/@hakibenita/things-you-must-know-about-django-admin-as-your-app-gets-bigger-6be0b0ee9614
106
    list_select_related = (
107
        'submission', 'submission__gene_bank_country', 'animal',
108
        'organism_part', 'developmental_stage', 'physiological_stage', 'owner'
109
    )
110
111
    list_filter = ('owner', 'status')
112
113
    fields = (
114
        ('name', 'alternative_id', 'biosample_id'),
115
        ('submission', 'owner', 'status'),
116
        ('description', 'publication'),
117
        ('animal', 'protocol', 'organism_part'),
118
        ('collection_date', 'collection_place', 'collection_place_latitude',
119
         'collection_place_longitude', 'collection_place_accuracy'),
120
        ('developmental_stage',
121
         'physiological_stage', 'animal_age_at_collection',
122
         'animal_age_at_collection_units', 'availability'),
123
        ('storage', 'storage_processing', 'preparation_interval',
124
         'preparation_interval_units'),
125
        'last_submitted',
126
    )
127
128
    # manage a fields with many FK keys
129
    # https://books.agiliq.com/projects/django-admin-cookbook/en/latest/many_fks.html
130
    raw_id_fields = ("animal", "submission", "publication")
131
132
133
class AnimalAdmin(admin.ModelAdmin):
134
    search_fields = ['name', 'biosample_id']
135
136
    list_per_page = 25
137
    list_display = (
138
        'name', 'biosample_id', 'submission', 'status', 'last_changed',
139
        'last_submitted', 'alternative_id', 'breed', 'sex', 'father', 'mother',
140
        'birth_date', 'birth_location', 'birth_location_latitude',
141
        'birth_location_longitude', 'birth_location_accuracy', 'description',
142
        'publication', 'owner'
143
        )
144
145
    list_filter = ('owner', 'status')
146
147
    # I don't want to change this
148
    readonly_fields = ("owner",)
149
150
    fields = (
151
        ('name', 'alternative_id', 'biosample_id'),
152
        ('submission', 'owner', 'status'),
153
        ('description', 'publication'),
154
        ('breed', 'sex'),
155
        ('father', 'mother'),
156
        ('birth_date', 'birth_location'),
157
        ('birth_location_latitude',
158
         'birth_location_longitude', 'birth_location_accuracy'),
159
        'last_submitted',
160
    )
161
162
    # manage a fields with many FK keys
163
    # https://books.agiliq.com/projects/django-admin-cookbook/en/latest/many_fks.html
164
    raw_id_fields = ("father", "mother", "breed", "submission", "publication")
165
166
    # https://medium.com/@hakibenita/things-you-must-know-about-django-admin-as-your-app-gets-bigger-6be0b0ee9614
167
    list_select_related = (
168
        'submission', 'submission__gene_bank_country', 'breed',
169
        'breed__specie', 'breed__country', 'sex', 'father', 'mother', 'owner')
170
171
    inlines = [SampleInline]
172
173
174
class SubmissionAdmin(admin.ModelAdmin):
175
    list_per_page = 25
176
    list_display = (
177
        'title', 'description', 'gene_bank_name', 'gene_bank_country',
178
        'datasource_type', 'datasource_version', 'organization', 'created_at',
179
        'updated_at', 'status', 'owner'
180
    )
181
182
    # I cannot edit a auto_add_now field
183
    readonly_fields = ('created_at', 'updated_at')
184
185
    list_filter = ('owner', 'status')
186
187
188
class PersonAdmin(admin.ModelAdmin):
189
    list_per_page = 25
190
    list_display = (
191
        'user_name', 'full_name', 'initials', 'affiliation', 'role',
192
    )
193
194
    def user_name(self, obj):
195
        return obj.user.username
196
197
    # rename column in admin
198
    user_name.short_description = "User Name"
199
200
    def full_name(self, obj):
201
        return "%s %s" % (obj.user.first_name, obj.user.last_name)
202
203
    full_name.short_description = "Full Name"
204
205
206
# https://simpleisbetterthancomplex.com/tutorial/2016/11/23/how-to-add-user-profile-to-django-admin.html
207
class PersonInLine(admin.StackedInline):
208
    model = Person
209
    can_delete = False
210
    verbose_name_plural = 'Person'
211
    fk_name = 'user'
212
213
214
class UserAdmin(BaseUserAdmin):
215
    inlines = (PersonInLine, )
216
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff',
217
                    'get_role')
218
    list_select_related = ('person', )
219
220
    # this will display a column Role in User admin
221
    def get_role(self, instance):
222
        return instance.person.role
223
    get_role.short_description = 'Role'
224
225
    # display the inlines only in the edit form
226
    def get_inline_instances(self, request, obj=None):
227
        if not obj:
228
            return list()
229
        return super(UserAdmin, self).get_inline_instances(request, obj)
230
231
232
class OrganizationAdmin(admin.ModelAdmin):
233
    list_per_page = 25
234
    search_fields = ['name']
235
    list_display = (
236
        'name', 'address', 'country', 'URI', 'role',
237
    )
238
239
240
class OntologyAdmin(admin.ModelAdmin):
241
    list_per_page = 25
242
    search_fields = ['library_name']
243
    list_display = (
244
        'library_name', 'library_uri', 'comment',
245
    )
246
247
248
class DictCountryAdmin(admin.ModelAdmin):
249
    list_per_page = 25
250
    list_display = ('label', 'term', 'confidence')
251
252
253
class DictSpecieAdmin(admin.ModelAdmin):
254
    list_per_page = 25
255
    list_display = (
256
        'label', 'term', 'confidence', 'general_breed_label',
257
        'general_breed_term')
258
259
260
# --- registering applications
261
262
# default admin class
263
admin.site.register(DictRole, admin.ModelAdmin)
264
admin.site.register(DictSex, admin.ModelAdmin)
265
admin.site.register(DictUberon, admin.ModelAdmin)
266
admin.site.register(DictDevelStage, admin.ModelAdmin)
267
admin.site.register(DictPhysioStage, admin.ModelAdmin)
268
269
# Custom admin class
270
admin.site.register(DictSpecie, DictSpecieAdmin)
271
admin.site.register(DictCountry, DictCountryAdmin)
272
admin.site.register(Submission, SubmissionAdmin)
273
admin.site.register(Animal, AnimalAdmin)
274
admin.site.register(Sample, SampleAdmin)
275
# admin.site.register(Name, NameAdmin)
276
admin.site.register(DictBreed, DictBreedAdmin)
277
admin.site.register(Person, PersonAdmin)
278
admin.site.register(Organization, OrganizationAdmin)
279
admin.site.register(Publication, admin.ModelAdmin)
280
admin.site.register(Ontology, OntologyAdmin)
281
282
# Re-register UserAdmin (to see related person data)
283
admin.site.unregister(User)
284
admin.site.register(User, UserAdmin)
285