def getDetailedPost(id, related): cursor = connection.cursor() try: cursor.execute( "SELECT " "posts.date AS date, " "posts.dislikes AS dislikes, " "forums.short_name AS forum, " "posts.id AS id, " "posts.isApproved AS isApproved, " "posts.isDeleted AS isDeleted, " "posts.isEdited AS isEdited, " "posts.isHighlighted AS isHighlighted, " "posts.isSpam AS isSpam, " "posts.likes AS likes, " "posts.message AS message, " "posts.post_id AS parent, " "posts.likes - posts.dislikes AS points, " "posts.thread_id AS thread, " "users.email AS user " "FROM posts RIGHT JOIN threads ON posts.thread_id = threads.id " "RIGHT JOIN forums ON threads.forum_id = forums.id " "JOIN users ON posts.user_id = users.id " "WHERE posts.id = %s """, (id,)) post = dictfetchone(cursor) cursor.close() if not post: raise Exception("Post doesn't exist") if 'user' in related: from technopark_db_api_app.queries import user_queries post['user'] = user_queries.getDetailedUser(post['user']) if 'thread' in related: from technopark_db_api_app.queries import thread_queries post['thread'] = thread_queries.getDetailedThread(post['thread'],[]) if 'forum' in related: from technopark_db_api_app.queries import forum_queries post['forum'] = forum_queries.getDetailedForum(post['forum'], []) post['isApproved'] = bool(post['isApproved']) post['isDeleted'] = bool(post['isDeleted']) post['isEdited'] = bool(post['isEdited']) post['isHighlighted'] = bool(post['isHighlighted']) post['isSpam'] = bool(post['isSpam']) post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S") except Exception: raise else: return post finally: cursor.close()
def update(request): try: parameters = json.loads(request.body, encoding='utf-8') validate_required_parameters(parameters, ['message', 'slug', 'thread']) thread_queries.updateThread(parameters['thread'], parameters['slug'], parameters['message']) thread = thread_queries.getDetailedThread(parameters['thread'], []) response_json = { 'code': 0, 'response': thread, } except Exception as e: response_json = { 'code': 1, 'response': str(e), } return HttpResponse(json.dumps(response_json, ensure_ascii=False), content_type='application/json')
def details(request): try: parameters = request.GET.dict() validate_required_parameters(parameters, ['thread']) validate_optional_parameters(parameters, ['related'], [[]]) thread = thread_queries.getDetailedThread(parameters['thread'], parameters['related']) response_json = { 'code': 0, 'response': thread, } except Exception as e: response_json = { 'code': 1, 'response': str(e), } return HttpResponse(json.dumps(response_json, ensure_ascii=False), content_type='application/json')
def getPostsList(short_name, since, order, limit, related): cursor = connection.cursor() try: query = ( "SELECT " "posts.date AS date, " "posts.dislikes AS dislikes, " "forums.short_name AS forum, " "posts.id AS id, " "posts.isApproved AS isApproved, " "posts.isDeleted AS isDeleted, " "posts.isEdited AS isEdited, " "posts.isHighlighted AS isHighlighted, " "posts.isSpam AS isSpam, " "posts.likes AS likes, " "posts.message AS message, " "posts.post_id as parent, " "posts.likes - posts.dislikes AS points, " "threads.id AS thread, users.email AS user " "FROM posts RIGHT JOIN threads ON posts.thread_id = threads.id " "RIGHT JOIN forums ON threads.forum_id = forums.id " "JOIN users ON posts.user_id = users.id " "WHERE forums.short_name=%r " % (str(short_name))) if since: query += "AND posts.date >= %r " % str(since) if order: query += "ORDER BY posts.date %s " % order if limit: query += "LIMIT %s " % limit cursor.execute(query) posts = dictfetchall(cursor) cursor.close() for post in posts: post['isApproved'] = bool(post['isApproved']) post['isDeleted'] = bool(post['isDeleted']) post['isEdited'] = bool(post['isEdited']) post['isHighlighted'] = bool(post['isHighlighted']) post['isSpam'] = bool(post['isSpam']) post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S") if 'user' in related: for post in posts: from technopark_db_api_app.queries import user_queries post['user'] = user_queries.getDetailedUser(post['user']) if 'thread' in related: for post in posts: from technopark_db_api_app.queries import thread_queries post['thread'] = thread_queries.getDetailedThread(post['thread'], []) if 'forum' in related: for post in posts: post['forum'] = getDetailedForum(post['forum'], []) except Exception: raise else: return posts finally: cursor.close()