def save_tweet(self, req): text = req.request.get("text") file = req.get_uploads() if not text: return { "message": "Please enter tweet", "status": False, "records": {} } if len(text) > 280: return { "message": "Only 280 characters allowed", "status": False, "records": {} } upload = None if file: upload = file[0].key() info = blobstore.BlobReader(upload) if info.blob_info.content_type not in ['image/jpeg', 'image/png']: blobstore.delete(upload) return { "status": False, "message": "Only png PNG and JPEG can be uploaded" } userv = AccountFunction.current_user() tkey = AccountFunction.tweet_key(userv.user_name) Tweet = TweetEntity(id=tkey, tweet=text, user_name=userv.user_name, user_email=userv.email, tweet_image=upload) Tweet.put() userv.tweet_count = userv.tweet_count + 1 userv.put() document = search.Document(doc_id=tkey, fields=[ search.TextField(name='tweet', value=text), search.TextField(name='user_name', value=userv.user_name) ], language='en') search.Index(name='tweets').put(document) return { "message": "Tweet saved successfully", "status": True, "records": {} }
def get(self): self.response.headers['Content-Type'] = 'text/html' template_value = self.template_value(self) current_user = AccountFunction.current_user() if current_user.user_name == self.request.params["user"]: return self.redirect("/profile") # logging.info(self.request.params) # params = self.request.params # if params: # params['user'] # # template_value["username"] = params['user'] template = JINJA_ENVIRONMENT.get_template('show_profile.html') self.response.write(template.render(template_value))
def update_tweet(self, req): text = req.request.get("text") if not text: return { "message": "Please enter tweet", "status": False, "records": {} } if len(text) > 280: return { "message": "Only 280 characters allowed", "status": False, "records": {} } userv = AccountFunction.current_user() t_key = ndb.Key( 'TweetEntity', userv.user_name + '/' + req.request.params["edit-tweet"]) tweet = t_key.get() tweet.tweet = text tweet.put() # userv.tweet_count = userv.tweet_count - 1 # userv.put() index = search.Index('tweets') index.delete(userv.user_name + '/' + req.request.params["edit-tweet"]) document = search.Document(doc_id=userv.user_name + '/' + req.request.params["edit-tweet"], fields=[ search.TextField(name='tweet', value=text), search.TextField(name='user_name', value=userv.user_name) ], language='en') search.Index(name='tweets').put(document) return { "message": "Tweet updated successfully", "status": True, "records": {} }