Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import sqlalchemy.exc
  2.  
  3. from models import Callback, User, Company
  4. from utilties import helpers
  5.  
  6. from flask import session, escape
  7. from json import dumps
  8.  
  9. from services import user_services, assistant_services, role_services, sub_services, company_services
  10. from utilties import helpers
  11.  
  12.  
  13. def signup(email, firstname, surname, password, companyName, companySize, companyPhoneNumber, websiteURL) -> Callback:
  14.  
  15. # Validate Email
  16. if helpers.isValidEmail(email):
  17. return Callback(False, 'Invalid Email.')
  18.  
  19. # Check if user exists
  20. user = user_services.getByEmail(email)
  21. if user:
  22. return Callback(False, 'User already exists.')
  23.  
  24. # Create a new user with its associated company and role
  25. role_callback: Callback = role_services.getByName('Admin')
  26. if not role_callback.Success:
  27. return Callback(False, 'Role does not exist')
  28.  
  29. company = Company(Name=companyName, Size=companySize, PhoneNumber=companyPhoneNumber, URL=websiteURL)
  30. user = user_services.create(firstname, surname, email, password, company, role_callback.Data)
  31.  
  32. # Subscribe to basic plan with 14 trial days
  33. sub_callback: Callback = sub_services.subscribe(email=email, planID='plan_D3lp2yVtTotk2f', trialDays=14)
  34.  
  35. # If subscription failed, remove the new created company and user
  36. if not sub_callback.Success:
  37. company_services.removeByName(companyName)
  38. user_services.removeByEmail(email)
  39. return sub_callback
  40.  
  41. # Return a callback with a message
  42. return Callback(True, 'Signed up successfully!')
  43.  
  44.  
  45. def login(email: str, password_to_check: str) -> Callback:
  46.  
  47. # Login Exception Handling
  48. if not (email or password_to_check):
  49. print("Invalid request: Email or password not received!")
  50. return Callback(False, "You entered an incorrect username or password.")
  51.  
  52. user_callback: Callback = user_services.getByEmail(email.lower())
  53. # If user is not found
  54. if not user_callback.Success:
  55. print("Invalid request: Email not found")
  56. return Callback(False, "Email not found.")
  57.  
  58. # Get the user from the callback object
  59. user: User = user_callback.Data
  60. if not helpers.hashPass(password_to_check, user.Password) == user.Password:
  61. print("Invalid request: Incorrect Password")
  62. return Callback(False, "Incorrect Password.")
  63.  
  64. if not user.Verified:
  65. print("Invalid request: Account is not verified")
  66. return Callback(False, "Account is not verified.")
  67.  
  68. # If all the tests are valid then do login process
  69. session['Logged_in'] = True
  70. session['userID'] = user.ID
  71. session['userEmail'] = user.Email
  72. session['UserPlan'] = helpers.getPlanNickname(user.SubID)
  73.  
  74. return Callback(True, "Login Successful")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement