-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit_page.py
80 lines (61 loc) · 2.36 KB
/
edit_page.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import cgi
import os
import webapp2
import facebook
from google.appengine.ext.webapp import template
from google.appengine.api import memcache, urlfetch
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from google.appengine.ext import db, search, webapp
from basehandler import BaseHandler
from model import User, Item, DisplayItem
class EditPage(BaseHandler):
"""
This handler is responsible for the edit page (when a user
wants to edit an item already stored in the datastore).
It displays the same postPortal, but with the fields
set to the old values of the item.
The url of this page will be of the form
/edit_item=.*
Here .* is the item's key.
"""
def get(self):
self.setupUser()
# parse the item key from the URL
itemKey = self.request.url.split("%3D")[1]
item = db.get(itemKey)
# pass the item object to the template
values = {'items':item}
path = os.path.join(os.path.dirname(__file__), 'edit_page.html')
self.response.out.write(template.render(path,values))
def post(self):
numArgs = len(self.request.arguments())
if numArgs == self.POST:
# Retrieve item
itemKey = self.request.url.split("%3D")[1]
item = db.get(itemKey)
# Resave all the information of the item
item.itemName = self.request.get('itemName')
item.price = self.request.get('price')
item.description = self.request.get('description')
item.category = self.request.get('category')
# Saves in datastore again
item.put()
# Redirect sell profile page
self.redirect('/seller_profile')
if numArgs == self.SEARCH:
# this is a search
self.redirect('/search=' + self.request.get('category') + '&' \
+ self.request.get('query'))
# this is probably bad
config = {}
config['webapp2_extras.sessions'] = dict(secret_key='1234')
application = webapp.WSGIApplication(
[('/edit_item=.*', EditPage)],
config=config,
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()