Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # app.py
  2. from flask import Flask, request, make_response, render_template, jsonify, redirect
  3. from functools import wraps
  4. import json
  5. app = Flask(__name__)
  6.  
  7. fishes_db = {}
  8.  
  9. last_id = 0
  10. def get_new_id():
  11.     global last_id
  12.     last_id += 1
  13.     return f'id_{last_id}'
  14.  
  15. def login_required(f):
  16.     @wraps(f)
  17.     def decorated(*args, **kwargs):
  18.         is_logged = request.cookies.get('logged')
  19.         if is_logged == 'True':
  20.             return f(*args, **kwargs)
  21.         else:
  22.             return make_response(
  23.                 'You are not logged in',
  24.                 401,
  25.                 {'WWW-Authenticate': 'Basic realm="Login Required"'}
  26.             )
  27.  
  28.     return decorated
  29.  
  30. @app.route('/')
  31. def hello_world():
  32.     return f''
  33.  
  34. @app.route('/login', methods=['POST'])
  35. def login():
  36.     auth = request.authorization
  37.     print(auth)
  38.  
  39.     if auth and auth.username == 'Akwarysta69' and auth.password == 'J3si07r':
  40.         resp = redirect('/hello')
  41.         resp.set_cookie('logged', 'True')
  42.         return resp
  43.     else:
  44.         return make_response(
  45.             'Wrong login or password',
  46.             401,
  47.             {}
  48.         )
  49.  
  50. @app.route('/logout', methods=['POST'])
  51. @login_required
  52. def logout():
  53.     resp = make_response('OK', 200, '')
  54.     resp.set_cookie('logged', '', expires=0)
  55.     return resp
  56.  
  57. @app.route('/hello', methods=['GET'])
  58. @login_required
  59. def hello_user():
  60.     return render_template(
  61.         'hello_user.html',
  62.         user='Akwarysta69'
  63.     )
  64.  
  65. @app.route('/fishes', methods=['GET', 'POST'])
  66. @login_required
  67. def fishes_info():
  68.     global fishes_db
  69.  
  70.     if request.method == 'GET':
  71.         return jsonify(fishes_db)
  72.  
  73.     elif request.method == 'POST':
  74.         new_fish = dict(request.get_json())
  75.         fishes_db[get_new_id()] = new_fish
  76.         return 'OK'
  77.  
  78. @app.route('/fishes/<fish_id>', methods=['GET', 'PUT', 'DELETE', 'PATCH'])
  79. @login_required
  80. def fish_info(fish_id):
  81.     global fishes_db
  82.  
  83.     if request.method == 'GET':
  84.         return jsonify(fishes_db[fish_id])
  85.  
  86.     elif request.method == 'PUT':
  87.         new_fish = dict(request.get_json())
  88.         fishes_db[fish_id] = new_fish
  89.         return 'OK'
  90.  
  91.     elif request.method == 'DELETE':
  92.         fishes_db.pop(fish_id)
  93.         return 'OK'
  94.  
  95.     elif request.method == 'PATCH':
  96.         new_values = dict(request.get_json())
  97.         for k, v in new_values.items():
  98.             fishes_db[fish_id][k] = v
  99.         return 'OK'
  100.  
  101.  
  102. if __name__ == '__main__':
  103.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement