2011-03-05 20:33:11 +01:00
from web . library . models import *
2011-03-06 12:31:51 +01:00
from django . http import HttpResponse , HttpResponseRedirect , Http404
2011-03-05 20:33:11 +01:00
from django . shortcuts import render_to_response , get_object_or_404
2011-03-06 17:57:12 +01:00
from django . db . models import Q
2011-03-05 20:33:11 +01:00
2011-03-06 18:14:25 +01:00
relations = { ' Author ' : u ' Forfatter ' , ' Illustrator ' : u ' Tegner ' , ' Translator ' : u ' Oversetter ' }
# Create your views here.
2011-03-05 20:33:11 +01:00
def BookView ( request , book_identifier ) :
2011-03-06 18:14:25 +01:00
book = get_object_or_404 ( Book , isbn = book_identifier )
2011-03-06 19:03:05 +01:00
people = book . persons . all ( ) . order_by ( ' person__last_name ' , ' person__first_name ' )
2011-03-06 18:14:25 +01:00
shelves = None
global relations
if book . category :
if book . category . placement :
shelves = book . category . placement . all ( )
contributors = { }
for person in people :
if str ( person . relation ) in relations :
relation = relations [ str ( person . relation ) ]
else :
relation = ' Medvirkende '
if not relation in contributors :
contributors [ relation ] = [ 0 , [ ] ]
# contributors[person.relation] = "%s %s" % (person.person.first_name, person.person.last_name)
# else:
contributors [ relation ] [ 0 ] + = 1
contributors [ relation ] [ 1 ] . append ( person )
print contributors
return render_to_response ( ' book/view.html ' , { ' book ' : book , ' people ' : people , ' shelves ' : shelves , ' contributors ' : contributors } )
2011-03-05 20:33:11 +01:00
def BookRedirect ( request , book_identifier ) :
2011-03-06 19:01:15 +01:00
ident = get_object_or_404 ( Id , id = book_identifier )
return HttpResponseRedirect ( ' /book/ ' + ident . book . isbn )
2011-03-05 20:33:11 +01:00
def PersonView ( request , person_identifier ) :
2011-03-06 18:14:25 +01:00
global relations
person = get_object_or_404 ( Person , id = person_identifier )
2011-03-06 19:03:05 +01:00
books = person . books . all ( ) . order_by ( ' book__published_year ' , ' book__title ' )
2011-03-06 18:14:25 +01:00
contributed = { }
for book in books :
if str ( book . relation ) in relations :
relation = relations [ str ( book . relation ) ]
else :
relation = ' Medvirkende '
if not relation in contributed :
contributed [ relation ] = [ 0 , [ ] ]
contributed [ relation ] [ 0 ] + = 1
contributed [ relation ] [ 1 ] . append ( book )
return render_to_response ( ' person/view.html ' , { ' title ' : ' Worblehat: person ' + person . first_name + ' ' + person . last_name , ' person ' : person , ' books ' : books , ' contributed ' : contributed } )
2011-03-06 12:31:51 +01:00
2011-03-06 19:01:15 +01:00
def MapIndex ( request ) :
return render_to_response ( ' map/index.html ' , { ' shelves ' : Placement . get_all_shelves ( ) } )
2011-03-06 12:31:51 +01:00
def MapView ( request , shelf_identifier ) :
2011-03-06 17:57:12 +01:00
if shelf_identifier not in Placement . get_all_shelves ( ) :
raise Http404 ( )
shelf_list = Placement . shelf_as_list_row_then_col ( shelf_identifier )
2011-03-06 19:01:15 +01:00
return render_to_response ( ' map/view.html ' , { ' shelf_name ' : shelf_identifier , ' shelf ' : shelf_list } )
def CategoryIndex ( request ) :
return render_to_response ( ' category/index.html ' , { ' categories ' : Category . objects . all ( ) } )
def CategoryView ( request , category_identifier ) :
category = get_object_or_404 ( Category , id = category_identifier )
book_list = category . book_set . all ( )
return render_to_response ( ' category/view.html ' , { ' category ' : category , ' book_list ' : book_list } )
2011-03-06 17:57:12 +01:00
def IndexView ( request ) :
return render_to_response ( ' index.html ' )
def SearchView ( request ) :
search_string = request . GET [ ' searchterm ' ]
results = search ( search_string )
return render_to_response ( ' search/search.html ' , { ' search_string ' : search_string , ' books ' : results [ 0 ] , ' people ' : results [ 1 ] , ' categories ' : results [ 2 ] } )
def search ( string ) :
book_q = Book . objects . select_related ( ' id ' , ' alt_titles ' )
person_q = Person . objects
category_q = Category . objects
for word in string . split ( " " ) :
book_q = book_q . filter ( Q ( title__icontains = word ) | Q ( subtitle__icontains = word ) |
Q ( alt_titles__alt_title__icontains = word ) | Q ( id__id__icontains = word ) )
person_q = person_q . filter ( Q ( first_name__icontains = word ) | Q ( last_name__icontains = word ) | Q ( id__icontains = word ) )
category_q = category_q . filter ( Q ( id__icontains = word ) | Q ( name__icontains = word ) )
return [ book_q . all ( ) , person_q . all ( ) , category_q . all ( ) ]
def remove_duplicates ( list ) :
d = { }
for i in list :
d [ i ] = None
return d . keys ( )