Provides url converters for flask to support pymonogs ObjectIDs
Project description
flask-objectid-converter
Provides url converters for flask to support pymonogs ObjectIDs
I found the snippet from here by Armin Ronacher but could not find a package for it.
Ideas for extended functionality: See ToDo.
Usage
add the Converter to the flask app
use it in routes ### Add Converter to app: ```python from flask import Flask from objectid_converter import ObjectIDConverter
app = Flask(name) app.url_map.converters[‘objectid’] = ObjectIDConverter ### Use in routespython @app.route(‘/users/’) def get_user(oid): return User.objects.get(id=oid) ```
Throws 404 if the requested value cant be decoded
get route via url_for
from flask import url_for
url_for(get_user, oid=User.id)
Encoding and Decoding
The converter encodes the objectid to base64. For manual encoding and decoding use the itsdangerous library:
import bson
from itsdangerous import base64_encode, base64_decode
# encoding
oid = bson.ObjectId()
base64_encode(oid.binary)
# decoding
value = 'SomeBase64DecodedString'
bson.ObjectId(base64_decode(value))
Testing
python setup.py test
ToDo
Make a proper flask extension with init_app, encode, decode functions, configuration of conversion algorithm (specify alphabet).
base64 is sufficient for me. So i will not touch this right now. If somebody needs this, let me know.
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
Hashes for flask-objectid-converter-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 869e91c13435d25aeeac36b2dea5a5e47835f6094d867cc2d02524b887e5a2b5 |
|
MD5 | 16f0ad8b39abf142069907e1cdcf191a |
|
BLAKE2b-256 | a7a62dd3f172cca1b84a0e24a33b36d8b2d446bdf8feb5366e7829f014745877 |