def inject_unit(unit, db_conn): """ """ entity_id = uniqid() version_id = uniqid() r.db('sagefy').table('units').insert({ 'id': version_id, 'created': r.now(), 'modified': r.now(), 'entity_id': entity_id, 'language': 'en', 'name': unit['name'], 'status': 'pending', 'available': True, 'tags': [], 'body': unit['body'], 'require_ids': unit.get('require_ids') or [], }).run(db_conn) return entity_id, version_id
def inject_proposal(kind, entity, user_id, topic_id, version_id, db_conn): proposal_id = uniqid() entity_body = entity.get('body', 'Video') r.db('sagefy').table('posts').insert({ 'id': proposal_id, 'created': r.now(), 'modified': r.now(), 'user_id': user_id, 'topic_id': topic_id, 'kind': 'proposal', 'entity_versions': [{ 'id': version_id, 'kind': 'unit' if kind == 'unit' else 'card', }], 'body': 'Create "%s"' % entity_body, 'name': 'Create "%s"' % entity_body, }).run(db_conn) return proposal_id
def log_in_user(user): """ Log in the given user. """ session_id = uniqid() redis.setex(session_id, 2 * 7 * 24 * 60 * 60, user["id"]) return session_id
def inject_votes(topic_id, proposal_id, db_conn): my_data = { 'created': r.now(), 'modified': r.now(), 'topic_id': topic_id, 'body': 'I agree.', 'kind': 'vote', 'replies_to_id': proposal_id, 'response': True } my_data['id'] = uniqid() my_data['user_id'] = reviewer_a_id r.db('sagefy').table('posts').insert(my_data).run(db_conn) my_data['id'] = uniqid() my_data['user_id'] = reviewer_b_id r.db('sagefy').table('posts').insert(my_data).run(db_conn)
def inject_choice_card(unit_id, card, db_conn): """ """ entity_id = uniqid() version_id = uniqid() r.db('sagefy').table('cards').insert({ 'id': version_id, 'created': r.now(), 'modified': r.now(), 'unit_id': unit_id, 'require_ids': [], 'kind': 'choice', 'body': card['body'], 'options': [{ 'value': option['value'], 'feedback': option['feedback'], 'correct': option['correct'] in ('Y', True), } for option in card['options']], 'order': 'random', 'max_options_to_show': 4, 'entity_id': entity_id, 'language': 'en', 'name': card['body'], 'status': 'pending', 'available': True, 'tags': [] }).run(db_conn) return entity_id, version_id
def log_in_user(user): """ Log in the given user. """ session_id = uniqid() redis.setex( session_id, 2 * 7 * 24 * 60 * 60, user['id'], ) return session_id
def inject_video_card(unit_id, card, db_conn): """ """ entity_id = uniqid() version_id = uniqid() r.db('sagefy').table('cards').insert({ 'id': version_id, 'created': r.now(), 'modified': r.now(), 'unit_id': unit_id, 'require_ids': [], 'kind': 'video', 'site': 'youtube', 'video_id': card['video_id'], 'entity_id': entity_id, 'language': 'en', 'name': 'Primary Video', 'status': 'pending', 'available': True, 'tags': [] }).run(db_conn) return entity_id, version_id
def inject_topic(kind, entity, entity_id, user_id, db_conn): topic_id = uniqid() entity_body = entity.get('body', 'Video') r.db('sagefy').table('topics').insert({ 'id': topic_id, 'created': r.now(), 'modified': r.now(), 'user_id': user_id, 'name': 'Create "%s"' % entity_body, 'entity': { 'id': entity_id, 'kind': 'unit' if kind == 'unit' else 'card' } }).run(db_conn) return topic_id
def get_email_token(user, send_email=True): """ Create an email token for the user to reset their password. """ token = uniqid() redis.setex( 'user_password_token_{id}'.format(id=user['id']), # key 60 * 10, # time bcrypt.encrypt(user['id'] + token) # value ) if send_email: send_mail(subject='Sagefy - Reset Password', recipient=user['email'], body=c('change_password_url').replace( '{url}', '%spassword?id=%s&token=%s' % ('https://sagefy.org/', user['id'], token))) return token
def get_email_token(self, send_email=True): """ Create an email token for the user to reset their password. """ token = uniqid() redis.setex( "user_password_token_{id}".format(id=self["id"]), # key 60 * 10, # time bcrypt.encrypt(self["id"] + token), # value ) if send_email: send_mail( subject="Sagefy - Reset Password", recipient=self["email"], body=c("change_password_url").replace( "{url}", "%spassword?id=%s&token=%s" % ("https://sagefy.org/", self["id"], token) ), ) return token
def get_email_token(user, send_email=True): """ Create an email token for the user to reset their password. """ token = uniqid() redis.setex( 'user_password_token_{id}'.format(id=user['id']), # key 60 * 10, # time bcrypt.encrypt(user['id'] + token) # value ) if send_email: send_mail( subject='Sagefy - Reset Password', recipient=user['email'], body=c('change_password_url').replace( '{url}', '%spassword?id=%s&token=%s' % ('https://sagefy.org/', user['id'], token) ) ) return token
'id': 'eileen', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'name': 'eileen', 'email': '*****@*****.**', 'password': bcrypt.encrypt('example1'), 'settings': { 'email_frequency': 'daily', 'view_sets': 'public', 'view_follows': 'public', } }]).run(db_conn)) for sample_id, unit_data in sample_data['units'].items(): unit_data['entity_id'] = sample_id unit_data['version_id'] = uniqid() r.table('units').insert({ 'id': unit_data['version_id'], 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'entity_id': unit_data['entity_id'], 'previous_id': None, 'language': 'en', 'status': 'accepted', 'available': True, 'tags': [], 'name': unit_data['name'], 'body': unit_data['body'], 'require_ids': unit_data['requires'], }).run(db_conn)
def test_uniqid_length(): # Expect the length of the ID to be 24 assert len(util.uniqid()) is 24
def test_uniqid_charset(): # Expect the ID to only have numbers and letters (mixed case) uid = util.uniqid() for c in uid: assert c in (string.ascii_lowercase + string.ascii_uppercase + string.digits)