Commit 9f44b4da authored by Sree Harsha Ramesh's avatar Sree Harsha Ramesh Committed by step21

Fix for #9: Display error message if no database present (#15)

* Fix issue #9 - added logger - handled sqlalchemy db errors

* Fix issue #9 - added logger - handled sqlalchemy db errors
parent 041e1d86
......@@ -7,6 +7,10 @@ app = Flask(__name__)
#app.config.from_object('config')
app.config.from_pyfile('config.py')
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
db = SQLAlchemy(app)
from poerelief import views, models
# encoding=utf-8
from poerelief import app, db, models, epidat_parse_bfs
from poerelief import app, db, models, epidat_parse_bfs, logger
from flask import render_template
import json
import random
import sqlite3
import sqlalchemy
sitenamed = "Poetic Relief"
sitenameh = u"תבליט פיוטי"
......@@ -28,8 +30,12 @@ def faq():
#This gives a doc as json. Why is this there in addition to the other one?
@app.route('/doc/<locid>')
def epidoc_json(locid):
doc = models.Epidat.query.filter_by(locid=locid).first_or_404()
data = {'availability': doc.availability, 'licence': doc.licence, 'title': doc.title, 'locid': doc.locid, 'urld': doc.urld, 'date': doc.date, 'insc': doc.insc, 'material': doc.material, 'condition': doc.condition, 'deconote': doc.deconote, 'decodesc': doc.decodesc, 'geoname': doc.geoname, 'geotype': doc.geotype, 'geocountry': doc.geocountry, 'georegion': doc.georegion, 'geocoord': doc.geocoord, 'graphics': doc.graphics, 'graphicsurl': doc.graphicsurl, 'idno': doc.idno, 'sex': doc.sex, 'pname': doc.pname, 'deathdate': doc.deathdate, 'edition': doc.edition, 'verso': doc.verso, 'recto': doc.recto, 'translation': doc.translation, 'linecomm': doc.linecomm, 'endcomm': doc.endcomm, 'proso': doc.proso, 'bibliography': doc.bibliography}
data = None
try:
doc = models.Epidat.query.filter_by(locid=locid).first_or_404()
data = {'availability': doc.availability, 'licence': doc.licence, 'title': doc.title, 'locid': doc.locid, 'urld': doc.urld, 'date': doc.date, 'insc': doc.insc, 'material': doc.material, 'condition': doc.condition, 'deconote': doc.deconote, 'decodesc': doc.decodesc, 'geoname': doc.geoname, 'geotype': doc.geotype, 'geocountry': doc.geocountry, 'georegion': doc.georegion, 'geocoord': doc.geocoord, 'graphics': doc.graphics, 'graphicsurl': doc.graphicsurl, 'idno': doc.idno, 'sex': doc.sex, 'pname': doc.pname, 'deathdate': doc.deathdate, 'edition': doc.edition, 'verso': doc.verso, 'recto': doc.recto, 'translation': doc.translation, 'linecomm': doc.linecomm, 'endcomm': doc.endcomm, 'proso': doc.proso, 'bibliography': doc.bibliography}
except sqlalchemy.exc.OperationalError as oe:
logger.error("Failed to connect to DB:%s", oe[0])
return json.dumps(data)
......@@ -42,19 +48,29 @@ def epidoc_json(locid):
#this gets a random dataset
@app.route('/doc/random')
def randomdoc():
rand = random.randrange(0, db.session.query(models.Epidat).count())
doc = db.session.query(models.Epidat)[rand]
data = {'availability': doc.availability, 'licence': doc.licence, 'title': doc.title, 'locid': doc.locid, 'urld': doc.urld, 'date': doc.date, 'insc': doc.insc, 'material': doc.material, 'condition': doc.condition, 'deconote': doc.deconote, 'decodesc': doc.decodesc, 'geoname': doc.geoname, 'geotype': doc.geotype, 'geocountry': doc.geocountry, 'georegion': doc.georegion, 'geocoord': doc.geocoord, 'graphics': doc.graphics, 'graphicsurl': doc.graphicsurl, 'idno': doc.idno, 'sex': doc.sex, 'pname': doc.pname, 'deathdate': doc.deathdate, 'edition': doc.edition, 'verso': doc.verso, 'recto': doc.recto, 'translation': doc.translation, 'linecomm': doc.linecomm, 'endcomm': doc.endcomm, 'proso': doc.proso, 'bibliography': doc.bibliography}
data = None
try:
rand = random.randrange(0, db.session.query(models.Epidat).count())
doc = db.session.query(models.Epidat)[rand]
data = {'availability': doc.availability, 'licence': doc.licence, 'title': doc.title, 'locid': doc.locid, 'urld': doc.urld, 'date': doc.date, 'insc': doc.insc, 'material': doc.material, 'condition': doc.condition, 'deconote': doc.deconote, 'decodesc': doc.decodesc, 'geoname': doc.geoname, 'geotype': doc.geotype, 'geocountry': doc.geocountry, 'georegion': doc.georegion, 'geocoord': doc.geocoord, 'graphics': doc.graphics, 'graphicsurl': doc.graphicsurl, 'idno': doc.idno, 'sex': doc.sex, 'pname': doc.pname, 'deathdate': doc.deathdate, 'edition': doc.edition, 'verso': doc.verso, 'recto': doc.recto, 'translation': doc.translation, 'linecomm': doc.linecomm, 'endcomm': doc.endcomm, 'proso': doc.proso, 'bibliography': doc.bibliography}
except sqlalchemy.exc.OperationalError as oe:
logger.error("Failed to connect to DB:%s", oe[0])
return json.dumps(data)
#this generates a specific json
@app.route('/doc/random/<locid>')
def permajson(locid):
if locid:
doc = models.Epidat.query.filter_by(locid=locid).first()
data = {'availability': doc.availability, 'licence': doc.licence, 'title': doc.title, 'locid': doc.locid, 'urld': doc.urld, 'date': doc.date, 'insc': doc.insc, 'material': doc.material, 'condition': doc.condition, 'deconote': doc.deconote, 'decodesc': doc.decodesc, 'geoname': doc.geoname, 'geotype': doc.geotype, 'geocountry': doc.geocountry, 'georegion': doc.georegion, 'geocoord': doc.geocoord, 'graphics': doc.graphics, 'graphicsurl': doc.graphicsurl, 'idno': doc.idno, 'sex': doc.sex, 'pname': doc.pname, 'deathdate': doc.deathdate, 'edition': doc.edition, 'verso': doc.verso, 'recto': doc.recto, 'translation': doc.translation, 'linecomm': doc.linecomm, 'endcomm': doc.endcomm, 'proso': doc.proso, 'bibliography': doc.bibliography}
else:
data = "No valid id-loc specified."
data = None
try:
if locid:
doc = models.Epidat.query.filter_by(locid=locid).first()
data = {'availability': doc.availability, 'licence': doc.licence, 'title': doc.title, 'locid': doc.locid, 'urld': doc.urld, 'date': doc.date, 'insc': doc.insc, 'material': doc.material, 'condition': doc.condition, 'deconote': doc.deconote, 'decodesc': doc.decodesc, 'geoname': doc.geoname, 'geotype': doc.geotype, 'geocountry': doc.geocountry, 'georegion': doc.georegion, 'geocoord': doc.geocoord, 'graphics': doc.graphics, 'graphicsurl': doc.graphicsurl, 'idno': doc.idno, 'sex': doc.sex, 'pname': doc.pname, 'deathdate': doc.deathdate, 'edition': doc.edition, 'verso': doc.verso, 'recto': doc.recto, 'translation': doc.translation, 'linecomm': doc.linecomm, 'endcomm': doc.endcomm, 'proso': doc.proso, 'bibliography': doc.bibliography}
else:
data = "No valid id-loc specified."
except sqlalchemy.exc.OperationalError as oe:
logger.error("Failed to connect to DB:%s", oe[0])
return json.dumps(data)
#This get a specific record
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment