예제 #1
0
 def post(self, gid, pid):
     u = request.authorization  # this gives dict
     uid = User.objects.get(
         username=u['username'])  # this gives user object
     user_id = str(uid.id)  # this gives the user id in string format
     body = request.get_json()
     user = User.objects.get(id=user_id)
     group = Group.objects.get(id=gid)
     post = Post.objects.get(id=pid)
     if post.approval:
         if user_id in group.role_dict:
             comment = Comment(**body, post_id=ObjectId(pid))
             comment.date_created = datetime.now()
             comment.save()
             # update last active status for user
             temp_dict = group.last_active_dict
             temp_dict[user_id] = datetime.now()
             group.update(set__last_active_dict=temp_dict)
             content = "{name} has posted a comment today".format(
                 name=user.username)
             queue.enqueue(
                 printhello())  # this is just a check that q works
             queue.enqueue(send_mail, user.email, content)
             # queue.enqueue(send_email(user.email, content))
             # send_email(user.email, content)
             return {'comment_id': str(comment.id)}, 200
         else:
             return "You ain't a member of this group", 200
예제 #2
0
    def post(self, topic_id):
        content = cgi.escape(self.request.get("content"))
        if content:
            Comment.save_comment(topic_id, content)
        else:
            user_id = users.get_current_user().email()
            Subscription.save_subscription(topic_id, user_id)

        return self.redirect_to("topic-details", topic_id=topic_id)
예제 #3
0
    def post(self, post_id):
        value_csrf = self.request.get('csrf-token')

        if not memcache.get(value_csrf):
            return self.write('CSRF Attack Detected!')

        post = Post.get_by_id(int(post_id))
        content = cgi.escape(self.request.get('comment'))
        Comment.save_comment(post_id, content)

        return self.redirect_to('post', post_id=post.key.id())
예제 #4
0
    def test_comment_delete_handler(self):
        # POST test topic via '/topic/add'
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)

        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        params = {"title": title, "content": content, "csrf_token": csrf_token}

        post = self.testapp.post('/topic/add', params)
        self.assertEqual(post.status_int, 302)

        # POST test comment via '/topic/details/<topic_id>'
        content = "This is a new comment. Just for testing purposes."

        params = {"content": content, "csrf_token": csrf_token}

        topic = Topic.query().get()
        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Comment.save_comment(topic_id, content)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()), params)
        self.assertEqual(post.status_int, 302)

        # Delete comment via '/comment/delete/<comment_id>'
        comment = Comment.query().get()
        params = {"csrf_token": csrf_token}

        post = self.testapp.post('/comment/delete/' + str(comment.key.id()), params, {'referer': '/user-comments'})
        self.assertEqual(post.status_int, 302)
        # check if comment.deleted field was set to True
        self.assertEqual(comment.deleted, True)

        post = self.testapp.post('/comment/delete/' + str(comment.key.id()), params, {'referer': '/topic/details/' + str(topic.key.id())})
        self.assertEqual(post.status_int, 302)
예제 #5
0
def run():
    """Analyse reddit comments from a subreddit."""
    print
    input_path = config.INPUT_PATH
    comment_row_list = csv_util.read_csv(input_path)
    comment_list = []
    pos_list = []
    neg_list = []

    # calculate sentiment for each comment
    for comment_row in comment_row_list:
        comment = Comment(comment_row)
        comment = calculate_sentiment(comment)
        comment_list.append(comment)

        if comment.compound_score > 0.2:
            pos_list.append(comment.body)
        elif comment.compound_score < -0.2:
            neg_list.append(comment.body)

    # write out CSV with sentiment score for each comment
    output_path = config.OUTPUT_PATH
    csv_util.write_row(output_path, comment=None, header=HEADER)
    for comment in comment_list:
        csv_util.write_row(output_path, comment=comment)

    # distribution analysis
    (pos_res, all_words_pos, neg_res,
     all_words_neg) = calculate_word_distributions(pos_list, neg_list)
예제 #6
0
파일: spider.py 프로젝트: murphyzh/music163
def parser_song(song_id, art):
    tree = get_tree(SONG_URL.format(song_id))
    song = session.query(Song).filter_by(id=song_id).all()
    r = post(COMMENTS_URL.format(song_id))
    if r.status_code != 200:
        print('API error: Song: {}'.format(song_id))
        return
    data = r.json()
    if not song:
        for404 = tree.xpath('//div[@class="n-for404"]')
        if for404:
            return
        try:
            song_name = tree.xpath('//em[@class="f-ff2"]/text()')[0].strip()
        except IndexError:
            try:
                song_name = tree.xpath(
                    '//meta[@name="keywords"]/@content')[0].strip()
            except IndexError:
                print('Fetch limit!')
                time.sleep(10)
                return parser_song(song_id, art)
        song = Song(id=song_id,
                    name=str(song_name),
                    art_id=art.id,
                    comment_count=int(data['total']))
        session.add(song)
        session.commit()
    else:
        song = song[0]
    for comment_ in data['hotComments']:
        comment_id = comment_['commentId']
        content = comment_['content']
        like_count = comment_['likedCount']
        user_ = comment_['user']
        if not user_:
            continue
        user = session.query(User).filter_by(id=user_['userId']).all()
        if not user:
            user = User(id=user_['userId'],
                        name=user_['nickname'],
                        picture=user_['avatarUrl'])
            try:
                session.add(user)
                session.commit()
            except Exception as e:
                continue
        else:
            user = user[0]
        comment = session.query(Comment).filter_by(id=comment_id).all()
        if not comment:
            comment = Comment(id=comment_id,
                              use_id=user.id,
                              song_id=song.id,
                              content=content,
                              like_count=like_count)
            session.add(comment)
            session.commit()
    return song
예제 #7
0
 def post(self):
     # If not a logged in user redirect to login
     if not self.user:
         self.redirect('/login')
     else:
         comment = self.request.get('comment')
         post_id = int(self.request.get('post_id'))
         post = Post.get_by_id(post_id, parent=blog_key())
         author = self.user
         if not comment:
             return  # do nothing if empty comment
         else:
             c = Comment(content=comment, author=author.key, post=post.key)
             c.put()
             comment = Comment.render_single_comment(c)
             # return JSON to Ajax
             self.write(json.dumps(({'comment': comment})))
예제 #8
0
    def insertComment(self,jokeid,userid,content):
        if jokeid=='':
            self.status=201
            self.message='jokeid不能为空'
            jsonStr=self.getJsonResult()

            self.write(jsonStr)
        elif userid=='':
            self.status=201
            self.message='userid不能为空'
            jsonStr=self.getJsonResult()
            self.write(jsonStr)
        elif content=='':
            self.status=201
            self.message='content不能为空'
            jsonStr=self.getJsonResult()
            self.write(jsonStr)
        elif not self.checkUser(userid):
            self.status=201
            self.message='该userid不存在'
            jsonStr=self.getJsonResult()

            self.write(jsonStr)
        elif not self.checkJoke(jokeid):
            self.status=201
            self.message='该jokeid不存在'
            jsonStr=self.getJsonResult()

            self.write(jsonStr)
        else:
            commentid=uuid.uuid1().hex;
            newcomment=Comment()
            newcomment.commentid=commentid
            newcomment.userid=userid
            newcomment.jokeid=jokeid
            newcomment.content=content

            self.db.add(newcomment)
            self.db.commit()
            self.db.close()
            
            self.status=200
            jsonStr=self.getJsonResult()

            self.write(jsonStr)
예제 #9
0
    def get(self):
        current_time = datetime.now()
        time_deleted_limit = current_time - timedelta(minutes=2)
        comments = Comment.query(
            Comment.deleted == True,
            Comment.time_updated < time_deleted_limit).fetch()

        for comment in comments:
            comment.key.delete()
예제 #10
0
    def post(self):
        if not self.user:
            self.redirect('/login')
        else:
            comment_id = int(self.request.get('comment_id'))
            comment = Comment.get_by_id(comment_id)

            if self.user.key.id() == comment.author.id():
                comment.key.delete()
                self.write(json.dumps(({'success': 'Comment deleted'})))
