コード例 #1
0
ファイル: comments.py プロジェクト: Igor1py/social-network
def get_comments():
    params = converts_keys(request.args.to_dict(), case='snake')
    set_filter_params(DEFAULT_COMMENT_LIMIT, MAX_COMMENT_LIMIT, params)
    cookies = request.cookies
    if 'token' in cookies:
        with connect(DSN) as connection:
            with connection.cursor(cursor_factory=RealDictCursor) as cursor:
                cursor.execute(Users.get_user_id(), cookies)
                record = cursor.fetchone()
                user_id = record['user_id']
    else:
        user_id = 0
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Comments.filter(**params), {
                'user_id': user_id,
                **params
            })
            comments = cursor.fetchall()
            cursor.execute(Comments.count(**params), params)
            record = cursor.fetchone()
    for comment in comments:
        put_out_author(comment)
    return jsonify(
        converts_keys({
            'comments': comments,
            **record
        }, case='camel'))
コード例 #2
0
ファイル: comment.py プロジェクト: housne/pavements
	def get(self):
		comments = Comments(postId=self.postId)
		comment = comments.get()
		if len(comment) > 0:
			for i, v in enumerate(comment):
				comment[i]['date'] = time.mktime(v['date'].timetuple())*1000
				comment[i]['avatar'] = 'http://www.gravatar.com/avatar/' + hashlib.md5(v['email'].lower()).hexdigest() + '?s=40'
			return {'code': 200, 'data': comment}
		return {'code': 204, 'message': 'No commets'}
コード例 #3
0
ファイル: comment.py プロジェクト: housne/pavements
	def create(self, data):
		if data['postId'] == None:
			return {'code': 401, 'message': 'forbiden'}
		self.__setCommentDate(data)
		print(self.userId)
		if self.userId is not None:
			empty = self.__isEmpty(['content'])
		else:
			empty = self.__isEmpty(['name', 'email', 'content'])
		if len(empty) > 0:
			return {'code': 403, 'message': empty}
		comments = Comments(id=self.id, postId=self.postId, name=self.name, website=self.website, email=self.email, content=self.content, date=self.date, userId=self.userId, parentId=self.parentId)
		comments.create()
		return {'code': 200}
コード例 #4
0
ファイル: comments.py プロジェクト: Igor1py/social-network
def delete_comment(comment_id):
    cookies = request.cookies
    if 'token' not in cookies:
        return jsonify(), 401
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Users.get_user_id(), cookies)
            user_id = cursor.fetchone()['user_id']
            cursor.execute(Comments.get_author_id(), {'id': comment_id})
            author_id = cursor.fetchone()['author_id']
    if user_id != author_id:
        return jsonify(), 401
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Comments.delete(), {'id': comment_id})
    return jsonify(), 205
コード例 #5
0
ファイル: comments.py プロジェクト: Igor1py/social-network
def update_comment(comment_id):
    payload = converts_keys(loads(request.data), case='snake')
    check_only_required_payload_props(payload, 'post_id', 'content')
    cookies = request.cookies
    if 'token' not in cookies:
        return jsonify(), 401
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Users.get_user_id(), cookies)
            user_id = cursor.fetchone()['user_id']
            cursor.execute(Comments.get_author_id(), {'id': comment_id})
            author_id = cursor.fetchone()['author_id']
    if user_id != author_id:
        return jsonify(), 401
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Comments.update(), {'id': comment_id, **payload})
            comment = cursor.fetchone()
    return jsonify(converts_keys(comment, case='camel'))
コード例 #6
0
def unlike():
    params = converts_keys(request.args.to_dict(), case='snake')
    cookies = request.cookies
    if 'token' not in cookies:
        return jsonify(), 401
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Users.get_user_id(), cookies)
            record = cursor.fetchone()
            if 'post_id' in params:
                cursor.execute(Posts.unlike(), {**record, **params})
            elif 'comment_id' in params:
                cursor.execute(Comments.unlike(), {**record, **params})
            elif 'reply_id' in params:
                cursor.execute(Replies.unlike(), {**record, **params})
            else:
                jsonify(), 400
    return jsonify(), 205
コード例 #7
0
ファイル: pipeline.py プロジェクト: smg7d/canary
def addNewComments(subredditName, session):
    now = int(datetime.now(tz=timezone.utc).timestamp())
    commentsAdded = 0

    existingPosts = [
        p for p in session.query(Post).filter(Post.subreddit == subredditName)
    ]
    for existingPost in existingPosts:
        if (now - existingPost.created) < 60 * 60 * 24:

            existingComments = [
                com.commentId for com in session.query(Comments).filter(
                    Comments.postId == existingPost.postId)
            ]

            post = reddit.submission(id=existingPost.postId)
            post.comments.replace_more(limit=None)
            commentList = post.comments.list()

            levelMap = {}
            for comment in commentList:
                if comment not in existingComments:
                    parentId = comment.parent_id[
                        3:]  #trim off prefix of t1_ or t3_
                    levelMap[comment.id] = levelMap.get(parentId, 0) + 1

                    #this is lazy nonetype reference handling. don't judge.
                    try:
                        author = "" if comment.author is None else comment.author.name
                    except:
                        author = ""

                    newComment = Comments(commentId=comment.id,
                                          parentId=parentId,
                                          level=levelMap[comment.id],
                                          commentText=comment.body,
                                          author=author,
                                          postId=comment.submission,
                                          created=int(comment.created_utc),
                                          edited=bool(comment.edited))

                    existingPost.comments.append(newComment)
                    commentsAdded += 1

                #logic to add to closure table goes here
                parentId = comment.parent_id[
                    3:]  #starts at the existing parent
                existingComment = session.query(Comments).filter(
                    Comments.commentId == comment.id).one()
                while (parentId != existingComment.postId):
                    newCommentClosure = CommentsClosure(
                        parentId=parentId,
                        childId=existingComment.commentId,
                        postId=existingComment.postId)

                    isInClosureAlready = False
                    for dClosure in existingComment.commentsClosures:
                        if dClosure.parentId == newCommentClosure.parentId and dClosure.childId == newCommentClosure.childId:
                            isInClosureAlready = True

                    if not isInClosureAlready:
                        existingComment.commentsClosures.append(
                            newCommentClosure)

                    parentComment = session.query(Comments).filter(
                        Comments.commentId == parentId).one()
                    if parentComment is None:
                        break

                    parentId = parentComment.parentId

                session.add(existingComment)

            session.add(existingPost)

    session.commit()
    logging.info(f"{subredditName}: {commentsAdded} comments added")