Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

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.

Files for flask-objectid-converter, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size flask-objectid-converter-1.0.0.tar.gz (3.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page