Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Creating blog
- Homework for python-courses
- """
- import pymysql.cursors
- import hashlib
- import uuid
- from collections import defaultdict
- class BlogApp:
- tokens_dict = defaultdict(list)
- def add_new_user(self, user_username, user_email, user_password):
- connection = pymysql.connect(host='localhost',
- user='BlogApp_User',
- password='BlogApp_Password',
- db='BlogApp',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- salt = b'some_salt'
- hash_obj = hashlib.md5()
- hash_obj.update(salt)
- hash_obj.update(user_password.encode('utf-8'))
- try:
- with connection.cursor() as cursor:
- sql = "INSERT INTO User (Username, Email, Password) VALUES (%s, %s, %s)"
- cursor.execute(sql, (user_username, user_email, hash_obj))
- connection.commit()
- with connection.cursor() as cursor:
- sql = "SELECT UserID, Username, Password FROM User WHERE Email=%s"
- cursor.execute(sql, user_email)
- result = cursor.fetchone()
- print(result)
- finally:
- connection.close()
- def authentication_user(self, user_email, user_password):
- connection = pymysql.connect(host='localhost',
- user='BlogApp_User',
- password='BlogApp_Password',
- db='BlogApp',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- sql = "SELECT * FROM User WHERE Email=%s AND Password=%s"
- cursor.execute(sql, (user_email, user_password))
- result = cursor.fetchone()
- if result is not None:
- user_token = uuid.uuid4()
- self.tokens_dict[user_email].append(user_token)
- connection.commit()
- finally:
- connection.close()
- token_status = True if user_email in self.tokens_dict else False
- print(token_status)
- return token_status
- def get_users_list(self):
- connection = pymysql.connect(host='localhost',
- user='BlogApp_User',
- password='BlogApp_Password',
- db='BlogApp',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- sql = "SELECT Username FROM User"
- cursor.execute(sql)
- result = cursor.fetchall()
- connection.commit()
- finally:
- connection.close()
- print(result)
- return result
- def get_auth_user_blogs_list(self):
- connection = pymysql.connect(host='localhost',
- user='BlogApp_User',
- password='BlogApp_Password',
- db='BlogApp',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- sql = "SELECT Username, Email FROM User"
- cursor.execute(sql)
- result = cursor.fetchall()
- connection.commit()
- print(result)
- finally:
- connection.close()
- for user in result:
- if user['Email'] in self.tokens_dict:
- print(user)
- return result
- def get_blogs_list(self):
- connection = pymysql.connect(host='localhost',
- user='BlogApp_User',
- password='BlogApp_Password',
- db='BlogApp',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- sql = "SELECT BlogName, BlogDescription FROM Blog"
- cursor.execute(sql)
- result = cursor.fetchall()
- connection.commit()
- finally:
- connection.close()
- print(result)
- return result
- def create_blog(self):
- pass
- def edit_blog(self):
- pass
- def delete_blog(self):
- pass
- def create_post(self):
- pass
- def edit_post(self):
- pass
- def delete_post(self):
- pass
- def add_comment(self):
- pass
- def get_user_comments(self):
- pass
- a = BlogApp()
- # print(a.tokens_dict)
- a.add_new_user('sexbeast', '111@gmail.com', '111')
- # print(a.tokens_dict)
- # a.authentication_user('111@gmail.com', '111')
- # print(a.tokens_dict)
- # a.get_users_list()
- # print(a.tokens_dict)
- # a.get_auth_user_blogs_list()
- # print(a.tokens_dict)
- # a.get_blogs_list()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement