-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
217 lines (196 loc) · 8.05 KB
/
app.py
File metadata and controls
217 lines (196 loc) · 8.05 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
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
# import libraries
from flask import Flask, session, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import sqlite3 as sql
from forms import *
import datetime
# -----------------
# create flask app
app = Flask(__name__)
app.config.from_object('config')
# connect sqlite database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///E:\\my_file\\Projectes\\flaskBlog\\blog.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# create user table in blog.db to assign users in it
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(255), nullable=False)
password = db.Column(db.String(255), nullable=False)
posts = db.relationship('Post', backref='user')
def __init__(self, name, email, password):
self.name = name
self.email = email
self.password = password
def to_json(self):
dict = {k:v for k,v in self.__dict__.items() if k != '_sa_instance_state'}
return dict
# create post table in blog.db to contain all posts
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Text(1500), nullable=False)
content = db.Column(db.String(50), nullable=False)
date_posted = db.Column(db.DateTime, default=datetime.datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'),
nullable=False)
# create the login page
@app.route('/login', methods=['POST', 'GET'])
def login(): # an exisiting user
form = Myform()
message = ''
if request.method == 'POST':
session.pop('logged_in', None)
if form.validate_on_submit():
getemail = request.form.get('email')
getpassword = request.form.get('password')
all_emails = [i[0] for i in User.query.with_entities(User.email).all()]
if str(getemail) in all_emails:
user = User.query.filter_by(email=getemail).first()
password = user.password
if password == str(getpassword):
the_dict = user.to_json()
session['logged_in'] = the_dict
message = 'hello %s' %str(the_dict['name'])
return redirect(url_for('dashboard'))
else:
message = 'password is not correct!'
else:
message = 'email not registered!'
return render_template('login.html', form=form, message=message)
# create the main page that will show all posts that have been published
@app.route('/')
def index():
name, message = '', ''
posts = Post.query.all()
all_post = []
if posts:
if len(posts) <=6:
for post in posts:
date_posted = post.date_posted.strftime("%d %B, %Y")
if len(str(post.content)) <=145:
limit_content = str(post.content)
else:
limit_content = str(post.content)[:145]+'....'
all_post.append([post.title,post.content, post.user.name,date_posted, limit_content, post.id])
else:
for i in range(0, 6+1):
post = posts[i]
date_posted = post.date_posted.strftime("%d %B, %Y")
if len(str(post.content)) <=145:
limit_content = str(post.content)
else:
limit_content = str(post.content)[:145]+'....'
all_post.append([post.title,post.content, post.user.name, date_posted, limit_content, post.id])
else:
message = 'No post has been sent yet!'
current_user = session.get('logged_in', False)
if current_user:
name = current_user['name']
return render_template('index2.html', posts=all_post, user=name, message=message)
@app.route('/post<int:postid>')
def post(postid):
post = Post.query.filter_by(id=postid).first()
return render_template('post.html', post=post)
@app.route('/posts<int:userid>')
def posts(userid):
message= ''
user_posts = Post.query.filter_by(user_id=userid).all()
all_post = []
if user_posts:
for post in user_posts:
date_posted = post.date_posted.strftime("%d %B, %Y")
if len(str(post.content)) <=145:
limit_content = str(post.content)
else:
limit_content = str(post.content)[:145]+'....'
all_post.append([post.title,post.content, post.user.name, date_posted, limit_content, post.id])
else:
message = 'You have not sent any post yet!'
return render_template('posts.html', posts=all_post, message=message)
# create dashboard to enable the user to publish and delete posts
@app.route('/dash', methods=['POST', 'GET'])
def dashboard():
current_user = session.get('logged_in', False)
if current_user:
name = current_user['name']
user = User.query.filter_by(name=name).first()
print(user.name, user.id)
return render_template('dash.html', user=user)
else:
meassage = 'Login First Please!'
return redirect(url_for('login', meassage=meassage))
# create add post page to add new posts
@app.route('/addpost', methods=['POST', 'GET'])
def addPost():
form = Addform()
message = ''
if request.method == 'POST':
if form.validate_on_submit():
title = request.form.get('title')
content = request.form.get('content')
current_user = session.get('logged_in', False)
if current_user:
try:
user = User.query.filter_by(id=current_user['id']).first()
post = Post(title=title, content=content, user=user)
db.session.add(post)
db.session.commit()
message = 'Post send successfully'
except Exception as e:
raise 'Sorry there is somethine wrong happend!'
else:
message = 'User not found!'
return render_template('add.html', message=message, form=form)
# create delete post page to delete posts
@app.route('/delpost<int:postid>', methods=['POST', 'GET', 'DELETE'])
def deletePost(postid):
# form = Delform()
message = ''
# if request.method == 'POST':
# if form.validate_on_submit():
# post_id = request.form.get('number')
if session.get('logged_in', False):
post = Post.query.filter_by(id=postid).first()
user_id = session['logged_in']['id']
if post.user_id == user_id:
Post.query.filter_by(id=postid).delete()
db.session.commit()
message = 'Post deleted successfully'
else:
message = 'You can not delete this post!'
else:
message = 'Something went wrong!'
return render_template('del.html', message=message)
# create logout page to pop the user from session
@app.route('/logout')
def logout():
current_user = session.get('logged_in', False)
if current_user:
session.pop('logged_in', None)
return redirect(url_for('index'))
# create register page to assign new user to database
@app.route('/register', methods=['POST', 'GET'])
def register(): # add new user to database
message = ''
status = False
form = Regform()
if request.method == 'POST':
if form.validate_on_submit():
name = request.form.get('name')
email = request.form.get('email')
password = request.form.get('password')
all_emails = [i[0] for i in User.query.with_entities(User.email).all()]
if email not in all_emails:
new_user = User(name, email, password)
db.session.add(new_user)
db.session.commit()
message = 'New user resigsted'
status = True
else:
message = 'The email is already registed!'
# sleep(5)
# return redirect(url_for('register'))
return render_template('reg.html', form=form, message=message, status=status)
if __name__ == '__main__':
app.run(debug=True)