Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 KB | None | 0 0
  1. """
  2. Creating blog
  3. Homework for python-courses
  4. """
  5.  
  6. import pymysql.cursors
  7. import hashlib
  8. import uuid
  9. from collections import defaultdict
  10.  
  11.  
  12. class BlogApp:
  13.  
  14.     tokens_dict = defaultdict(list)
  15.  
  16.     def add_new_user(self, user_username, user_email, user_password):
  17.         connection = pymysql.connect(host='localhost',
  18.                                      user='BlogApp_User',
  19.                                      password='BlogApp_Password',
  20.                                      db='BlogApp',
  21.                                      charset='utf8mb4',
  22.                                      cursorclass=pymysql.cursors.DictCursor)
  23.  
  24.         salt = b'some_salt'
  25.         hash_obj = hashlib.md5()
  26.         hash_obj.update(salt)
  27.         hash_obj.update(user_password.encode('utf-8'))
  28.  
  29.         try:
  30.             with connection.cursor() as cursor:
  31.                 sql = "INSERT INTO User (Username, Email, Password) VALUES (%s, %s, %s)"
  32.                 cursor.execute(sql, (user_username, user_email, hash_obj))
  33.             connection.commit()
  34.  
  35.             with connection.cursor() as cursor:
  36.                 sql = "SELECT UserID, Username, Password FROM User WHERE Email=%s"
  37.                 cursor.execute(sql, user_email)
  38.                 result = cursor.fetchone()
  39.                 print(result)
  40.         finally:
  41.             connection.close()
  42.  
  43.     def authentication_user(self, user_email, user_password):
  44.         connection = pymysql.connect(host='localhost',
  45.                                      user='BlogApp_User',
  46.                                      password='BlogApp_Password',
  47.                                      db='BlogApp',
  48.                                      charset='utf8mb4',
  49.                                      cursorclass=pymysql.cursors.DictCursor)
  50.  
  51.         try:
  52.             with connection.cursor() as cursor:
  53.                 sql = "SELECT * FROM User WHERE Email=%s AND Password=%s"
  54.                 cursor.execute(sql, (user_email, user_password))
  55.                 result = cursor.fetchone()
  56.                 if result is not None:
  57.                     user_token = uuid.uuid4()
  58.                     self.tokens_dict[user_email].append(user_token)
  59.                 connection.commit()
  60.  
  61.         finally:
  62.             connection.close()
  63.  
  64.         token_status = True if user_email in self.tokens_dict else False
  65.         print(token_status)
  66.         return token_status
  67.  
  68.     def get_users_list(self):
  69.         connection = pymysql.connect(host='localhost',
  70.                                      user='BlogApp_User',
  71.                                      password='BlogApp_Password',
  72.                                      db='BlogApp',
  73.                                      charset='utf8mb4',
  74.                                      cursorclass=pymysql.cursors.DictCursor)
  75.  
  76.         try:
  77.             with connection.cursor() as cursor:
  78.                 sql = "SELECT Username FROM User"
  79.                 cursor.execute(sql)
  80.                 result = cursor.fetchall()
  81.                 connection.commit()
  82.  
  83.         finally:
  84.             connection.close()
  85.         print(result)
  86.         return result
  87.  
  88.     def get_auth_user_blogs_list(self):
  89.         connection = pymysql.connect(host='localhost',
  90.                                      user='BlogApp_User',
  91.                                      password='BlogApp_Password',
  92.                                      db='BlogApp',
  93.                                      charset='utf8mb4',
  94.                                      cursorclass=pymysql.cursors.DictCursor)
  95.  
  96.         try:
  97.             with connection.cursor() as cursor:
  98.                 sql = "SELECT Username, Email FROM User"
  99.                 cursor.execute(sql)
  100.                 result = cursor.fetchall()
  101.                 connection.commit()
  102.                 print(result)
  103.  
  104.         finally:
  105.             connection.close()
  106.         for user in result:
  107.             if user['Email'] in self.tokens_dict:
  108.                 print(user)
  109.         return result
  110.  
  111.     def get_blogs_list(self):
  112.         connection = pymysql.connect(host='localhost',
  113.                                      user='BlogApp_User',
  114.                                      password='BlogApp_Password',
  115.                                      db='BlogApp',
  116.                                      charset='utf8mb4',
  117.                                      cursorclass=pymysql.cursors.DictCursor)
  118.  
  119.         try:
  120.             with connection.cursor() as cursor:
  121.                 sql = "SELECT BlogName, BlogDescription FROM Blog"
  122.                 cursor.execute(sql)
  123.                 result = cursor.fetchall()
  124.                 connection.commit()
  125.  
  126.         finally:
  127.             connection.close()
  128.         print(result)
  129.         return result
  130.  
  131.     def create_blog(self):
  132.         pass
  133.  
  134.     def edit_blog(self):
  135.         pass
  136.  
  137.     def delete_blog(self):
  138.         pass
  139.  
  140.     def create_post(self):
  141.         pass
  142.  
  143.     def edit_post(self):
  144.         pass
  145.  
  146.     def delete_post(self):
  147.         pass
  148.  
  149.     def add_comment(self):
  150.         pass
  151.  
  152.     def get_user_comments(self):
  153.         pass
  154.  
  155.  
  156. a = BlogApp()
  157.  
  158. # print(a.tokens_dict)
  159. a.add_new_user('sexbeast', '111@gmail.com', '111')
  160. # print(a.tokens_dict)
  161. # a.authentication_user('111@gmail.com', '111')
  162. # print(a.tokens_dict)
  163. # a.get_users_list()
  164. # print(a.tokens_dict)
  165. # a.get_auth_user_blogs_list()
  166. # print(a.tokens_dict)
  167. # a.get_blogs_list()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement