gopy.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python3
  2. import flask
  3. from flask import make_response, request, redirect, jsonify, render_template, url_for
  4. from flask_sqlalchemy import SQLAlchemy
  5. from flask_marshmallow import Marshmallow
  6. from flask_migrate import Migrate
  7. import argparse
  8. db = SQLAlchemy()
  9. app = flask.Flask(__name__)
  10. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:m9Eb5OflQb@mysql.mysql.svc.cluster.local/gopy'
  11. db.init_app(app)
  12. migrate = Migrate(app, db)
  13. ma = Marshmallow(app)
  14. class Link(db.Model):
  15. id = db.Column(db.Integer, primary_key=True)
  16. name = db.Column(db.String(255), unique=True)
  17. target = db.Column(db.String(255))
  18. hit_count = db.Column(db.Integer)
  19. owner_name = db.Column(db.String(255))
  20. def __repr__(self):
  21. return '<Link id=(id) name=(name) target=(target)>'.format(
  22. id=self.id, name=self.name, target=self.target, hit_count=self.hit_count, owner_name=self.owner_name
  23. )
  24. class LinkSchema(ma.Schema):
  25. class Meta:
  26. fields = ('id', 'name', 'target', 'hit_count', 'owner_name')
  27. def get_links():
  28. links = Link.query.order_by(Link.name).all()
  29. schema = LinkSchema(many=True)
  30. link_json = schema.dump(links)
  31. return links
  32. def link_exists(link_name):
  33. try:
  34. link = Link.query.filter_by(name=link_name).first()
  35. except Exception as e:
  36. print(e)
  37. return e
  38. if link is None:
  39. return False
  40. else:
  41. return True
  42. @app.route('/<string:name>', strict_slashes=False, methods=['GET'])
  43. def redirect_to_link(name):
  44. try:
  45. link = Link.query.filter_by(name=name).first()
  46. except e:
  47. return redirect("/list", code=302)
  48. if link is None:
  49. return redirect("/list", code=302)
  50. try:
  51. link.hit_count += 1
  52. except TypeError:
  53. link.hit_count = 1
  54. db.session.commit()
  55. print("updating link hit count")
  56. return redirect(link.target, code=302)
  57. @app.post('/add', strict_slashes=False)
  58. def add_link():
  59. if link_exists(request.form['link_name']) is True:
  60. print("Link exists")
  61. else:
  62. print("Creating link")
  63. db.create_all()
  64. link = Link(name=request.form['link_name'], target=request.form['target'], hit_count=0, owner_name="unknown")
  65. db.session.add(link)
  66. db.session.commit()
  67. return redirect("/list", code=302)
  68. @app.route('/<string:link_name>/add', strict_slashes=False)
  69. def add_link_form(link_name):
  70. return render_template('add.html', link_name=link_name)
  71. @app.post('/<string:link_name>/edit', strict_slashes=False)
  72. def edit_link(link_name):
  73. db.create_all()
  74. link = Link.query.filter_by(name=link_name).first()
  75. print("Setting link target to {}".format(request.form['target']))
  76. link.target = request.form['target']
  77. db.session.commit()
  78. return redirect("/list", code=302)
  79. @app.route('/<string:link_name>/edit', strict_slashes=False)
  80. def edit_link_form(link_name):
  81. link = Link.query.filter_by(name=link_name).first()
  82. return render_template('edit.html', link=link)
  83. @app.post('/<int:link_id>/delete', strict_slashes=False)
  84. def delete_link(link_id):
  85. link = Link.query.filter_by(id=link_id).first()
  86. db.session.delete(link)
  87. db.session.commit()
  88. return redirect(url_for('list_links'))
  89. @app.route('/<int:link_id>/delete', strict_slashes=False)
  90. def delete_link_form(link_id):
  91. link = Link.query.filter_by(id=link_id).first()
  92. return render_template('delete.html', link=link)
  93. @app.route('/list')
  94. def list_links():
  95. links = get_links()
  96. return render_template('list.html', links=links)
  97. @app.route('/')
  98. def landing_page():
  99. return redirect("/list", code=302)
  100. #@app.route('/links/<int:id>', methods=['GET'])
  101. if __name__ == "__main__":
  102. parser = argparse.ArgumentParser()
  103. parser.add_argument("admin", help="Make admin functions available", action="store_true")
  104. parser.parse_args()
  105. app.run(host='0.0.0.0')