def create_posts(slug_or_id): posts = ujson.loads(request.data) if not posts: return ujsonify(None), status_codes['NOT_FOUND'] thread, code = thread_db.get(slug_or_id=slug_or_id) if code == status_codes['NOT_FOUND']: return ujsonify(None), code forum, code = forum_db.get(slug=thread['forum']) if code == status_codes['NOT_FOUND']: return ujsonify(None), code created = format_time(datetime.now()) data = [] for post in posts: post_id = posts_db.get_id() if 'parent' not in post: data.append( (post['author'], created, forum['slug'], post_id, post['message'], 0, thread['id'], [post_id], post_id)) else: parent, code = posts_db.get(post['parent']) if code == status_codes['NOT_FOUND'] or thread['id'] != parent['thread']: return ujsonify(None), status_codes['CONFLICT'] path = posts_db.get_path(parent=post['parent']) path.append(post_id) data.append( (post['author'], created, forum['slug'], post_id, post['message'], post['parent'], thread['id'], path, path[0])) post['created'] = created post['forum'] = forum['slug'] post['id'] = post_id post['thread'] = thread['id'] code = posts_db.create(data=data, forum=thread['forum']) if code == status_codes['CREATED']: return ujsonify(posts), code return ujsonify(None), 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 get_threads(slug, limit, since, desc): content = None code = status_codes['OK'] try: with get_db_cursor() as cursor: cursor.execute(get_threads_sql(since=since, desc=desc), { 'forum': slug, 'since': since, 'limit': limit }) content = cursor.fetchall() for param in content: param['created'] = format_time(param['created']) except psycopg2.DatabaseError as e: print('Error %s' % e) code = status_codes['NOT_FOUND'] 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(content): code = status_codes['OK'] try: with get_db_cursor(commit=True) as cursor: cursor.execute(update_thread_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 if content is not None: content['created'] = format_time(content['created']) return content, 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