예제 #11
0
    def post(self, topic_id):
        csrf_token = self.request.get("csrf_token")
        mem_token = memcache.get(
            key=csrf_token)  # find if this CSRF exists in memcache

        if not mem_token:  # if token does not exist in memcache, write the following message
            return self.write("Attack attempt detected...")

        user = users.get_current_user()

        if not user:
            return self.write(
                "Please login before you're allowed to post a topic.")

        topic = Topic.get_by_id(int(topic_id))
        text = self.request.get("comment")

        Comment.create(content=text, user=user, topic=topic)

        return self.redirect_to("topic_details", topic_id=topic.key.id())
예제 #12
0
    def get(self, topic_id):
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)

        topic = Topic.get_by_id(int(topic_id))
        comment = Comment.query(Comment.topic_id == topic.key.id()).order(
            Comment.created).fetch()

        params = {"topic": topic, "comment": comment, "csrf_token": csrf_token}

        return self.render_template("topic_details.html", params=params)
예제 #13
0
    def post(self, topic_id):
        user = users.get_current_user()
        time = datetime.datetime.now()

        csrf_token = self.request.get("csrf_token")
        mem_token = memcache.get(key=csrf_token)

        if mem_token:
            return self.write("Hacker at the doors")

        comment = self.request.get("comment")
        topic = Topic.get_by_id(int(topic_id))
        new_comment = Comment(content=comment,
                              topic_id=topic.key.id(),
                              author_email=user.email(),
                              topic_title=topic.title,
                              created=time)
        new_comment.put()

        return self.redirect_to("topic-details", topic_id=topic.key.id())
예제 #14
0
    def get(self, post_id):
        post = Post.get_by_id(int(post_id))
        user = users.get_current_user()
        email = ''

        if user:
            email = user.email()
        comments = Comment.query(Comment.user_email == email,
                                 Comment.postID == post.key.id()).fetch()
        params = {'post': post, 'comments': comments}
        return self.render_template('delete_comment.html', params=params)
예제 #15
0
    def get(self):
        user = users.get_current_user()

        user_comments = Comment.query(
            Comment.user_id == user.email(),
            Comment.deleted == False).order(-Comment.create_time).fetch()

        params = {
            "user_comments": user_comments
        }

        return self.render_template_with_csrf("user_comments.html", params)
예제 #16
0
    def get(self, gid, pid):
        user = request.authorization  # this gives dict
        uid = User.objects.get(username=user['username'])  # this gives user object
        group = Group.objects.get(id=gid)
        post = Post.objects.get(id=pid)
        if gid in post.group_id:
            if post.approval:
                comments = Comment.objects(post_id=pid).to_json()
                return Response(comments, mimetype="application/json", status=200)

        else:
            return "You do not have the required access", 200
예제 #17
0
    def post(self, slug):
        context = self.get_context(slug)
        form = context.get('form')

        if form.validate():
            comment = Comment()
            form.populate_obj(comment)

            post = context.get('post')
            post.comments.append(comment)
            post.save()

            return redirect(url_for('posts.detail', slug=slug))
        return render_template('posts/detail.html', **context)
예제 #18
0
def store_comment(post_id: int, request: MultiDict, engine: Engine) -> str:
    """Extract comment information from request object; validate and store

    :param post_id: post 'id' to which the comment refers
    :param request: Flask request object
    :param engine: SQLAlchemy engine object
    """

    # fill in comment form
    form = CommentForm(request.form)

    # validate response, comment and store comment in database
    if request.method == 'POST':
        if form.validate():
            comment = Comment(post_id, request.form.get('name'),
                              request.form.get('content'),
                              request.form.get('email'),
                              request.form.get('occupation'))

            if comment.to_sql(engine):
                return 'success'

        # indicate source of error on failed validation
        elif len(request.form.get('content')) > 1000:
            return 'Comment too long. A maximum of 1000 characters is allowed.'
        elif len(request.form.get('content')) < 5:
            return 'Comment too short. At least 5 characters are required.'
        elif len(request.form.get('name')) > 20:
            return 'Name too long. A maximum of 20 characters is allowed.'
        elif len(request.form.get('email')) > 50:
            return 'Email address too long. A maximum of 50 characters is allowed.'
        elif len(request.form.get('email')) < 5:
            return 'Email address too short. At least 5 characters are required.'
        elif len(request.form.get('occupation')) > 100:
            return 'Occupation name too long. A maximum of 100 characters is allowed.'

    return ''
