Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.75 KB | None | 0 0
  1. """
  2. Creating blog
  3. Homework for python-courses
  4. """
  5.  
  6. import hashlib
  7. import uuid
  8. import time
  9. import pymysql.cursors
  10.  
  11.  
  12. class BlogApp:
  13.     tokens_dict = {}
  14.     db_connection = pymysql.connect(host='localhost',
  15.                                     user='BlogApp_User',
  16.                                     password='BlogApp_Password',
  17.                                     db='BlogApp',
  18.                                     charset='utf8mb4',
  19.                                     cursorclass=pymysql.cursors.DictCursor)
  20.  
  21.     def add_new_user(self, user_username, user_email, user_password):
  22.  
  23.         # salt = b'some_salt'
  24.         # hash_obj = hashlib.md5()
  25.         # hash_obj.update(salt)
  26.         # hash_obj.update(user_password.encode('utf-8'))
  27.         with self.db_connection.cursor() as cursor:
  28.             sql = "INSERT INTO User (Username, Email, Password) VALUES (%s, %s, %s)"
  29.             cursor.execute(sql, (user_username, user_email, user_password))
  30.         self.db_connection.commit()
  31.  
  32.     def authenticate_user(self, user_email, user_password):
  33.  
  34.         with self.db_connection.cursor() as cursor:
  35.             sql = "SELECT UserID FROM User WHERE Email=%s AND Password=%s"
  36.             cursor.execute(sql, (user_email, user_password))
  37.             user_id_dict = cursor.fetchone()
  38.             if user_id_dict is not None:
  39.                 user_token = uuid.uuid4()
  40.                 user_id = user_id_dict.get('UserID')
  41.                 self.tokens_dict.update({user_token: user_id})
  42.             else:
  43.                 raise KeyError('Incorrect Login/Password Pair. Please, Try Again')
  44.  
  45.         return user_token
  46.  
  47.     def get_users_list(self):
  48.  
  49.         with self.db_connection.cursor() as cursor:
  50.             sql = "SELECT Username FROM User"
  51.             cursor.execute(sql)
  52.             users_list = cursor.fetchall()
  53.  
  54.         return users_list
  55.  
  56.     def get_auth_user_blog_list(self, user_token):
  57.  
  58.         user_id = self.tokens_dict.get(user_token)
  59.         if user_id is None:
  60.             raise KeyError('Incorrect User Token')
  61.  
  62.         with self.db_connection.cursor() as cursor:
  63.             sql = "SELECT BlogID, BlogName, BlogDescription FROM Blog WHERE UserID=%s"
  64.             cursor.execute(sql, user_id)
  65.             auth_user_blog_list = cursor.fetchall()
  66.  
  67.         return auth_user_blog_list
  68.  
  69.     def get_blog_list(self):
  70.  
  71.         with self.db_connection.cursor() as cursor:
  72.             sql = "SELECT BlogName, BlogDescription FROM Blog"
  73.             cursor.execute(sql)
  74.             blog_list = cursor.fetchall()
  75.  
  76.         return blog_list
  77.  
  78.     def create_blog(self, user_token, blog_name, blog_description):
  79.  
  80.         user_id = self.tokens_dict.get(user_token)
  81.         if user_id is None:
  82.             raise KeyError('Incorrect User Token')
  83.  
  84.         with self.db_connection.cursor() as cursor:
  85.             sql = "INSERT INTO Blog (BlogDescription, UserID, BlogName) VALUES (%s, %s, %s)"
  86.             cursor.execute(sql, (blog_description, user_id, blog_name))
  87.         self.db_connection.commit()
  88.  
  89.     def edit_blog(self, user_token, blog_id, changed_name=None, changed_description=None):
  90.  
  91.         user_id = self.tokens_dict.get(user_token)
  92.         if user_id is None:
  93.             raise KeyError('Incorrect User Token')
  94.  
  95.         with self.db_connection.cursor() as cursor:
  96.             sql = "SELECT UserID FROM Blog WHERE BlogID=%s"
  97.             cursor.execute(sql, blog_id)
  98.             blog_user_id = cursor.fetchone()
  99.  
  100.         if blog_user_id.get('UserID') == user_id:
  101.             with self.db_connection.cursor() as cursor:
  102.                 if changed_name is not None:
  103.                     sql = "UPDATE Blog SET BlogName=%s WHERE BlogID=%s"
  104.                     cursor.execute(sql, (changed_name, blog_id))
  105.                 if changed_description is not None:
  106.                     sql = "UPDATE Blog SET BlogDescription=%s WHERE BlogID=%s"
  107.                     cursor.execute(sql, (changed_description, blog_id))
  108.             self.db_connection.commit()
  109.         else:
  110.             raise ValueError('You Don\'t Have Enough Permission To Edit Someones Blog')
  111.  
  112.     def delete_blog(self, blog_id, user_token):
  113.  
  114.         user_id = self.tokens_dict.get(user_token)
  115.         if user_id is None:
  116.             raise KeyError('Incorrect User Token')
  117.  
  118.         with self.db_connection.cursor() as cursor:
  119.             sql = "SELECT UserID FROM Blog WHERE BlogID=%s"
  120.             cursor.execute(sql, blog_id)
  121.             blog_user_id = cursor.fetchone()
  122.  
  123.         if blog_user_id.get('UserID') == user_id:
  124.             with self.db_connection.cursor() as cursor:
  125.                 sql = "DELETE FROM Blog WHERE BlogID=%s"
  126.                 cursor.execute(sql, blog_id)
  127.             self.db_connection.commit()
  128.         else:
  129.             raise ValueError('You Don\'t Have Enough Permission To Delete Someones Blog')
  130.  
  131.     def create_post(self, user_token, blog_id, post_description, post_name):
  132.  
  133.         user_id = self.tokens_dict.get(user_token)
  134.         if user_id is None:
  135.             raise KeyError('Incorrect User Token')
  136.  
  137.         with self.db_connection.cursor() as cursor:
  138.             sql = "INSERT INTO Post (UserID, BlogID, PostDescription, PostName) VALUES (%s, %s, %s, %s)"
  139.             cursor.execute(sql, (user_id, blog_id, post_description, post_name))
  140.         self.db_connection.commit()
  141.  
  142.     def edit_post(self, user_token, post_id, changed_name=None, changed_description=None):
  143.  
  144.         user_id = self.tokens_dict.get(user_token)
  145.         if user_id is None:
  146.             raise KeyError('Incorrect User Token')
  147.  
  148.         with self.db_connection.cursor() as cursor:
  149.             sql = "SELECT UserID FROM Post WHERE PostID=%s"
  150.             cursor.execute(sql, post_id)
  151.             post_user_id = cursor.fetchone()
  152.             if post_user_id is None:
  153.                 raise ValueError('There Is No Your Post With This post_id')
  154.  
  155.         if post_user_id.get('UserID') == user_id:
  156.             with self.db_connection.cursor() as cursor:
  157.                 if changed_name is not None:
  158.                     sql = "UPDATE Post SET PostName=%s WHERE PostID=%s"
  159.                     cursor.execute(sql, (changed_name, post_id))
  160.                 if changed_description is not None:
  161.                     sql = "UPDATE Post SET PostDescription=%s WHERE PostID=%s"
  162.                     cursor.execute(sql, (changed_description, post_id))
  163.             self.db_connection.commit()
  164.         else:
  165.             raise ValueError('You Don\'t Have Enough Permission To Edit Someones Post')
  166.  
  167.     def delete_post(self, user_token, post_id):
  168.  
  169.         user_id = self.tokens_dict.get(user_token)
  170.         if user_id is None:
  171.             raise KeyError('Incorrect User Token')
  172.  
  173.         with self.db_connection.cursor() as cursor:
  174.             sql = "SELECT UserID FROM Post WHERE PostID=%s"
  175.             cursor.execute(sql, post_id)
  176.             post_user_id = cursor.fetchone()
  177.             if post_user_id is None:
  178.                 raise ValueError('There Is No Your Post With This post_id')
  179.  
  180.         if post_user_id.get('UserID') == user_id:
  181.             with self.db_connection.cursor() as cursor:
  182.                 sql = "DELETE FROM Post WHERE PostID=%s"
  183.                 cursor.execute(sql, post_id)
  184.                 self.db_connection.commit()
  185.         else:
  186.             raise ValueError('You Don\'t Have Enough Permission To Delete Someones Blog')
  187.  
  188.     def add_comment(self, user_token, comment_body, post_id, parrent_id=None):
  189.  
  190.         user_id = self.tokens_dict.get(user_token)
  191.         if user_id is None:
  192.             raise KeyError('Incorrect User Token')
  193.  
  194.         if parrent_id is not None:
  195.             with self.db_connection.cursor() as cursor:
  196.                 sql = "SELECT CommentID FROM Comment WHERE PostID=%s"
  197.                 cursor.execute(sql, post_id)
  198.                 result = cursor.fetchall()
  199.                 if result is None:
  200.                     raise ValueError('There Is No Comment With This comment_id')
  201.  
  202.             for comment in result:
  203.                 if parrent_id == comment.get('CommentID'):
  204.                     with self.db_connection.cursor() as cursor:
  205.                         sql = "INSERT INTO Comment (UserID, CommentBody, PostID, ParrentID) VALUES (%s, %s, %s, %s)"
  206.                         cursor.execute(sql, (user_id, comment_body, post_id, parrent_id))
  207.                     break
  208.             self.db_connection.commit()
  209.  
  210.         else:
  211.             with self.db_connection.cursor() as cursor:
  212.                 sql = "INSERT INTO Comment (UserID, CommentBody, PostID) VALUES (%s, %s, %s)"
  213.                 cursor.execute(sql, (user_id, comment_body, post_id))
  214.             self.db_connection.commit()
  215.  
  216.     def get_user_comments(self, user_token):
  217.  
  218.         user_id = self.tokens_dict.get(user_token)
  219.         if user_id is None:
  220.             raise KeyError('Incorrect User Token')
  221.  
  222.         with self.db_connection.cursor() as cursor:
  223.             sql = "SELECT CommentID, CommentBody FROM Comment WHERE UserID=%s"
  224.             cursor.execute(sql, user_id)
  225.             user_comments = cursor.fetchall()
  226.  
  227.         return user_comments
  228.  
  229.     def __del__(self):
  230.         self.db_connection.close()
  231.  
  232.  
  233. blog = BlogApp()
  234.  
  235. # blog.add_new_user('check', 'the@gmail.com', 'sound')
  236. user_token = blog.authenticate_user('the@gmail.com', 'sound')
  237. # print(blog.get_users_list())
  238. # print(blog.get_blog_list())
  239. # blog.create_blog(user_token, 'dva chasa nochi', 'tyt kruto')
  240. # blog.edit_blog(user_token, 6, 'dva s polovinoi', 'ochen kruta')
  241. # blog.delete_blog(6, user_token)
  242. # blog.create_post(user_token, 4, 'nnnydavai', 'oldman')
  243. # blog.edit_post(user_token, 3, 'aaaaannnnnydavai', 'hhhhy i am oldman')
  244. # blog.delete_post(user_token, 3)
  245. # blog.add_comment(user_token, 'hihiihihihi penis detrov', 2)
  246. # blog.add_comment(user_token, 'ti masky poteryal', 2, 7)
  247. # print(blog.get_user_comments(user_token))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement