def forum_details(request): response = {} if not request.method == "GET": return JsonResponse({"code": 2, "response": "Method in not supported"}) if "forum" not in request.GET: return JsonResponse({"code": 3, "response": "Missing field"}) forum = request.GET.get("forum") cursor = connection.cursor() forum_data = get_forum_by_shortname(cursor, forum) cursor.close() if not forum_data: return JsonResponse({"code": 1, "response": "Forum does not exist"}) user_data = get_user_by_id(forum_data[3]) if "related" in request.GET: if request.GET["related"] != "user": return JsonResponse({"code": 3, "response": "Wrong related parameter"}) followers, following = get_follow_data(user_data[0]) subs = get_subscriptions(user_data[0]) user_info = { "username": user_data[1], "email": user_data[2], "name": user_data[3], "about": user_data[4], "isAnonymous": user_data[5], "id": user_data[0], "followers": [f[0] for f in followers], "following": [f[0] for f in following], "subscriptions": [s[0] for s in subs], } else: user_info = user_data[2] response = {"id": forum_data[0], "name": forum_data[1], "short_name": forum_data[4], "user": user_info} return JsonResponse({"code": 0, "response": response})
def thread_create(request): response = {} if not request.method == 'POST': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) try: request_params = json.loads(request.body) forum = request_params.get('forum', None) title = request_params.get('title', None) user = request_params.get('user', None) message = request_params.get('message', None) slug = request_params.get('slug', None) date = request_params.get('date', None) is_deleted = request_params.get('isDeleted', False) if type(is_deleted) is not bool: return JsonResponse({ 'code': 3, 'response': 'Wrong isDeleted parameter type' }) if not 'isClosed' in request_params or not (forum and title and user and message and slug and date): return JsonResponse({ 'code': 3, 'response': 'Missing field' }) is_closed = request_params.get('isClosed') if type(is_closed) is not bool: return JsonResponse({ 'code': 3, 'response': 'Wrong isClosed parameter type' }) try: datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') except ValueError: return JsonResponse({ 'code': 3, 'response': 'Wrong date format' }) cursor = connection.cursor() user_data = get_user_by_email(cursor, user) if not user_data: return JsonResponse({ 'code': 1, 'response': 'User not found' }) forum_data = get_forum_by_shortname(cursor, forum) if not forum_data: return JsonResponse({ 'code': 1, 'response': 'Forum not found' }) sql_select = "SELECT * FROM thread WHERE title = %s AND forum_id = %s" cursor.execute(sql_select, (title, forum_data[0])) sql_response = cursor.fetchone() if sql_response: response = { 'title': title, 'forum': forum_data[4], 'message': sql_response[6], 'slug': sql_response[7], 'isClosed': bool(sql_response[3]), 'isDeleted': bool(sql_response[8]), 'id': sql_response[0], 'date': sql_response[5].strftime('%Y-%m-%d %H:%M:%S'), 'user': get_user_by_id(sql_response[4])[2] } return JsonResponse({'code': 0, 'response': response}) sql_insert = "INSERT INTO thread VALUES (null, %s, %s, %s, %s, %s, %s, %s, %s, 0, 0, 0, 0)" sql_insert_data = (forum_data[0], title, is_closed, user_data[0], date, message, slug, is_deleted) cursor.execute(sql_insert, sql_insert_data) response.update({ 'title': title, 'forum': forum_data[4], 'message': message, 'slug': slug, 'isClosed': is_closed, 'isDeleted': is_deleted, 'id': cursor.lastrowid, 'date': date, 'user': user_data[2] }) except ValueError: return JsonResponse({ 'code': 3, 'response': 'No JSON object could be decoded' }) return JsonResponse({ 'code': 0, 'response': response })
def thread_list(request): response = [] if not request.method == 'GET': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) if not ('forum' in request.GET or 'user' in request.GET): return JsonResponse({ 'code': 3, 'response': 'Missing field' }) if 'forum' in request.GET and 'user' in request.GET: return JsonResponse({ 'code': 3, 'response': 'Provide only forum or user' }) if "since" in request.GET: since = request.GET['since'] try: since = datetime.datetime.strptime(since, '%Y-%m-%d %H:%M:%S') except ValueError: return JsonResponse({ 'code': 3, 'response': 'Since id param is wrong' }) else: since = 0 if "limit" in request.GET: limit = request.GET['limit'] try: limit = int(limit) except ValueError: return JsonResponse({ 'code': 3, 'response': 'Limit param is wrong' }) else: limit = None if "order" in request.GET: order = request.GET['order'] if order != 'asc' and order != 'desc': return JsonResponse({ 'code': 3, 'response': 'Order param is wrong' }) else: order = 'desc' by_forum = 'forum' in request.GET cursor = connection.cursor() if by_forum: forum = request.GET.get('forum') forum_data = get_forum_by_shortname(cursor, forum) if not forum_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'Forum not found' }) search_by = forum_data[0] else: user_email = request.GET.get('user') user_data = get_user_by_email(cursor, user_email) if not user_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'Thread not found' }) search_by = user_data[0] sql = "SELECT * FROM thread WHERE date>=%s AND " sql += " forum_id = %s" if by_forum else " user_id = %s" sql += " ORDER BY date " sql += order if limit: sql += " LIMIT %s" cursor.execute(sql, (since, search_by, limit)) else: cursor.execute(sql, (since, search_by)) data = cursor.fetchall() for t in data: response.append({ 'user': get_user_by_id(t[4])[2] if by_forum else user_data[2], 'forum': forum_data[4] if by_forum else get_forum_by_id(t[1])[4], 'id': t[0], 'title': t[2], 'isClosed': bool(t[3]), 'date': t[5].strftime('%Y-%m-%d %H:%M:%S'), 'message': t[6], 'slug': t[7], 'isDeleted': bool(t[8]), 'posts': t[12], 'likes': t[10], 'dislikes': t[9], 'points': t[11], }) return JsonResponse({ 'code': 0, 'response': response })
def post_list(request): response = [] if not request.method == 'GET': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) if not ('forum' in request.GET or 'thread' in request.GET): return JsonResponse({ 'code': 3, 'response': 'Missing field' }) if 'forum' in request.GET and 'thread' in request.GET: return JsonResponse({ 'code': 3, 'response': 'Provide only forum or thread' }) if "since" in request.GET: since = request.GET['since'] try: since = datetime.datetime.strptime(since, '%Y-%m-%d %H:%M:%S') except ValueError: return JsonResponse({ 'code': 3, 'response': 'Since id param is wrong' }) else: since = 0 if "limit" in request.GET: limit = request.GET['limit'] try: limit = int(limit) except ValueError: return JsonResponse({ 'code': 3, 'response': 'Limit param is wrong' }) else: limit = None if "order" in request.GET: order = request.GET['order'] if order != 'asc' and order != 'desc': return JsonResponse({ 'code': 3, 'response': 'Order param is wrong' }) else: order = 'desc' by_forum = 'forum' in request.GET search_by = None cursor = connection.cursor() if by_forum: forum = request.GET.get('forum') forum_data = get_forum_by_shortname(cursor, forum) if not forum_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'Forum not found' }) search_by = forum_data[0] else: thread_id = request.GET.get('thread') thread_data = get_thread_by_id(thread_id) if not thread_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'Thread not found' }) search_by = thread_id sql = "SELECT * FROM post WHERE date>=%s AND " sql += " forum_id = %s" if by_forum else " thread_id = %s" sql += " ORDER BY date " sql += order if limit: sql += " LIMIT %s" cursor.execute(sql, (since, search_by, limit)) else: cursor.execute(sql, (since, search_by)) data = cursor.fetchall() forum_cache = {} user_cache = {} for p in data: response.append({ 'id': int(p[0]), 'forum': forum_data[4] if by_forum else forum_cache.setdefault(p[1], get_forum_by_id(p[1])[4]), 'thread': p[2], 'user': user_cache.setdefault(p[3], get_user_by_id(p[3])[2]), 'message': p[4], 'date': p[5].strftime('%Y-%m-%d %H:%M:%S'), 'parent': p[6], 'isApproved': bool(p[8]), 'isHighlighted': bool(p[9]), 'isSpam': bool(p[10]), 'isEdited': bool(p[11]), 'isDeleted': bool(p[12]), 'likes': p[13], 'dislikes': p[14], 'points': p[15], }) return JsonResponse({ 'code': 0, 'response': response })
def post_create(request): response = {} if not request.method == 'POST': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) try: request_params = json.loads(request.body) except ValueError: return JsonResponse({ 'code': 3, 'response': 'No JSON object could be decoded' }) if not ('date' in request_params and 'thread' in request_params and 'message' in request_params and 'user' in request_params and 'forum' in request_params): return JsonResponse({ 'code': 3, 'response': 'Missing field' }) date = request_params.get('date') thread_id = request_params.get('thread') message = request_params.get('message') user_email = request_params.get('user') forum_name = request_params.get('forum') cursor = connection.cursor() # Optional params: if 'isApproved' in request_params: is_approved = request_params.get('isApproved') if type(is_approved) is not bool: cursor.close() return JsonResponse({ 'code': 3, 'response': 'Wrong isApproved parameter type' }) else: is_approved = False if 'isHighlighted' in request_params: is_highlited = request_params.get('isHighlighted') if type(is_highlited) is not bool: cursor.close() return JsonResponse({ 'code': 3, 'response': 'Wrong isHighlited parameter type' }) else: is_highlited = False if 'isEdited' in request_params: is_edited = request_params.get('isEdited') if type(is_edited) is not bool: cursor.close() return JsonResponse({ 'code': 3, 'response': 'Wrong isEdited parameter type' }) else: is_edited = False if 'isSpam' in request_params: is_spam = request_params.get('isSpam') if type(is_spam) is not bool: cursor.close() return JsonResponse({ 'code': 3, 'response': 'Wrong isSpam parameter type' }) else: is_spam = False if 'isDeleted' in request_params: is_deleted = request_params.get('isDeleted') if type(is_deleted) is not bool: cursor.close() return JsonResponse({ 'code': 3, 'response': 'Wrong isDeleted parameter type' }) else: is_deleted = False if 'parent' in request_params: parent = request_params.get('parent') else: parent = None forum_data = get_forum_by_shortname(cursor, forum_name) if not forum_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'Forum does not exist' }) cursor.execute("SELECT id, posts FROM thread WHERE id=%s AND forum_id=%s", (thread_id, forum_data[0])) thread_data = cursor.fetchone() if not thread_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'Thread does not exist or no such thread in provided forum' }) user_data = get_user_by_email(cursor, user_email) if not user_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'User does not exist' }) if not parent: posts_count = int(thread_data[1]) post_path = '{0:011d}'.format(posts_count + 1) else: try: parent = int(parent) except ValueError: cursor.close() return JsonResponse({ 'code': 3, 'response': 'Wrong parent parameter type' }) cursor.execute("SELECT path FROM post WHERE id = %s AND thread_id = %s", (parent,thread_id)) parent_post_data = cursor.fetchone() if not parent_post_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'Post not found or post required is in another thread' }) cursor.execute("SELECT COUNT(*) FROM post WHERE parent = %s", (parent,)) parent_childs = int(cursor.fetchone()[0]) post_path = parent_post_data[0] + '.' + '{0:011d}'.format(parent_childs + 1) sql = "INSERT INTO post VALUES (null, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 0, 0, 0)" insert_data = (forum_data[0], thread_data[0], user_data[0], message, date, parent, post_path, is_approved, is_highlited, is_spam, is_edited, is_deleted) cursor.execute(sql, insert_data) response.update({ 'id': cursor.lastrowid, 'forum': forum_name, 'thread': thread_id, 'user': user_email, 'message': message, 'parent': parent, 'isSpam': is_spam, 'isHighlighted': is_highlited, 'isEdited': is_edited, 'isApproved': is_approved, 'date': date, }) cursor.execute("UPDATE thread SET posts = posts+1 WHERE id = %s", (thread_id,)) return JsonResponse({ 'code': 0, 'response': response, })
def forum_list_posts(request): response = [] if not request.method == "GET": return JsonResponse({"code": 2, "response": "Method in not supported"}) if "forum" not in request.GET: return JsonResponse({"code": 3, "response": "Missing field"}) forum = request.GET.get("forum") cursor = connection.cursor() forum_data = get_forum_by_shortname(cursor, forum) if not forum_data: cursor.close() return JsonResponse({"code": 1, "response": "Forum not found"}) if "since" in request.GET: since = request.GET["since"] try: since = datetime.datetime.strptime(since, "%Y-%m-%d %H:%M:%S") except ValueError: cursor.close() return JsonResponse({"code": 3, "response": "Since id param is wrong"}) else: since = 0 if "limit" in request.GET: limit = request.GET["limit"] try: limit = int(limit) except ValueError: cursor.close() return JsonResponse({"code": 3, "response": "Limit param is wrong"}) else: limit = None if "order" in request.GET: order = request.GET["order"] if order != "asc" and order != "desc": cursor.close() return JsonResponse({"code": 3, "response": "Order param is wrong"}) else: order = "desc" user_related = False forum_related = False thread_related = False if "related" in request.GET: related = request.GET.getlist("related") for r in related: if r != "forum" and r != "user" and r != "thread": cursor.close() return JsonResponse({"code": 3, "response": "Wrong related params"}) if "forum" in related: forum_related = True if "thread" in related: thread_related = True if "user" in related: user_related = True sql = "SELECT * FROM post WHERE forum_id = %s AND date>=%s ORDER BY date " sql += order if limit: sql += " LIMIT %s" cursor.execute(sql, (forum_data[0], since, limit)) else: cursor.execute(sql, (forum_data[0], since)) posts = cursor.fetchall() for p in posts: if forum_related: forum_info = { "id": forum_data[0], "name": forum_data[1], "short_name": forum_data[4], "user": get_user_by_id(forum_data[3])[2], "isDeleted": bool(forum_data[2]), } else: forum_info = forum_data[4] thread_data = get_thread_by_id(p[2]) if thread_related: thread_info = { "id": thread_data[0], "forum": forum_data[4], "title": thread_data[2], "isClosed": bool(thread_data[3]), "user": get_user_by_id(thread_data[4])[2], "date": thread_data[5].strftime("%Y-%m-%d %H:%M:%S"), "message": thread_data[6], "slug": thread_data[7], "isDeleted": bool(thread_data[8]), "dislikes": thread_data[9], "likes": thread_data[10], "points": thread_data[11], "posts": thread_data[12], } else: thread_info = thread_data[0] user_data = get_user_by_id(p[3]) if user_related: followers, following = get_follow_data(user_data[0]) subs = get_subscriptions(user_data[0]) user_info = { "id": user_data[0], "username": user_data[1], "email": user_data[2], "name": user_data[3], "about": user_data[4], "isAnonymous": bool(user_data[5]), "followers": [f[0] for f in followers], "following": [f[0] for f in following], "subscriptions": [s[0] for s in subs], } else: user_info = user_data[2] response.append( { "id": p[0], "forum": forum_info, "thread": thread_info, "user": user_info, "message": p[4], "date": p[5].strftime("%Y-%m-%d %H:%M:%S"), "parent": p[6], "isApproved": bool(p[8]), "isHighlighted": bool(p[9]), "isSpam": bool(p[10]), "isEdited": bool(p[11]), "isDeleted": bool(p[12]), "likes": p[13], "dislikes": p[14], "points": p[15], } ) cursor.close() return JsonResponse({"code": 0, "response": response})
def forum_list_users(request): response = [] if not request.method == "GET": return JsonResponse({"code": 2, "response": "Method in not supported"}) if "forum" not in request.GET: return JsonResponse({"code": 3, "response": "Missing field"}) forum = request.GET.get("forum") cursor = connection.cursor() forum_data = get_forum_by_shortname(cursor, forum) if not forum_data: cursor.close() return JsonResponse({"code": 1, "response": "Forum does not exist"}) if "since_id" in request.GET: since_id = request.GET["since_id"] try: since_id = int(since_id) except ValueError: cursor.close() return JsonResponse({"code": 3, "response": "Since id param is wrong"}) else: since_id = 0 if "limit" in request.GET: limit = request.GET["limit"] try: limit = int(limit) except ValueError: cursor.close() return JsonResponse({"code": 3, "response": "Limit param is wrong"}) else: limit = None if "order" in request.GET: order = request.GET["order"] if order != "asc" and order != "desc": cursor.close() return JsonResponse({"code": 3, "response": "Order param is wrong"}) else: order = "desc" sql = """SELECT u.id, username, email, name, about, is_anonymous FROM post p LEFT JOIN user u ON u.id = p.user_id WHERE p.forum_id=%s and p.user_id >= %s GROUP BY p.user_id""" if limit: sql += " LIMIT %s" cursor.execute(sql, (forum_data[0], since_id, limit)) else: cursor.execute(sql, (forum_data[0], since_id)) data = cursor.fetchall() t1 = datetime.datetime.now() if order == "desc": data = sorted(data, key=itemgetter(3), reverse=True) else: data = sorted(data, key=itemgetter(3)) t2 = datetime.datetime.now() print t2 - t1 for u in data: followers, following = get_follow_data(u[0]) subs = get_subscriptions(u[0]) response.append( { "username": u[1], "email": u[2], "name": u[3], "about": u[4], "isAnonymous": u[5], "id": u[0], "followers": [f[0] for f in followers], "following": [f[0] for f in following], "subscriptions": [s[0] for s in subs], } ) return JsonResponse({"code": 0, "response": response})