-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
112 lines (105 loc) · 5.25 KB
/
controller.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
#
import os
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from model.Categoria import Categoria
from model.Coisa import Coisa
class ControllerInicial(webapp.RequestHandler):
def get(self):
template_values = {
'texto': 'Página Inicial',
}
path = os.path.join(os.path.dirname(__file__), 'view/inicial.html')
self.response.out.write(template.render(path, template_values))
class ControllerCategoria(webapp.RequestHandler):
def get(self, pCmd = '', pKey = ''):
template_values = {}
if pCmd == 'listar' or pCmd == '':
objs = Categoria.all()
objs.order('descricao')
template_values = {'objs': objs, 'qtd' : objs.count()}
path = os.path.join(os.path.dirname(__file__), 'view/categorias.html')
self.response.out.write(template.render(path, template_values))
elif pCmd == 'novo':
obj = Categoria(descricao = '')
template_values = {'obj' : obj, 'key' : ''}
path = os.path.join(os.path.dirname(__file__), 'view/categoria.html')
self.response.out.write(template.render(path, template_values))
elif pCmd == 'editar':
obj = db.get(db.Key(pKey))
template_values = {'key' : obj.key(), 'descricao' : obj.descricao}
path = os.path.join(os.path.dirname(__file__), 'view/categoria.html')
self.response.out.write(template.render(path, template_values))
elif pCmd == 'excluir':
obj = db.get(db.Key(pKey))
obj.delete()
self.redirect('/categoria/listar')
def post(self, pCmd = ''):
template_values = {}
if pCmd == 'salvar':
if self.request.get('key') == '':
obj = Categoria(descricao = self.request.get('descricao'))
obj.put()
else:
obj = db.get(db.Key(self.request.get('key')))
obj.descricao = self.request.get('descricao')
obj.put() #db.put(obj)
self.redirect('/categoria/listar')
class ControllerCoisa(webapp.RequestHandler):
def get(self, pCmd = '', pKey = ''):
template_values = {}
if pCmd == 'listar' or pCmd == '':
objs = Coisa.all()
objs.order('nome')
template_values = {'objs': objs}
path = os.path.join(os.path.dirname(__file__), 'view/coisas.html')
self.response.out.write(template.render(path, template_values))
elif pCmd == 'novo':
cats = Categoria.all().order('descricao')
template_values = {'cats' : cats}
path = os.path.join(os.path.dirname(__file__), 'view/coisa.html')
self.response.out.write(template.render(path, template_values))
elif pCmd == 'editar':
obj = db.get(db.Key(pKey))
cats = Categoria.all().order('descricao')
template_values = {'key' : obj.key(), 'nome' : obj.nome, 'descricao' : obj.descricao,
'preco' : obj.preco, 'categoria' : obj.categoria.key(), 'cats' : cats}
path = os.path.join(os.path.dirname(__file__), 'view/coisa.html')
self.response.out.write(template.render(path, template_values))
def post(self, pCmd = ''):
template_values = {}
if pCmd == 'salvar':
if self.request.get('key') == '':
obj = Coisa()
obj.nome = self.request.get('nome')
obj.descricao = self.request.get('descricao')
prc = self.request.get('preco').replace(',', '.')
obj.preco = float(prc)
obj.categoria = db.Key(self.request.get('categoria'))
obj.put()
else:
obj = db.get(db.Key(self.request.get('key')))
obj.nome = self.request.get('nome')
obj.descricao = self.request.get('descricao')
prc = self.request.get('preco').replace(',', '.')
obj.preco = float(prc)
obj.categoria = db.Key(self.request.get('categoria'))
obj.put()
self.redirect('/coisa/listar')
def main():
application = webapp.WSGIApplication([('/', ControllerInicial),
('/inicio.*', ControllerInicial),
('/categoria', ControllerCategoria),
(r'/categoria/(.*)/(.*)', ControllerCategoria),
(r'/categoria/(.*)', ControllerCategoria),
('/coisa', ControllerCoisa),
(r'/coisa/(.*)/(.*)', ControllerCoisa),
(r'/coisa/(.*)', ControllerCoisa),
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()