| 1 | from pyramid.view import view_config |
|---|
| 2 | from pyramid.response import Response |
|---|
| 3 | from pyramid import exceptions |
|---|
| 4 | from pyramid.url import route_url |
|---|
| 5 | from pyramid.httpexceptions import HTTPFound |
|---|
| 6 | |
|---|
| 7 | from couchdbkit import Server |
|---|
| 8 | from couchdbkit.exceptions import ResourceNotFound |
|---|
| 9 | |
|---|
| 10 | import json |
|---|
| 11 | import deform |
|---|
| 12 | |
|---|
| 13 | from isisdm.mapper import IsisModel, Text, Number |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | DATABASENAME = 'test' #old for test is: 'test_database' |
|---|
| 17 | |
|---|
| 18 | def positive_integer(node, x): |
|---|
| 19 | if int(x) != x or x <= 0: |
|---|
| 20 | raise Invalid('invalid positive integer') |
|---|
| 21 | |
|---|
| 22 | class Book(IsisModel): |
|---|
| 23 | title = Text(required=True) |
|---|
| 24 | pages = Number(validator=positive_integer) |
|---|
| 25 | |
|---|
| 26 | def my_view(request): |
|---|
| 27 | form = deform.Form(Book.get_schema(), buttons=('submit',)) |
|---|
| 28 | if request.method == 'POST': |
|---|
| 29 | appstruct = form.validate(request.POST.items()) |
|---|
| 30 | book = Book.deserialize(json.dumps(appstruct)) |
|---|
| 31 | server = Server() |
|---|
| 32 | db = server.get_or_create_db('scielobooks') |
|---|
| 33 | db.save_doc(book.to_struct()) |
|---|
| 34 | return {'form':form.render()} |
|---|
| 35 | |
|---|
| 36 | def get_by_sbid(sbid): |
|---|
| 37 | server = Server() |
|---|
| 38 | db = server.get_or_create_db(DATABASENAME) |
|---|
| 39 | try: |
|---|
| 40 | document = db.get(sbid) |
|---|
| 41 | except ResourceNotFound: |
|---|
| 42 | return None |
|---|
| 43 | return document |
|---|
| 44 | |
|---|
| 45 | #defaults features starts here |
|---|
| 46 | def details_book(request): |
|---|
| 47 | STATIC_FILES_PATH = '/static/books/%s/pdf/%s.pdf' |
|---|
| 48 | sbid = request.matchdict['sbid'] |
|---|
| 49 | document = get_by_sbid(sbid) |
|---|
| 50 | isbn = document['isbn'] |
|---|
| 51 | pdf_url = STATIC_FILES_PATH % (sbid,isbn) |
|---|
| 52 | view_url = "/id/%s/all/pdf/view" % (sbid) |
|---|
| 53 | if document == None or 'a' in document['type']: |
|---|
| 54 | raise exceptions.NotFound() |
|---|
| 55 | for num,chapter in enumerate(document['chapterslist']): |
|---|
| 56 | shortname = document['shortname'] |
|---|
| 57 | if num>=10: |
|---|
| 58 | partnumber = str(num) |
|---|
| 59 | else: |
|---|
| 60 | partnumber = '0'+str(num) |
|---|
| 61 | chapter['url'] = STATIC_FILES_PATH % (sbid, partnumber) |
|---|
| 62 | chapter['preview_url'] = "/id/%s/%s/pdf/view" % (sbid, partnumber) |
|---|
| 63 | document['pdf_url'] = pdf_url |
|---|
| 64 | document['preview_url'] = view_url |
|---|
| 65 | return document |
|---|
| 66 | |
|---|
| 67 | def details_chapter(request): |
|---|
| 68 | #URL: /id/{sbid}/{chapter} |
|---|
| 69 | sbid = request.matchdict['sbid'] |
|---|
| 70 | try: |
|---|
| 71 | chapter = int(request.matchdict['chapter']) |
|---|
| 72 | except ValueError: |
|---|
| 73 | raise exceptions.NotFound('Not a valid chapter') |
|---|
| 74 | document = get_by_sbid(sbid) #here you are getting a monographic sbid, not the analictic one! |
|---|
| 75 | if document == None or 'a' in document['type']: |
|---|
| 76 | raise exceptions.NotFound() |
|---|
| 77 | try: |
|---|
| 78 | chapter_sbid = document['chapterslist'][chapter]['chaptersbid'] |
|---|
| 79 | except IndexError: |
|---|
| 80 | raise exceptions.NotFound('Not a valid chapter') |
|---|
| 81 | document['chapterslist'] = [chap['chaptertitle'] for chap in document['chapterslist']] |
|---|
| 82 | chapter_document = get_by_sbid(chapter_sbid) |
|---|
| 83 | document['chaptertitle'] = chapter_document['chaptertitle'] |
|---|
| 84 | document['pages'] = chapter_document['pages'] |
|---|
| 85 | return document |
|---|
| 86 | |
|---|
| 87 | def pdf_book_viewer(request): |
|---|
| 88 | #URL pattern: "/id/{sbid}/{chapter}/pdf/view" |
|---|
| 89 | STATIC_FILES_PATH = '/static/books/%s/pdf/%s.pdf' |
|---|
| 90 | matchdict = request.matchdict |
|---|
| 91 | |
|---|
| 92 | sbid = matchdict['sbid'] |
|---|
| 93 | chapter = matchdict['chapter'] |
|---|
| 94 | |
|---|
| 95 | book_metadata = get_by_sbid(sbid) |
|---|
| 96 | if not book_metadata: |
|---|
| 97 | raise exceptions.NotFound('Book Not Found - Not a valid SBID') |
|---|
| 98 | isbn = book_metadata['isbn'] |
|---|
| 99 | title = book_metadata['title'] |
|---|
| 100 | |
|---|
| 101 | if chapter == 'all': |
|---|
| 102 | filename = str(isbn) |
|---|
| 103 | else: |
|---|
| 104 | filename = str(chapter) |
|---|
| 105 | |
|---|
| 106 | pdf_url = STATIC_FILES_PATH % (sbid,filename) |
|---|
| 107 | |
|---|
| 108 | return {'title':title, |
|---|
| 109 | 'sbid':sbid, |
|---|
| 110 | 'isbn':isbn, |
|---|
| 111 | 'pdf_url':pdf_url |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | def download(request): |
|---|
| 115 | #URL pattern: "/id/{sbid}/{chapter}/{type}/{name}.{ext}" |
|---|
| 116 | STATIC_FILES_PATH = '/static/books/%s/%s/%s.%s' |
|---|
| 117 | |
|---|
| 118 | matchdict = request.matchdict |
|---|
| 119 | |
|---|
| 120 | sbid = matchdict['sbid'] |
|---|
| 121 | chapter = matchdict['chapter'] |
|---|
| 122 | document_type = matchdict['type'] |
|---|
| 123 | filename = matchdict['name'] |
|---|
| 124 | |
|---|
| 125 | if document_type != matchdict['ext']: |
|---|
| 126 | raise exceptions.NotFound() |
|---|
| 127 | |
|---|
| 128 | book_metadata = get_by_sbid(sbid) |
|---|
| 129 | if not book_metadata: |
|---|
| 130 | raise exceptions.NotFound('Book Not Found - Not a valid SBID') |
|---|
| 131 | isbn = book_metadata['isbn'] |
|---|
| 132 | title = book_metadata['title'] |
|---|
| 133 | |
|---|
| 134 | if chapter != 'all': |
|---|
| 135 | if chapter != filename : |
|---|
| 136 | raise exceptions.NotFound('Book Not Found - Not a valid chapter') |
|---|
| 137 | filename = chapter |
|---|
| 138 | |
|---|
| 139 | ebook_url = STATIC_FILES_PATH % (sbid,document_type,filename,document_type) |
|---|
| 140 | return HTTPFound(location=ebook_url) |
|---|
| 141 | |
|---|
| 142 | #future features starts here |
|---|
| 143 | def images(request): |
|---|
| 144 | return Response() |
|---|
| 145 | |
|---|
| 146 | def isbn_search(request): |
|---|
| 147 | return Response() |
|---|
| 148 | |
|---|
| 149 | def multimedia(request): |
|---|
| 150 | return Response() |
|---|
| 151 | |
|---|
| 152 | def cover(request): |
|---|
| 153 | return Response() |
|---|
| 154 | |
|---|
| 155 | #experimental views starts here!!! |
|---|
| 156 | def browsing_all(request): |
|---|
| 157 | """Only for internal tests""" |
|---|
| 158 | STATIC_VIEW_PATH = '/id/%s/all/pdf/view' |
|---|
| 159 | STATIC_FILES_PATH = '/id/%s/all/pdf/%s.pdf' |
|---|
| 160 | s = Server() |
|---|
| 161 | db = s.get_or_create_db(DATABASENAME) |
|---|
| 162 | search = db.all_docs() |
|---|
| 163 | result_list = [] |
|---|
| 164 | for field in search: |
|---|
| 165 | result_list.append( {'sbid':field['key'], |
|---|
| 166 | 'title':field['value']} ) |
|---|
| 167 | return {'title':'All Books', |
|---|
| 168 | 'result_list':result_list |
|---|
| 169 | } |
|---|
| 170 | |
|---|