2010-09-27 17:08:23 +02:00
|
|
|
import os
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE']='web.settings'
|
|
|
|
from web.library.models import *
|
|
|
|
from django.db.models import Q
|
2010-09-27 19:32:24 +02:00
|
|
|
from util import *
|
2010-09-27 17:08:23 +02:00
|
|
|
|
2010-09-27 19:32:24 +02:00
|
|
|
def search_book_cmd(search_strings, search_description=False):
|
|
|
|
books = search_book(search_strings, search_description)
|
2010-09-27 19:39:35 +02:00
|
|
|
format = '%-13s %-10s %-40s %-30s'
|
2010-09-27 19:32:24 +02:00
|
|
|
for book in books:
|
|
|
|
b_id = book.getid() or ''
|
2011-03-04 14:11:21 +01:00
|
|
|
title = cut_str(book.title, 40, '*')
|
2010-09-27 19:39:35 +02:00
|
|
|
authors = map(lambda p: p.first_name+' '+p.last_name,
|
|
|
|
book.get_authors())
|
2011-03-04 14:11:21 +01:00
|
|
|
authors_str = cut_str(', '.join(authors), 30, '*')
|
2010-09-27 19:39:35 +02:00
|
|
|
print format % (book.isbn, b_id, title, authors_str)
|
2010-09-27 19:32:24 +02:00
|
|
|
|
|
|
|
def search_book(search_strings, search_description=False):
|
2010-09-27 22:44:57 +02:00
|
|
|
basic_query = Book.objects.select_related('persons__person','id','alt_titles')
|
2010-09-27 19:32:24 +02:00
|
|
|
for word in search_strings:
|
2010-09-27 22:44:57 +02:00
|
|
|
basic_query=basic_query.filter(Q(title__icontains=word) |
|
2010-09-27 17:08:23 +02:00
|
|
|
Q(subtitle__icontains=word) |
|
2010-09-27 22:44:57 +02:00
|
|
|
Q(alt_titles__alt_title__icontains=word) |
|
2010-09-27 17:08:23 +02:00
|
|
|
Q(id__id__icontains=word) |
|
2010-09-27 22:44:57 +02:00
|
|
|
Q(persons__person__first_name__icontains=word) |
|
|
|
|
Q(persons__person__last_name__icontains=word))
|
2010-09-27 17:08:23 +02:00
|
|
|
return remove_duplicates(basic_query.all())
|
|
|
|
|
|
|
|
def remove_duplicates(list):
|
|
|
|
d = {}
|
|
|
|
for i in list:
|
|
|
|
d[i]=None
|
|
|
|
return d.keys()
|