Advertisement
Guest User

Untitled

a guest
Dec 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. from __future__ import unicode_literals
  2. from django.db import models
  3. import bcrypt # to hash the password
  4. import re
  5. from django.core.exceptions import ValidationError
  6. from django.core.validators import validate_email
  7. from datetime import date, datetime
  8.  
  9. # # Create your models here.
  10. # def check_string(string):
  11. # for char in string: # iterate through the string
  12. # if not char.isalpha():
  13. # return True # if character is not a letter, returns true (true that it's not proper)
  14. # return False
  15. def checkHireday(strhday): # function to check if entered birthday is from the future
  16. # set hday and today variable to match type and format
  17. hday = datetime.strptime(str(strhday), '%Y-%m-%d').date()
  18. today = date.today()
  19. if hday<today: # if hday is greater than today this is (future)
  20. return True # returns true (is from future)
  21. return False # otherwise returns false
  22.  
  23.  
  24. class UserManager(models.Manager):
  25. def user_validator(self, postData): # for creating user
  26. errors = [] # empty error list
  27. if len(postData['name'])<2:
  28. # if first name is less than 2 characters
  29. errors.append("Please enter your name !")
  30. if not re.match("^[A-z][A-z|\.|\s]+$", postData['name']):
  31. # if first name contains other chars
  32. errors.append("Please enter a valid name !")
  33. if len(postData['username'])<2:
  34. # if first name is less than 2 characters
  35. errors.append("Please enter your Username!")
  36. if not re.match("^[A-z][A-z|\.|\s]+$", postData['username']):
  37. # if alias contains other chars
  38. errors.append("Please enter a valid Username !")
  39. try:
  40. # while validate_email doesn't throw errors
  41. validate_email(postData['email'])
  42. # check if email is valid
  43. # this will check if email input is empty or in proper form (---@---.com)
  44. except ValidationError:
  45. # if it throws an error
  46. errors.append("Please enter valid email address !")
  47. if len(self.filter(email=postData['email'])):
  48. # check if email already exists
  49. errors.append("User with that email already exists !")
  50. if len(postData['password'])<2:
  51. # check if password is longer than 8 characters
  52. errors.append("Password should be at least 8 characters !")
  53. elif postData['password']!=postData['c_password']:
  54. # checks if password and c_password matches
  55. errors.append("Password doesn't match !")
  56. elif postData['password'].lower()=='password':
  57. # check if password is 'password'
  58. errors.append(" Your password cannot be word 'password' try something different ... !")
  59. if not postData['hday']:
  60. # if no hday was added
  61. errors.append("Please enter hire date !")
  62. elif checkHireday(postData['hday']):
  63. # if hireday entered is future date
  64. errors.append("You can't be hired in the past!")
  65. return errors
  66. # returns the errors list
  67.  
  68. def create_user(self, postData):
  69. # to create users
  70. # set each variable to according post data
  71. name = postData['name']
  72. username = postData['username']
  73. email = postData['email']
  74. hireday = postData['hday']
  75. # hash the password
  76. password = bcrypt.hashpw(postData['password'].encode(), bcrypt.gensalt())
  77. # return the newly created user
  78. return self.create(name=name, username=username , email=email, password=password, hireday=hireday)
  79.  
  80. # validates login
  81. def login_validator(self, postData):
  82. result = {'errors':[], 'user':None}
  83. # empty dictionary with keys 'errors' and 'user'
  84. matched_user = self.filter(email=postData['email'])
  85. # filters the user with the same email address
  86. if not len(postData['password']):
  87. # if password wasn't entered
  88. result['errors'].append('Please enter your password')
  89. if not len(postData['email']):
  90. # if email wasn't entered
  91. result['errors'].append('Please enter your email')
  92. elif not len(matched_user):
  93. # if no user was returned with that email
  94. result['errors'].append('No matching email')
  95. # if password doesn't match the user's password
  96. elif not bcrypt.checkpw(postData['password'].encode(), matched_user[0].password.encode()):
  97. result['errors'].append('No matching email')
  98. #append an object to the end of the list
  99. else:
  100. # if no errors
  101. result['user'] = matched_user[0]
  102. # set 'user' in dictionary to that matched user object
  103. return result
  104. # return result
  105.  
  106. class User(models.Model):
  107. name = models.CharField(max_length = 200)
  108. username = models.CharField(max_length = 200)
  109. email = models.CharField(max_length = 200)
  110. password = models.CharField(max_length = 200)
  111. hireday = models.DateField(auto_now_add=False, auto_now=False)
  112. created_at = models.DateTimeField(auto_now_add=True)
  113. updated_at = models.DateTimeField(auto_now=True)
  114. objects = UserManager()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement