Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (92 loc) · 3.68 KB
/
Copy pathmain.py
File metadata and controls
123 lines (92 loc) · 3.68 KB
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
113
114
115
116
117
118
119
120
121
122
123
# encoding:utf-8
#!/usr/bin/env python
# imports básicos
import os
import webapp2
import jinja2
# import del modulo creado con el datastore
from modelos import RegistroContacto
# imports de modulos necesarios para usar oauth2
from oauth2client.appengine import OAuth2Decorator
from apiclient.discovery import build
import httplib2
# Crear una variable estática del entorno de nuestros templates
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.abspath('templates')),
autoescape=True,
extensions=['jinja2.ext.autoescape']
)
# creación del objeto decorator, objeto utilizado para OAuth2
decorator = OAuth2Decorator(
client_id='<TU_ID_DE_CLIENTE>',
client_secret='<TU_CLIENTE_SECRETO>',
scope='https://www.googleapis.com/auth/userinfo.profile ' +
'https://www.googleapis.com/auth/userinfo.email ',
)
# construcción del servicio de userinfo
http = httplib2.Http()
userinfo_service = build('oauth2', 'v2', http=http)
# Una clase controladora de peticiones HTTP (get, post, put, delete)
class ControladorInicio(webapp2.RequestHandler):
@decorator.oauth_aware
def get(self):
# validar si el usuario Google esta o no autentificado
if not decorator.has_credentials():
# creamos un objeto con parámetro para injectarlo en el template
variables = {
'url': decorator.authorize_url(),
'has_credentials': decorator.has_credentials()
}
# declaración y obtención de un template
template = JINJA_ENVIRONMENT.get_template('login.html')
# renderizar el template con las variables injectadas
self.response.write(template.render(variables))
else:
# en este caso se utiliza la autenticación y utilizar
# el servicio userinfo_service y obtener datos del usuario
http = decorator.http()
data = userinfo_service.userinfo().get().execute(http=http)
variables = {
'data': data,
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(variables))
class ControladorRegistros(webapp2.RequestHandler):
@decorator.oauth_required
def get(self):
# presentar el formulario de ingreso de registro
template = JINJA_ENVIRONMENT.get_template('form_registro.html')
self.response.write(template.render())
def post(self):
# recibir parametro por POST
nombre = self.request.get('nombre')
email = self.request.get('email')
comentario = self.request.get('comentario')
# crear instancia del objeto de RegistroContacto
registro = RegistroContacto()
registro.nombre = nombre
registro.email = email
registro.comentario = comentario
# metodo de guardar objeto en el DataStore
registro.put()
# forma de redirigir a una url en webapp2
self.redirect('/')
class ControladorListadoRegistros(webapp2.RequestHandler):
@decorator.oauth_required
def get(self):
# se crea una instancia del objeto registro
registro = RegistroContacto()
# llamar el metodo creado en el modulo modelos.py
registros = registro.listar_registros()
variables = {
'registros': registros,
}
template = JINJA_ENVIRONMENT.get_template('listar_registros.html')
self.response.write(template.render(variables))
# Manejo de url de nuestra aplicación
app = webapp2.WSGIApplication([
('/', ControladorInicio),
('/registro', ControladorRegistros),
('/listado', ControladorListadoRegistros),
(decorator.callback_path, decorator.callback_handler()),
], debug=True)