aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.py
blob: 20c8a0daa03745f7f0a9f5c8324421a886b64b76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from src import schema

def ordered_keys(subscriptable_object, *keys):
    """
    returns a tuple with the values for KEYS in the order KEYS are provided,
    from SUBSCRIPTABLE_OBJECT. Useful for working with dictionaries when
    parameter ordering is important. Used for sql transactions.
    """
    return tuple([subscriptable_object[key] for key in keys])


def schema_values(scheme, obj):
    """
    Returns the values in the database order for a given
    schema. Used for sql transactions.
    """
    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",
            "last_mod", "created", "reply_count", "pinned")

    elif scheme == "message":
        return ordered_keys(obj,
            "thread_id", "post_id", "author",
            "created", "edited", "body")
Un proyecto texto-plano.xyz