def user_details(request): if not request.method == 'GET': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) user_email = request.GET.get('user', None) if not user_email: return JsonResponse({ 'code': 3, 'response': 'Missing field' }) cursor = connection.cursor() user_data = get_user_by_email(cursor, user_email) if not user_data: return JsonResponse({ 'code': 1, 'response': 'User does not exist' }) followers, following = get_follow_data(user_data[0]) subs = get_subscriptions(user_data[0]) response = { 'id': user_data[0], 'username': user_data[1], 'email': user_data[2], 'name': user_data[3], 'about': user_data[4], 'isAnonymous': user_data[5], '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})
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 user_follow(request): response = {} if not request.method == 'POST': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) try: request_params = json.loads(request.body) follower = request_params.get('follower', None) followee = request_params.get('followee', None) if not (followee and follower): return JsonResponse({ 'code': 3, 'response': 'Missing field' }) cursor = connection.cursor() follower_user = get_user_by_email(cursor, follower) followee_user = get_user_by_email(cursor, followee) if not (followee_user and follower_user): return JsonResponse({ 'code': 1, 'response': 'User does not exist' }) sql = "INSERT IGNORE INTO user_user_follow VALUES (null, %s, %s);" cursor.execute(sql, (follower_user[0], followee_user[0])) followers, following = get_follow_data(follower_user[0]) subs = get_subscriptions(follower_user[0]) response = { 'id': follower_user[0], 'username': follower_user[1], 'email': follower_user[2], 'name': follower_user[3], 'about': follower_user[4], 'isAnonymous': follower_user[5], 'followers': [ f[0] for f in followers ], 'following': [ f[0] for f in following ], 'subscriptions': [ s[0] for s in subs ] } except ValueError: return JsonResponse({ 'code': 3, 'response': 'No JSON object could be decoded' }) return JsonResponse({'code': 0, 'response':response})
def user_update_profile(request): response = {} if not request.method == 'POST': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) try: request_params = json.loads(request.body) if not ('name' in request_params and 'about' in request_params and 'user' in request_params): return JsonResponse({ 'code': 3, 'response': 'Missing field' }) user_email = request_params.get('user') about = request_params.get('about') name = request_params.get('name') cursor = connection.cursor() user_data = get_user_by_email(cursor, user_email) if not user_data: cursor.close() return JsonResponse({ 'code': 1, 'response': 'User does not exist' }) sql = "UPDATE user SET name = %s, about = %s WHERE email = %s" cursor.execute(sql, (name, about, user_email)) followers, following = get_follow_data(user_data[0]) subs = get_subscriptions(user_data[0]) response.update({ 'name': name, 'about': about, 'email': user_email, 'isAnonymous': user_data[5], 'username': user_data[1], 'id': user_data[0], 'followers': [ f for f in followers ], 'following': [ f for f in following ], 'subscriptions': [ s for s in subs ] }) except ValueError: return JsonResponse({ 'code': 3, 'response': 'No JSON object could be decoded' }) return JsonResponse({ 'code': 0, 'response': response, })
def user_list_following(request): response = [] if not request.method == 'GET': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) if 'user' not in request.GET: return JsonResponse({ 'code': 3, 'response': 'Missing field' }) if "since_id" in request.GET: since_id = request.GET['since_id'] try: since_id = int(since_id) except ValueError: 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: 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' cursor = connection.cursor() user_data = get_user_by_email(cursor, request.GET.get('user')) if not user_data: return JsonResponse({ 'code': 1, 'response': 'User does not exist' }) sql = """SELECT u.id, u.username, u.email, u.name, u.about, u.is_anonymous FROM user_user_follow LEFT JOIN user u ON to_user_id = u.id WHERE from_user_id = %s AND to_user_id>=%s ORDER BY u.name """ sql += order if limit: sql += " LIMIT %s" cursor.execute(sql, (user_data[0], since_id, limit)) else: cursor.execute(sql, (user_data[0], since_id)) followers = cursor.fetchall() for f in followers: followers, following = get_follow_data(f[0]) subs = get_subscriptions(f[0]) response.append({ 'username': f[1], 'email': f[2], 'name': f[3], 'about': f[4], 'isAnonymous': f[5], 'id': f[0], 'followers': [ follower[0] for follower in followers ], 'following': [ follower[0] for follower in following ], 'subscriptions': [ s[0] for s in subs ] }) return JsonResponse({ 'code': 0, 'response': response })
def thread_details(request): response = {} if not request.method == 'GET': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) if 'thread' not in request.GET: return JsonResponse({ 'code': 3, 'response': 'Missing field' }) thread_id = request.GET.get('thread') thread_data = get_thread_by_id(thread_id) if not thread_data: return JsonResponse({ 'code': 1, 'response': 'Thread not found' }) related = request.GET.getlist('related') forum_data = get_forum_by_id(thread_data[1]) user_data = get_user_by_id(thread_data[4]) for case in related: if case not in ['user', 'forum']: return JsonResponse({ 'code': 3, 'response': 'Wrong related param' }) if 'forum' in 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] if 'user' in related: 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 = { 'user': user_info, 'forum': forum_info, 'id': thread_data[0], 'title': thread_data[2], 'isClosed': bool(thread_data[3]), 'date': thread_data[5].strftime('%Y-%m-%d %H:%M:%S'), 'message': thread_data[6], 'slug': thread_data[7], 'isDeleted': bool(thread_data[8]), 'posts': thread_data[12], 'likes': thread_data[10], 'dislikes': thread_data[9], 'points': thread_data[11], } return JsonResponse({ 'code': 0, 'response': response, })
def post_details(request): response = {} if not request.method == 'GET': return JsonResponse({ 'code': 2, 'response': 'Method in not supported' }) if 'post' not in request.GET: return JsonResponse({ 'code': 3, 'response': 'Missing field' }) post_id = request.GET.get('post') post_data = get_post_by_id(post_id) if not post_data: return JsonResponse({ 'code': 1, 'response': 'Post not found' }) user_data = get_user_by_id(post_data[3]) thread_data = get_thread_by_id(post_data[2]) forum_data = get_forum_by_id(post_data[1]) if 'related' in request.GET: related = request.GET.getlist('related') for r in related: if r != 'forum' and r != 'user' and r != 'thread': return JsonResponse({ 'code': 3, 'response': 'Wrong related params' }) if 'user' in 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] if 'forum' in 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] if 'thread' in 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] else: user_info = user_data[2] thread_info = thread_data[0] forum_info = forum_data[4] response.update({ 'id': int(post_id), 'forum': forum_info, 'thread': thread_info, 'user': user_info, 'message': post_data[4], 'date': post_data[5].strftime('%Y-%m-%d %H:%M:%S'), 'parent': post_data[6], 'isApproved': bool(post_data[8]), 'isHighlighted': bool(post_data[9]), 'isSpam': bool(post_data[10]), 'isEdited': bool(post_data[11]), 'isDeleted': bool(post_data[12]), 'likes': post_data[13], 'dislikes': post_data[14], 'points': post_data[15], }) 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})