regluit/core/admin.py

322 lines
9.6 KiB
Python
Raw Normal View History

2017-12-22 18:19:26 +00:00
#
#django imports
#
2016-07-26 14:34:45 +00:00
from django import forms
2017-12-22 18:19:26 +00:00
from django.contrib.admin import ModelAdmin, register
2018-07-24 15:33:39 +00:00
from django.urls import reverse
2017-12-22 18:19:26 +00:00
from selectable.forms import (
2017-07-19 18:26:04 +00:00
AutoCompleteSelectWidget,
AutoCompleteSelectField,
AutoCompleteSelectMultipleWidget,
AutoCompleteSelectMultipleField,
)
2016-07-26 14:34:45 +00:00
2017-12-22 18:19:26 +00:00
#
# regluit imports
#"""
from .lookups import (
2016-07-26 14:34:45 +00:00
PublisherNameLookup,
WorkLookup,
OwnerLookup,
EditionLookup,
EbookLookup,
2016-07-26 14:34:45 +00:00
)
2017-12-22 18:19:26 +00:00
from . import models
2016-07-26 14:34:45 +00:00
class ClaimAdminForm(forms.ModelForm):
work = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=WorkLookup,
widget=AutoCompleteSelectWidget(lookup_class=WorkLookup),
required=True,
)
2016-07-26 14:34:45 +00:00
user = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=OwnerLookup,
widget=AutoCompleteSelectWidget(lookup_class=OwnerLookup),
required=True,
)
2016-07-26 14:34:45 +00:00
class Meta(object):
model = models.Claim
exclude = ()
2017-12-22 18:19:26 +00:00
@register(models.Claim)
2016-07-26 14:34:45 +00:00
class ClaimAdmin(ModelAdmin):
list_display = ('work', 'rights_holder', 'status')
date_hierarchy = 'created'
form = ClaimAdminForm
2017-12-22 18:19:26 +00:00
2016-07-26 14:34:45 +00:00
class RightsHolderAdminForm(forms.ModelForm):
owner = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=OwnerLookup,
widget=AutoCompleteSelectWidget(lookup_class=OwnerLookup),
required=True,
)
2016-07-26 14:34:45 +00:00
class Meta(object):
model = models.RightsHolder
exclude = ()
2017-12-22 18:19:26 +00:00
@register(models.RightsHolder)
2016-07-26 14:34:45 +00:00
class RightsHolderAdmin(ModelAdmin):
date_hierarchy = 'created'
form = RightsHolderAdminForm
2017-12-22 18:19:26 +00:00
@register(models.Acq)
2016-07-26 14:34:45 +00:00
class AcqAdmin(ModelAdmin):
readonly_fields = ('work', 'user', 'lib_acq', 'watermarked')
search_fields = ['user__username']
date_hierarchy = 'created'
2017-12-22 18:19:26 +00:00
@register(models.Premium)
2016-07-26 14:34:45 +00:00
class PremiumAdmin(ModelAdmin):
list_display = ('campaign', 'amount', 'description')
date_hierarchy = 'created'
2017-12-14 21:24:26 +00:00
fields = ('type', 'amount', 'description', 'limit')
2016-07-26 14:34:45 +00:00
2017-07-19 18:27:07 +00:00
class CampaignAdminForm(forms.ModelForm):
managers = AutoCompleteSelectMultipleField(
2017-12-22 18:19:26 +00:00
lookup_class=OwnerLookup,
widget=AutoCompleteSelectMultipleWidget(lookup_class=OwnerLookup),
required=True,
)
2017-07-19 18:27:07 +00:00
class Meta(object):
model = models.Campaign
2017-12-14 21:24:26 +00:00
fields = (
2017-12-22 18:19:26 +00:00
'managers', 'name', 'description', 'details', 'license', 'paypal_receiver',
2017-12-14 21:24:26 +00:00
'status', 'type', 'email', 'do_watermark', 'use_add_ask', 'charitable',
)
2017-07-19 18:27:07 +00:00
2017-12-22 18:19:26 +00:00
@register(models.Campaign)
2016-07-26 14:34:45 +00:00
class CampaignAdmin(ModelAdmin):
list_display = ('work', 'created', 'status')
date_hierarchy = 'created'
search_fields = ['work']
2017-07-19 18:27:07 +00:00
form = CampaignAdminForm
2017-12-22 18:19:26 +00:00
@register(models.Work)
2016-07-26 14:34:45 +00:00
class WorkAdmin(ModelAdmin):
search_fields = ['title']
ordering = ('title',)
list_display = ('title', 'created')
date_hierarchy = 'created'
2016-08-25 21:56:16 +00:00
fields = ('title', 'description', 'language', 'featured', 'publication_range',
2017-12-22 18:19:26 +00:00
'age_level', 'openlibrary_lookup')
2016-07-26 14:34:45 +00:00
2017-12-22 18:19:26 +00:00
@register(models.Author)
2016-07-26 14:34:45 +00:00
class AuthorAdmin(ModelAdmin):
search_fields = ('name',)
date_hierarchy = 'created'
ordering = ('name',)
exclude = ('editions',)
2017-12-22 18:19:26 +00:00
subject_authorities = (
('', 'keywords'),
('lcsh', 'LC subjects'),
('lcc', 'LC classifications'),
('bisacsh', 'BISAC heading'),
)
2016-07-26 14:34:45 +00:00
class SubjectAdminForm(forms.ModelForm):
2017-12-22 18:19:26 +00:00
authority = forms.ChoiceField(choices=subject_authorities, required=False)
2016-07-26 14:34:45 +00:00
class Meta(object):
model = models.Subject
fields = 'name', 'authority', 'is_visible'
2017-12-22 18:19:26 +00:00
@register(models.Subject)
2016-07-26 14:34:45 +00:00
class SubjectAdmin(ModelAdmin):
search_fields = ('name',)
date_hierarchy = 'created'
ordering = ('name',)
form = SubjectAdminForm
2017-12-22 18:19:26 +00:00
2016-07-26 14:34:45 +00:00
class EditionAdminForm(forms.ModelForm):
work = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=WorkLookup,
label='Work',
widget=AutoCompleteSelectWidget(lookup_class=WorkLookup),
required=True,
)
2016-07-26 14:34:45 +00:00
publisher_name = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=PublisherNameLookup,
label='Publisher Name',
widget=AutoCompleteSelectWidget(lookup_class=PublisherNameLookup),
required=False,
)
2016-07-26 14:34:45 +00:00
class Meta(object):
model = models.Edition
exclude = ()
2017-12-22 18:19:26 +00:00
@register(models.Edition)
2016-07-26 14:34:45 +00:00
class EditionAdmin(ModelAdmin):
list_display = ('title', 'publisher_name', 'created')
date_hierarchy = 'created'
ordering = ('title',)
form = EditionAdminForm
class PublisherAdminForm(forms.ModelForm):
name = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=PublisherNameLookup,
label='Name',
widget=AutoCompleteSelectWidget(lookup_class=PublisherNameLookup),
required=True,
)
2016-07-26 14:34:45 +00:00
class Meta(object):
model = models.Publisher
exclude = ()
2017-12-22 18:19:26 +00:00
@register(models.Publisher)
2016-07-26 14:34:45 +00:00
class PublisherAdmin(ModelAdmin):
list_display = ('name', 'url', 'logo_url', 'description')
ordering = ('name',)
form = PublisherAdminForm
2017-12-22 18:19:26 +00:00
@register(models.PublisherName)
2016-07-26 14:34:45 +00:00
class PublisherNameAdmin(ModelAdmin):
list_display = ('name', 'publisher')
ordering = ('name',)
search_fields = ['name']
2017-12-22 18:19:26 +00:00
@register(models.Relation)
2016-07-26 14:34:45 +00:00
class RelationAdmin(ModelAdmin):
list_display = ('code', 'name')
search_fields = ['name']
class EbookAdminForm(forms.ModelForm):
edition = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=EditionLookup,
label='Edition',
widget=AutoCompleteSelectWidget(lookup_class=EditionLookup, attrs={'size':60}),
required=True,
)
class Meta(object):
model = models.Ebook
exclude = ('user', 'filesize', 'download_count')
2017-12-22 18:19:26 +00:00
@register(models.Ebook)
2016-07-26 14:34:45 +00:00
class EbookAdmin(ModelAdmin):
form = EbookAdminForm
2017-12-22 18:19:26 +00:00
search_fields = ('edition__title', '^url') # search by provider using leading url
list_display = ('__unicode__', 'created', 'user', 'edition')
2016-07-26 14:34:45 +00:00
date_hierarchy = 'created'
ordering = ('edition__title',)
readonly_fields = ('user', 'filesize', 'download_count')
2016-07-26 14:34:45 +00:00
class EbookFileAdminForm(forms.ModelForm):
edition = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=EditionLookup,
label='Edition',
widget=AutoCompleteSelectWidget(lookup_class=EditionLookup, attrs={'size':60}),
required=True,
)
ebook = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=EbookLookup,
label='Ebook',
widget=AutoCompleteSelectWidget(lookup_class=EbookLookup, attrs={'size':60}),
required=False,
)
class Meta(object):
model = models.EbookFile
fields = ('file', 'format', 'edition', 'ebook', 'source')
2017-12-22 18:19:26 +00:00
@register(models.EbookFile)
2017-07-19 19:21:51 +00:00
class EbookFileAdmin(ModelAdmin):
form = EbookFileAdminForm
2017-08-08 17:08:18 +00:00
search_fields = ('ebook__edition__title', 'source') # search by provider using leading url
list_display = ('created', 'format', 'ebook_link', 'asking')
2017-07-19 19:21:51 +00:00
date_hierarchy = 'created'
ordering = ('edition__work',)
2019-03-25 16:45:53 +00:00
fields = ('file', 'format', 'edition', 'edition_link', 'ebook', 'ebook_link', 'source', 'mobied')
readonly_fields = ('file', 'edition_link', 'ebook_link', 'source')
2017-08-08 17:08:18 +00:00
def edition_link(self, obj):
if obj.edition:
2017-12-22 18:19:26 +00:00
link = reverse("admin:core_edition_change", args=[obj.edition_id])
return u'<a href="%s">%s</a>' % (link, obj.edition)
return u''
2017-08-08 17:08:18 +00:00
def ebook_link(self, obj):
if obj.ebook:
2017-12-22 18:19:26 +00:00
link = reverse("admin:core_ebook_change", args=[obj.ebook_id])
return u'<a href="%s">%s</a>' % (link, obj.ebook)
return u''
edition_link.allow_tags = True
ebook_link.allow_tags = True
2017-08-08 17:08:18 +00:00
2017-07-19 19:21:51 +00:00
2017-12-22 18:19:26 +00:00
@register(models.Wishlist)
2016-07-26 14:34:45 +00:00
class WishlistAdmin(ModelAdmin):
date_hierarchy = 'created'
2017-12-22 18:19:26 +00:00
@register(models.UserProfile)
2016-07-26 14:34:45 +00:00
class UserProfileAdmin(ModelAdmin):
search_fields = ('user__username',)
date_hierarchy = 'created'
exclude = ('user',)
2017-12-22 18:19:26 +00:00
@register(models.Gift)
2016-07-26 14:34:45 +00:00
class GiftAdmin(ModelAdmin):
2017-12-22 18:19:26 +00:00
list_display = ('to', 'acq_admin_link', 'giver',)
2016-07-26 14:34:45 +00:00
search_fields = ('giver__username', 'to')
2017-12-22 18:19:26 +00:00
readonly_fields = ('giver', 'acq',)
2016-07-26 14:34:45 +00:00
def acq_admin_link(self, gift):
return "<a href='/admin/core/acq/%s/'>%s</a>" % (gift.acq_id, gift.acq)
2016-07-26 14:34:45 +00:00
acq_admin_link.allow_tags = True
2017-12-22 18:19:26 +00:00
@register(models.CeleryTask)
2016-07-26 14:34:45 +00:00
class CeleryTaskAdmin(ModelAdmin):
2017-12-22 18:19:26 +00:00
pass
@register(models.Press)
2016-07-26 14:34:45 +00:00
class PressAdmin(ModelAdmin):
list_display = ('title', 'source', 'date')
ordering = ('-date',)
2016-08-25 21:56:16 +00:00
class WorkRelationAdminForm(forms.ModelForm):
to_work = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=WorkLookup,
label='To Work',
widget=AutoCompleteSelectWidget(lookup_class=WorkLookup),
required=True,
)
2016-08-25 21:56:16 +00:00
from_work = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=WorkLookup,
label='From Work',
widget=AutoCompleteSelectWidget(lookup_class=WorkLookup),
required=True,
)
2016-08-25 21:56:16 +00:00
class Meta(object):
model = models.WorkRelation
exclude = ()
2017-12-22 18:19:26 +00:00
@register(models.WorkRelation)
2016-08-25 21:56:16 +00:00
class WorkRelationAdmin(ModelAdmin):
form = WorkRelationAdminForm
list_display = ('to_work', 'relation', 'from_work')
2017-12-22 18:19:26 +00:00
2016-08-25 21:56:16 +00:00
class IdentifierAdminForm(forms.ModelForm):
work = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=WorkLookup,
label='Work',
widget=AutoCompleteSelectWidget(lookup_class=WorkLookup, attrs={'size':60}),
required=False,
)
2016-08-25 21:56:16 +00:00
edition = AutoCompleteSelectField(
2017-12-22 18:19:26 +00:00
lookup_class=EditionLookup,
label='Edition',
widget=AutoCompleteSelectWidget(lookup_class=EditionLookup, attrs={'size':60}),
required=False,
)
2016-08-25 21:56:16 +00:00
class Meta(object):
model = models.Identifier
exclude = ()
2017-12-22 18:19:26 +00:00
@register(models.Identifier)
2016-08-25 21:56:16 +00:00
class IdentifierAdmin(ModelAdmin):
form = IdentifierAdminForm
list_display = ('type', 'value')
search_fields = ('type', 'value')
2017-12-22 18:19:26 +00:00
@register(models.Offer)
2016-08-25 21:56:16 +00:00
class OfferAdmin(ModelAdmin):
list_display = ('work', 'license', 'price', 'active')
search_fields = ('work__title',)
readonly_fields = ('work',)