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

forhåpentligvis fungerende personsøk

This commit is contained in:
Øyvind Almelid 2010-09-28 13:22:54 +00:00
parent 8f1a033456
commit 46524f32cd
1 changed files with 52 additions and 4 deletions

View File

@ -1,6 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
os.environ['DJANGO_SETTINGS_MODULE']='web.settings'
from web.library.models import *
from django.db.models import Q
from util import *
import getopt
import sys
import search
@ -8,11 +13,54 @@ import search
def show_book_or_person(ids, commit_format=False, tmp_file=False):
pass
# def search_book():
# pass
def search_book_cmd(search_strings, search_description=False):
books = search_book(search_strings, search_description)
format = '%-13s %-10s %-40s %-30s'
for book in books:
b_id = book.getid() or ''
title = cut_str(book.title, 40)
authors = map(lambda p: p.first_name+' '+p.last_name,
book.get_authors())
authors_str = cut_str(', '.join(authors), 30)
print format % (book.isbn, b_id, title, authors_str)
def search_person():
pass
def search_book(search_strings, search_description=False):
basic_query = Book.objects.select_related('persons__person','id','alt_titles')
for word in search_strings:
basic_query=basic_query.filter(Q(title__icontains=word) |
Q(subtitle__icontains=word) |
Q(alt_titles__alt_title__icontains=word) |
Q(id__id__icontains=word) |
Q(persons__person__first_name__icontains=word) |
Q(persons__person__last_name__icontains=word))
return remove_duplicates(basic_query.all())
def remove_duplicates(list):
d = {}
for i in list:
d[i]=None
return d.keys()
def search_person_cmd(search_strings, search_description=False):
people = search_people(search_strings, search_description)
format = '%-30s %-10s %-60s'
for person in people:
name = cut_str(person.first_name+' '+person.last_name, 30)
p_id = cut_str(person.id, 10)
books = cut_str(', '.join(map(lambda x: x.book.title, person.books.all())))
print format % (name,p_id,books)
def search_person(search_strings, search_description=False):
basic_query=Person.objects.select_related('books__book__alt_titles')
for word in search_strings:
basic_query=basic_query.filter(Q(first_name__icontains=word) |
Q(last_name__icontains=word) |
Q(id__icontains=word) |
Q(books__book__isbn__icontains=word) |
Q(books__book__title__icontains=word) |
Q(books__book__alt_titles__alt_title=word) |
Q(books__book__id__id__icontains=word))
return remove_duplicates(basic_query.all())
def commit(filename=None):
pass