aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility/redis_tools.py
blob: 0754e16fb12efb79b4bfbe4b8cf3a6955908e21a (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import print_function, division, absolute_import

import simplejson as json

import redis # used for collections
Redis = redis.StrictRedis()

import logging

from flask import (render_template, flash)

from utility.logger import getLogger
logger = getLogger(__name__)

def is_redis_available():
    try:
        Redis.ping()
    except:
        return False
    return True

def get_user_id(column_name, column_value):
    user_list = Redis.hgetall("users")
    for key in user_list:
        user_ob = json.loads(user_list[key])
        if column_name in user_ob and user_ob[column_name] == column_value:
            return key

    return None

def get_user_by_unique_column(column_name, column_value):
    item_details = None

    user_list = Redis.hgetall("users")
    if column_name != "user_id":
        for key in user_list:
            user_ob = json.loads(user_list[key])
            if column_name in user_ob and user_ob[column_name] == column_value:
                item_details = user_ob
    else:
        item_details = json.loads(user_list[column_value])

    return item_details

def set_user_attribute(user_id, column_name, column_value):
    user_info = json.loads(Redis.hget("users", user_id))
    user_info[column_name] = column_value

    Redis.hset("users", user_id, json.dumps(user_info))

def get_user_collections(user_id):
    collections = None
    collections = Redis.hget("collections", user_id)

    if collections:
        return json.loads(collections)
    else:
        return []

def save_user(user, user_id):
    Redis.hset("users", user_id, json.dumps(user))

def save_collections(user_id, collections_ob):
    Redis.hset("collections", user_id, collections_ob)

def save_verification_code(user_email, code):
    Redis.hset("verification_codes", code, user_email)

def check_verification_code(code):
    email_address = None
    user_details = None
    email_address = Redis.hget("verification_codes", code)
    return email_address

    if email_address:
        user_details = get_user_by_unique_column('email_address', email_address)
        return user_details
    else:
        return None
        flash("Invalid code: Password reset code does not exist or might have expired!", "error")