def publisher_register(request): #模板中发生提交事件,在下面代码中处理 if request.method == 'POST': #一旦发生post事件,在这里处理 username = request.POST['username'] password = request.POST['password'] nickname = request.POST['nickname'] sex = request.POST['sex'] signature = request.POST['signature'] url = request.FILES['headerimg'] #处理前端数据 #建模 pub = Publisher(username=username, password=password, nickname=nickname, sex=sex, signature=signature, url=url) pub.save() return HttpResponse( "<script type = 'text/javascript'>alert('注册成功');window.history.go(-2);</script>" ) return render(request, "register.html") pass
def create_publisher(name): try: new_publisher = Publisher(name=name) db.session.add(new_publisher) db.session.commit() return jsonify(new_publisher.as_dict()) except Exception as error: return error('creating a new publisher', error)
def new_item(request, *args, **kwargs): errors = [] type = kwargs.pop('type') if type == 'author': form = AuthorForm(request.POST) if form.is_valid(): cd = form.cleaned_data author = Author(first_name=cd.get('first_name'), last_name=cd['last_name'], email=cd['email']) author.save() elif type == 'publisher': form = PubForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd pub = Publisher(name=cd['name'], address=cd['address'], city=cd['city'], state_province=cd['state_province'], country=cd['country'], website=cd['website']) pub.save() elif type == 'book': form = BookForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd names = cd['authors'].split(',') pub_name = cd['publisher'] pub = '' try: pub = Publisher.objects.get(name=pub_name.strip()) except Exception: print 'Publisher[%s] not exist.' % pub_name book = Book(title=cd['title'], publisher=pub, publication_date=cd['pub_date']) book.save() for name in names: try: author = Author.objects.get(first_name=name.strip()) print author book.authors.add(author) except Exception as e: print e print 'Author[%s] not exist.' % name # errors.append('%s not exist.' %name) # book = Book(title=cd['title'], ) return form
def new_item(request, *args, **kwargs): errors=[] type = kwargs.pop('type') if type == 'author': form = AuthorForm(request.POST) if form.is_valid(): cd = form.cleaned_data author = Author(first_name=cd.get('first_name'), last_name=cd['last_name'], email=cd['email']) author.save() elif type == 'publisher': form = PubForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd pub = Publisher(name=cd['name'], address=cd['address'], city=cd['city'], state_province=cd['state_province'], country=cd['country'], website=cd['website']) pub.save() elif type == 'book': form = BookForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd names = cd['authors'].split(',') pub_name = cd['publisher'] pub = '' try: pub = Publisher.objects.get(name=pub_name.strip()) except Exception: print 'Publisher[%s] not exist.' %pub_name book = Book(title=cd['title'], publisher=pub, publication_date=cd['pub_date']) book.save() for name in names: try: author = Author.objects.get(first_name=name.strip()) print author book.authors.add(author) except Exception as e: print e print 'Author[%s] not exist.' %name # errors.append('%s not exist.' %name) # book = Book(title=cd['title'], ) return form
def build_db(filename): # Delete existing DB if os.path.exists(f"{filename}.sqlite3"): os.remove(f"{filename}.sqlite3") # Create DB structure db.create_all() # Add data to the DB with open(f"{filename}.csv") as f: content = csv.reader(f) next(content) for line in content: # add book info to my database and then commit it book = Book( bookID = line[0], bookName = line[1], bookAuthor = line[2], ) # add user info to my database and then commit it review = Review( isbnID = line[5], averageReview = line[3], ratingsCount = line[8], ) # add student to my database and then commit it publisher = Publisher( bookID = line[0], isbnID = line[5], bookPublisher = line[11], ) db.session.add(review) db.session.add(publisher) db.session.add(book) db.session.commit()
def get(self): self.response.headers.add_header('Access-Control-Allow-Origin', '*') self.response.headers['Content-Type'] = 'application/json' id_user = self.request.get('user') objemp = Usuarios.query(Usuarios.email == id_user).get() if objemp is None: myList = [] json_string = json.dumps(myList, default=MyClass) self.response.write(json_string) return None strKey = objemp.key.urlsafe() ownerKey = ndb.Key(urlsafe=strKey) myEntities = Publisher.query(Publisher.user_key == ownerKey) myList = [] for i in myEntities: myObj = DemoClass() myObj.entityKey = i.entityKey myObj.nameA = i.nameA myObj.location = i.location myObj.year = i.year myObj.logo = i.logo myList.append(myObj) json_string = json.dumps(myList, default=MyClass) self.response.write(json_string)
def get(self, publisher_name): publisher = Publisher.get_by_urlname(publisher_name) collections = Collection.all_by_publisher(publisher.key) response = dict( publisher=simplejson.loads(publisher.json), collections=[simplejson.loads(x.json) for x in collections]) self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(response))
def get(self, publisher_name, collection_name): publisher = Publisher.get_by_urlname(publisher_name) collection = Collection.get_by_urlname(collection_name, publisher.key) response = dict( publisher=simplejson.loads(publisher.json), collection=simplejson.loads(collection.json)) self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(response))
def upsert_paper(info, author_id): try: with db_session: publisher_id = info['publisher_id'] if publisher_id: publisher = Publisher.get(publisher_id=publisher_id) if publisher: pass else: publisher = Publisher(publisher_id=publisher_id) publisher.name = info['publishername'] except: pass with db_session: p = Paper.get(paper_id=info['paper_id']) if p: logger.debug('paper has existed, paper_id=%s', info['paper_id']) return paper = Paper(paper_id=info['paper_id']) paper.title = info['title'] paper.abstract = info['abstract'] paper.cite_num = info['cite_num'] paper.cited_num = info['cited_num'] publisher_id = info['publisher_id'] if publisher_id: publisher = Publisher.get(publisher_id=publisher_id) if publisher: paper.publisher = publisher if author_id is None: return a = Author.get(author_id=author_id) if a: paper.authors.add(a) else: a_info = api.get_author(author_id) author = Author(author_id=a_info['author_id']) author.name = a_info['name'] author.image_url = a_info['image_url'] author.organization = a_info['organization'] author.home_page = a_info['home_page'] author.paper_count = a_info['paper_count'] author.citied_count = a_info['cited_count'] paper.authors.add(author)
def hello(request): # return HttpResponse(datetime.datetime.strptime(('%Y-%m-%d %H:%M:%S',datetime.datetime.time()))) now = datetime.datetime.now() # html = "<html><body>It is now %s.</body></html>" % now t = get_template("mytemplete.html") html = t.render(Context({"person_name": now})) p = Publisher(name='Apress', address='2855 Telegraph Ave.', city='Berkeley', state_province='CA', country='U.S.A.', website='http://www.apress.com/') p.save() item_list = Publisher.objects.all() html = t.render(Context({"item_list": item_list})) return HttpResponse(html)
def hello(request): # return HttpResponse(datetime.datetime.strptime(('%Y-%m-%d %H:%M:%S',datetime.datetime.time()))) now = datetime.datetime.now() # html = "<html><body>It is now %s.</body></html>" % now t=get_template("mytemplete.html") html=t.render(Context({"person_name":now})) p=Publisher(name='Apress', address='2855 Telegraph Ave.', city='Berkeley', state_province='CA', country='U.S.A.', website='http://www.apress.com/') p.save() item_list=Publisher.objects.all() html=t.render(Context({"item_list":item_list})) return HttpResponse(html)
def loadPublishers(): publisher_id = 1 for i in range(len(df)): publisher_id = publisher_id publisher_name = df['publishers'][i][0]['name'] try: publisher_wiki_url = df['publishers'][i][0][ 'wikipedia_url'] #nullable except: publisher_wiki_url = None try: publisher_parent_company = df['publishers'][i][0][ 'parent company'] #nullable except: publisher_parent_company = None try: publisher_founded = df['publishers'][i][0]['founded'] #nullable except: publisher_founded = None try: publisher_description = df['publishers'][i][0][ 'description'] #nullable except: publisher_description = None try: publisher_owner = df['publishers'][i][0]['owner'] #nullable except: publisher_owner = None try: publisher_image_url = df['publishers'][i][0][ 'image_url'] #nullable except: publisher_image_url = None try: publisher_website = df['publishers'][i][0]['website'] #nullable except: publisher_website = None publishers = db.session.query(Publisher).all() pList = [] for publisher in publishers: pList.append(publisher.name) if publisher_name not in pList: newPub = Publisher(id=publisher_id, name=publisher_name, wiki_url=publisher_wiki_url, parent_company=publisher_parent_company, founded=publisher_founded, description=publisher_description, image_url=publisher_image_url, website=publisher_website) db.session.add(newPub) db.session.commit() publisher_id += 1
def _new_publisher(self, feed): name = feed.get('publisher') \ or feed.get('author') \ or feed.get('contributors') \ or 'unknown' safe_name = self._sanitize_title(name) publisher, is_new = Publisher.get_or_create(name=safe_name) print('Saved publisher "{}": new={}'.format(safe_name, is_new)) return publisher
def test_source_insert_8(self): s = Publisher(id='2', name='TESTTWO') session.add(s) session.commit() r = session.query(Publisher).filter_by(id='2').one() self.assertEqual(str(r.id), '2') session.query(Publisher).filter_by(id='2').delete() session.commit()
def test_publisher_1(self): s = Publisher(name='Bloopers', parentCompany = 'Blah', owner = "Public asdfasdf", location = "U.S.", yearFounded = "1920-10-22", image="https://www.dedicatedteacher.com/media/images/publishers/large/sch_700.jpg", website = "http://www.scholastic.com/home/", wikipedia= "https://en.wikipedia.org/wiki/Scholastic_Corporation", description="Scholastic Corporation is an American multinational publishing, education and media company known for publishing, selling, and distributing books and educational materials for schools, teachers, parents, and children. ") testSession.add(s) testSession.commit() r = testSession.query(Publisher).filter_by(name = 'Bloopers').one() self.assertEqual(str(r.name), 'Bloopers') testSession.query(Publisher).filter_by(name = 'Bloopers').delete() testSession.commit()
def get(self, publisher_name, collection_name): publisher = Publisher.get_by_urlname(publisher_name) collection = Collection.get_by_urlname(collection_name, publisher.key) logging.info(str(collection)) records = Record.all_by_collection(collection.key) response = dict( publisher=simplejson.loads(publisher.json), collection=simplejson.loads(collection.json), records=[simplejson.loads(x.record) for x in records]) self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(response))
def pub_add(): if request.method == 'POST': pub_obj = request.form.to_dict() print(pub_obj) pub_name = pub_obj.get('p_name') pub_obj = Publisher(p_name=pub_name) db_session.add(pub_obj) db_session.commit() db_session.close() return redirect('/pub_list') return render_template('publish/publish_add.html')
def create_publishers(): book = load_json('books.json') counter = 0 dupe = False for oneBook in book: title = oneBook['title'] publishers_attribute = oneBook['publishers'] authors_attribute = oneBook['authors'] for j in authors_attribute: author = j['name'] for i in publishers_attribute: publisher = i['name'] try: wiki = i['wikipedia_url'] except: wiki = "None" try: desc = i['description'] except: desc = "None" try: owner = i['owner'] except: owner = "None" try: img_url = i['image_url'] except: img_url = "None" try: website = i['website'] except: website = "None" newPublisher = Publisher(id=counter, name=publisher, wikipedia_url=wiki, description=desc, owner=owner, image_url=img_url, website=website) cursor.execute('SELECT name FROM publisher') rows = cursor.fetchall() for row in rows: if publisher in row: dupe = True continue if (not dupe): db.session.add(newPublisher) db.session.commit() counter += 1 print("we added a publisher into the DB! it was " + publisher) else: print("there was a dupe!") dupe = False
def test_publisher_model_2(self): """Test querying the database by attribute using simple keywords""" with app.test_request_context(): data1 = Publisher("name1", "about1", "date1", "country1", "hq1") data2 = Publisher("name2", "about2", "date2", "country2", "hq2") db.session.add(data1) db.session.commit() db.session.add(data2) db.session.commit() publisher = db.session.query(Publisher).filter_by( headquarters="hq1").first() self.assertEqual(publisher.name, "name1") self.assertEqual(publisher.about, "about1") db.session.delete(data1) db.session.commit() db.session.delete(data2) db.session.commit()
def create_publishers(pub): if Publisher.query.get(pub['name']) is None: name = pub['name'] owner = pub.get('owner') description = pub.get('description') wikipedia_url = pub.get('wikipedia_url') image_url = pub.get('image_url') website = pub.get('website') newPublisher = Publisher(name = name,owner = owner,website = website,description = description,wiki_url = wikipedia_url,img_url = image_url) db.session.add(newPublisher) db.session.commit()
def test_min_publisher(self): s = Publisher(name='hi') db.session.add(s) db.session.commit() r = Publisher.query.get("hi") self.assertEqual(r.name, 'hi') self.assertEqual(r.owner, None) db.session.query(Publisher).filter_by(name="hi").delete() db.session.commit()
def _import_impression(self, work, publishers_page, f_path): # creates a new PDFParser to get the impression self.logger.debug('Parsing {}'.format(f_path)) parser = PDFParser(f_path) code = parser.get_impression_code() if code: self.logger.debug('Impression: ' + code) # Create an Impression PDF Document. document = Document(title=code) with open(f_path, 'rb') as fh: pdf_file = File(fh) document.file.save(os.path.basename(f_path), pdf_file) document.tags.add('impression') # creates a new impression impression = Impression() impression.title = code impression.impression_title = parser.get_title() impression.content = parser.get_text_content() impression.pdf = document try: sort_order = self._order_of_impressions.index(code.lower()) except Exception: self.logger.error( u'{0} missing from order of impressions, which consists of: {1}' .format(code, ', '.join(self._order_of_impressions))) sort_order = 999 impression.sort_order = sort_order impression.slug = safe_slugify(impression.title, Impression) impression.comments = parser.get_comments() self._import_copies(impression, parser, code) publisher_code = impression.title.split('-')[-1] publisher = Publisher.objects.filter(title=publisher_code).first() if not publisher: publisher = Publisher(title=publisher_code) publisher.slug = slugify(publisher_code) publishers_page.add_child(instance=publisher) impression.publisher = publisher work.add_child(instance=impression)
def _import_impression (self, work, publishers_page, f_path): # creates a new PDFParser to get the impression self.logger.debug('Parsing {}'.format(f_path)) parser = PDFParser(f_path) code = parser.get_impression_code() if code: self.logger.debug('Impression: ' + code) # Create an Impression PDF Document. document = Document(title=code) with open(f_path, 'rb') as fh: pdf_file = File(fh) document.file.save(os.path.basename(f_path), pdf_file) document.tags.add('impression') # creates a new impression impression = Impression() impression.title = code impression.impression_title = parser.get_title() impression.content = parser.get_text_content() impression.pdf = document try: sort_order = self._order_of_impressions.index(code.lower()) except Exception: self.logger.error( u'{0} missing from order of impressions, which consists of: {1}'.format(code, ', '.join(self._order_of_impressions))) sort_order = 999 impression.sort_order = sort_order impression.slug = safe_slugify(impression.title, Impression) impression.comments = parser.get_comments() self._import_copies(impression, parser, code) publisher_code = impression.title.split('-')[-1] publisher = Publisher.objects.filter(title=publisher_code).first() if not publisher: publisher = Publisher(title=publisher_code) publisher.slug = slugify(publisher_code) publishers_page.add_child(instance=publisher) impression.publisher = publisher work.add_child(instance=impression)
def publisher_register(request): # 模板中发生提交事件以后要在下面的代码中处理 if request.method == 'POST': # 一旦发生POST事件,则在这里处理 # 接收前端提出的数据 username = request.POST['username'] password = request.POST['password'] nickname = request.POST['nickname'] sex = request.POST['sex'] signature = request.POST['signature'] # 处理前端提交的数据 # 建模 pub = Publisher(username=username, password=password, nickname=nickname, sex=sex, signature=signature) # 存入数据库 pub.save() return HttpResponse("<script type='text/javascript>" "alert('注册成功');" "window.history.back();" "</script>") # 安装模板 return render(request, "publisher/register.html")
def test_add_Publisher1(self): with app.test_request_context(): publisher1 = Publisher(id=1000000, name="publisher1") db.session.add(publisher1) db.session.commit() gamequery = db.session.query(Publisher).filter_by( id="1000000").first() self.assertEqual(gamequery.id, 1000000) self.assertEqual(gamequery.name, "publisher1") db.session.delete(publisher1) db.session.commit()
def addPublisher(publisherName, publisherParCom, publisherOwner, publisherLoc, publisherYear, publisherImage, publisherWebpage, publisherWiki, publisherDesc, newBook): pub = session.query(Publisher).filter( Publisher.name == publisherName).all() if len(pub) == 0: pub = Publisher(name=publisherName, parentCompany=publisherParCom, owner=publisherOwner, location=publisherLoc, yearFounded=publisherYear, image=publisherImage, website=publisherWebpage, wikipedia=publisherWiki, description=publisherDesc) pub.books = [newBook] session.add(pub) return pub else: pub = pub[0] pub.books.append(newBook) session.add(pub) return pub
def test_add_Publisher2(self): with app.test_request_context(): publisher2 = Publisher() init_len = len(Publisher.query.all()) db.session.add(publisher2) db.session.commit() changed_len = len(Publisher.query.all()) self.assertEqual(init_len + 1, changed_len) init_len = changed_len db.session.delete(publisher2) db.session.commit() changed_len = len(Publisher.query.all()) self.assertEqual(init_len - 1, changed_len)
def test_randIn_publisher(self): s = Publisher(name="herro", owner="victor", website="hhtps://askdfj.com", wiki_url="kajsldfalkdsf") db.session.add(s) db.session.commit() r = Publisher.query.get('herro') self.assertEqual(r.owner, "victor") self.assertEqual(r.name, 'herro') db.session.query(Publisher).filter_by(name="herro").delete() db.session.commit()
def test_source_insert_4(self): s = Publisher(id='99100', name='bum') db.session.add(s) db.session.commit() cursor.execute("SELECT id, name FROM Publisher") rows = cursor.fetchall() swag = '' for row in rows: if (row[1] == 'bum'): swag = row[1] break self.assertEqual(swag, 'bum') db.session.delete(s) db.session.commit()
def test_publisher_model_1(self): """Test querying the database by attribute using simple keywords""" with app.test_request_context(): data = Publisher("name", "about", "date", "country", "hq") db.session.add(data) db.session.commit() publisher = db.session.query(Publisher).filter_by( country="country").first() self.assertEqual(publisher.name, "name") self.assertEqual(publisher.about, "about") db.session.delete(data) db.session.commit()
def newPublisher(): """Create a publisher once authenticated.""" with session_scope() as session: images = session.query(Image).order_by(Image.alt_text).all() if request.method == 'GET': return render_template('new-publisher.html', images=images) if request.method == 'POST': publisher = Publisher( name=request.form["publisher_name"], country=request.form["country"], image_id=request.form["publisher_image"], owner_id=login_session['user_id'] ) session.add(publisher) session.commit() return redirect(url_for('allPublishers'))
def test_publisher_2(self): publisher = Publisher(ident=99999, name="Publisher", num_games=3, year_founded=2000, country="England", website="Fake.com", gameid=501, characterid=501, picture="fake.com/picture") session = self.db.session session.add(publisher) session.commit() result = session.query(Publisher).get(99999) self.assertEqual(result.num_games, 3) session.delete(result) session.commit()
def test_max_publisher(self): s = Publisher(name=str("a" * 100), owner=str("a" * 100), website=str("a" * 100), description=str("a" * 100), wiki_url=str("a" * 100), img_url=str("a" * 200)) db.session.add(s) db.session.commit() r = Publisher.query.get(str("a" * 100)) self.assertEqual(r.owner, str("a" * 100)) self.assertEqual(r.description, str("a" * 100)) db.session.query(Publisher).filter_by(name=str("a" * 100)).delete() db.session.commit()
def create_publishers(): publisher = load_json('publishers.json') for onePublisher in publisher['publishers_data']: name = onePublisher['name'] id = onePublisher['id'] location = onePublisher.get('location') founding = onePublisher.get('founded') parentCompany = onePublisher.get('parent_company') website = onePublisher.get('website') imageURL= onePublisher.get('image_url') newPublisher = Publisher(name = name, id = id, imageURL = imageURL, website=website, location=location,founding=founding, parentCompany=parentCompany) # After I create the book, I can then add it to my session. db.session.add(newPublisher) # commit the session to my DB. db.session.commit()
def save_article(article_): img_url = article_.find(class_='img_box2').find( 'img').attrs['src'].split('url=')[1] text_box = article_.find(class_='txt-box') title = text_box.find('h4').find('a').text article_url = text_box.find('h4').find('a').attrs['href'] summary = text_box.find('p').text create_at = datetime.fromtimestamp(float(text_box.find( class_='s-p').attrs['t'])) publisher_name = text_box.find(class_='s-p').find('a').attrs['title'] article = Article(img_url=img_url, title=title, article_url=article_url, summary=summary, create_at=create_at, publisher=Publisher.get_or_create(publisher_name)) try: article.save() except (NotUniqueError, InvalidBSON): pass
def create_publishers(authorDict, publisherDict, bookDict): book = load_json('books.json') publishersList = [] for oneBook in book: for onePublisher in oneBook['publishers']: title = oneBook['title'] publishersdict = oneBook['publishers'] publisher = get_info(oneBook, "publishers", "name") if publisher not in publishersList: publishersList.append(publisher) publisherNum = publisherDict[publisher] author = get_info(oneBook, "authors", "name") authorNum = authorDict[author] google_id = oneBook['google_id'] bookNum = bookDict[google_id] parent_company = onePublisher.get('parent company') owner = onePublisher.get('owner') founded = onePublisher.get('founded') location = onePublisher.get('location') website = onePublisher.get('website') wikipedia_url = onePublisher.get('wikipedia_url') image_url = onePublisher.get('image_url') description = onePublisher.get('description') newPublisher = Publisher(bookNum=bookNum, title=title, authorNum=authorNum, publisherNum=publisherNum, publisher=publisher, parent_company=parent_company, owner=owner, location=location, founded=founded, author=author, wikipedia_url=wikipedia_url, description=description, website=website, image_url=image_url) db.session.add(newPublisher) db.session.commit()
def save_article(article_): img_url = article_.find( class_='img_box2').find('img').attrs['src'].split('url=')[1] text_box = article_.find(class_='txt-box') title = text_box.find('h4').find('a').text article_url = text_box.find('h4').find('a').attrs['href'] summary = text_box.find('p').text create_at = datetime.fromtimestamp( float(text_box.find(class_='s-p').attrs['t'])) publisher_name = text_box.find(class_='s-p').find('a').attrs['title'] article = Article(img_url=img_url, title=title, article_url=article_url, summary=summary, create_at=create_at, publisher=Publisher.get_or_create(publisher_name)) try: article.save() except (NotUniqueError, InvalidBSON): pass
def cleanup(): Article.drop_collection() Publisher.drop_collection()
def save_acquisition(request): if request.POST.get('book').isnumeric(): book = Book.objects.get(id=request.POST.get('book')) new_book = False else: book = Book(title=request.POST.get('book')) new_book = True book.subtitle = request.POST.get('subtitle') book.save() if request.POST.get('isbn'): isbn = request.POST.get('isbn') if isbnpy.isValid(isbn): if isbnpy.isI10(isbn): isbn = isbnpy.convert(isbn) try: record = Record.objects.get(isbn13=isbn) new_record = False except Record.DoesNotExist: record = Record(isbn13=isbn) new_record = True else: if not new_book: try: record = Record.objects.get(book=book, edition=request.POST.get('book')) new_record = False except Record.DoesNotExist: record = Record(book=book) new_record = True else: record = Record(book=book) new_record = True record.book = book record.format = request.POST.get('format') if record.format != 'ebook': if new_record: record.quantity = request.POST.get('quantity') else: record.quantity += int(request.POST.get('quantity')) record.excerpt = request.POST.get('excerpt') record.edition = request.POST.get('edition') record.notes = request.POST.get('notes') record.ddc = request.POST.get('ddc') record.lcc = request.POST.get('lcc') record.pagination = request.POST.get('pagination') record.format = request.POST.get('format') record.type = request.POST.get('type') if record.format != 'eBook': record.quantity = request.POST.get('quantity') record.publication_has_month = False record.publication_has_day = False if request.POST.get('year'): dt = datetime(int(request.POST.get('year')), 1, 1) if request.POST.get('month'): record.publication_has_month = True dt = dt.replace(month=int(request.POST.get('month'))) if request.POST.get('day'): record.publication_has_day = True dt = dt.replace(day=int(request.POST.get('day'))) record.date_of_publication = dt else: record.date_of_publication = None if request.FILES.get('small_cover'): record.small_cover = request.FILES.get('small_cover') if request.FILES.get('medium_cover'): record.medium_cover = request.FILES.get('medium_cover') if request.FILES.get('large_cover'): record.large_cover = request.FILES.get('large_cover') if not record.date_added: record.date_added = datetime.today() record.save() if request.FILES.get('ebook'): ebooks = request.FILES.getlist('ebook') for ebook in ebooks: ebook_file = BookFile(record=record, file=ebook) existing_files = record.ebooks(ebook_file.format) for existing_file in existing_files: existing_file.delete() ebook_file.save() book.subjects.clear() for subject in request.POST.getlist('subjects'): if subject.isnumeric(): book.subjects.add(Subject.objects.get(id=subject)) else: new_subject = Subject(name=subject) new_subject.save() book.subjects.add(new_subject) record.authors.clear() for author in request.POST.getlist('authors'): if author.isnumeric(): record.authors.add(Author.objects.get(id=author)) else: new_author = Author(name=author) new_author.save() record.authors.add(new_author) record.languages.clear() for language in request.POST.getlist('languages'): record.languages.add(Language.objects.get(id=language)) publisher = request.POST.get('publisher') if publisher: if publisher.isnumeric(): record.publisher_id = publisher else: new_publisher = Publisher(name=publisher) new_publisher.save() record.publisher = new_publisher record.save() return redirect(reverse_lazy('view_record', kwargs={'pk': record.id}))
def acquisition(request): record_data = {} record = None if request.GET.get('isbn'): isbn = request.GET.get('isbn') if isbnpy.isValid(isbn): url = 'http://openlibrary.org/api/volumes/brief/json/isbn:' + isbn response = urllib2.urlopen(url) # response = urllib2.urlopen('http://127.0.0.1/json/3.json') data = json.load(response) if isbnpy.isI10(isbn): isbn = isbnpy.convert(isbn) if data == {}: record_data['isbn13'] = isbn record_form = RecordForm(instance=record) return render(request, 'acquisition.html', {'data': record_data, 'form': record_form}) data = data.itervalues().next()['records'].itervalues().next() try: record = Record.objects.get(isbn13=isbn) new_record = False except Record.DoesNotExist: record = Record(isbn13=isbn) new_record = True # pp(data) if record.book_id: book = record.book else: book = Book() book.title = data['data']['title'] if data['details']['details'].has_key('subtitle'): book.subtitle = data['details']['details']['subtitle'] book.save() if data['details']['details'].has_key('pagination'): record.pagination = data['data']['pagination'] elif data['details']['details'].has_key('number_of_pages'): record.pagination = str(data['data']['number_of_pages']) + ' p.' if data['details']['details'].has_key('physical_format'): record.format = data['details']['details']['physical_format'] if record.format.startswith('electronic'): record.format = 'eBook' # record.openlibrary_url = data['data']['url'] if data['details']['details'].has_key('weight'): record.weight = data['details']['details'].get('weight') if data['details']['details'].has_key('physical_dimensions'): record.dimensions = data['details']['details'].get('physical_dimensions') if data['data'].has_key('classifications'): if data['data']['classifications'].has_key('dewey_decimal_class'): record.ddc = data['data']['classifications'].get('dewey_decimal_class')[0] if data['data']['classifications'].has_key('lc_classifications'): record.lcc = data['data']['classifications'].get('lc_classifications')[0] try: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%B %d, %Y').date() record.publication_has_month = True record.publication_has_day = True except ValueError: try: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%Y').date() record.publication_has_month = False record.publication_has_day = False except ValueError: try: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%B %Y').date() record.publication_has_day = False record.publication_has_month = True except ValueError: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%m/%d/%Y').date() record.publication_has_day = True record.publication_has_month = True if data['data'].has_key('identifiers'): if data['data']['identifiers'].has_key('openlibrary'): record.openlibrary_id = data['data']['identifiers']['openlibrary'][0] if data['data']['identifiers'].has_key('goodreads'): record.goodreads_id = data['data']['identifiers']['goodreads'][0] if data['data']['identifiers'].has_key('librarything'): record.librarything_id = data['data']['identifiers']['librarything'][0] if data['data']['identifiers'].has_key('oclc'): record.oclc_id = data['data']['identifiers']['oclc'][0] if data['data']['identifiers'].has_key('lccn'): record.lccn_id = data['data']['identifiers']['lccn'][0] if data['data'].has_key('by_statement'): record.by_statement = data['data'].get('by_statement') if data['data'].has_key('notes'): record.notes = data['data'].get('notes') if data['data'].has_key('excerpts'): record.excerpt = data['data'].get('excerpts')[0].get('text') record.book = book setting = LibrarySetting.get() record.type = setting.default_type if new_record: record.date_added = datetime.today() record.save() if data['details']['details'].has_key('languages'): record.languages.clear() for lang in data['details']['details']['languages']: lang_key = lang['key'].replace('/languages/', '') try: book_lang = Language.objects.get(code=lang_key) except Language.DoesNotExist: try: book_lang = Language.objects.get(code=lang_key[:-1]) except Language.DoesNotExist: raise Exception( "Please add a language with code " + lang_key + " or " + lang_key[:-1] + " first!") record.languages.add(book_lang) if data['data'].has_key('publish_places'): record.published_places.clear() for place in data['data']['publish_places']: try: published_place = Place.objects.get(name=place['name']) except Place.DoesNotExist: published_place = Place(name=place['name']) published_place.save() record.published_places.add(published_place) record.authors.clear() if data['details']['details'].has_key('authors'): for author in data['details']['details']['authors']: author_key = author['key'].replace('/authors/', '') try: book_author = Author.objects.get(identifier=author_key) except Author.DoesNotExist: book_author = Author(identifier=author_key) book_author.name = author['name'] book_author.save() record.authors.add(book_author) elif data['data'].has_key('authors'): for author in data['data']['authors']: author_key = author['url'].replace('http://openlibrary.org/authors/', '') try: book_author = Author.objects.get(identifier=author_key) except Author.DoesNotExist: book_author = Author(identifier=author_key) book_author.name = author['name'] book_author.save() record.authors.add(book_author) if data['data'].has_key('ebooks'): if data['data']['ebooks'][0].has_key('formats'): formats = data['data']['ebooks'][0]['formats'] for book_format in formats: ebooks = record.ebooks(book_format) for ebook in ebooks: ebook.delete() if formats[book_format].has_key('url'): url = formats[book_format].get('url') result = urllib.urlretrieve(url) book_file = BookFile(record=record) book_file.file.save( os.path.basename(url), File(open(result[0])) ) book_file.save() subjects = None if data['details']['details'].has_key('subjects'): subjects = data['details']['details']['subjects'] elif data['data'].has_key('subjects'): subjects = data['data']['subjects'] if subjects: record.book.subjects.clear() for subject in subjects: if type(subject) == dict: subject = title_case(subject['name']) try: book_subject = Subject.objects.get(name=subject) except Subject.DoesNotExist: book_subject = Subject(name=subject) book_subject.save() record.book.subjects.add(book_subject) # record.publishers.clear() # for publisher in data['details']['details']['publishers']: # try: # book_publisher = Publisher.objects.get(name=publisher['name']) # except Publisher.DoesNotExist: # book_publisher = Publisher(name=publisher['name']) # book_publisher.save() # record.publishers.add(book_publisher) try: book_publisher = Publisher.objects.get(name=data['details']['details']['publishers'][0]) except Publisher.DoesNotExist: book_publisher = Publisher(name=data['details']['details']['publishers'][0]) book_publisher.save() record.publisher = book_publisher if data['data'].has_key('cover'): if data['data']['cover'].has_key('large'): cover_url = data['data']['cover']['large'] result = urllib.urlretrieve(cover_url) record.large_cover.save( os.path.basename(cover_url), File(open(result[0])) ) if data['data']['cover'].has_key('medium'): cover_url = data['data']['cover']['medium'] result = urllib.urlretrieve(cover_url) record.medium_cover.save( os.path.basename(cover_url), File(open(result[0])) ) if data['data']['cover'].has_key('small'): cover_url = data['data']['cover']['small'] result = urllib.urlretrieve(cover_url) record.small_cover.save( os.path.basename(cover_url), File(open(result[0])) ) # thumbnail_url = data['details']['thumbnail_url'] # result = urllib.urlretrieve(thumbnail_url) # record.thumbnail.save( # os.path.basename(thumbnail_url), # File(open(result[0])) # ) # import pdb # # pdb.set_trace() record_data = RecordSerializer(record).data record_form = RecordForm(instance=record) return render(request, 'acquisition.html', {'data': record_data, 'form': record_form})
def get(self): response = [simplejson.loads(x.json) for x in Publisher.query().fetch()] self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(response))