flask-dynamo icon indicating copy to clipboard operation
flask-dynamo copied to clipboard

Bug in documentation

Open jldiaz opened this issue 5 years ago • 8 comments

The example in the documentation contains this:

app.config['DYNAMO_TABLES'] = [
    {
         TableName='users',
         KeySchema=[dict(AttributeName='username', KeyType='HASH')],
         AttributeDefinitions=[dict(AttributeName='username', AttributeType='S')],
         ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
    }, {
         TableName='groups',
         KeySchema=[dict(AttributeName='name', KeyType='HASH')],
         AttributeDefinitions=[dict(AttributeName='name', AttributeType='S')],
         ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
    }
 ]

Which is invalid syntax, because the keys in the dict are not quoted. A quick fix is to change the braces to dict( ... )

Also, the example endpoint:

@app.route('/create_user')
def create_user():
    dynamo.tables['users'].put_item(data={
        'username': 'rdegges',
        'first_name': 'Randall',
        'last_name': 'Degges',
        'email': '[email protected]',
    })

...does not work, because the parameter for put_item() should be Item instead of data, and the view should return some valid response instead of None, for example return "", or better jsonify the value returned by put_item().

Although simple to fix, I find these issues worrying, because they are at least 2 years old, and apparently nobody noticed them yet. Is the library maintained?

jldiaz avatar Apr 18 '19 23:04 jldiaz

@jldiaz can you show the right code?

edusantana avatar May 18 '19 20:05 edusantana

@edusantana Yes, of course:

# app.py

from flask import Flask, jsonify
from flask_dynamo import Dynamo

app = Flask(__name__)
app.config['DYNAMO_TABLES'] = [
    dict(
         TableName='users',
         KeySchema=[dict(AttributeName='username', KeyType='HASH')],
         AttributeDefinitions=[dict(AttributeName='username', AttributeType='S')],
         ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
    ), 
   dict(
         TableName='groups',
         KeySchema=[dict(AttributeName='name', KeyType='HASH')],
         AttributeDefinitions=[dict(AttributeName='name', AttributeType='S')],
         ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
    )
 ]

# ...

@app.route('/create_user')
def create_user():
    r = dynamo.tables['users'].put_item(Item={
        'username': 'rdegges',
        'first_name': 'Randall',
        'last_name': 'Degges',
        'email': '[email protected]',
    })
   return jsonify(r)

jldiaz avatar May 28 '19 15:05 jldiaz

@jldiaz Just looking today, I was wondering the same. That said, thanks for sharing your implementation.

ghukill avatar Jul 31 '19 20:07 ghukill

So strange that this hasn't been merged into the docs.

siran avatar Sep 27 '19 15:09 siran

C'mon @rdegges , fix the doc 🔨

rhutkovich avatar Oct 07 '19 12:10 rhutkovich

⚠️ For app factory approach users there is another typo in the doc: Flask.current_app.extensions['dynamodb'] should be flask.current_app.extensions['dynamo']

rhutkovich avatar Oct 07 '19 13:10 rhutkovich

I've opened up a PR for this here: https://github.com/rdegges/flask-dynamo/pull/44

joshuaj004 avatar Mar 12 '20 17:03 joshuaj004

+1. Bumped into this now and updated docs would have helped. Also the put_item needs to be Item= app.extensions['dynamo'].tables['table_name'].put_item(Item=data)

theonlysinjin avatar Jun 25 '22 08:06 theonlysinjin