예제 #19
0
    def get(self, cid, gid):
        user = request.authorization
        uid = User.objects.get(username=user['username'])
        uid = str(uid.id)

        try:
            group = Group.objects.get(id=gid)
            if uid in group.role_dict:
                comment = Comment.objects(id=cid).to_json()
                return Response(comment,
                                mimetype="application/json",
                                status=200)
            else:
                return "You are not member of the group", 500
        except:
            return "Invalid group or comment id", 500
예제 #20
0
    def get(self, topic_id):
        topic = Topic.get_by_id(int(topic_id))
        comments = comments = Comment.query(Comment.topic_id == topic.key.id(),
                                            Comment.deleted == False).order(
                                                Comment.created).fetch()

        csrf_token = str(uuid.uuid4())  # convert UUID to string
        memcache.add(key=csrf_token, value=True, time=600)

        params = {
            "topic": topic,
            "comments": comments,
            "csrf_token": csrf_token
        }

        return self.render_template("topic_details.html", params=params)
예제 #21
0
 def delete(self, gid, cid):
     user = request.authorization  # this gives dict
     uid = User.objects.get(
         username=user['username'])  # this gives user object
     user_id = str(uid.id)  # this gives the user id in string format
     comment = Comment.objects.get(id=cid)
     group = Group.objects.get(id=gid)
     try:
         role = group.role_dict[user_id]
         comments = Comment.objects(user_id=user_id)
         if comment in comments or role == A or role == MD:
             comment.delete()
             return "Comment deleted", 200
         else:
             return "You don't have the permission required", 200
     except:
         return "You ain't a member of the group", 200
예제 #22
0
    def post(self, post_id):
        post = Post.get_by_id(int(post_id))

        user = users.get_current_user()
        email = ''

        if user:
            email = user.email()

        comments = Comment.query(Comment.user_email == email,
                                 Comment.postID == post.key.id()).fetch()

        for comment in comments:
            if comment.user_email == email:
                comment.deleted = True
                comment.put()

        return self.redirect_to('main-page')
예제 #23
0
 def post(self):
     if not self.user:
         self.redirect('/login')
     else:
         comment_id = int(self.request.get('comment_id'))
         new_comment = self.request.get('new_comment')
         comment = Comment.get_by_id(comment_id)
         if comment:
             if comment.author.id() == self.user.key.id():
                 comment.content = new_comment
                 comment.put()
                 self.write(
                     json.dumps(({
                         'comment':
                         self.render_comment(comment.content)
                     })))
         else:
             self.write(json.dumps(({'comment': "There was no comment"})))
예제 #24
0
    def test_topic_details_handler(self):
        # Create test topic
        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        topic = Topic(user_id=os.environ['USER_EMAIL'],
                      title=title,
                      content=content)
        topic.put()

        # GET
        topic = Topic.query().get()
        get = self.testapp.get('/topic/details/' + str(topic.key.id()))
        self.assertEqual(get.status_int, 200)
        self.assertEqual(topic.title, title)

        # POST
        # 1. POST test comment via '/topic/details/<topic_id>'
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)
        content = "This is a new comment. Just for testing purposes."

        params = {"content": content, "csrf_token": csrf_token}

        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Comment.save_comment(topic_id, content)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()),
                                 params)
        self.assertEqual(post.status_int, 302)

        comment = Comment.query().get()
        self.assertEqual(comment.content, content)

        # 2. POST test subscription via '/topic/details/<topic_id>'
        params = {"csrf_token": csrf_token}

        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Subscription.save_comment(topic_id, user_id)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()),
                                 params)
        self.assertEqual(post.status_int, 302)

        subscription = Subscription.query().get()
        self.assertEqual(subscription.user_id, os.environ['USER_EMAIL'])
