Skip to main content

Provides url converters for flask to support pymonogs ObjectIDs

Project description

version license pyversions pipeline status coverage

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-objectid-converter-1.0.0.tar.gz (3.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page