gopy.py 3.5 KB

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