def insert_subject_version(db_conn, current_data, next_data): """ Create a new version of an existing subject. """ schema = subject_schema query = """ INSERT INTO subjects ( entity_id , previous_id , name , user_id , body , members ) VALUES (%(entity_id)s, %(previous_id)s, %(name)s, %(user_id)s, %(body)s, %(members)s) RETURNING *; """ data = { 'entity_id': current_data['entity_id'], 'previous_id': current_data['version_id'], 'user_id': convert_slug_to_uuid(next_data['user_id']), 'name': next_data.get('name') or current_data.get('name'), 'body': next_data.get('body') or current_data.get('body'), 'members': (next_data.get('members') or current_data.get('members') or []), } errors = is_valid_members(db_conn, data) + ensure_no_cycles(db_conn, data) if errors: return None, errors data, errors = insert_row(db_conn, schema, query, data) if not errors: save_entity_to_es('subject', deliver_subject(data, access='view')) return data, errors
def insert_subject(db_conn, data): """ Create a new version of a new a subject, saving to ES. """ schema = subject_schema query = """ INSERT INTO subjects_entity_id (entity_id) VALUES (%(entity_id)s); INSERT INTO subjects ( entity_id , name , user_id , body , members ) VALUES (%(entity_id)s, %(name)s, %(user_id)s, %(body)s, %(members)s) RETURNING *; """ data = { 'entity_id': uuid.uuid4(), 'name': data['name'], 'user_id': convert_slug_to_uuid(data['user_id']), 'body': data['body'], 'members': data.get('members', []), } errors = is_valid_members(db_conn, data) + ensure_no_cycles(db_conn, data) if errors: return None, errors data, errors = insert_row(db_conn, schema, query, data) if not errors: save_entity_to_es('subject', deliver_subject(data, access='view')) return data, errors
def insert_vote(db_conn, data): """ Create a new vote. """ schema = vote_schema query = """ INSERT INTO posts ( user_id , topic_id , kind , body , replies_to_id , response ) VALUES (%(user_id)s, %(topic_id)s, %(kind)s, %(body)s, %(replies_to_id)s, %(response)s) RETURNING *; """ data = { 'user_id': convert_slug_to_uuid(data['user_id']), 'topic_id': convert_slug_to_uuid(data['topic_id']), 'kind': 'vote', 'body': data.get('body'), 'replies_to_id': data.get('replies_to_id'), 'response': data['response'], } errors = is_valid_reply(db_conn, data) if errors: return None, errors errors = is_valid_reply_kind(db_conn, data) if errors: return None, errors data, errors = insert_row(db_conn, schema, query, data) if not errors: add_post_to_es(db_conn, data) return data, errors
def insert_user(db_conn, data): """ Save the user to the database. """ schema = user_schema query = """ INSERT INTO users ( name , email , password , settings ) VALUES (%(name)s, %(email)s, %(password)s, %(settings)s) RETURNING *; """ data = { 'name': data.get('name', '').lower().strip(), 'email': data.get('email', '').lower().strip(), 'password': data.get('password', ''), 'settings': { 'email_frequency': 'daily', 'view_subjects': 'private', 'view_follows': 'private', }, } data, errors = insert_row(db_conn, schema, query, data) if not errors: add_user_to_es(data) send_mail( subject='Welcome to Sagefy', recipient=data['email'], body=WELCOME_TEXT, ) return data, errors
def insert_proposal(db_conn, data): """ Create a new proposal. """ schema = proposal_schema query = """ INSERT INTO posts ( user_id , topic_id , kind , body , replies_to_id , entity_versions ) VALUES (%(user_id)s, %(topic_id)s, %(kind)s, %(body)s, %(replies_to_id)s, %(entity_versions)s) RETURNING *; """ data = { 'user_id': convert_slug_to_uuid(data['user_id']), 'topic_id': convert_slug_to_uuid(data['topic_id']), 'kind': 'proposal', 'body': data.get('body'), 'replies_to_id': data.get('replies_to_id'), 'entity_versions': data['entity_versions'], } errors = is_valid_reply(db_conn, data) if errors: return None, errors errors = validate_entity_versions(db_conn, data) if errors: return None, errors data, errors = insert_row(db_conn, schema, query, data) if not errors: add_post_to_es(db_conn, data) return data, errors
def insert_notice(db_conn, data): """ Create a new notice. """ schema = notice_schema query = """ INSERT INTO notices ( user_id , kind , data ) VALUES (%(user_id)s, %(kind)s, %(data)s) RETURNING *; """ data = pick(data, ('user_id', 'kind', 'data')) data, errors = insert_row(db_conn, schema, query, data) return data, errors
def test_insert_row_err(db_conn): query = """ INSERT INTO users ( name , email , password , settings ) VALUES (%(name)s, %(email)s, %(password)s, %(settings)s) RETURNING *; """ data = { 'name': 'Hi', 'email': '*****@*****.**', 'password': '******', 'settings': {}, } result, errors = insert_row(db_conn, user_schema, query, data) assert errors assert not result
def insert_user_subject(db_conn, user_id, subject_id): """ Add a new user subjects entry to the database. """ schema = user_subjects_schema query = """ INSERT INTO users_subjects ( user_id , subject_id ) VALUES (%(user_id)s, %(subject_id)s) RETURNING *; """ data = { 'user_id': convert_slug_to_uuid(user_id), 'subject_id': convert_slug_to_uuid(subject_id), } data, errors = insert_row(db_conn, schema, query, data) return data, errors
def insert_response(db_conn, data): """ Create a new response. """ schema = response_schema query = """ INSERT INTO responses ( user_id , card_id , unit_id , response , score , learned ) VALUES (%(user_id)s, %(card_id)s, %(unit_id)s, %(response)s, %(score)s, %(learned)s) RETURNING *; """ data = pick( data, ('user_id', 'card_id', 'unit_id', 'response', 'score', 'learned')) return insert_row(db_conn, schema, query, data)
def insert_topic(db_conn, data): """ Create a new topic. """ schema = topic_schema query = """ INSERT INTO topics ( user_id , entity_id , entity_kind , name ) VALUES (%(user_id)s, %(entity_id)s, %(entity_kind)s, %(name)s) RETURNING *; """ data = pick(data, ('user_id', 'entity_id', 'entity_kind', 'name')) if data.get('entity_id'): data['entity_id'] = convert_slug_to_uuid(data['entity_id']) data, errors = insert_row(db_conn, schema, query, data) if not errors: add_topic_to_es(data) return data, errors
def insert_card_parameters(db_conn, data): """ Insert Card Parameters [hidden] """ schema = card_parameters_schema query = """ INSERT INTO cards_parameters ( entity_id , guess_distribution , slip_distribution ) VALUES (%(entity_id)s, %(guess_distribution)s, %(slip_distribution)s) RETURNING *; """ data = pick(data, ( 'entity_id', 'guess_distribution', 'slip_distribution', )) data, errors = insert_row(db_conn, schema, query, data) return data, errors
def insert_unit_version(db_conn, current_data, next_data): """ Create a new version of a existing unit, saving to ES. """ schema = unit_schema query = """ INSERT INTO units ( entity_id , previous_id , name , user_id , body , require_ids ) VALUES (%(entity_id)s, %(previous_id)s, %(name)s, %(user_id)s, %(body)s, %(require_ids)s) RETURNING *; """ data = { 'entity_id': current_data['entity_id'], 'previous_id': current_data['version_id'], 'user_id': convert_slug_to_uuid(next_data['user_id']), 'name': next_data.get('name') or current_data.get('name'), 'body': next_data.get('body') or current_data.get('body'), 'require_ids': [ convert_slug_to_uuid(require_id) for require_id in next_data.get('require_ids') or current_data.get('require_ids') or [] ], } errors = ensure_requires(db_conn, data) + ensure_no_cycles(db_conn, data) if errors: return None, errors data, errors = insert_row(db_conn, schema, query, data) if not errors: save_entity_to_es('unit', deliver_unit(data, access='view')) return data, errors
def insert_unit(db_conn, data): """ Create a new version of a new unit, saving to ES. """ schema = unit_schema query = """ INSERT INTO units_entity_id (entity_id) VALUES (%(entity_id)s); INSERT INTO units ( entity_id , name , user_id , body , require_ids ) VALUES (%(entity_id)s , %(name)s, %(user_id)s, %(body)s, %(require_ids)s) RETURNING *; """ data = { 'entity_id': uuid.uuid4(), 'name': data['name'], 'user_id': convert_slug_to_uuid(data['user_id']), 'body': data['body'], 'require_ids': [ convert_slug_to_uuid(require_id) for require_id in data.get('require_ids', []) ], } errors = ensure_requires(db_conn, data) + ensure_no_cycles(db_conn, data) if errors: return None, errors data, errors = insert_row(db_conn, schema, query, data) if not errors: save_entity_to_es('unit', deliver_unit(data, access='view')) return data, errors
def insert_follow(db_conn, data): """ Create a new follow (user <-> entity). """ already_has = get_follow(db_conn, convert_slug_to_uuid(data.get('user_id')), convert_slug_to_uuid(data.get('entity_id'))) if already_has: return {}, [{ 'name': 'entity_id', 'message': 'You already followed this entity.', 'ref': 'EqvXy6p0TneEnB1qD__fdg', }] schema = follow_schema query = """ INSERT INTO follows ( user_id , entity_id , entity_kind ) VALUES (%(user_id)s, %(entity_id)s, %(entity_kind)s) RETURNING *; """ data = pick(data, ('user_id', 'entity_id', 'entity_kind')) data = { 'user_id': convert_slug_to_uuid(data.get('user_id')), 'entity_id': convert_slug_to_uuid(data.get('entity_id')), 'entity_kind': data.get('entity_kind'), } errors = is_valid_entity(db_conn, data) if errors: return None, [{ 'name': 'entity_id', 'message': 'invalid entity', 'ref': 'ph-XrElITuyixCzqu_OTTA', }] data, errors = insert_row(db_conn, schema, query, data) return data, errors