-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOfferWeb.py
More file actions
93 lines (77 loc) · 3.44 KB
/
OfferWeb.py
File metadata and controls
93 lines (77 loc) · 3.44 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
from flask import Flask, render_template, send_from_directory
import os
from flask_sqlalchemy import SQLAlchemy
import datetime as dt
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
##CONNECT TO DB
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("DB_URI")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_POOL_RECYCLE'] = 1800 # 30 minutes
app.config['SQLALCHEMY_POOL_TIMEOUT'] = 30 # 30 seconds
db = SQLAlchemy(app)
##CONFIGURE TABLES
class OfferWeb(db.Model):
__tablename__ = "offer_list"
id = db.Column(db.Integer, primary_key=True)
store = db.Column(db.String(250), nullable=False)
region = db.Column(db.String(250), nullable=False)
offer_title = db.Column(db.String(250), nullable=False)
valid = db.Column(db.Integer, nullable=False)
s_date = db.Column(db.String(250), nullable=False)
e_date = db.Column(db.String(250), nullable=False)
featured = db.Column(db.Integer, nullable=False)
fe_date = db.Column(db.String(250), nullable=False)
img_url = db.Column(db.String(250), nullable=False)
file_src = db.Column(db.String(250), unique=True, nullable=False)
with app.app_context():
db.create_all()
def validity():
format = '%d-%m-%Y'
for offer in db.session.execute(db.select(OfferWeb)).scalars():
date_e = dt.datetime.strptime(offer.e_date, format).date()
date_s = dt.datetime.strptime(offer.s_date, format).date()
if offer.fe_date != 0:
date_fe = dt.datetime.strptime(offer.fe_date, format).date()
else:
date_fe = 0
today = dt.datetime.today().date()
if date_s > today or date_e < today:
offer.valid = 0
else:
offer.valid = 1
if date_fe < today or date_fe == 0:
offer.featured = 0
else:
offer.featured = 1
if today > (date_e + dt.timedelta(days=30)):
db.session.delete(offer)
db.session.commit()
@app.route("/")
def home():
validity()
feature = [fe_off for fe_off in db.session.execute(db.Select(OfferWeb)).scalars() if fe_off.featured == 1 and fe_off.valid == 1]
stores = {store.store for store in db.session.execute(db.Select(OfferWeb)).scalars()}
return render_template("home.html", featured=feature, stores=stores, year=dt.date.today().year)
@app.route("/about")
def about():
return render_template("about.html", year=dt.date.today().year)
@app.route("/contact")
def contact():
return render_template("contact.html", year=dt.date.today().year, phone="+91xxxxxxxxxx", email="example@gmail.com")
@app.route("/store/<name>")
def store(name):
validity()
offer_store = [offers for offers in db.session.execute(db.Select(OfferWeb)).scalars() if offers.valid == 1 and offers.store == name]
e_offer = [offers for offers in db.session.execute(db.Select(OfferWeb)).scalars() if offers.valid != 1 and offers.store == name]
return render_template("store.html", name=name, val=offer_store, exp=e_offer, year=dt.date.today().year)
@app.route("/offer/<int:off_id>")
def offer(off_id):
validity()
offer = [offer for offer in db.session.execute(db.Select(OfferWeb)).scalars() if offer.valid == 1 and offer.id == off_id][0]
return render_template("offer.html", offer=offer, year=dt.date.today().year)
@app.route('/ads.txt')
def ads_txt():
return send_from_directory(directory='.', path='ads.txt')
if __name__ == '__main__':
app.run(host='0.0.0.0')