def get(self): is_admin = users.is_current_user_admin() forum = Forum.from_url(self.request.path_info) user = users.get_current_user() if not forum or (forum.is_disabled and not is_admin): return self.redirect("/") if not is_admin and not self.settings.check_ip(self.request.remote_addr): return self.response.out.write('Your IP address has been banned') if not is_admin and not self.settings.check_user( user ): return self.redirect( users.create_login_url("/") ) # Get user either by google user id or cookie if user: fuser = FofouUser.gql("WHERE user = :1", user).get() else: fuser = FofouUser.gql("WHERE cookie = :1", self.cookie ).get() tvals = { 'user': user, 'isadmin': is_admin, 'forum': forum, 'fuser': fuser or { 'email': user.email() if user else "", 'name': user.nickname() if user else "", 'remember_me': True }, 'post': { 'subject': '' }, 'login_url' : users.create_login_url(self.request.url), 'logout_url' : users.create_logout_url(self.request.url) } topic_id = self.request.get('id') if topic_id: tvals['topic'] = db.get(db.Key.from_path('Topic', int(topic_id))) if not tvals['topic']: return self.redirect( forum.root() ) self.template_out("skins/default/post.html", tvals)
def post(self): forum = Forum.from_url(self.request.path_info) is_admin = users.is_current_user_admin() user = users.get_current_user() if not forum or (forum.is_disabled and not is_admin): return self.redirect("/") if not is_admin and not self.settings.check_ip(self.request.remote_addr): return self.response.out.write('Your IP address has been banned') if not is_admin and not self.settings.check_user( user ): return self.redirect( users.create_login_url("/") ) name = self.request.get('name').strip() email = self.request.get('email').strip() subject = self.request.get('subject').strip() message = to_unicode( self.request.get('message') ).strip() homepage = self.request.get('homepage').strip() homepage = "" if homepage == "http://" else homepage remember = bool(self.request.get('remember')) try: topic_id = int( self.request.get('topic_id') or 0 ) if topic_id: topic = db.get(db.Key.from_path('Topic', topic_id)) else: topic = None except ValueError: topic = None if topic and topic.is_locked: return self.redirect( "%stopic?id=%s" % (forum.root(), topic.id) ) # Perform simple validation errors = { 'valid': True } # First post must have a subject if not topic and not subject: errors['valid'] = False errors['subject'] = "Subject required for new topic" if not message: errors['valid'] = False errors['message'] = "Message is required" # sha.new() doesn't accept Unicode strings, so convert to utf8 first sha1_digest = sha.new( message.encode('UTF-8') ).hexdigest() if Post.gql("WHERE sha1_digest = :1 AND topic = :2", sha1_digest, topic).get(): errors['valid'] = False errors['message'] = "This is a duplicate post" if not errors['valid']: return self.template_out("skins/default/post.html", { 'isadmin': is_admin, 'user': user, 'errors': errors, 'forum': forum, 'topic': topic and { 'id': topic_id, 'subject': topic.subject }, 'post': { 'message': message, 'subject': subject }, 'fuser': { 'name': name, 'email': email, 'homepage': homepage, 'remember_me': remember } }) # Get user either by google user id or cookie. Create user objects if don't already exist if user: fuser = FofouUser.gql("WHERE user = :1", user).get() else: fuser = FofouUser.gql("WHERE cookie = :1", self.cookie).get() if not fuser: fuser = FofouUser( user = user or users.User('*****@*****.**'), remember_me = remember, email = email or '*****@*****.**', name = name or 'Anonymous', homepage = homepage, cookie = self.cookie ) else: fuser.remember_me = remember fuser.email = email or '*****@*****.**' fuser.name = name or 'Anonymous' fuser.homepage = homepage if not topic: topic = Topic(forum=forum, subject=subject, created_by=fuser.name) forum.num_topics += 1 else: topic.ncomments += 1 forum.num_posts += 1 topic.put() fuser.put() post = Post( topic = topic, user = fuser, user_ip = self.request.remote_addr, message = message, sha1_digest = sha1_digest, user_name = fuser.name, user_email = fuser.email, user_homepage = homepage ) post.put() forum.put() self.redirect( "%stopic?id=%s" % (forum.root(), topic.id) )