Advertisement
Guest User

Python database

a guest
Apr 5th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. import jsonpickle, os, hashlib
  2.  
  3.  
  4. class AccountInfo:
  5. def __init__(self, password, secret_code=''):
  6. self.password = password
  7. self.secret_code = secret_code
  8. self.admin = False
  9.  
  10.  
  11. class Request:
  12. def __init__(self, username, admin_request=False, delete_request=False):
  13. self.username = username
  14. self.admin_request = admin_request
  15. self.delete_request = delete_request
  16.  
  17.  
  18. def load_dictionary(path):
  19. if os.path.isfile(path) and os.stat(path).st_size != 0:
  20. with open(path) as file:
  21. contents = file.read()
  22. return jsonpickle.decode(contents)
  23. return {}
  24.  
  25.  
  26. def save_dictionary(dictionary, path):
  27. with open(path, 'w+') as file:
  28. string = jsonpickle.encode(dictionary, file)
  29. file.write(string)
  30.  
  31.  
  32. def main():
  33. current_account = ''
  34.  
  35. while 1 == 1:
  36. print("Menu: ")
  37. print("1. Register")
  38. print("2. Login")
  39. print("3. Show accounts")
  40. print("4. Logout")
  41. print("5. Forgot your password")
  42. print("6. Admin request")
  43. print("7. View requests")
  44. print("8. View status")
  45. print("9. View Admins")
  46. print("10. Delete account")
  47. print("11. Exit")
  48. choice = input()
  49.  
  50. if choice == "1":
  51. register()
  52. elif choice == "2":
  53. current_account = login()
  54. elif choice == "3":
  55. show_accounts(current_account)
  56. elif choice == "4":
  57. current_account = logout(current_account)
  58. elif choice == "5":
  59. forgot_password()
  60. elif choice == "6":
  61. admin_request(current_account)
  62. elif choice == "7":
  63. view_requests(current_account)
  64. elif choice == "8":
  65. view_status(current_account)
  66. elif choice == "9":
  67. view_admins()
  68. elif choice == "10":
  69. delete_account(current_account)
  70. elif choice == "11":
  71. exit()
  72.  
  73.  
  74.  
  75. def register():
  76. print("Enter your desired username: ")
  77. username = input()
  78. if username in database:
  79. print("Account already exists.")
  80. exit()
  81. print("Enter your desired password: ")
  82. password = input()
  83. print("Enter your secret code (4 digits long): ")
  84. secret_code = input()
  85. if len(secret_code) != 4:
  86. print("Your secret code must be 4 digits long.")
  87. return
  88. if not secret_code.isdigit():
  89. print("Your code must be a 4 digit password.")
  90. return
  91. database[username] = AccountInfo(hashlib.sha512(password.encode('utf-8')).hexdigest(), secret_code)
  92. if username == "shredder8910" or username == "asher" or username == "cookie":
  93. database[username].admin = True
  94. save_dictionary(database, database_path)
  95.  
  96.  
  97. def login():
  98. print("Enter your username: ")
  99. username = input()
  100. print("Enter your password: ")
  101. password = input()
  102. encrypted = hashlib.sha512(password.encode('utf-8')).hexdigest()
  103. if username not in database:
  104. print("Your account does not exist.")
  105. return ''
  106. if not database[username].password:
  107. print("Incorrect password.")
  108. return ''
  109. else:
  110. print("Logged in as " + username)
  111. return username
  112.  
  113.  
  114. def show_accounts(current_account):
  115. if current_account in database:
  116. if database[current_account].admin:
  117. print("Accounts: ")
  118. for item in database.items():
  119. print("Username: " + item[0] + " Password: " + item[1].password)
  120. else:
  121. print("You don't have admin privileges.")
  122. else:
  123. print("Your account does not exist or you are not logged in.")
  124.  
  125. def logout(current_account):
  126. if current_account != '':
  127. print("You're now logged out.")
  128. else:
  129. print("You're not logged in.")
  130. return ''
  131.  
  132.  
  133. def forgot_password():
  134. print("Enter your username: ")
  135. username = input()
  136. print("Enter your secret code you used when you created your account: ")
  137. secret_code = input()
  138. if username in database:
  139. if database[username].secretCode == secret_code:
  140. print("Verification success.")
  141. print("Enter your new password: ")
  142. new_password = input()
  143. if database[username].password == new_password:
  144. print("You must select a new password.")
  145. else:
  146. database[username].password = new_password
  147. else:
  148. print("Account doesn't exist")
  149.  
  150.  
  151. def admin_request(current_account):
  152. if current_account != '':
  153. if current_account in database:
  154. if database[current_account].admin:
  155. print("You're already an admin.")
  156. else:
  157. requests[current_account] = Request(current_account, True)
  158. print("Admin request sent.")
  159. save_dictionary(requests, requests_path)
  160. else:
  161. print("You're not logged in.")
  162.  
  163.  
  164. def view_status(current_account):
  165. if current_account != '':
  166. if current_account in database:
  167. print("User: " + current_account + " | Admin: " + str(database[current_account].admin))
  168. else:
  169. print("Your account does not exist.")
  170. else:
  171. print("You're not logged in.")
  172.  
  173. def view_admins():
  174. for username, account_info in database.items():
  175. if account_info.admin:
  176. print("User: " + username + " | Admin: " + str(account_info.admin))
  177.  
  178.  
  179. def view_requests(current_account):
  180. resolved_requests = []
  181. if current_account in database:
  182. if database[current_account].admin:
  183. for username, request in requests.items():
  184. if request.admin_request:
  185. print("Request from " + username + " to be admin.")
  186. print("Allow to be admin? (yes or no): ")
  187. answer = input()
  188. if answer.lower() == "yes":
  189. database[username].admin = True
  190. print("Admin access granted.")
  191. else:
  192. print("Admin access denied.")
  193. if request.delete_request:
  194. print("Request from " + username + " for their account to be deleted.")
  195. print("Allow account to be deleted? (yes or no): ")
  196. answer = input()
  197. if answer.lower() == "yes":
  198. database.pop(username, None)
  199. print("Account deleted.")
  200. else:
  201. print("Delete request denied.")
  202. resolved_requests.append(username)
  203.  
  204. for resolved in resolved_requests:
  205. requests.pop(resolved, None)
  206. save_dictionary(requests, requests_path)
  207. save_dictionary(database, database_path)
  208.  
  209.  
  210. def delete_account(current_account):
  211. if current_account != '':
  212. if current_account in database:
  213. print("What is your secret code?")
  214. secret_code = input()
  215. if database[current_account].secret_code != secret_code:
  216. print("Incorrect secret code.")
  217. return
  218. print("Secret code valid.")
  219. print("Type DELETE to send a request for your account to be deleted.")
  220. response = input()
  221. if response == "DELETE":
  222. request = requests.get(current_account, Request(current_account))
  223. requests[current_account] = request
  224. requests[current_account].delete_request = True
  225. print("Request sent.")
  226. save_dictionary(requests, requests_path)
  227. else:
  228. print("Request cancelled.")
  229. else:
  230. print("Your account does not exist.")
  231. else:
  232. print("You're not logged in.")
  233.  
  234.  
  235. jsonpickle.set_encoder_options('simplejson', indent=4)
  236. database_path = 'database.json'
  237. requests_path = 'requests.json'
  238. database = load_dictionary(database_path)
  239. requests = load_dictionary(requests_path)
  240. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement