-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_tweet.py
54 lines (51 loc) · 1.7 KB
/
edit_tweet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import webapp2
import jinja2
from google.appengine.api import users
from google.appengine.ext import ndb
from myuser import MyUser
from profile import Profile
from tweet import Tweet
import logging
import datetime
import os
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)
class EditTweet(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
user = users.get_current_user()
myuser_key = ndb.Key('MyUser', user.user_id())
myuser = myuser_key.get()
x = self.request.get('value_1')
template_values = {
'myuser' : myuser,
'x' : x
}
template =JINJA_ENVIRONMENT.get_template('edit_tweet.html')
self.response.write(template.render(template_values))
def post(self):
action = self.request.get('button')
if action == 'Submit':
user = users.get_current_user()
myuser_key = ndb.Key('MyUser', user.user_id())
myuser = myuser_key.get()
x = self.request.get('value')
y = self.request.get('tweet')
if x in myuser.tweet:
myuser.tweet.remove(x)
myuser.tweet.append(y)
myuser.put()
tweet_key = ndb.Key('Tweet', x )
tweet = tweet_key.get()
tweet.key.delete()
tweet = Tweet(id=y)
tweet.username = myuser.username
tweet.tweet = y
tweet.tweettime = datetime.datetime.now()
tweet.put()
self.redirect('/profile')
elif action == 'Back':
self.redirect('/')