def update(request): cursor = connection.cursor() try: json_request = loads(request.body) except ValueError as value_err: cursor.close() return HttpResponse(dumps({"code": codes.INVALID_QUERY, "response": unicode(value_err)})) try: message = json_request["message"] slug = json_request["slug"] threadId = json_request["thread"] except KeyError as key_err: cursor.close() return HttpResponse( dumps({"code": codes.INCORRECT_QUERY, "response": "Not found: {}".format(unicode(key_err))}) ) threadId = general_utils.validate_id(threadId) if threadId == False: cursor.close() return HttpResponse(dumps({"code": codes.INVALID_QUERY, "response": "thread id should be int"})) try: cursor.execute(SELECT_THREAD_BY_ID, [threadId]) if cursor.rowcount == 0: cursor.close() return HttpResponse(dumps({"code": codes.NOT_FOUND, "response": "thread not found"})) cursor.execute(UPDATE_THREAD, [unicode(message), unicode(slug), threadId]) thread, related_obj = get_thread_by_id(cursor, threadId) except DatabaseError as db_err: cursor.close() return HttpResponse(dumps({"code": codes.UNKNOWN_ERR, "response": unicode(db_err)})) cursor.close() return HttpResponse(dumps({"code": codes.OK, "response": thread}))
def details(request): cursor = connection.cursor() if request.method != "GET": cursor.close() return HttpResponse(dumps({"code": codes.INVALID_QUERY, "response": "request method should be GET"})) threadId = general_utils.validate_id(request.GET.get("thread")) if threadId is None: cursor.close() return HttpResponse(dumps({"code": codes.INVALID_QUERY, "response": "thread id not found"})) if threadId == False: cursor.close() return HttpResponse(dumps({"code": codes.INVALID_QUERY, "response": "thread id should be int"})) try: thread, relatedIds = get_thread_by_id(cursor, threadId) except TypeError: cursor.close() return HttpResponse(dumps({"code": codes.INCORRECT_QUERY, "response": "thread doesn't exist"})) except DatabaseError as db_err: cursor.close() return HttpResponse(dumps({"code": codes.UNKNOWN_ERR, "response": unicode(db_err)})) related = request.GET.getlist("related") related_functions_dict = {"user": get_user_by_id, "forum": get_forum_by_id} for related_ in related: if related_ in ["user", "forum"]: get_related_info_func = related_functions_dict[related_] thread[related_], relatedIds_ = get_related_info_func(cursor, relatedIds[related_]) else: cursor.close() return HttpResponse(dumps({"code": codes.INCORRECT_QUERY, "response": "incorrect related parameter"})) cursor.close() return HttpResponse(dumps({"code": codes.OK, "response": thread}))
def vote(request): __cursor = connection.cursor() try: json_request = loads(request.body) except ValueError as value_err: __cursor.close() return HttpResponse(dumps({'code': codes.INVALID_QUERY, 'response': unicode(value_err)})) try: thread_id = json_request['thread'] vote = json_request['vote'] except KeyError as key_err: __cursor.close() return HttpResponse(dumps({'code': codes.INCORRECT_QUERY, 'response': 'Not found: {}'.format(unicode(key_err))})) thread_id = general_utils.validate_id(thread_id) if thread_id == False: __cursor.close() return HttpResponse(dumps({'code': codes.INVALID_QUERY, 'response': 'post id should be int'})) try: vote = int(vote) if abs(vote) != 1: raise ValueError except ValueError: __cursor.close() return HttpResponse(dumps({'code': codes.INCORRECT_QUERY, 'response': 'incorrect vote value'})) if vote < 0: column_name = 'dislikes' else: column_name = 'likes' try: __cursor.execute(SELECT_THREAD_BY_ID, [thread_id, ]) if not __cursor.rowcount: __cursor.close() return HttpResponse(dumps({'code': codes.NOT_FOUND, 'response': 'thread not found'})) thread_id_qs = __cursor.execute(UPDATE_THREAD_VOTES.format(column_name, column_name), [thread_id, ]) thread, related_obj = get_thread_by_id(__cursor, thread_id) except DatabaseError as db_err: __cursor.close() return HttpResponse(dumps({'code': codes.UNKNOWN_ERR, 'response': unicode(db_err)})) __cursor.close() return HttpResponse(dumps({'code': codes.OK, 'response': thread}))
def details(request): __cursor = connection.cursor() if request.method != 'GET': __cursor.close() return HttpResponse(dumps({'code': codes.INVALID_QUERY, 'response': 'request method should be GET'})) thread_id = general_utils.validate_id(request.GET.get('thread')) if thread_id is None: __cursor.close() return HttpResponse(dumps({'code': codes.INVALID_QUERY, 'response': 'thread id not found'})) if thread_id == False: __cursor.close() return HttpResponse(dumps({'code': codes.INVALID_QUERY, 'response': 'thread id should be int'})) try: thread, related_ids = get_thread_by_id(__cursor, thread_id) #print related_ids except TypeError: __cursor.close() return HttpResponse(dumps({'code': codes.INCORRECT_QUERY, 'response': 'thread doesn\'t exist'})) except DatabaseError as db_err: __cursor.close() return HttpResponse(dumps({'code': codes.UNKNOWN_ERR, 'response': unicode(db_err)})) related = request.GET.getlist('related') related_functions_dict = {'user': get_user_by_id, 'forum': get_forum_by_id } for related_ in related: if related_ in ['user', 'forum']: get_related_info_func = related_functions_dict[related_] thread[related_], related_ids_ = get_related_info_func(__cursor, related_ids[related_]) else: __cursor.close() return HttpResponse(dumps({'code': codes.INCORRECT_QUERY, 'response': 'incorrect related parameter'})) __cursor.close() return HttpResponse(dumps({'code': codes.OK, 'response': thread}))
def vote(request): cursor = connection.cursor() try: json_request = loads(request.body) except ValueError as value_err: cursor.close() return HttpResponse(dumps({"code": codes.INVALID_QUERY, "response": unicode(value_err)})) try: threadId = json_request["thread"] vote = json_request["vote"] except KeyError as key_err: cursor.close() return HttpResponse( dumps({"code": codes.INCORRECT_QUERY, "response": "Not found: {}".format(unicode(key_err))}) ) threadId = general_utils.validate_id(threadId) if threadId == False: cursor.close() return HttpResponse(dumps({"code": codes.INVALID_QUERY, "response": "post id should be int"})) try: vote = int(vote) if abs(vote) != 1: raise ValueError except ValueError: cursor.close() return HttpResponse(dumps({"code": codes.INCORRECT_QUERY, "response": "incorrect vote value"})) if vote < 0: column_name = "dislikes" else: column_name = "likes" try: cursor.execute(SELECT_THREAD_BY_ID, [threadId]) if cursor.rowcount == 0: cursor.close() return HttpResponse(dumps({"code": codes.NOT_FOUND, "response": "thread not found"})) cursor.execute(UPDATE_THREAD_VOTES.format(column_name, column_name), [threadId]) thread, related_obj = get_thread_by_id(cursor, threadId) except DatabaseError as db_err: cursor.close() return HttpResponse(dumps({"code": codes.UNKNOWN_ERR, "response": unicode(db_err)})) cursor.close() return HttpResponse(dumps({"code": codes.OK, "response": thread}))
def update(request): __cursor = connection.cursor() try: json_request = loads(request.body) except ValueError as value_err: __cursor.close() return HttpResponse(dumps({'code': codes.INVALID_QUERY, 'response': unicode(value_err)})) try: thread_id = json_request['thread'] message = unicode(json_request['message']) slug = unicode(json_request['slug']) except KeyError as key_err: __cursor.close() return HttpResponse(dumps({'code': codes.INCORRECT_QUERY, 'response': 'Not found: {}'.format(unicode(key_err))})) thread_id = general_utils.validate_id(thread_id) if thread_id == False: __cursor.close() return HttpResponse(dumps({'code': codes.INVALID_QUERY, 'response': 'thread id should be int'})) try: __cursor.execute(SELECT_THREAD_BY_ID, [thread_id, ]) if not __cursor.rowcount: __cursor.close() return HttpResponse(dumps({'code': codes.NOT_FOUND, 'response': 'thread not found'})) __cursor.execute(UPDATE_THREAD, [message, slug, thread_id, ]) thread, related_obj = get_thread_by_id(__cursor, thread_id) except DatabaseError as db_err: __cursor.close() return HttpResponse(dumps({'code': codes.UNKNOWN_ERR, 'response': unicode(db_err)})) __cursor.close() return HttpResponse(dumps({'code': codes.OK, 'response': thread}))