Exemplo n.º 1
0
def addcomments():
    res = check()
    if res.status_code == 200 and request.method == "POST":
        res = json.loads(res.text)
        userid = request.args["whoPageUserId"]

        body = json.dumps(
            {"comment": {"userid": res["userid"], "postid": request.args["postid"], "text": request.form["text"]}}
        )

        res_b2 = requests.post("http://localhost:8002/comments/add", data=body, headers=headers)

        return redirect(url_for("posts", userid=userid))

    return redirect("index")
Exemplo n.º 2
0
def addcomments():
    res = check()
    if res.status_code == 200 and request.method == 'POST':
        res = json.loads(res.text)
        userid = request.args['whoPageUserId']

        body = json.dumps({'comment':{'userid': res['userid'], 
                                      'postid': request.args['postid'], 
                                      'text': request.form['text']}})

        res_b2 = requests.post('http://localhost:8002/comments/add', data=body, headers=headers)

        return redirect(url_for('posts', userid=userid))

    return redirect('index')
Exemplo n.º 3
0
def addposts():
    res = check()
    if res.status_code == 200 and request.method == "POST":
        res = json.loads(res.text)

        title = request.form["title"]
        text = request.form["text"]
        data = {"entry": {"userid": res["userid"], "title": title, "text": text}}

        body = json.dumps(data)
        res_b2 = requests.post("http://localhost:8002/posts/add", data=body, headers=headers)

        if res_b2.status_code == 201:
            flash("New post has been added")
        elif res_b2.status_code == 400:
            flash("Failed add new post. Bad Request")

        return redirect(url_for("posts", userid=res["userid"]))

    return render_template("posts.html")
Exemplo n.º 4
0
def addposts():
    res = check()
    if res.status_code == 200 and request.method == 'POST':
        res = json.loads(res.text)

        title = request.form['title']
        text = request.form['text']
        data = {'entry': {'userid': res['userid'], 'title': title, 'text': text}}

        body = json.dumps(data)
        res_b2 = requests.post('http://localhost:8002/posts/add', data=body, headers=headers)

        if res_b2.status_code == 201:
            flash('New post has been added')
        elif res_b2.status_code == 400:
            flash('Failed add new post. Bad Request')

        return redirect(url_for('posts', userid=res['userid']))

    return render_template('posts.html')
Exemplo n.º 5
0
def posts():
    res = check()
    if res.status_code == 200 and request.method == "GET":
        status = {
            "user_status_code": True,
            "post_status_code": False,
            "permission_add_post_code": True,
            "permission_add_comment_code": True,
        }
        res = json.loads(res.text)
        userid = res["userid"]
        if "userid" in request.args:
            # permissions for posts
            if int(request.args["userid"]) != int(userid):
                status["permission_add_post_code"] = False
                userid = request.args["userid"]

                # permissions for comments. This is "id" my friend?
                body = json.dumps({"userid": res["userid"], "friendid": userid})
                res_b1 = requests.get("http://localhost:8001/isfriend", data=body, headers=headers)
                if int(json.loads(res_b1.text)) == 0:
                    status["permission_add_comment_code"] = False

        res_b1 = requests.get("http://localhost:8001/me/" + str(userid), headers=headers)

        if res_b1.status_code != 200:
            status["user_status_code"] = False
            return render_template("layout.html", access=True, status=status)
        user = json.loads(res_b1.text)

        page = 1
        if "page" in request.args:
            page = request.args.get("page")

        # get posts
        body = json.dumps({"page": page, "userid": userid})
        res_b2 = requests.get("http://localhost:8002/posts", data=body, headers=headers)

        if res_b2.status_code == 200:
            res_b2 = json.loads(res_b2.text)
            status["post_status_code"] = True

            # get comments
            # get posts_id
            listpostid = []
            for p in res_b2["items"]:
                listpostid.append(p["postid"])
            # print "_________ ", listpostid

            # get comments
            if len(listpostid) > 0:
                body = json.dumps({"listpostid": listpostid})
                res_comments = requests.get("http://localhost:8002/comments", data=body, headers=headers)

                # merge Comments and Posts
                if res_comments.status_code == 200:
                    res_comments = json.loads(res_comments.text)
                    # print "__________ comments ", res_comments

                    # get users_name whoAddComment
                    listuserid = []
                    for p in res_comments:
                        listuserid.append(p["user_id_whoAdd"])

                    if len(listuserid) > 0:
                        body = json.dumps({"listuserid": listuserid})
                        res_b1 = requests.get("http://localhost:8001/userlist", data=body, headers=headers)
                        # print "listuserid: ", listuserid
                        if res_b1.status_code == 200:
                            res_b1 = json.loads(res_b1.text)
                            return render_template(
                                "posts.html",
                                access=True,
                                posts=res_b2,
                                comments=res_comments,
                                users=res_b1,
                                user=user,
                                status=status,
                            )

        return render_template("posts.html", access=True, posts=res_b2, user=user, status=status)

    return redirect("index")
Exemplo n.º 6
0
def posts():
    res = check()
    if res.status_code == 200 and request.method == 'GET':
        status = {'user_status_code': True, 'post_status_code': False, 
                  'permission_add_post_code': True, 'permission_add_comment_code': True}
        res = json.loads(res.text)
        userid = res['userid']
        if 'userid' in request.args:
            #permissions for posts
            if int(request.args['userid']) != int(userid):
                status['permission_add_post_code'] = False
                userid = request.args['userid']

                #permissions for comments. This is "id" my friend?
                body = json.dumps({'userid': res['userid'], 'friendid': userid})
                res_b1 = requests.get('http://localhost:8001/isfriend', data=body, headers=headers)
                if int(json.loads(res_b1.text)) == 0:
                    status['permission_add_comment_code'] = False

        res_b1 = requests.get('http://localhost:8001/me/'+str(userid), headers=headers)

        if res_b1.status_code != 200:
            status['user_status_code'] = False
            return render_template('layout.html', access=True, status=status)
        user = json.loads(res_b1.text)

        page = 1
        if 'page' in request.args:
            page = request.args.get('page')

        #get posts
        body = json.dumps({'page': page, 'userid': userid})
        res_b2 = requests.get('http://localhost:8002/posts', data=body, headers=headers)

        if res_b2.status_code == 200:
            res_b2 = json.loads(res_b2.text)
            status['post_status_code'] = True

            #get comments
                #get posts_id
            listpostid = []
            for p in res_b2['items']:
                listpostid.append(p['postid'])
            # print "_________ ", listpostid

            #get comments
            if len(listpostid) > 0:
                body = json.dumps({'listpostid': listpostid})
                res_comments = requests.get('http://localhost:8002/comments', data=body, headers=headers)

                # merge Comments and Posts
                if res_comments.status_code == 200:
                    res_comments = json.loads(res_comments.text)
                    # print "__________ comments ", res_comments

                    #get users_name whoAddComment
                    listuserid = []
                    for p in res_comments:
                        listuserid.append(p['user_id_whoAdd'])

                    if len(listuserid) > 0:
                        body = json.dumps({'listuserid': listuserid})
                        res_b1 = requests.get('http://localhost:8001/userlist', data=body, headers=headers)
                    # print "listuserid: ", listuserid
                        if res_b1.status_code == 200:
                            res_b1 = json.loads(res_b1.text)
                            return render_template('posts.html', access=True, posts=res_b2, comments=res_comments, users=res_b1, user=user, status=status)


        return render_template('posts.html', access=True, posts=res_b2, user=user, status=status)

    return redirect('index')