-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNOTES.txt
More file actions
264 lines (204 loc) · 8.46 KB
/
NOTES.txt
File metadata and controls
264 lines (204 loc) · 8.46 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
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
Blog1:
- Creating a Virtual Environment:
python3 -m venv envName
- Activate the Environment:
$ venv\Scripts\activate
- Setting up environment variable for our flask app
export FLASK_APP=microblog.py
- To run our flask app
flask run
-setting flask debug mode
set FLASK_DEBUG=0 or 1
Chapter 5:
Werkzeug is used for password hashing technique.
>>> from werkzeug.security import generate_password_hash
>>> hash=generate_password_hash("foobar")
>>> hash
>>> check_password_hash(hash, 'foobar') TRUE
Chapter 6: part-vi-profile-page-and-avatars
Avatars:
A. Creating a profile page and adding an avatar, will use Gravatar which will take the email has value for a dynamic image generation
Ex:
>>> from hashlib import md5
>>> 'https://www.gravatar.com/avatar/' + md5(b'john@example.com').hexdigest()
'https://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6'
B. Added the subtemplate _post.html
C. Added the Edit Profile section.
D. Flask migration
flask db migrate -m "Added two new fields in User Model: about_me and last_seen"
flask db upgrade
E. Added before_request for updating the last_seen column for user
F. Added the Edit Profile Form in the Forms.py with 3 fields as of now.
Chapter 7: Error Handling.
- Using Flask Debugger for debugging the code.
A. Adding Custom Error Pages 404.html and 500.html.
B. Setting up the Email Configuration for showing the Errors(Didn't work up).
C. Setting up File Logger for the Debugging, it will save .log file and save the logs.
D. Set the FLASK_DEBUG as default true or 1 in .flaskenv file.
Chapter 8: Adding Followers
- For Implementing Followers and Followed, we can't simply use RD tables as we need many to many relationship, so for that we need an association table.
- Added a new Association table for maintaining followers and following count.
- Added followers and following methods to find out count of following, followers, follow unfollow etc.
- Added method to query many to many relationship DB for retrieving user, post and follower and their posts results
- Utilizing unittest package to test out our DB model, added tests.py file for testing our functionalities.
Chapter 9: Adding Posts from the DB and explore Page, and Pagination.
- Showing the posts from the DB now on the profile.
- Adding the explore section to look for other people's posts.
- Adding the Pagination on Posts on Explore and Profile Page.
Chapter 10: Adding Email Support for Password Reset.
- Using Brevo SMTP Service for Free Email Service.
- Implemented JWT for resetting link.
- Added Password Reset Link in the Reset Password Mail using JWT Authentication.
Chapter 11: Added Bootstrap Flask Framework for CSS Changes.
- Added Bootstrap and its implementation specifically based for Flask apps using Macros.
- Created a separate file bootstrap_wtf.html which handles the various forms UI using Macros in Python
Chapter 12: Solving UTC to the local time for the Users using Flask-Moment(Moment.js for Flask).
- Utilized Moment.js + flask_moment package to reformat the Date from UTC to local timezone for post and user profile sections.
Chapter 13: L18n and L10n (Localization and Internationalization).
- Adding the Translation workflow for the other non English speakers.
- Utilizing the flask babel and langdetect packages to transpile the language from one format to the other and detect the language of the post and store this value in the DB for that post.
- If the locale language is different from the language of the post, then the page will get automatically translated.
Chapter 14: Language Translation option using AJAX.
- using langdetect module in python.
- Utilizing pythonanywhere free text translation API as other APIs are paid.
- If we enter some post text different than the locale langauge, then we will get a translation link which will translate the text in realtime for that post.
Chapter 15: Improvising the Application Structure.
- Refactoring of the Overall Application.
- We used Blueprint methodology to create separate functionalities for different parts of our application
- It was a total complex task ngl.
- Fixed the Reset Mail issue also.
- Added .flaskenv and .env for env variables setup.
Chapter 20: Popover on Profile using JS.
- Using Bootstrap Popover property to add a dynamic popover on hovering on the profile of the User.
- It will show the Details of the user.
Chapter 21: Adding Notification for the User.
- Sending message to each other.
- Incorporated Websockets for implementing the realtime Messaging Service.
- The interval based notification updation.
Chapter 23: Application Programming Interface Providing.
- Adding different API routes for our application.
- API endpoints to give the User related info in JSON format to the user in the browser, but only the information.
- Used the httpie library.
Routes:
-Return a user.
GET /api/users/<id>
-Return the collection of all users.
GET /api/users
-Return the followers of this user.
GET /api/users/<id>/followers
-Return the users this user is following.
GET /api/users/<id>/following
-Register a new user account.
POST /api/users
http POST http://localhost:5000/api/users username=test password=test@123 email=test@example.com "about_me=Hello, my name is TEST."
-Modify a user.
PUT /api/users/<id>
Generting a Token:
http --auth <username>:<password> POST http://localhost:5000/api/tokens
Using token to access APIs:
http -A bearer --auth <token> GET http://localhost:5000/api/users/1
#### DATABASE ACCESS:
Temp User Credentials:
username: Alice
password: Alice@123
# Start with running python first
-- importing the app
>>> from flaskapp import app, db
>>> from flaskapp.models import User, Post
>>> import sqlalchemy as sa
-- Pushing the app context
>>> app.app_context().push()
-- Adding the User
u = User(username="alice", email="alice@example.com")
db.session.add(u)
db.session.commit()
u
--Querying the DB
>>> query = sa.select(User)
>>> users = db.session.scalars(query).all()
>>> users
or
>>> users = db.session.scalars(query)
>>> for u in users:
... print(u.id, u.username)
Query based on the parameter, like id:
>>> u = db.session.get(User, 1)
>>> u
<User susan>
Adding a Blog post:
>>> u = db.session.get(User, 1)
>>> p = Post(body='my first post!', author=u)
>>> db.session.add(p)
>>> db.session.commit()
>>> # print post author and body for all posts
>>> query = sa.select(Post)
>>> posts = db.session.scalars(query)
>>> for p in posts:
... print(p.id, p.author.username, p.body)
...
1 john my first post!
# get all users in reverse alphabetical order
>>> query = sa.select(User).order_by(User.username.desc())
>>> db.session.scalars(query).all()
[<User susan>, <User john>]
# get all users that have usernames starting with "s"
>>> query = sa.select(User).where(User.username.like('s%'))
>>> db.session.scalars(query).all()
[<User susan>]
# Pagination
>>> query = sa.select(Post).order_by(Post.timestamp.desc())
>>> posts = db.paginate(query, page=1, per_page=20, error_out=False).items
### BULK INSERTION IN DB FROM SCRATCH:
from flaskapp import app, db
from flaskapp.models import User, Post
import sqlalchemy as sa
app.app_context().push()
query = sa.select(User)
users = db.session.scalars(query).all()
users
USERS SHOULD BE CREATED FROM REGISTRATION METHOD(So that we can login later on)
u = User(username="alice", email="alice@example.com")
db.session.add(u)
db.session.commit()
u = User(username="bob", email="bob@example.com")
db.session.add(u)
db.session.commit()
u = User(username="clara", email="clara@example.com")
db.session.add(u)
db.session.commit()
u = User(username="daisy", email="daisy@example.com")
db.session.add(u)
db.session.commit()
(Inserting Posts for the Registered Users)
u = db.session.get(User, 1)
p = Post(body='Weather is Good!', author=u)
db.session.add(p)
db.session.commit()
p = Post(body='Traffic is High!', author=u)
db.session.add(p)
db.session.commit()
u = db.session.get(User, 2)
p = Post(body='Life is Good!', author=u)
db.session.add(p)
db.session.commit()
p = Post(body='Life Sucks!', author=u)
db.session.add(p)
db.session.commit()
u = db.session.get(User, 3)
p = Post(body='Need to Sleep!', author=u)
db.session.add(p)
db.session.commit()
p = Post(body='Sleep is Life!', author=u)
db.session.add(p)
db.session.commit()
u = db.session.get(User, 4)
p = Post(body='The power of Now!', author=u)
db.session.add(p)
db.session.commit()
p = Post(body='Now is everything!', author=u)
db.session.add(p)
db.session.commit()
query = sa.select(Post)
posts = db.session.scalars(query)
for p in posts:
print(p.id, p.author.username, p.body)