2011-11-18 14:22:21 +00:00
|
|
|
from selectable.base import ModelLookup
|
|
|
|
from selectable.registry import registry
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2015-01-16 13:56:36 +00:00
|
|
|
from django.db.models import Count
|
2017-12-22 17:42:29 +00:00
|
|
|
from regluit.core.models import Work, PublisherName, Edition, Subject, EditionNote, Ebook
|
2016-10-20 19:24:47 +00:00
|
|
|
from regluit.utils.text import sanitize_line
|
2011-11-18 14:22:21 +00:00
|
|
|
|
|
|
|
class OwnerLookup(ModelLookup):
|
|
|
|
model = User
|
2012-10-02 22:47:52 +00:00
|
|
|
search_fields = ('username__icontains',)
|
2012-12-13 03:35:35 +00:00
|
|
|
|
|
|
|
class WorkLookup(ModelLookup):
|
|
|
|
model = Work
|
|
|
|
search_fields = ('title__istartswith',)
|
2017-12-22 18:19:26 +00:00
|
|
|
def get_item_label(self, item):
|
|
|
|
return "%s (%s, %s)"%(item.title, item.id, item.language)
|
|
|
|
|
|
|
|
def get_item_value(self, item):
|
|
|
|
return "%s (%s, %s)"%(item.title, item.id, item.language)
|
|
|
|
|
2012-12-13 03:35:35 +00:00
|
|
|
def get_query(self, request, term):
|
|
|
|
results = super(WorkLookup, self).get_query(request, term)
|
|
|
|
return results
|
|
|
|
|
2013-03-26 14:56:49 +00:00
|
|
|
class PublisherNameLookup(ModelLookup):
|
|
|
|
model = PublisherName
|
|
|
|
search_fields = ('name__icontains',)
|
2013-05-13 21:32:30 +00:00
|
|
|
def create_item(self, value):
|
2016-10-20 19:24:47 +00:00
|
|
|
value = sanitize_line(value)
|
2013-05-13 21:32:30 +00:00
|
|
|
publisher_name, created = PublisherName.objects.get_or_create(name=value)
|
|
|
|
publisher_name.save()
|
|
|
|
return publisher_name
|
2017-12-22 18:19:26 +00:00
|
|
|
|
2017-12-22 17:42:29 +00:00
|
|
|
class EbookLookup(ModelLookup):
|
|
|
|
model = Ebook
|
|
|
|
search_fields = ('edition__title__icontains',)
|
|
|
|
filters = {'edition__isnull': False, }
|
|
|
|
|
|
|
|
def get_item(self, value):
|
|
|
|
item = None
|
|
|
|
if value:
|
|
|
|
try:
|
|
|
|
item = Ebook.objects.get(pk=value)
|
|
|
|
except (ValueError, Ebook.DoesNotExist):
|
|
|
|
item = None
|
|
|
|
return item
|
|
|
|
|
2013-07-08 15:14:25 +00:00
|
|
|
class EditionLookup(ModelLookup):
|
|
|
|
model = Edition
|
|
|
|
search_fields = ('title__icontains',)
|
2013-07-26 23:07:52 +00:00
|
|
|
filters = {'ebooks__isnull': False, }
|
2013-07-08 15:14:25 +00:00
|
|
|
|
2013-07-26 23:51:45 +00:00
|
|
|
def get_query(self, request, term):
|
|
|
|
return super(EditionLookup, self).get_query(request, term).distinct()
|
|
|
|
|
|
|
|
def get_item(self, value):
|
|
|
|
item = None
|
|
|
|
if value:
|
|
|
|
try:
|
|
|
|
item = Edition.objects.get(pk=value)
|
|
|
|
except (ValueError, Edition.DoesNotExist):
|
|
|
|
item = None
|
|
|
|
return item
|
|
|
|
|
2015-01-14 20:07:54 +00:00
|
|
|
class SubjectLookup(ModelLookup):
|
|
|
|
model = Subject
|
|
|
|
search_fields = ('name__icontains',)
|
2017-12-22 18:19:26 +00:00
|
|
|
|
2015-01-16 13:56:36 +00:00
|
|
|
def get_query(self, request, term):
|
2017-12-22 18:19:26 +00:00
|
|
|
return super(SubjectLookup, self).get_query(
|
|
|
|
request, term
|
|
|
|
).annotate(Count('works')).order_by('-works__count')
|
2015-01-14 20:07:54 +00:00
|
|
|
|
2016-08-16 21:16:44 +00:00
|
|
|
class EditionNoteLookup(ModelLookup):
|
|
|
|
model = EditionNote
|
|
|
|
search_fields = ('note__icontains',)
|
|
|
|
def create_item(self, value):
|
|
|
|
new_note, created = EditionNote.objects.get_or_create(note=value)
|
|
|
|
new_note.save()
|
|
|
|
return new_note
|
|
|
|
|
2012-12-13 03:35:35 +00:00
|
|
|
registry.register(OwnerLookup)
|
2013-03-26 14:56:49 +00:00
|
|
|
registry.register(WorkLookup)
|
2013-07-08 15:14:25 +00:00
|
|
|
registry.register(PublisherNameLookup)
|
2015-01-14 20:07:54 +00:00
|
|
|
registry.register(EditionLookup)
|
2016-08-16 21:16:44 +00:00
|
|
|
registry.register(SubjectLookup)
|
2017-12-22 17:42:29 +00:00
|
|
|
registry.register(EditionNoteLookup)
|
2017-12-22 18:19:26 +00:00
|
|
|
registry.register(EbookLookup)
|