bbj/src/utils.py

31 lines
993 B
Python
Raw Normal View History

2017-04-02 07:35:58 +00:00
from src import schema
def ordered_keys(subscriptable_object, *keys):
"""
2017-04-09 12:45:51 +00:00
returns a tuple with the values for KEYS in the order KEYS are provided,
2017-04-02 07:35:58 +00:00
from SUBSCRIPTABLE_OBJECT. Useful for working with dictionaries when
2017-04-09 12:45:51 +00:00
parameter ordering is important. Used for sql transactions.
2017-04-02 07:35:58 +00:00
"""
return tuple([subscriptable_object[key] for key in keys])
def schema_values(scheme, obj):
"""
Returns the values in the database order for a given
2017-04-09 12:45:51 +00:00
schema. Used for sql transactions.
2017-04-02 07:35:58 +00:00
"""
if scheme == "user":
return ordered_keys(obj,
"user_id", "user_name", "auth_hash", "quip",
"bio", "color", "is_admin", "created")
elif scheme == "thread":
return ordered_keys(obj,
"thread_id", "author", "title",
2017-04-08 07:14:45 +00:00
"last_mod", "created", "reply_count", "pinned")
2017-04-02 07:35:58 +00:00
elif scheme == "message":
return ordered_keys(obj,
"thread_id", "post_id", "author",
"created", "edited", "body")