예제 #25
0
    def get(self, post_id):
        post = Post.get_by_id(int(post_id))
        comments = Comment.query(Comment.postID == post.key.id(), Comment.deleted == False).order(-Comment.time_posted).fetch()
        admin = users.is_current_user_admin()
        author = post.user_email
        user = users.get_current_user()
        email = ''

        if user:
            email = user.email()

        params = {
            'post': post,
            'comments': comments,
            'admin': admin,
            'author': author,
            'email': email,
        }
        return self.render_template('post.html', params=params)
예제 #26
0
def add_post_comment():
    groups = Group.objects
    for group in groups:
        temp_dict = group.last_active_dict
        for user_id in group.role_dict.keys():
            temp_dict[user_id] = datetime.now()
            name = ''.join([
                random.choice(string.ascii_letters + string.digits)
                for k in range(9)
            ])
            content1 = "post is {name}".format(name=name)
            post = Post(user_id=ObjectId(user_id),
                        group_id=ObjectId(group.id),
                        content=content1).save()
            content2 = "comment is {name}".format(name=name)
            Comment(user_id=ObjectId(user_id),
                    post_id=post.id,
                    content=content2).save()
        group.update(set__last_active_dict=temp_dict)
예제 #27
0
    def from_sql(engine: Engine, post_id: int,
                 comment_id: int) -> Optional[Comment]:
        """Generate a Comment object from a SQL table

        :param engine: SQLAlchemy engine object
        :param post_id: Entry identifier in comments table
        :param comment_id: Comment identifier in comments table
        :return: populated Comment object
        """
        comments = get_comments(engine, post_id, comment_id)

        comment = None

        # populate comment if 'id' exists
        if len(comments) > 0:
            c = comments[0]

            comment = Comment(c['post_id'], c['name'], c['content'],
                              c['email'])

        return comment
예제 #28
0
    def get(self, topic_id):
        topic = Topic.get_by_id(int(topic_id))
        # get comments
        comments = (Comment.query(
            Comment.topic_id == topic_id,
            Comment.deleted == False).order(-Comment.create_time).fetch()
        )

        params = {
            "topic": topic,
            "comments": comments
        }

        user = users.get_current_user()
        if user:
            subscribed = Subscription.query(
                Subscription.user_id == user.email(),
                Subscription.topic_id == topic_id).fetch()
            if subscribed:
                params["subscribed"] = True

        return self.render_template_with_csrf("topic_details.html", params)
예제 #29
0
    def insertComment(self, jokeid, userid, content):
        if jokeid == '':
            self.status = 201
            self.message = 'jokeid不能为空'
            jsonStr = self.getJsonResult()

            self.write(jsonStr)
        elif userid == '':
            self.status = 201
            self.message = 'userid不能为空'
            jsonStr = self.getJsonResult()
            self.write(jsonStr)
        elif content == '':
            self.status = 201
            self.message = 'content不能为空'
            jsonStr = self.getJsonResult()
            self.write(jsonStr)
        elif not self.checkUser(userid):
            self.status = 201
            self.message = '该userid不存在'
            jsonStr = self.getJsonResult()

            self.write(jsonStr)
        elif not self.checkJoke(jokeid):
            self.status = 201
            self.message = '该jokeid不存在'
            jsonStr = self.getJsonResult()

            self.write(jsonStr)
        else:
            commentid = uuid.uuid1().hex
            newcomment = Comment()
            newcomment.commentid = commentid
            newcomment.userid = userid
            newcomment.jokeid = jokeid
            newcomment.content = content

            self.db.add(newcomment)
            self.db.commit()
            self.db.close()

            self.status = 200
            jsonStr = self.getJsonResult()

            self.write(jsonStr)
예제 #30
0
    def post(self, comment_id):
        Comment.delete_comment(comment_id)

        return self.redirect(self.request.referer)
예제 #31
0
    def get(self, topic_id):
        count_comments = Comment.query(
            Comment.topic_id == topic_id,
            Comment.deleted == False).count()

        return self.write(count_comments)