Пример #1
0
	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']
Пример #2
0
	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']
Пример #3
0
	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']
Пример #4
0
 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
Пример #5
0
 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
Пример #6
0
 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
Пример #7
0
 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
Пример #8
0
 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
Пример #9
0
	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
Пример #10
0
 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
Пример #11
0
 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
Пример #12
0
	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
Пример #13
0
 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
Пример #14
0
 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
Пример #15
0
	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
Пример #16
0
	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
Пример #17
0
 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)
Пример #18
0
	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)
Пример #19
0
	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)