-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
416 lines (335 loc) · 18.9 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from flask import Flask, render_template, url_for, request, redirect, \
session, make_response, g, flash, abort, Markup
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required
from datetime import datetime, timedelta, date
import requests, time, dateutil.parser, pytz, os, sqlite3
from flask.ext.mail import Message, Mail
from flask_wtf import Form
from flask.ext.wtf import Form
from wtforms import TextField, IntegerField, DecimalField, StringField, SelectField, TextAreaField, SubmitField, BooleanField, validators
from wtforms.validators import Required, DataRequired, ValidationError
from wtforms.validators import DataRequired
from wtforms.fields.html5 import DateField, EmailField
from flask.ext.admin.form import widgets
from flask_celery import make_celery
from flask.ext.sqlalchemy import get_debug_queries # works with the gist debugging sqlachemy
import pdfkit
from flask_login import current_user
#from flask_debugtoolbar import DebugToolbarExtension
###########################################################################
################ invoke-rc.d rabbitmq-server start ########################
################ celery -A app.celery worker --loglevel=info ##############
###########################################################################
###########################################################################
################ settings
###########################################################################
###########################################################################
########################################################################
## ### ### ### ### ### ##### #### ##### ###
# ####### ########## ######### ######## ##### # ### ### ######## #######
## ##### ######## ######### ######## ##### ## ## ### # #### #####
#### ### ########## ######### ######## ##### ### # ### ### ####### ###
# #### ###### ######### ###### ### #### #### #### ####
########################################################################
# Create app
app = Flask(__name__)
app.config.from_pyfile('config.py')
# Make sure debug = False in production
db = SQLAlchemy(app)
celery = make_celery(app)
#mail=Mail(app)
# Setup Flask-Mail
app.config.update(
DEBUG=True,
#EMAIL SETTINGS for google
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT=465,
MAIL_USE_SSL=True,
# Dont forget to include user name and password
MAIL_USERNAME = '',
MAIL_PASSWORD = '',
MAIL_FAIL_SILENTLY = 'False'
)
mail=Mail(app)
######################################################
# ##### ##### ##### ###### ### ######## ###
# ### #### ### #### ### #### ####### ####### #######
# # # # ### ##### ### #### ### ##### ######## #####
# ## ## #### ### #### ### #### ####### ########## ###
# ##### ##### ##### ###### ### ### ####
######################################################
# Creates a many to many relationship for roles and users
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
# User models
class Role(db.Model, RoleMixin):
__tablename__= 'role'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
__tablename__= 'user'
id = db.Column(db.Integer, primary_key=True, unique=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
first_name = db.Column(db.String(255))
last_name = db.Column(db.String(255))
active = db.Column(db.Boolean())
roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic'))
confirmed_at = db.Column(db.DateTime())
last_login_at = db.Column(db.DateTime(), default=datetime.now())
current_login_at = db.Column(db.DateTime())
last_login_ip = db.Column(db.String(24))
current_login_ip = db.Column(db.String(24))
login_count = db.Column(db.Integer())
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
class Subscribers(db.Model):
id = db.Column(db.Integer, primary_key=True, unique=True)
email = db.Column(db.String(255), unique=False)
area = db.Column(db.String(255))
def __init__(self, email=email, area=area):
self.email = email
self.area = area
#############################################
# ##### ##### #### ##### #### ###
# ######## ### #### ### ### ### ### #######
# ##### ##### ### #### # # # #### #####
# ######## ### #### ### ### ## ## ###### ###
# ######### ##### ### ### ##### ### ####
#############################################
#class ContactForm(Form):
# firstName = TextField('First Name', [validators.DataRequired("Enter your first name")])
# lastName = TextField('Last Name', [validators.DataRequired("Enter your last name")])
# email = TextField('E-mail', [validators.DataRequired("Enter a valid email address"), validators.Email("Enter a valid email address")])
# subject = TextField('Subject', [validators.DataRequired("What's the nature of your message?")])
# message = TextAreaField('Message', [validators.DataRequired("Didn't you want to say something?")])
# submit = SubmitField('Send')
# Contact Form
class ContactForm(Form):
email = TextField('E-mail', [validators.DataRequired("Enter a valid email address"), validators.Email("Enter a valid email address")])
message = TextAreaField('Message', [validators.DataRequired("Didn't you want to say something?")])
submit = SubmitField('Send')
####################################################################################
# ### #### ### #### ##### ### ### ### ####### #### ###
#### ###### ####### ########## ####### ##### ##### ##### ####### ####### ### #######
#### ###### ###### ######## ######## ### ###### ##### ##### ### ### #### #####
#### ###### ########## ###### ######### # ####### ##### ######## ## ## ####### ###
#### ###### ### ####### ########## ###### ### ##### ### ##### ####
####################################################################################
@app.route('/cu')
def cu():
return render_template('cu.html', current_user=current_user)
@app.route('/googlef5ce05674f2c2ab6.html')
def googleverify():
return render_template('googlef5ce05674f2c2ab6.html')
###############################################
# ##### ### ### ### ####### #### ###
# ##### ##### ##### ####### ####### ### #######
## ### ###### ##### ##### ### ### #### #####
### # ####### ##### ######## ## ## ####### ###
#### ###### ### ##### ### ##### ####
###############################################
#@app.route('/', methods=('GET', 'POST'))
#def index():
#
# form = ContactForm()
# if request.method == 'POST':
# if form.validate() == False:
# flash('You must enter something into all of the fields')
# return render_template('index.html', form = form)
# else:
# msg = Message(form.subject.data, sender='[email protected]', recipients=['[email protected]'])
# msg.body = """
# From: %s %s <%s>
# %s
# """ % (form.firstName.data, form.lastName.data, form.email.data, form.message.data)
# mail.send(msg)
# return render_template('index.html', success=True)
# elif request.method == 'GET':
# return render_template('index.html',
# title = 'Contact Us',
# form = form)
# landing page, no login required
@app.route('/developer', methods=('GET', 'POST'))
def home():
form = ContactForm()
if request.method == 'POST':
if form.validate() == False:
flash('You must enter something into all of the fields')
return render_template('index.html', form = form)
else:
msg = Message(subject="Little Web", sender='[email protected]', recipients=['[email protected]'])
msg.body = """
From: <%s>
%s
""" % (form.email.data, form.message.data)
# Without Celery
mail.send(msg)
# With Celery
#send_async_email.delay(msg)
return render_template('index.html', success=True)
elif request.method == 'GET':
return render_template(
'index.html',
title='Home',
year=datetime.now().year,
message='Your contact page.',
form=form)
############################################
# ### ### ###### #### ###
#### ######## ##### ### #### ####### #######
#### ######## ##### #### ### ###### #####
#### ######## ##### ### #### ########## ###
#### ###### ### ###### ### ####
############################################
@app.route('/', methods=['GET', 'POST'])
def tides():
if request.method == 'POST':
area = request.form['area']
if request.form['email'] != '':
email = request.form['email']
newSubscriber = Subscribers(email, area)
db.session.add(newSubscriber)
db.session.commit()
if area == 'Margate' or 'Port Edward' or 'Port Shepstone' or 'Ramsgate' or 'Southbroom' or 'Amazimtoti' or 'Durban Central' or 'Umhlanga' or 'Richards Bay' or 'Ballito' or 'Kingsburgh' or 'Scottsburgh' or 'St Lucia':
if area == "Richards Bay" or area == "St Lucia":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Richards_Bay.json")
elif area == "Hibberdene" or area == "Port Shepstone":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Port_Shepstone.json")
elif area == "Port Edward" or area == "Southbroom":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Port_Edward.json")
elif area == "Ramsgate" or area == "Margate":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Margate.json")
else:
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Durban.json")
tides = requests.get("https://www.worldtides.info/api?extremes&lat=-29.850833&lon=30.993056&key=7438a1be-d38c-45f5-91dd-ef96f379d048")
if area == "Hibberdene" or area == "Port Shepstone":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Port_Shepstone.json")
elif area == "Port Edward" or area == "Southbroom":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Port_Edward.json")
elif area == "Ramsgate" or area == "Margate":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Margate.json")
elif area == "Richards Bay" or area == "St Lucia":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Richards_Bay.json")
else:
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Durban.json")
elif area == 'Port St Johns' or 'East London' or 'Port Alfred' or 'Port Elizabeth' or 'Jefferys Bay':
if area == "Port St Johns":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Port_Saint_Johns.json")
elif area == "East London":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/East_London.json")
elif area == "Port Alfred":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Port_Alfred.json")
elif area == "Port Elizabeth":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Port_Elizabeth.json")
elif area == "Port Alfred" or area == "Jefferys Bay":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Port_Alfred.json")
tides = requests.get("https://www.worldtides.info/api?extremes&lat=-33.029&lon=27.855&key=7438a1be-d38c-45f5-91dd-ef96f379d048")
if area == "Port St Johns":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Port_Saint_Johns.json")
elif area == "East London":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/East_London.json")
elif area == "Port Alfred":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Port_Alfred.json")
elif area == "Port Elizabeth":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Port_Elizabeth.json")
elif area == "Port Alfred" or area == "Jefferys Bay":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Port_Alfred.json")
elif area == 'Simons Town' or 'Hout Bay' or 'Camps Bay' or 'Green Point' or 'Hermanus' or 'Mossel Bay' or 'George' or 'Plettenberg Bay':
if area == "Hermanus":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Hermanus.json")
elif area == "Mossel Bay":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Mossel_Bay.json")
elif area == "George":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/George.json")
elif area == "Plettenberg Bay":
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Plettenberg_Bay.json")
else:
current = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/conditions/q/ZA/Cape_Town.json")
tides = requests.get("https://www.worldtides.info/api?extremes&lat=-33.925&lon=18.424&key=7438a1be-d38c-45f5-91dd-ef96f379d048")
if area == "Hibberdene":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Hermanus.json")
elif area == "Mossel Bay":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Mossel_Bay.json")
elif area == "George":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/George.json")
elif area == "Plettenberg Bay":
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Plettenberg_Bay.json")
else:
forecast = requests.get("http://api.wunderground.com/api/408fe6a2816fb9d0/forecast/q/ZA/Cape_Town.json")
json_object = current.json()
json_object2 = tides.json()
json_object3 = forecast.json()
tides = []
#time0 = dateutil.parser.parse(time0)
tz = pytz.timezone('Africa/Johannesburg')
today_counter = 0
tomorrow_counter = 0
day_after_counter = 0
today = datetime.now().strftime('%Y-%m-%d ')
tomorrow = datetime.today() + timedelta(days=1)
tomorrow = tomorrow.strftime('%Y-%m-%d ')
for digit in range(0, 7):
tide = json_object2['extremes'][digit]['type']
time = datetime.fromtimestamp(json_object2['extremes'][digit]['dt'], tz)
date = time.strftime('%Y-%m-%d ')
if date == today:
if today_counter == 0:
time = time.strftime('%H:%M' + ' today')
tides.append({tide:time})
today_counter = 1
else:
time = time.strftime('%H:%M')
tides.append({tide:time})
elif date == tomorrow:
if tomorrow_counter == 0:
time = time.strftime('%H:%M' + ' tomorrow')
tides.append({tide:time})
tomorrow_counter = 1
else:
time = time.strftime('%H:%M')
tides.append({tide:time})
else:
if day_after_counter == 0:
time = time.strftime('%H:%M' + ' day after')
tides.append({tide:time})
day_after_counter = 1
else:
time = time.strftime('%H:%M')
tides.append({tide:time})
weather = json_object['current_observation']['weather']
temp = json_object['current_observation']['temp_c']
wind_kph = json_object['current_observation']['wind_kph']
wind_dir = json_object['current_observation']['wind_dir']
icon_url1 = json_object['current_observation']['icon_url']
today = json_object3['forecast']['txt_forecast']['forecastday'][0]['fcttext_metric']
tomorrow = json_object3['forecast']['txt_forecast']['forecastday'][2]['fcttext_metric']
icon_url2 = json_object3['forecast']['txt_forecast']['forecastday'][0]['icon_url']
icon_url3 = json_object3['forecast']['txt_forecast']['forecastday'][2]['icon_url']
copyright = json_object2['copyright']
if area:
return render_template('tides.html',
area=area,
weather=weather,
temp=temp,
wind_dir=wind_dir,
wind_kph=wind_kph,
icon_url1=icon_url1,
icon_url2=icon_url2,
icon_url3=icon_url3,
tides=tides,
today=today,
tomorrow=tomorrow,
copyright=copyright,
date=datetime.now(),
title='Easy Tides'
)
else:
return render_template('fetchtides.html', title='Easy Tides')
if __name__ == '__main__':
app.run()