From d173b9396e1e0c87a1be732ef65602fee91c8dd0 Mon Sep 17 00:00:00 2001 From: almelid Date: Sat, 18 Sep 2010 15:20:50 +0000 Subject: [PATCH] =?UTF-8?q?begynt=20p=C3=A5=20funksjonen=20book=5Ffrom=5Fx?= =?UTF-8?q?ml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/interface.py | 50 +++++++++++++++++++++++++++++++----- python/web/library/admin.py | 7 +++++ python/web/library/models.py | 25 +++++++++++++----- python/web/urls.py | 6 ++--- 4 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 python/web/library/admin.py diff --git a/python/interface.py b/python/interface.py index 3ac672c..6fe4b06 100644 --- a/python/interface.py +++ b/python/interface.py @@ -1,13 +1,15 @@ +from web.library.models import * from xml.dom import minidom from PyZ3950 import zoom +import readline exit_commands = ['exit', 'abort', 'quit', 'bye', 'eat flaming death', 'q'] -class Bibsys(): - def __init__(self): - self.conn = zoom.Connection ('z3950.bibsys.no', 2100) - self.conn.databaseName = 'BIBSYS' - self.conn.preferredRecordSyntax = 'XML' +class ZLibrary(): + def __init__(self, host, port, dbname, syntax): + self.conn = zoom.Connection(host, port) + self.conn.databaseName = dbname + self.conn.preferredRecordSyntax = syntax def isbn_search(self, isbn): query = zoom.Query('CCL', 'ISBN='+isbn) @@ -17,6 +19,17 @@ class Bibsys(): def close(self): self.conn.close() +class Congress(ZLibrary): + def __init__(self): + ZLibrary.__init__(self, 'z3950.loc.gov', 7090, 'VOYAGER', 'USMARC') + +class Bibsys(ZLibrary): + def __init__(self): + ZLibrary.__init__(self, 'z3950.bibsys.no', 2100, 'BIBSYS', 'XML') +# self.conn = zoom.Connection ('z3950.bibsys.no', 2100) +# self.conn.databaseName = 'BIBSYS' +# self.conn.preferredRecordSyntax = 'XML' + #class Menu(): def get_book_loop(): @@ -27,8 +40,33 @@ def get_book_loop(): break else: r = bib.isbn_search(input) - if len(r) > 0: + if len(r) == 1: + print "Found one match" print r[0] + book_from_xml(r[0]) + # document = minidom.parseString(str(r[0])[9:]) + # print document.getElementsByTagName('dc:title')[0].childNodes[0].data + elif len(r) > 1: + print "Found several matches" + else: + print "No results found" bib.close() +def book_from_xml(dc): + b = Book() + document = minidom.parseString(str(dc)[9:]) + titles = document.getElementsByTagName('dc:title') + if titles.length == 1: + title = "" + for node in titles[0].childNodes: + if node.nodeType == node.TEXT_NODE: + title += " "+node.data + title = title.strip().split('/') + (title, authors) = (title[:-1],title[-1]) + authors = authors.split(',') + print title, authors + else: + print "Several titles found, owshi" + + get_book_loop() diff --git a/python/web/library/admin.py b/python/web/library/admin.py new file mode 100644 index 0000000..2674e96 --- /dev/null +++ b/python/web/library/admin.py @@ -0,0 +1,7 @@ +from web.library.models import * +from django.contrib import admin + +mod = [Category, BookSeries, ReferenceType, Reference, Book, AlternativeTitle, Copy, Person, Relation, BookPerson, Placement] + +for model in mod: + admin.site.register(model) diff --git a/python/web/library/models.py b/python/web/library/models.py index 65b3270..3840589 100644 --- a/python/web/library/models.py +++ b/python/web/library/models.py @@ -1,5 +1,10 @@ from django.db import models +book_fields = {'isbn' : 'ISBN', 'id' : 'Identifier', 'title' : 'Title', +'subtitle' : 'Subtitle', 'category' : 'Category', 'publisher':'Publisher', +'published_year':'Published year', 'edition' : 'Edition', 'num_pages' : 'Number of pages', +'series' : 'Series', 'description' : 'Description'} + class Category(models.Model): id = models.CharField(max_length=255, primary_key=True) name = models.CharField(max_length=255) @@ -8,6 +13,13 @@ class BookSeries(models.Model): id = models.CharField(max_length=20, primary_key=True) title = models.CharField(max_length=511) +class ReferenceType(models.Model): + name = models.CharField(max_length=31) + +class Reference(models.Model): + reference_type = models.ForeignKey(ReferenceType) + text = models.CharField(max_length=1023) + class Book(models.Model): isbn = models.CharField(max_length=13, primary_key=True) id = models.CharField(max_length=255, unique=True) @@ -22,6 +34,11 @@ class Book(models.Model): description = models.CharField(max_length=1023, null=True, blank=True) picture = models.ImageField(upload_to='%Y/%m/%d/pictures', null=True, blank=True) thumbnail = models.ImageField(upload_to='%Y/%m/%d/thumbnails', null=True, blank=True) + references = models.ManyToManyField(Reference, related_name='books') + + def full_print(self): + for field in book_fields.items(): + print '%15s: %-30s' % (field[1],eval('self.'+field[0])) class AlternativeTitle(models.Model): book = models.ForeignKey(Book) @@ -36,6 +53,7 @@ class Person(models.Model): id = models.CharField(max_length=255, primary_key=True) first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) + references = models.ManyToManyField(Reference, related_name='persons') class Relation(models.Model): name = models.CharField(max_length=31) @@ -45,13 +63,6 @@ class BookPerson(models.Model): person = models.ForeignKey(Person) relation = models.ForeignKey(Relation) -class ReferenceType(models.Model): - name = models.CharField(max_length=31) - -class Reference(models.Model): - reference_type = models.ForeignKey(ReferenceType) - text = models.CharField(max_length=1023) - class Placement(models.Model): category = models.ForeignKey(Category) shelf = models.CharField(max_length=10) diff --git a/python/web/urls.py b/python/web/urls.py index a0e814b..c13d89e 100644 --- a/python/web/urls.py +++ b/python/web/urls.py @@ -1,8 +1,8 @@ from django.conf.urls.defaults import * # Uncomment the next two lines to enable the admin: -# from django.contrib import admin -# admin.autodiscover() +from django.contrib import admin +admin.autodiscover() urlpatterns = patterns('', # Example: @@ -13,5 +13,5 @@ urlpatterns = patterns('', # (r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: - # (r'^admin/', include(admin.site.urls)), + (r'^admin/', include(admin.site.urls)), )