-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
93 lines (73 loc) · 2.59 KB
/
index.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
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
import json
from flask_mail import Mail
with open('templates/config.json', 'r') as c:
parameter = json.load(c)["parameter"]
app = Flask(__name__)
# app.config.update(
# MAIL_SERVER = 'smtp.gmail.com',
# MAIL_PORT = '465',
# MAIL_USE_SSL = True,
# MAIL_USERNAME = '[email protected],
# MAIL_PASSWORD= 'mail_password'
# )
# SQLALCHEML is an ORM
mail = Mail(app)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:@localhost/blog"
db = SQLAlchemy(app)
class Contact(db.Model):
'''sr_no name email phone message '''
sr_no = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(30), nullable=False)
email = db.Column(db.String(30), nullable=False)
phone = db.Column(db.String(12), nullable=False)
message = db.Column(db.String(200), nullable=True)
class Posts(db.Model):
'''sr_no title tagline slug content date '''
sr_no = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), nullable=False)
tagline = db.Column(db.String(100), nullable=False)
slug = db.Column(db.String(50), nullable=False)
content= db.Column(db.String(500), nullable=False)
date = db.Column(db.String(20), nullable=True)
@app.route("/")
def home():
posts = Posts.query.filter_by().all()[0:5]
return render_template('home.html', param=parameter, posts=posts)
@app.route("/about")
def about():
name = "Mayank Khurmai"
return render_template("about.html", fname=name)
@app.route("/post/<string:post_slug>", methods=['GET'])
def post_route(post_slug):
post = Posts.query.filter_by(slug=post_slug).first()
return render_template('post.html', params=parameter, post=post)
@app.route("/contact", methods = ['GET', 'POST'])
def contact():
if(request.method=='POST'):
''' Adding entry to the database'''
''' sr_no name email phone message '''
name = request.form.get('name')
email = request.form.get('email')
phone = request.form.get('phone')
message = request.form.get('message')
entry = Contact(name=name, email = email, phone = phone, message = message)
db.session.add(entry)
db.session.commit()
mail.send_message('New message from ' + name,
sender=email,
recipients = "[email protected]",
body = message + "\n" + phone
)
return render_template('contact.html')
app.run(debug=True)
#
#
#
#
#
#
#
#
# Ending of Python Script