No project description provided
Project description
Flask-PydanqlAPI
Overview
Flask-PydanqlAPI is a Flask extension that offers seamless integration with the Pydanql library, simplifying interactions with PostgreSQL databases. This extension automates the generation of RESTful API endpoints for your database models, offering optional features like JWT authentication and query filtering.
⚠️ Early Version Warning: This is an early version of the Flask-PydanqlAPI extension and is subject to changes. Although it is fully functional, future versions may introduce breaking changes. Your feedback is highly appreciated!
Features
- Simple CRUD operations through Pydanql models
- Optional JWT Authentication
- Customizable query and return fields
- Optional support for additional filtering on queries
Getting Started
Basic Setup
Here's a minimal example to show how to set up the Flask-PydanqlAPI extension without authentication and custom filtering.
# Easily create a full-fledged API with all CRUD actions. Create, Read, Update
# and Delete. Advanced search options, extensible, and even many more.
#
# GET Books from /books/find?year__range=1950,1960&title__like=Lord
from flask import Flask
from flask_pydanql_api import PydanqlAPI, Endpoint
from pydanql.model import ObjectBaseModel
app = Flask(__name__)
# Define youre Model
class Book(ObjectBaseModel):
title: str
author: str
year: int
# Define youre API-Endpoint
class Books(Endpoint):
slug = 'books'
model = Book
# Connect to your postgreSQL Database
app.config['PYDANQL_API_DB'] = { 'database': ..., 'user': ..., 'password': ... }
app.config['PYDANQL_API_ENDPOINTS'] = [Books]
PydanqlAPI(app)
if __name__ == '__main__':
app.run(debug=True)
Advanced Setup with JWT and Filtering
Here's an example that includes JWT authentication and custom filtering.
from flask import Flask, request, jsonify
from flask_pydanql_api import PydanqlAPI, Endpoint
from pydanql.model import ObjectBaseModel
from pydanql.table import Table
from datetime import datetime
from flask_jwt_extended import create_access_token, verify_jwt_in_request, get_jwt_identity
class Car(ObjectBaseModel):
brand: str
model: str
year: int
color: str
miles: float
owner: str
def miles_per_year(self):
current_year = datetime.now().year
years_since_manufacture = current_year - self.year + 1
return float(self.miles / years_since_manufacture)
def description(self):
return f"A {self.color} {self.model} build by {self.brand}"
class Cars(Endpoint):
slug = 'cars' # part of the url to accesse the table
model = Car # The object for table entries
allowed_query_fields = ['brand', 'color', 'year', 'owner']
visible_fields = ['owner', 'brand', 'color', 'year', 'model', 'slug', 'miles_per_year', 'description']
@staticmethod
def _filter(query_type, query_table):
verify_jwt_in_request()
current_user = get_jwt_identity()
if query_type == 'find':
return {'owner': current_user}
if query_type == 'get':
return {'owner': current_user}
if query_type == 'create':
return {'owner': current_user}
if query_type == 'delete':
return {'owner': current_user}
return jsonify({'error': 'Not todo', 'detail': 'todo'}), 405
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = 'super-secret'
app.config['PYDANQL_API_DB'] = {
'database': 'testdb',
'user': 'testuser',
'password': 'testpass',
'host': 'localhost',
'port': '5432'
}
app.config['PYDANQL_API_ENDPOINTS'] = [Cars]
PydanqlAPI(app)
@app.route('/login', methods=['POST'])
def login():
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)
Contributing
We are open to contributions. Please fork the repository and submit your pull requests!
License
Flask-PydanqlAPI is licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Flask-Pydanql-API-0.0.3a0.tar.gz
.
File metadata
- Download URL: Flask-Pydanql-API-0.0.3a0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1e538cf91dca36a4f56c1efb726e1bee9837a43110856c711b7d3fe85fd127c |
|
MD5 | f77da95fc2e02917ee0d9757a2b38e0c |
|
BLAKE2b-256 | 3d56e6923cbb6a1004dcb6fa3573b1ae13b90c4e9ec1dd70d1da4347bf92d9a6 |
File details
Details for the file Flask_Pydanql_API-0.0.3a0-py3-none-any.whl
.
File metadata
- Download URL: Flask_Pydanql_API-0.0.3a0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31dffbde5e766ec002b90dd3d8f6c246347d928433b4549fe6704c3deab3f258 |
|
MD5 | 31fd914089cf5f38c867debb6416c54f |
|
BLAKE2b-256 | 7ff781e2980c535e62a1d12b7d4a1b4ff85571d2f0956cb9e0fac5941e4114d2 |