コード例 #1
0
ファイル: main.py プロジェクト: hyungmogu/multi-user-blog
    def post(self, post_id):
        data = json.loads(self.request.body)
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))
        title = data["title"]
        content = data["content"]
        user = User.get_by_id(int(cookie_val.split("|")[0]))

        if not self.blog_exists(blog):
            message = "Invalid. The requested page doesn't exist."
            self.send_response(404, message)
            return
        if not self.is_signed_in(cookie_val):
            message = "Invalid. Only signed in User can post comments"
            self.send_response(401, message)
            return
        if not (title and content):
            message = "Invalid. Title and texts must not be empty."
            self.send_response(400, message)
            return

        comment = Comment(title=title, content=content, blog=blog, author=user)
        comment_id = comment.put().id()

        message = json.dumps({
            "success":
            "Comment successfully added to database.",
            "id":
            comment_id,
            "title":
            title,
            "content":
            content,
            "author":
            user.username,
            "date_created":
            comment.date_created.strftime("%B %d %Y %I:%M%p")
        })
        self.send_response(200, message)
コード例 #2
0
    def post(self, post_id):
        post_button = self.request.get("postbutton").split(',')
        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)
        user_key = self.user.key()

        # When the user clicks 'like'
        if post_button[0] == "like":
            # If the user already liked the post, decrease likes_total (unlike
            # post)
            if self.user.key().id() != post.postuser.key().id():
                if user_key in post.likedby:
                    post.likedby.remove(user_key)
                    post.likes_total -= 1
                    post.put()
                else:
                    # Increase the likes_total, and append the user to 'likedby'
                    # list
                    post.likes_total += 1
                    post.likedby.append(user_key)
                    post.put()
                self.redirect('/blog/%s' % str(post.key().id()))
                return

        elif post_button[0] == "delete":
            if self.user.key().id() != post.postuser.key().id():
                post.delete()
                self.redirect('/')
                return
            else:
                self.redirect('/login')
                return

        # Add new comment to the post
        elif post_button[0] == "comment":
            if self.user:
                comment_text = self.request.get("comment_text")
                commentuser = self.user
                newcomment = Comment(parent=post,
                                     commentuser=commentuser,
                                     commenttext=comment_text)
                newcomment.put()
                self.redirect('/blog/%s' % str(post.key().id()))
                return
            else:
                self.redirect('/login')
                return

        # Edit existing post comment
        elif post_button[0] == "editcomment":
            key = db.Key.from_path('Comment',
                                   int(post_button[1]),
                                   parent=post.key())
            c = db.get(key)
            # If the logged in user is also the comment author, open the
            # comment in edit mode
            if c.commentuser.key().id() == self.user.key().id():
                c.editmode = True
                c.put()
            self.redirect('/blog/%s' % str(post.key().id()))
            return

        # Delete existing comment
        elif post_button[0] == "deletecomment":
            key = db.Key.from_path('Comment',
                                   int(post_button[1]),
                                   parent=post.key())
            c = db.get(key)
            if c and self.user.key().id() == c.commentuser.key().id():
                c.delete()
                self.redirect('/blog/%s' % str(post.key().id()))
                return
            else:
                self.redirect('/login')
                return

        # Submit the edited comment to the db
        elif post_button[0] == "submitcommentedit":
            key = db.Key.from_path('Comment',
                                   int(post_button[1]),
                                   parent=post.key())
            c = db.get(key)
            if c and self.user.key().id() == c.commentuser.key().id():
                c.commenttext = self.request.get("comment_edit_text")
                c.editmode = False
                c.put()
                self.redirect('/blog/%s' % str(post.key().id()))
                return

        else:
            comments = db.GqlQuery(
                "SELECT * FROM Comment WHERE ANCESTOR IS :1 ", post)
            for c in comments:
                c.editmode = True
                c.put()
                self.redirect('/blog/%s' % str(post.key().id()))
