Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

Implementert kommandoen 'list'.

Flyttet samtidig på litt kode.  Plukket ut printedelene av
søkefunksjonene til egne funksjoner som printer en liste med
bøker/personer, slik at de samme funksjonene kan brukes av
'list'-kommandoen også.  Flyttet dessuten søkefunksjonen for personer
inn i search.py (skal vi først ha en fil som heter search.py (noe jeg
er litt usikker på poenget med) bør denne også ligge der).
This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-03-08 13:30:17 +00:00
parent 25215718ee
commit 1ee76ee0b6
2 changed files with 55 additions and 30 deletions

View File

@ -4,16 +4,17 @@ from web.library.models import *
from django.db.models import Q from django.db.models import Q
from util import * from util import *
def search_book_cmd(search_strings, search_description=False): def search_person(search_strings, search_description=False):
books = search_book(search_strings, search_description) basic_query=Person.objects.select_related('books__book__alt_titles')
format = '%-13s %-10s %-40s %-30s' for word in search_strings:
for book in books: basic_query=basic_query.filter(Q(first_name__icontains=word) |
b_id = book.getid() or '' Q(last_name__icontains=word) |
title = cut_str(book.title, 40, '*') Q(id__icontains=word) |
authors = map(lambda p: p.first_name+' '+p.last_name, Q(books__book__isbn__icontains=word) |
book.get_authors()) Q(books__book__title__icontains=word) |
authors_str = cut_str(', '.join(authors), 30, '*') Q(books__book__alt_titles__alt_title=word) |
print format % (book.isbn, b_id, title, authors_str) Q(books__book__id__id__icontains=word))
return remove_duplicates(basic_query.all())
def search_book(search_strings, search_description=False): def search_book(search_strings, search_description=False):
basic_query = Book.objects.select_related('persons__person','id','alt_titles') basic_query = Book.objects.select_related('persons__person','id','alt_titles')

View File

@ -55,26 +55,46 @@ def remove_duplicates(list):
d[i]=None d[i]=None
return d.keys() return d.keys()
def search_person_cmd(search_strings, search_description=False): def list_cmd(what):
people = search_person(search_strings, search_description) if what == 'person':
format = '%-10s %-20s %-70s' print_person_list(Person.objects.all())
for person in people: elif what == 'book':
print_book_list(Book.objects.all())
elif what == 'category':
print_category_list(Category.objects.all())
else:
print 'I don\' know what \'%s\' is.' % what
def print_person_list(lst):
format = '%-13s %-20s %-70s'
for person in lst:
name = cut_str(person.first_name+' '+person.last_name, 20) name = cut_str(person.first_name+' '+person.last_name, 20)
p_id = cut_str(person.id, 10) p_id = cut_str(person.id, 10)
books = cut_str(', '.join(map(lambda x: x.book.title, person.books.all())), 70) books = cut_str(', '.join(map(lambda x: x.book.title, person.books.all())), 70)
print format % (p_id, name, books) print format % (p_id, name, books)
def search_person(search_strings, search_description=False): def print_book_list(lst):
basic_query=Person.objects.select_related('books__book__alt_titles') format = '%-13s %-10s %-40s %-30s'
for word in search_strings: for book in lst:
basic_query=basic_query.filter(Q(first_name__icontains=word) | b_id = book.getid() or ''
Q(last_name__icontains=word) | title = cut_str(book.title, 40, '*')
Q(id__icontains=word) | authors = map(lambda p: p.first_name+' '+p.last_name,
Q(books__book__isbn__icontains=word) | book.get_authors())
Q(books__book__title__icontains=word) | authors_str = cut_str(', '.join(authors), 30, '*')
Q(books__book__alt_titles__alt_title=word) | print format % (book.isbn, b_id, title, authors_str)
Q(books__book__id__id__icontains=word))
return remove_duplicates(basic_query.all()) def print_category_list(lst):
format = '%-20s %s'
for category in lst:
print format % (category.id, category.name)
def search_person_cmd(search_strings, search_description=False):
people = search.search_person(search_strings, search_description)
print_person_list(people)
def search_book_cmd(search_strings, search_description=False):
books = search.search_book(search_strings, search_description)
print_book_list(books)
def commit(filename=None): def commit(filename=None):
if filename: if filename:
@ -187,10 +207,14 @@ commands = { 'show':
{ 'args': [('ids', (1,None))], { 'args': [('ids', (1,None))],
'options': ['commit_format', 'tmp_file'], 'options': ['commit_format', 'tmp_file'],
'fun': show_book_or_person }, 'fun': show_book_or_person },
'list':
{ 'args': [('what', (1,1))],
'options': [],
'fun': list_cmd },
'search': 'search':
{ 'args': [('search_strings', (1,None))], { 'args': [('search_strings', (1,None))],
'options': ['search_description'], 'options': ['search_description'],
'fun': search.search_book_cmd }, 'fun': search_book_cmd },
'search-person': 'search-person':
{ 'args': [('search_strings', (1,None))], { 'args': [('search_strings', (1,None))],
'options': [], 'options': [],