-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
83 lines (61 loc) · 2.44 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from flask import Flask, request, jsonify
from flask_pydanql_api import PydanqlAPI, Endpoint
from pydanql.model import ObjectBaseModel
from datetime import datetime
from flask_jwt_extended import create_access_token, verify_jwt_in_request, get_jwt_identity, JWTManager
class Book(ObjectBaseModel):
"""This is a basic Pydanql model for books"""
title: str
author: str
year: int
owner: str
def years_since_published(self) -> int:
"""Custom method to calculate the years since the book is published"""
current_year = datetime.now().year
return current_year - self.year + 1
def description(self) -> str:
"""Custom method that generates a description"""
return f"The Book \"{self.title}\" by {self.author} was published in the year {self.year}."
class Books(Endpoint):
"""Use the endpoint class for advanced configuration"""
# part of the url to accesse the table /<slug>/find?title__like=Lord
slug = 'books'
# The object for table entries
model = Book
# Fields from the model that can be queried
allowed_query_fields = ['title', 'author', 'year']
# Fields that are exposed in the result
visible_fields = ['title', 'author', 'year', 'owner', 'slug']
@staticmethod
def _filter(query_type: str, query_table: str):
verify_jwt_in_request()
if query_type in ['find', 'get', 'create', 'delete', 'update']:
return {'owner': get_jwt_identity()}
app = Flask(__name__)
# Setup JWTManager
app.config['JWT_SECRET_KEY'] = 'super-secret'
JWTManager(app)
# Setup FlaskPydanqlAPI
app.config['PYDANQL_API_DB'] = {
'database': 'testdb',
'user': 'testuser',
'password': 'testpass',
'host': 'localhost',
'port': '5432'
}
app.config['PYDANQL_API_ENDPOINTS'] = [Books]
PydanqlAPI(app)
@app.route('/login', methods=['POST'])
def login():
"""Custom route to handle the login with JWTManager"""
if request.json is None:
return jsonify({"error": "Bad Request", "message": "No JSON payload provided"}), 400
username = request.json.get('username', None)
password = request.json.get('password', None)
# In a real-world app, you'd validate these credentials against a database
if password != 'password':
return jsonify({'login': False}), 401
access_token = create_access_token(identity=username)
return jsonify(access_token=access_token), 200
if __name__ == '__main__':
app.run(debug=True)