コード例 #3
0
ファイル: comments.py プロジェクト: thiur/worldwidelexicon
 def requesthandler(self):
     guid = self.request.get('guid')
     cl = self.request.get('cl')
     comment = clean(self.request.get('comment'))
     if len(cl) < 1 and len(comment) > 4:
         cl = TestLanguage.language(text=comment)
     remote_addr = self.request.remote_addr
     ip = self.request.get('ip')
     if len(ip) > 0:
         remote_addr = ip
     username = self.request.get('username')
     pw = self.request.get('pw')
     session=''
     location = geo.get(remote_addr)
     if type(location) is dict:
         try:
             city = location['city']
             state= location['state']
             country= location['country']
         except:
             city = ''
             state = ''
             country = ''
         try:
             latitude=location['latitude']
             longitude=location['longitude']
         except:
             latitude = None
             longitude = None
     if len(comment) > 5 and len(guid) > 7:
         emptyform=False
     else:
         emptyform=True
     if not emptyform:
         spamchecked = False
         akismetkey = Settings.get('akismet')
         root_url = Settings.get('root_url')
         if len(root_url) > 0 and string.count(root_url, 'http://') < 1:
             root_url = 'http://' + root_url
         a = Akismet()
         a.setAPIKey(akismetkey, blog_url = root_url)
         if a.verify_key():
             data = dict()
             data['user_ip']=remote_addr
             data['user_agent']=self.request.headers['User-Agent']
             if a.comment_check(comment, data):
                 spam=True
             else:
                 spam=False
             spamchecked=True
         else:
             spam=False
             spamchecked=False
         result = False
         if len(username) > 0:
             session = Users.auth(username=username, pw=pw, session='')
             if len(session) < 8:
                 username=''
         if not spam:
             tdb = db.Query(Translation)
             tdb.filter('guid = ', guid)
             item = tdb.get()
             if item is not None:
                 md5hash = item.md5hash
                 sl = item.sl
                 tl = item.tl
                 st = item.st
                 tt = item.tt
                 domain = item.domain
                 url = item.url
                 professional = item.professional
                 author = item.username
                 cdb = db.Query(Comment)
                 cdb.filter('guid = ', guid)
                 cdb.filter('remote_addr = ', remote_addr)
                 item = cdb.get()
                 if item is None:
                     item = Comment()
                     item.guid = guid
                     item.md5hash = md5hash
                     item.tl = tl
                     item.cl = cl
                     item.comment = comment
                     item.username = username
                     item.spamchecked = spamchecked
                     item.spam = spam
                     item.remote_addr = remote_addr
                     timestamp = datetime.datetime.now()
                     item.minute = timestamp.minute
                     item.hour = timestamp.hour
                     item.day = timestamp.day
                     item.month = timestamp.month
                     item.year = timestamp.year
                     item.domain = domain
                     item.url = url
                     item.city = city
                     item.state = state
                     item.country = country
                     try:
                         item.latitude = latitude
                         item.longitude = longitude
                     except:
                         pass
                     item.put()
                     if professional and len(author) > 0:
                         LSP.comment(guid, comment, lsp=author, username=username, remote_addr=remote_addr)
                     result = True
         self.response.headers['Content-Type']='text/plain'
         if result:
             self.response.out.write('ok')
         else:
             self.error(500)
             self.response.out.write('error')
     else:
         tdb = db.Query(Translation)
         tdb.order('-date')
         item = tdb.get()
         if item is not None:
             guid = item.guid
         else:
             guid = ''
         t = '<table><form action=/comments/submit method=post accept-charset=utf-8>'
         t = t + '<tr><td>GUID of Translation (guid)</td><td><input type=text name=guid value="' + guid + '"></td></tr>'
         t = t + '<tr><td>Comment (comment)</td<td><input type=text name=comment></td></tr>'
         t = t + '<tr><td>Username (username, optional)</td><td><input type=text name=username></td></tr>'
         t = t + '<tr><td>Password (pw, optional)</td><td><input type=text name=pw></td></tr>'
         t = t + '<tr><td colspan=2><input type=submit value=SUBMIT></td></tr></table></form>'
         www.serve(self,t,sidebar=self.__doc__, title = '/comments/submit')