-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md
499 lines (389 loc) · 13.9 KB
/
README.md
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# **PyGram** Social Network
<p>PyGram is a social network that developed with python.
here are the main features of this project:
</p>
1. Home Page
2. Sign Up and Login (and account verification)
3. User Profile (dashboard)
4. Direct messages
5. Chat with others
6. Search users and pages
7. Users single page
8. Sharing new post and delete post
9. Followers and Following pages
10. Like and Comment on posts
# Getting Started
- [Project Structure](#Project-Structure)
- [Dependencies](#Dependencies)
- [Requirements](#Requirements)
- [Site Routes](#Site-Routes)
- [Project Modules](#BluePrints)
- [Data Base Diagram](#Database-Diagram)
- [About redis](#Redis)
- [About sqlalchemy](#SqlAlchemy)
- [About flask-migration](#Flask-Migration)
- [View site pages](#Site-Pages)
# Project-Structure
```bash
PyGram
├───app.py
├───requirements.txt
├───dependencies.txt
├───routes.txt
├───README.md
├───.env
├───.gitignore
├───Documentation
├───migrations
└───PyGram
├───__init__.py
├───controller.py
├───models.py
├───config.py
├───mod_direct_message
│ ├───__init__.py
│ └───controller.py
├───mod_follow
│ ├───__init__.py
│ └───controller.py
├───mod_post
│ ├───__init__.py
│ └───controller.py
├───mod_profile
│ ├───__init__.py
│ └───controller.py
├───mod_search
│ ├───__init__.py
│ └───controller.py
├───mod_setting
│ ├───__init__.py
│ └───controller.py
├───mod_user_page
│ ├───__init__.py
│ └───controller.py
├───static
│ ├───Site-Screen
│ ├───img
│ ├───styles
│ │ └───all of css files..
│ └───upload
│ ├───dir_of_user1
│ ├───dir_of_user2
│ └───dir_of_user3
├───Templates
│ └───all of html files..
└───utils
├───check_auth.py
├───registration.py
└───upload.py
```
# Dependencies
| Modules | Description
|---------------------------|-----------------------------------------------
| flask | web framework
| python-dotenv | for read dotenv files
| flask-sqlalchemy | ORM
| mysqlclient or pymysql | mysql connector
| flask-migrate | database migration
| flask-jwt-extended | for Authorization (access and refreshh token)
| flask-mail | for sending email
| redis | we use this nosql database for saving users registration token
| flask_whooshalchemy | search engine
# Requirements
```python
>> pip freeze
alembic==1.4.2
blinker==1.4
click==7.1.2
Flask==1.1.2
Flask-Autodoc==0.1.2
Flask-JWT-Extended==3.24.1
Flask-Mail==0.9.1
Flask-Migrate==2.5.3
Flask-Selfdoc==1.2.3
Flask-SQLAlchemy==2.4.1
itsdangerous==1.1.0
Jinja2==2.11.2
Mako==1.1.2
MarkupSafe==1.1.1
PyJWT==1.7.1
PyMySQL==0.9.3
python-dateutil==2.8.1
python-dotenv==0.13.0
python-editor==1.0.4
redis==3.4.1
six==1.14.0
SQLAlchemy==1.3.16
Werkzeug==1.0.1
Whoosh==2.7.4
```
# Site-Routes
| Endpoint | Methods | Rule
|---------------------------|-----------|---------------------------------------------
|direct.chat | GET | /direct/chat/<string:username>
|direct.direct_index | GET | /direct/<string:username>
|direct.send_message | POST | /direct/send_message/<string:username>
|follow.follow_index | GET | /follow/<string:username>
|index | GET | /
|login | GET, POST | /login/
|post.comment | POST | /post/comment/<string:username>/<string:date>
|post.delete_post | GET | /post/delete/<string:username>/<string:date>
|post.like | GET | /post/like/<string:username>/<string:date>
|post.new_post | GET, POST | /post/new/<string:username>
|post.post_index | GET | /post/<string:username>
|post.single_post | GET | /post/single/<string:username>/<string:date>
|profile.logout | GET | /profile/logout/
|profile.profile_index | GET | /profile/<string:username>
|search.search | GET | /search/search/
|setting.setting_index | GET, POST | /setting/<string:username>
|signup | GET, POST | /signup/
|signup_process | GET | /signup-process/
|static | GET | /static/<path:filename>
|user_page.follow_process | GET | /user_page/follow-process/<string:username>/
|user_page.unfollow_process | GET | /user_page/unfollow-process/<string:username>/
|user_page.user_page_index | GET | /user_page/<string:username>
# BluePrints
- mod_direct_message
- mod_follow
- mod_post
- mod_profile
- mod_search
- mod_setting
- mod_user_page
# Database-Diagram
![Database Models Diagram](Documentation/PyGramDB.png)
# Redis
Redis => [Key Value DataBse] your data have a key for example => mtaghizadeh_register : 12345 [mtaghizadeh_register is username and mode is register and token is 12345]
- download redis :
https://github.com/antirez/redis/issues/6276
- Check Redis Connection:
1. in cmd(admin)
```python
>>> redis-cli
>>> ping
>>> pong
```
2. in services
- install redis in python or venv
```python
>>> pip install redis
```
- add redis key in flask config (.env)
```python
>> REDIS_SERVER_URL=redis://127.0.0.1:6379 # redis default port
```
- import redis in app.py:
```python
>> from redis import Redis
>> redis = Redis.from_url(app.config['REDIS_SERVER_URL'])
```
- test in flask sell:
```python
>>> from app import redis
>>> redis.ping()
>>> True # if connection is ok.
```
- redis add:
```python
>> redis.set(key, value, expiry_date(s))
```
- redis get value by key:
```python
>> redis.get(name=key)
>>> GET mtaghizadeh_register # (in redis-cli)
```
- redis delete:
```python
>> redis.delete(key)
```
# SQLAlchemy
SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.
Documentation: https://www.sqlalchemy.org/
- for using SqlAlchemy ORM in flask we should install extension of "flask-sqlalchemy"
```python
>>> pip install flask-sqlalchemy
```
- mysql-connector:
```python
>>> pip install pymysql
or
>>> pip install mysql-client
```
- create models in flask shell:
```python
>>> from app import db
>>> create_all()
```
- create models with flask-migration
```python
>>> flask db init
>>> flask db migrate
>>> flask db upgrade or downgrade
```
- create class as a entity(table) of database
```python
class User(db.Model):
__tablename__ = "users"
name = Column(String(32), nullable = False)
lastname = Column(String(32), nullable = False)
email = Column(String(256), nullable = False)
username = Column(String(64), primary_key = True)
country = Column(String(32), nullable = False)
password = Column(String(128), nullable = False)
avatar = Column(String(256), nullable = False, default = "/static/upload/avatar.png")
active = Column(Boolean, nullable = False, default = False)
# One To Many:
# list of posts for create a foreign key we use backreference to list of this mode(Parent) from another model
posts = relationship("Post", backref="user")
# and other list for anothers classes
class Post(db.Model):
__tablename__ = "posts"
username = Column(String(64), ForeignKey('users.username'), primary_key = True) # Foreign Key is primary key of parent table
caption = Column(Text(), nullable = True)
picture = Column(String(256), nullable = False)
post_date = Column(DateTime(), default = dt.datetime.utcnow, primary_key = True)
```
- select * from table
```python
user = User.query.all()
```
- select from table where
```python
user = User.query.filter_by(username="mtaghizadeh").first()
or
user = User.query.filter(User.username="mtaghizadeh").first()
```
- delete from table where
```python
post = Post.query.filter_by(username=username, post_date=date).first()
db.session.delete(post)
db.session.commit()
```
- add or update table
```python
# add user to database:
user = User()
user.name = name
user.lastname = lastname
user.email = email
user.username = username
user.country = country
user.password = password
db.session.add(user)
db.session.commit()
```
- Pagination in sql alchemy and sql or command
```python
from sqlalchemy import or_ ### by default is and
q = request.args.get("q")
name_cnd = User.name.ilike(f'%{q}%')
lastname_cnd = User.lastname.ilike(f'%{q}%')
username_cnd = User.username.ilike(f'%{q}%')
### using or_ SQLAlchemy : by default is and
# before pagination
# found_users = User.query.filter(or_(
# name_cnd,
# lastname_cnd,
# username_cnd
# )).all()
# pagination
p = request.args.get("p", default=1, type=int)
found_users = User.query.filter(or_(
name_cnd,
lastname_cnd,
username_cnd
)).paginate(p, 5)
```
- join in tables
```python
# Join Follow and post for get post of followers of this user
following_cnd = Follow.query.filter_by(follower_username = username).all()
query = db.session.query(
Follow,
Post
).filter(
Follow.follower_username == user.username
).filter(
Follow.following_username == Post.username
).order_by(Post.post_date.desc()).all()
```
# Flask-Migration
for simple altering in database (tracking all of database changes) :)))
```python
>> pip install flask-migrate
>> flask db init ---> create migration folder (just in starting project)
# then by default create migration directory of your project and create alembic table in your database
```
### if you have any change in your model:
```python
>>> flask db migrate # alter your db if you change it...
>> flask db upgrade # commit the changes(upgrade)
>> flask db downgrade
```
# Site-Pages
### Index Page
home page of site that contains login and signup functionality
![Database Models Diagram](Documentation/Site-Pages/1-index.png)
### Signup Page
User can register in this page
![Database Models Diagram](Documentation/Site-Pages/2-signup.png)
- signup process completed
![Database Models Diagram](Documentation/Site-Pages/3-signup.png)
- user active filed in database is Flase(0) before activation
![Database Models Diagram](Documentation/Site-Pages/4-signup.png)
- we create random token between [00000 , 99999] and save that in redis database
![Database Models Diagram](Documentation/Site-Pages/5-signup.png)
- after registeration we send an email to user email account with **flask-mail**
that contains a link like this:
http://127.0.0.1:5000/signup-process/?username=mtaghizadeh&token=266781
![Database Models Diagram](Documentation/Site-Pages/6-signup.png)
- after activtivation we delete token from redis database
![Database Models Diagram](Documentation/Site-Pages/7-signup.png)
- after activtivation we create directory(with user.username) for upload user avatr and post.pictures
![Database Models Diagram](Documentation/Site-Pages/8-signup.png)
### Login
user can login to pygram
- user should be activate
![Database Models Diagram](Documentation/Site-Pages/9-login.png)
### Setting Page
user can change profile picture in this page
- user can **upload** any image for changing profile avatar
![Database Models Diagram](Documentation/Site-Pages/10-setting.png)
### User Posts Page
user can share new post in this page and see all of posts published nad manage that (delete or modify)
- in first time this page like this
![Database Models Diagram](Documentation/Site-Pages/11-post.png)
- after sharing posts
![Database Models Diagram](Documentation/Site-Pages/12-post.png)
- user can sharing post with picture and caption
![Database Models Diagram](Documentation/Site-Pages/13-new-post.png)
- user can choose any image from his computeer
![Database Models Diagram](Documentation/Site-Pages/14-new-post.png)
- after sharing post, upload post picture in user directory in server and path of file save in database (Post Table)
- any file upload in sever with random name from uuid library
![Database Models Diagram](Documentation/Site-Pages/15-new-post.png)
### Search Page
user can search any user in this page
- we paginate all result with **SQLAlchemy paginate() method**
![Database Models Diagram](Documentation/Site-Pages/16-search.png)
### Single User Page
- after searching user or click on user page link user can see page of single user
![Database Models Diagram](Documentation/Site-Pages/17-single_user_page.png)
- user can follow or unfollow and send direct message to single user
![Database Models Diagram](Documentation/Site-Pages/18-single_user_page.png)
### Single User Post
- all of information of this single post
- user can **like and comment** this post
![Database Models Diagram](Documentation/Site-Pages/19-single-post.png)
### Follower and Following Page
- in this page user can see all of his followers and following
- user can click any user and redirect to single user page
![Database Models Diagram](Documentation/Site-Pages/20-follower-following.png)
# Single Direct Message Page(Chat Page)
- user can send message to another user and see message order by sending date
![Database Models Diagram](Documentation/Site-Pages/21-single-direct-message.png)
### User Profile Page
- in this page user can see all of posts that sharing with his following
- user can like that and can send comment to this post
![Database Models Diagram](Documentation/Site-Pages/22-profile.png)