def get_path(parent): content = None try: with get_db_cursor() as cursor: cursor.execute(GET_PATH_SQL, {'parent': parent}) content = cursor.fetchone() except psycopg2.DatabaseError as e: print('Error %s' % e) return content['path']
def get_id(): content = None try: with get_db_cursor() as cursor: cursor.execute("SELECT nextval('posts_id_seq')") content = cursor.fetchone() except psycopg2.DatabaseError as e: print('Error %s' % e) return content['nextval']
def count(): content = None try: with get_db_cursor() as cursor: cursor.execute("SELECT COUNT(*) FROM posts") content = cursor.fetchone() except psycopg2.DatabaseError as e: print('Error %s' % e) return content['count']
def create(content): code = status_codes['CREATED'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(CREATE_USER_SQL, content) except psycopg2.IntegrityError as e: print('Error %s' % e) code = status_codes['CONFLICT'] with get_db_cursor() as cursor: cursor.execute(GET_USER_SQL, { 'nickname': content['nickname'], 'email': content['email'] }) content = cursor.fetchall() except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] content = None return content, code
def create(content): code = status_codes['CREATED'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(CREATE_FORUM_SQL, content) content = cursor.fetchone() except psycopg2.IntegrityError as e: print('Error %s' % e) code = status_codes['CONFLICT'] with get_db_cursor(commit=True) as cursor: cursor.execute(GET_FORUM_SQL, {'slug': content['slug']}) content = cursor.fetchone() if content is None: code = status_codes['NOT_FOUND'] except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] content = None return content, code
def get(slug): content = None code = status_codes['OK'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(GET_FORUM_SQL, {'slug': slug}) content = cursor.fetchone() if content is None: code = status_codes['NOT_FOUND'] except psycopg2.DatabaseError as e: print('Error %s' % e) return content, code
def update_thread_count(amount, slug): code = status_codes['OK'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(UPDATE_THREADS_COUNT_SQL, { 'amount': amount, 'slug': slug }) except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] return code
def create(content): code = status_codes['CREATED'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(create_thread_sql(content=content), content) content = cursor.fetchone() except psycopg2.IntegrityError as e: print('Error %s' % e) code = status_codes['CONFLICT'] with get_db_cursor(commit=True) as cursor: cursor.execute(get_thread_sql(slug_or_id=content['slug']), {'slug_or_id': content['slug']}) content = cursor.fetchone() except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] content = None if content is None: code = status_codes['NOT_FOUND'] else: content['created'] = format_time(content['created']) return content, code
def create(data, forum): code = status_codes['CREATED'] try: with get_db_cursor(commit=True) as cursor: psycopg2.extras.execute_values(cursor, INSERT_POSTS_SQL, data) cursor.execute(UPDATE_POSTS_ON_FORUM_SQL, {'amount': len(data), 'forum': forum}) except psycopg2.IntegrityError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] return code
def get(nickname): content = None code = status_codes['OK'] try: with get_db_cursor() as cursor: cursor.execute(GET_USER_SQL, { 'nickname': nickname, 'email': None }) content = cursor.fetchone() if content is None: code = status_codes['NOT_FOUND'] except psycopg2.DatabaseError as e: print('Error %s' % e) return content, code
def get_users(slug, limit, since, desc): content = None code = status_codes['OK'] try: with get_db_cursor() as cursor: cursor.execute(get_users_sql(since=since, desc=desc), { 'forum': slug, 'since': since, 'limit': limit }) content = cursor.fetchall() except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] return content, code
def get(identifier): content = None code = status_codes['OK'] try: with get_db_cursor() as cursor: cursor.execute(GET_POST_SQL, {'id': identifier}) content = cursor.fetchone() if content is None: code = status_codes['NOT_FOUND'] else: content['created'] = format_time(content['created']) content['isEdited'] = content['isedited'] del content['isedited'] except psycopg2.DatabaseError as e: print('Error %s' % e) return content, code
def update(content): code = status_codes['OK'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(update_user_sql(content=content), content) content = cursor.fetchone() if content is None: code = status_codes['NOT_FOUND'] except psycopg2.IntegrityError as e: print('Error %s' % e) code = status_codes['CONFLICT'] content = None except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] content = None return content, code
def get(slug_or_id): content = None code = status_codes['OK'] try: with get_db_cursor() as cursor: cursor.execute(get_thread_sql(slug_or_id=slug_or_id), {'slug_or_id': slug_or_id}) content = cursor.fetchone() if content is None: code = status_codes['NOT_FOUND'] except psycopg2.DatabaseError as e: print('Error %s' % e) if content is None: code = status_codes['NOT_FOUND'] else: content['created'] = format_time(content['created']) return content, code
def sort(limit, offset, sort, desc, slug_or_id): content = None code = status_codes['OK'] try: with get_db_cursor() as cursor: params = {'slug_or_id': slug_or_id, 'limit': limit, 'offset': offset} if sort == 'flat': cursor.execute(posts_flat_sort_sql(slug_or_id=slug_or_id, desc=desc), params) elif sort == 'tree': cursor.execute(posts_tree_sort_sql(slug_or_id=slug_or_id, desc=desc), params) elif sort == 'parent_tree': cursor.execute(posts_parent_tree_sort_sql(slug_or_id=slug_or_id, desc=desc), params) content = cursor.fetchall() for param in content: param['created'] = format_time(param['created']) except psycopg2.DatabaseError as e: print('Error %s' % e) return content, code
def update(identifier, content): code = status_codes['OK'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(UPDATE_POST_SQL, {'message': content['message'], 'id': identifier}) content = cursor.fetchone() if content is None: code = status_codes['NOT_FOUND'] else: content['created'] = format_time(content['created']) content['isEdited'] = content['isedited'] del content['isedited'] except psycopg2.IntegrityError as e: print('Error %s' % e) code = status_codes['CONFLICT'] except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] return content, code
def update_votes(content): try: with get_db_cursor(commit=True) as cursor: cursor.execute(UPDATE_VOTES_SQL, content) except psycopg2.DatabaseError as e: print('Error %s' % e)
def set_id(identifier): try: with get_db_cursor() as cursor: cursor.execute("SELECT setval('posts_id_seq', %(id)s, false)", {'id': identifier}) except psycopg2.DatabaseError as e: print('Error %s' % e)
def clear(): try: with get_db_cursor(commit=True) as cursor: cursor.execute("DELETE FROM posts") except psycopg2.DatabaseError as e: print('Error %s' % e)