Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F90442858
admin.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Nov 1, 17:16
Size
15 KB
Mime Type
text/x-python
Expires
Sun, Nov 3, 17:16 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
22075932
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
admin.py
View Options
from
django.contrib
import
admin
from
django
import
forms
from
datetime
import
date
,
datetime
import
re
from
import_export.admin
import
ImportExportModelAdmin
from
django.contrib.admin
import
TabularInline
from
django.contrib.admin
import
SimpleListFilter
from
django.contrib.admin
import
RelatedOnlyFieldListFilter
from
django.forms.models
import
BaseInlineFormSet
from
django.utils.translation
import
gettext_lazy
as
_
#from inline_actions.admin import InlineActionsMixin
#from inline_actions.admin import InlineActionsModelAdminMixin
from
django.shortcuts
import
render
from
django.http
import
HttpResponseRedirect
from
django.urls
import
reverse
from
django.utils.html
import
escape
,
mark_safe
,
format_html
from
.models
import
Country
from
.models
import
Language
from
.models
import
Issn
from
.models
import
Oa
from
.models
import
Publisher
from
.models
import
Journal
from
.models
import
Organization
from
.models
import
Version
from
.models
import
Licence
from
.models
import
Cost_factor_type
from
.models
import
Cost_factor
from
.models
import
Term
from
.models
import
ConditionType
from
.models
import
ConditionSet
from
.models
import
OrganizationCondition
from
.models
import
JournalCondition
# Register your models here.
@admin.register
(
Issn
)
class
IssnAdmin
(
ImportExportModelAdmin
):
list_filter
=
(
'issn_type'
,
'journal__publisher__name'
,
)
list_display
=
(
"id"
,
"issn"
,
'journal'
)
class
IssnInline
(
admin
.
TabularInline
):
model
=
Issn
readonly_fields
=
(
'issn'
,
'issn_type'
,)
# This Inline is stricty read-only for the moment
def
has_change_permission
(
self
,
request
,
obj
=
None
):
return
False
def
has_add_permission
(
self
,
request
,
obj
=
None
):
return
False
def
has_delete_permission
(
self
,
request
,
obj
=
None
):
return
False
@admin.register
(
Journal
)
class
JournalAdmin
(
ImportExportModelAdmin
):
list_display
=
(
"id"
,
"name"
,
"get_journal_issns"
,)
list_filter
=
(
'publisher__name'
,
'oa_status'
)
filter_horizontal
=
(
'publisher'
,
'language'
,
)
search_fields
=
(
'name'
,
'classIssn__issn'
)
inlines
=
(
IssnInline
,
)
def
get_journal_issns
(
self
,
obj
):
return
list
(
Issn
.
objects
.
filter
(
journal
=
obj
))
get_journal_issns
.
short_description
=
'ISSNs'
@admin.register
(
Language
)
class
LanguageAdmin
(
ImportExportModelAdmin
):
pass
@admin.register
(
Organization
)
class
OrganizationAdmin
(
ImportExportModelAdmin
):
list_display
=
(
"id"
,
"name"
)
list_filter
=
(
'is_funder'
,)
filter_horizontal
=
(
'country'
,
)
search_fields
=
(
'name'
,
)
@admin.register
(
Version
)
class
VersionAdmin
(
ImportExportModelAdmin
):
pass
@admin.register
(
Country
)
class
CountryAdmin
(
ImportExportModelAdmin
):
pass
@admin.register
(
Oa
)
class
OaAdmin
(
ImportExportModelAdmin
):
pass
@admin.register
(
Publisher
)
class
PublisherAdmin
(
ImportExportModelAdmin
):
list_display
=
(
"id"
,
"name"
)
# Experimental: what will happen with 200+ countries in the database?
list_filter
=
(
'country__name'
,
)
filter_horizontal
=
(
'country'
,
)
@admin.register
(
Term
)
class
TermAdmin
(
ImportExportModelAdmin
):
list_display
=
(
"id"
,
"__str__"
,
)
list_filter
=
(
'version'
,
'licence'
,
'ir_archiving'
)
filter_horizontal
=
(
'version'
,
'cost_factor'
,
'licence'
,
)
# textarea input is better for comments
def
get_form
(
self
,
request
,
obj
=
None
,
**
kwargs
):
kwargs
[
'widgets'
]
=
{
'comment'
:
forms
.
Textarea
}
return
super
()
.
get_form
(
request
,
obj
,
**
kwargs
)
@admin.register
(
ConditionType
)
class
ConditionTypeAdmin
(
ImportExportModelAdmin
):
list_display
=
(
"id"
,
"condition_issuer"
)
class
JournalConditionFormset
(
forms
.
BaseInlineFormSet
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
JournalConditionFormset
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
self
.
queryset
=
self
.
queryset
.
select_related
(
"journal"
,
'condition_set'
)
#class JournalConditionInline(InlineActionsMixin, TabularInline):
class
JournalConditionInline
(
TabularInline
):
model
=
JournalCondition
fields
=
(
'journal'
,
'valid_from'
,
'valid_until'
)
extra
=
1
inline_actions
=
[
'connect_all_journals'
]
autocomplete_fields
=
(
'journal'
,
)
formset
=
JournalConditionFormset
def
connect_all_journals
(
self
,
request
,
obj
,
parent_obj
=
None
):
# Do stuff here, then return None to go to current view
return
None
connect_all_journals
.
short_description
=
(
"Connect Condition Set with some or all Journals"
)
def
get_queryset
(
self
,
request
):
qs
=
super
(
JournalConditionInline
,
self
)
.
get_queryset
(
request
)
.
prefetch_related
()
return
qs
.
select_related
(
'journal'
)
class
OrganizationConditionFormset
(
forms
.
BaseInlineFormSet
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
OrganizationConditionFormset
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
self
.
queryset
=
self
.
queryset
.
select_related
(
"organization"
,
'condition_set'
)
class
OrganizationConditionInline
(
TabularInline
):
#class OrganizationConditionInline(InlineActionsMixin, TabularInline):
# model = OrganizationCondition
model
=
ConditionSet
.
organization
.
through
extra
=
1
autocomplete_fields
=
(
'organization'
,
)
formset
=
OrganizationConditionFormset
@admin.action
(
description
=
'Apply selected condition sets to multiple Journals'
)
def
connect_with_all_journals
(
modeladmin
,
request
,
queryset
):
# print(request.POST)
if
request
.
POST
.
get
(
'apply'
):
try
:
valid_from
=
date
.
fromisoformat
(
request
.
POST
[
'valid_from'
])
valid_until
=
date
.
fromisoformat
(
request
.
POST
[
'valid_until'
])
issn_list
=
set
([
x
for
x
in
re
.
split
(
' |,|;|
\n
|
\r
|
\t
'
,
request
.
POST
[
'issn_list'
])
if
len
(
x
)
>
0
])
print
(
issn_list
)
if
valid_from
>
valid_until
:
raise
ValueError
# print((valid_from, valid_until))
if
len
(
issn_list
)
==
0
:
all_journals
=
Journal
.
objects
.
all
()
else
:
journal_ids
=
list
(
Issn
.
objects
.
filter
(
issn__in
=
issn_list
)
.
values_list
(
'journal'
,
flat
=
True
)
.
distinct
())
# print(journal_ids)
all_journals
=
Journal
.
objects
.
filter
(
id__in
=
journal_ids
)
print
(
all_journals
)
print
(
len
(
issn_list
),
len
(
all_journals
))
# all_journals =[]
# The following block could certainly be optimized! AB 2021-08-12
for
condition_set
in
queryset
:
# print('-----------------')
# print(condition_set)
for
j
in
all_journals
:
# print(j)
# search for existing connections
existing_connections
=
JournalCondition
.
objects
.
filter
(
journal
=
j
,
condition_set
=
condition_set
,
valid_from__lt
=
date
.
today
(),
valid_until__gt
=
date
.
today
())
# print(existing_connections)
if
len
(
existing_connections
)
==
0
:
new_journal_condition
=
JournalCondition
(
journal
=
j
,
condition_set
=
condition_set
,
valid_from
=
valid_from
,
valid_until
=
valid_until
)
new_journal_condition
.
save
()
else
:
# This should not happen, or could it?
print
(
f
'{j} already connected with {condition_set}'
)
return
None
except
ValueError
:
pass
return
render
(
request
,
'admin/get_validity_dates.html'
,
context
=
{
'queryset'
:
queryset
,
'objects'
:
'journals'
})
@admin.action
(
description
=
'Apply selected condition sets to all Organizations'
)
def
connect_with_all_organizations
(
modeladmin
,
request
,
queryset
):
if
request
.
POST
.
get
(
'apply'
):
try
:
valid_from
=
date
.
fromisoformat
(
request
.
POST
[
'valid_from'
])
valid_until
=
date
.
fromisoformat
(
request
.
POST
[
'valid_until'
])
if
valid_from
>
valid_until
:
raise
ValueError
# print((valid_from, valid_until))
all_orgs
=
Organization
.
objects
.
all
()
for
condition_set
in
queryset
:
# print('-----------------')
# print(condition_set)
for
o
in
all_orgs
:
# print(o)
# search for existing connections
existing_connections
=
OrganizationCondition
.
objects
.
filter
(
organization
=
o
,
condition_set
=
condition_set
,
valid_from__lt
=
date
.
today
(),
valid_until__gt
=
date
.
today
())
# print(existing_connections)
if
len
(
existing_connections
)
==
0
:
new_organization_condition
=
OrganizationCondition
(
organization
=
o
,
condition_set
=
condition_set
,
valid_from
=
valid_from
,
valid_until
=
valid_until
)
new_organization_condition
.
save
()
return
None
except
ValueError
:
pass
return
render
(
request
,
'admin/get_validity_dates.html'
,
context
=
{
'queryset'
:
queryset
,
'objects'
:
'organizations'
})
@admin.action
(
description
=
'Set valid_until date'
)
def
end_validity
(
modeladmin
,
request
,
queryset
):
# print(request.POST)
if
request
.
POST
.
get
(
'apply'
):
try
:
valid_until
=
date
.
fromisoformat
(
request
.
POST
[
'date'
])
print
(
valid_until
)
for
x_condition
in
queryset
:
x_condition
.
valid_until
=
valid_until
x_condition
.
save
()
return
None
except
ValueError
:
pass
return
render
(
request
,
'admin/get_single_validity_date.html'
,
context
=
{
'queryset'
:
queryset
,
'limit'
:
'end'
,
'objects'
:
'selected journal-condition connections'
})
class
ConditionSetAdminForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
ConditionSet
fields
=
[
'condition_type'
,
'term'
,
'source'
,
'comment'
,
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
#start = datetime.now()
super
(
ConditionSetAdminForm
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
self
.
fields
[
'term'
]
.
queryset
=
Term
.
objects
.
all
()
.
prefetch_related
(
'licence'
,
'cost_factor'
,
'version'
)
#print('ConditionSetAdminForm.__init__(): ', datetime.now(), datetime.now()-start)
@admin.register
(
ConditionSet
)
class
ConditionSetAdmin
(
ImportExportModelAdmin
):
# class ConditionSetAdmin(InlineActionsModelAdminMixin, ImportExportModelAdmin):
list_display
=
(
"id"
,
"condition_type"
,
"comment"
)
search_fields
=
[
'organization__name'
,
'journal__name'
,
'comment'
]
list_filter
=
(
'condition_type'
,
)
form
=
ConditionSetAdminForm
filter_horizontal
=
(
'term'
,
)
exclude
=
(
'organization'
,
'journal'
,
)
inlines
=
(
OrganizationConditionInline
,
JournalConditionInline
,
)
# inlines = (OrganizationConditionInline, )
actions
=
[
connect_with_all_journals
,
connect_with_all_organizations
]
# textarea input is better for comments
def
get_form
(
self
,
request
,
obj
=
None
,
**
kwargs
):
kwargs
[
'widgets'
]
=
{
'comment'
:
forms
.
Textarea
}
return
super
()
.
get_form
(
request
,
obj
,
**
kwargs
)
def
get_queryset
(
self
,
request
):
#start = datetime.now()
test_model_qs
=
super
(
ConditionSetAdmin
,
self
)
.
get_queryset
(
request
)
test_model_qs
=
test_model_qs
.
prefetch_related
(
'organization'
,
'journal'
)
#print('ConditionSetAdmin.get_queryset(): ', datetime.now(), datetime.now()-start)
return
test_model_qs
class
XConditionValidListFilter
(
SimpleListFilter
):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title
=
_
(
'currently valid'
)
# Parameter for the filter that will be used in the URL query.
parameter_name
=
'valid'
def
lookups
(
self
,
request
,
model_admin
):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
return
(
(
'true'
,
_
(
'True'
)),
(
'false'
,
_
(
'False'
)),
)
def
queryset
(
self
,
request
,
queryset
):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
# Compare the requested value (either '80s' or '90s')
# to decide how to filter the queryset.
if
self
.
value
()
==
'true'
:
return
queryset
.
filter
(
valid_from__lte
=
date
.
today
(),
valid_until__gte
=
date
.
today
())
if
self
.
value
()
==
'false'
:
return
queryset
.
exclude
(
valid_from__lte
=
date
.
today
(),
valid_until__gte
=
date
.
today
())
class
ConditionSetListDynamicFilter
(
SimpleListFilter
):
title
=
_
(
'condition sets (publisher-dependant)'
)
parameter_name
=
'condition_set'
def
lookups
(
self
,
request
,
model_admin
):
if
'journal__publisher__name'
in
request
.
GET
:
# A publisher name filter is in effect
journal_publisher_name
=
request
.
GET
[
'journal__publisher__name'
]
condition_sets
=
set
([
c
.
condition_set
for
c
in
model_admin
.
model
.
objects
.
all
()
.
filter
(
journal__publisher__name
=
journal_publisher_name
)])
else
:
condition_sets
=
set
([
c
.
condition_set
for
c
in
model_admin
.
model
.
objects
.
all
()])
return
[(
s
.
id
,
str
(
s
))
for
s
in
condition_sets
]
def
queryset
(
self
,
request
,
queryset
):
if
self
.
value
():
return
queryset
.
filter
(
condition_set__id__exact
=
self
.
value
())
@admin.register
(
OrganizationCondition
)
class
OrganizationConditionAdmin
(
ImportExportModelAdmin
):
def
link_to_conditionset
(
self
,
obj
):
link
=
reverse
(
"admin:django_api_conditionset_change"
,
args
=
[
obj
.
condition_set
.
id
])
return
format_html
(
f
'<a href="{link}">{obj.condition_set}</a>'
)
link_to_conditionset
.
short_description
=
'Condition Set'
list_display
=
(
"id"
,
"organization_name"
,
"link_to_conditionset"
,
"valid_from"
,
"valid_until"
)
list_filter
=
(
'condition_set__condition_type'
,
XConditionValidListFilter
,
(
'condition_set'
,
RelatedOnlyFieldListFilter
))
def
organization_name
(
self
,
obj
):
return
obj
.
organization
.
name
@admin.register
(
Licence
)
class
LicenceAdmin
(
ImportExportModelAdmin
):
pass
@admin.register
(
JournalCondition
)
class
JournalConditionAdmin
(
ImportExportModelAdmin
):
def
link_to_conditionset
(
self
,
obj
):
link
=
reverse
(
"admin:django_api_conditionset_change"
,
args
=
[
obj
.
condition_set
.
id
])
return
format_html
(
f
'<a href="{link}">{obj.condition_set}</a>'
)
link_to_conditionset
.
short_description
=
'Condition Set'
list_display
=
(
"id"
,
"journal_name"
,
"link_to_conditionset"
,
"valid_from"
,
"valid_until"
)
list_filter
=
(
'condition_set__condition_type'
,
XConditionValidListFilter
,
'journal__publisher__name'
,
ConditionSetListDynamicFilter
)
actions
=
(
end_validity
,
)
def
journal_name
(
self
,
obj
):
return
obj
.
journal
.
name
# unsuccessful attempt
# def formfield_for_foreignkey(self, db_field, request, **kwargs):
# if db_field.name == "journal":
# kwargs["queryset"] = Journal.objects.filter(publisher__name__in=Publisher.objects.order_by().values('name').distinct())
# return super().formfield_for_foreignkey(db_field, request, **kwargs)
@admin.register
(
Cost_factor
)
class
Cost_factorAdmin
(
ImportExportModelAdmin
):
list_display
=
(
"id"
,
"comment"
,
"amount"
,
"symbol"
)
list_filter
=
(
'cost_factor_type'
,
'symbol'
)
# textarea input is better for comments
def
get_form
(
self
,
request
,
obj
=
None
,
**
kwargs
):
kwargs
[
'widgets'
]
=
{
'comment'
:
forms
.
Textarea
}
return
super
()
.
get_form
(
request
,
obj
,
**
kwargs
)
@admin.register
(
Cost_factor_type
)
class
Cost_factor_typeAdmin
(
ImportExportModelAdmin
):
list_display
=
(
"id"
,
"name"
)
Event Timeline
Log In to Comment