-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·109 lines (89 loc) · 3.45 KB
/
Copy pathmain.py
File metadata and controls
executable file
·109 lines (89 loc) · 3.45 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
#!./venv/bin/python3
# Copyright 2018 Google LLC
#
# 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.
# [START gae_python37_render_template]
# FLASK_APP=main.py MAIL_USERNAME=holbietextme@gmail.com MAIL_PASSWORD=ryuichiiscool python main.py
import datetime
from flask import flash, Flask, render_template, redirect, request, url_for
from flask_mail import Mail, Message
from google.cloud import datastore
from os import getenv
from threading import Thread
app = Flask(__name__)
app.url_map.strict_slashes = False
# app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'hard to guess string' # might not need
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = getenv('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = getenv('MAIL_PASSWORD')
app.config['MAIL_SENDER'] = getenv('MAIL_USERNAME')
app.config['SSL_REDIRECT'] = False
mail = Mail(app)
storage = datastore.Client()
def store_time(dt):
entity = datastore.Entity(key=storage.key('visit'))
entity.update({
'timestamp': dt
})
storage.put(entity)
def fetch_times(limit):
query = storage.query(kind='visit')
query.order = ['-timestamp']
times = query.fetch(limit=limit)
return times
@app.route('/')
@app.route('/index.html')
def root():
# Store the current access time in Datastore.
store_time(datetime.datetime.now())
# Fetch the most recent 10 access times from Datastore.
times = fetch_times(10)
return render_template('index.html', times=times)
@app.route('/api/user', methods=['POST'])
def api_user():
try:
User = datastore.Entity(key=storage.key('user'))
tmp = {}
for key, value in request.json().items():
tmp[key] = value
tmp['created_at'] = datetime.datetime.now()
User.update(tmp)
storage.put(User)
return 'success'
except Exception as e:
return e
def send_email_async(app, msg):
with app.app_context():
mail.send(msg)
def send_email(to, subject): # , template, **kwargs):
msg = Message(subject, sender=app.config['MAIL_SENDER'], recipients=[to])
# render_template(template + '.txt', **kwargs)
msg.body = 'this is the body'
# render_template(template + '.html', **kwargs)
msg.html = '<h1>header html</h1>'
thr = Thread(target=send_email_async, args=[app, msg])
thr.start()
return thr
if __name__ == '__main__':
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
# Flask's development server will automatically serve static files in
# the "static" directory. See:
# http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
# App Engine itself will serve those files as configured in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
# [START gae_python37_render_template]