-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (91 loc) · 3.57 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
# coding: utf-8
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
from models import RegistraCafe
from google.appengine.ext.webapp import template
from google.appengine.api import users
from datetime import datetime, timedelta
from google.appengine.api import mail
from google.appengine.api import memcache
import os
# HOrario de verão
#timezone = 2
#Horario Normal
timezone = 3
#Modo de desenvolvimento
DEBUG = False
def doRender(handler, tname='index.html', values={}):
temp = os.path.join(os.path.dirname(__file__),'templates/' + tname)
if not os.path.isfile(temp):
return False
# Make a copy of the dictionary and add the path
newval = dict(values)
newval['path'] = handler.request.path
outstr = template.render(temp, newval)
handler.response.out.write(outstr)
return True
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write('Hello world!')
class Logout(webapp2.RequestHandler):
def get(self):
self.redirect(users.create_logout_url('/'))
class HomePageHandler(webapp2.RequestHandler):
def get(self):
context = memcache.get('context_home')
if not context:
context = {}
context['fezcafe'] = RegistraCafe.getJaFez()
L = []
for item in RegistraCafe.getAll().fetch(5):
L.append({'username': item.userName,
'dateTimeText': item.dateTimeText})
context['dados'] = L
context['quantidade'] = RegistraCafe.getQuantidadeCafe()
memcache.add('context_home',context, 21600)
context['user'] = users.get_current_user()
doRender(self,'homepage.pt',context)
def post(self):
user_logado = users.get_current_user()
date = datetime.now() - timedelta(hours=timezone)
if not RegistraCafe.getJaFez() and\
(date.hour >= 9 and date.hour < 18) and\
(date.weekday() != 5 and date.weekday() != 6) or DEBUG == True:
fez = RegistraCafe(user = user_logado,
date_creation = date)
fez.save()
memcache.delete('context_home')
todos_usuarios = RegistraCafe.getQuantidadeCafe()
for item in todos_usuarios:
user = item.get('user_email','')
self.sendMail(user, date, user_logado.email())
self.redirect('/')
def sendMail(self,send_to,date,user):
msg = """ O Usuario %s Fez o cafe as %s .
""" %(user,date.strftime('%d/%m/%Y %H:%M'))
message = mail.EmailMessage()
message.sender = 'cesaraugusto@liberiun.com'
message.to = send_to
message.subject = '** AVISO - QUEM FEZ CAFE **'
message.body = msg.decode( 'utf-8', 'ignore')
message.send()
L = [('/', HomePageHandler),
('/hello', MainHandler),
('/logout', Logout),
]
app = webapp2.WSGIApplication(L,debug=DEBUG)