Created
October 26, 2013 16:12
-
-
Save yareally/7171286 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from redis import StrictRedis | |
from models.user import User | |
import scrypt | |
class UserHelper(object): | |
""" | |
Handles user queries to redis | |
@param redis_connection: | |
@type redis_connection: StrictRedis | |
@param user_id: if not empty, assumed the user exists already | |
@type user_id: int | |
""" | |
_NEXT_USER_KEY = 'next.user.id' | |
# used to reference a key+value belonging to a specific user | |
# order of the format is (user_id, key_name) | |
_USER_ID_STRING_KEY = 'user:%s:%s' | |
def __init__(self, redis_connection, user_id=None): | |
self.redis = redis_connection | |
# get the next unused id for the user to be inserted | |
if not user_id: | |
self.user_id = self._next_user_id() | |
else: | |
self.user_id = user_id | |
def add_user(self, twit_user): | |
""" | |
@param twit_user: | |
@type twit_user: User | |
""" | |
twit_user.id = self.user_id | |
trans = self.redis.pipeline() | |
for key, value in twit_user: | |
if value == twit_user.password: | |
value = self.hash_password(twit_user.password) | |
trans.set(self._set_user_string(key), value) | |
self.redis.transaction(trans) | |
def get_user_by_id(self, user_id=None): | |
""" | |
@param user_id: | |
""" | |
if not user_id: | |
user_id = self.user_id | |
if not self.user_id_exists(user_id): | |
return None | |
user = User() | |
for key, value in user: | |
user[key] = self.redis.get(self._set_user_string(key, user_id)) | |
return user | |
def user_id_exists(self, user_id=None): | |
""" | |
@param user_id: | |
@type user_id: int | |
""" | |
if not user_id: | |
user_id = self.user_id | |
return self.redis.get(self._set_user_string(User.id.__name__, user_id)) | |
def email_exists(self, email): | |
""" | |
@param email: | |
@type email: str | |
""" | |
return self._user_property_exists(User.email, email) | |
def username_exists(self, username): | |
""" | |
@param username: | |
@type username: str | |
""" | |
return self._user_property_exists(User.username, username) | |
def _user_property_exists(self, redis_index, user_property): | |
""" | |
@param redis_index: Name of the indexed property to check against in redis | |
@type redis_index: property | |
@param user_property: variable to check for existence in the db | |
@type user_property: str | |
""" | |
return self.redis.get(self._set_user_string(redis_index.__name__, user_property)) | |
def _set_user_string(self, key, user_id=None): | |
if not user_id: | |
user_id = self.user_id | |
return self._USER_ID_STRING_KEY % (user_id, key) | |
def _next_user_id(self): | |
return self.redis.incr(self._NEXT_USER_KEY) | |
@staticmethod | |
def hash_password(password, salt=''): | |
""" | |
@param password: | |
@param salt: | |
@return: | |
""" | |
if not salt: | |
salt = os.urandom(24).encode('base_64') | |
return scrypt.hash(password, salt) | |
class MessageHelper(object): | |
""" | |
Handles message queries to redis | |
""" | |
_USER_MSG_STRING_KEY = 'user:%d:%s' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment