Commit c57d9e0b authored by Emanuele Santoro's avatar Emanuele Santoro

Fix problem with database

parent ecd5c6fb
*~
__pycache__
*_ENV.sh
*.pyc
......@@ -11,19 +11,23 @@ musho_url = os.getenv("MUSHO_URL", "http://localhost:%d" % port)
debug = os.getenv("DEBUG", None)
database_url = os.getenv("DATABASE_URL", 'sqlite:////tmp/musho.db')
print("DATABASE_URL:\t\t%s" % database_url + "?sslmode=disable")
print("MUSHO_URL:\t\t%s" % musho_url)
print("PORT:\t\t\t%d" % port)
print("DEBUG:\t\t\t%s" % debug)
musho = Flask(__name__)
musho.config['SQLALCHEMY_DATABASE_URI'] = database_url
musho.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
musho.config['SQLALCHEMY_ECHO'] = False
# musho.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# musho.config['SQLALCHEMY_ECHO'] = True
# musho.config['SQLALCHEMY_POOL_SIZE'] = int(os.getenv("POOL_SIZE", "16"))
db = SQLAlchemy(musho)
db.create_all()
# db.create_all()
class Link(db.Model):
__tablename__ = "links"
id = db.Column(db.Integer, primary_key=True)
short_url = db.Column(db.String(), unique=True)
full_url = db.Column(db.String(), unique=True)
......@@ -53,28 +57,26 @@ def saveLink() :
hash.update(url.encode('ascii','ignore'))
digest = hash.hexdigest()
short_url = digest[0:8]
new_link = Link(short_url, url)
db.session.add(new_link)
db.session.commit()
## check if already present
present = Link.query.filter_by(full_url=url).first()
if present is None :
db.session.add(new_link)
db.session.commit()
full_url = musho_url +"/l/" + short_url
return render_template('shortened_link.html', url=full_url)
@musho.route("/l/<string:link_id>")
def resolveLink(link_id):
@musho.route("/l/<string:short_url>")
def resolveLink(short_url):
if debug:
print("Risolvo il link '%s'" % link_id)
target = Link.query.filter_by(short_url=link_id).first()
print("Risolvo il link '%s'" % short_url)
target = Link.query.filter_by(short_url=short_url).first()
if target is not None :
print("Target is not none")
return redirect(target.full_url)
else :
return redirect("/")
if __name__ == "__main__" :
musho.run(port=port)
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