# A gateway in necessary as a web server cannot communicate directly with Python.
# In this case, imports are focused on generating hash code to protect passwords.
from werkzeug.security import generate_password_hash, check_password_hash
import json
from datetime import date 

# Define a User Class/Template
# -- A User represents the data we want to manage
class User:    
    # constructor of a User object, initializes the instance variables within object (self)
    def __init__(self, name, uid, password, graduationclass, birth, age): # name, uid, and password are presets for each user
        self._name = name    # variables with self prefix become part of the object, 
        self._uid = uid
        self.set_password(password)
        self._birth = birth
        self._age = age
        self._graduationclass = graduationclass

    # a name getter method, extracts name from object
    @property
    def name(self):
        return self._name
    
    # a setter function, allows name to be updated after initial object creation
    @name.setter
    def name(self, name):
        self._name = name
    
    # a getter method, extracts email from object
    @property
    def uid(self):
        return self._uid
    
    # a setter function, allows name to be updated after initial object creation
    @uid.setter
    def uid(self, uid):
        self._uid = uid
        
    # check if uid parameter matches user id in object, return boolean
    def is_uid(self, uid):
        return self._uid == uid
    
    @property
    def password(self):
        return self._password[0:10] + "..." # because of security only show 1st characters

    # update password, this is conventional setter
    def set_password(self, password):
        """Create a hashed password."""
        self._password = generate_password_hash(password, method='sha256')

    # check password parameter versus stored/encrypted password
    def is_password(self, password):
        """Check against hashed password."""
        result = check_password_hash(self._password, password)
        return result
    
    @property 
    def graduationclass(self):
        return self._graduationclass

    @graduationclass.setter
    def graduationclass(self, graduationclass):
        self._graduationclass = graduationclass

    
    # output content using str(object) in human readable form, uses getter
    def __str__(self):
        return f'name: "{self.name}", id: "{self.uid}", psw: "{self.password}", graduationclass: "{self.graduationclass}", birth: "{self.birth}", age: "{self.age}"'

    # output command to recreate the object, uses attribute directly
    def __repr__(self):
        return f'Person(name={self._name}, uid={self._uid}, password={self._password}, graduationclass={self._graduationclass}, birth={self.birth}, age={self._age})' # return will store the name back to you but not print it

 # a name getter method, extracts date of birth from object
    @property
    def birth(self):
        return self._birth
    
    # a setter function, allows date of birth to be updated after initial object creation
    @birth.setter
    def birth(self, birth):
        self._birth = birth

    @property 
    def age(self):
        return self._age

    @age.setter
    def age(self, age):
        self._age = age

# tester method to print users
def tester(users, uid, psw, graduationclass, birth, age):
    result = None
    for user in users:
        # test for match in database
        if user.uid == uid and user.is_password(psw):  # check for match
            print("* ", end="")
            result = user
        # print using __str__ method
        print(str(user))
    return result

from datetime import date

def agecalculation(born):
    today = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day)) 


# place tester code inside of special if!  This allows include without tester running
if __name__ == "__main__": # to see the actual code, it is important bc everything under here is only rn in the file it is in

    # define user objects
    u1 = User(name='Amitha', uid='amitha', password='12' , graduationclass=2023, birth=[2005, 10, 31], age= agecalculation(date(2005, 10, 31)))
    u2 = User(name='Joselyn', uid='joselyn', password='13' , graduationclass=2024, birth=[2006, 9, 15], age= agecalculation(date(2006, 9, 15)))
    u3 = User(name='Lina', uid='lina', password='14' , graduationclass=2024, birth=[2006, 2, 20], age= agecalculation(date(2006, 2, 20)))
    u4 = User(name='Naja', uid='naja', password='15', graduationclass=2025, birth=[2007, 3, 8], age= agecalculation(date(2007, 3, 8)))
    u5 = User(name='Mr.Yeung', uid='yeung', password='16', graduationclass=2015, birth=[1995, 4, 10], age= agecalculation(date(2006, 9, 15)))
    # put user objects in list for convenience
    users = [u1, u2, u3, u4]

    # print(u1.name + " is " + agecalculation(u1.birth) + " years old")
    # Find user

    print("Test 1, find user 1")
    u = tester(users, u1.uid, "12", u1.graduationclass, u1.birth, u1.age )

    
    # Change user
    print("Test 2, change user 1 from Amitha to Lina")
    u.name = "Lina"
    u.uid = "linaawad7"
    u.set_password("14")
    u.graduationclass = 2024
    u.birth = [2006, 2, 10]
    u.age = agecalculation(date(2006, 2, 10))
    u = tester(users, u.uid, "12", u.graduationclass, u.birth, u.age)


    # Make dictionary
    ''' 
    The __dict__ in Python represents a dictionary or any mapping object that is used to store the attributes of the object. 
    Every object in Python has an attribute that is denoted by __dict__. 
    Use the json.dumps() method to convert the list of Users to a JSON string.
    '''
    print("Test 3, make a dictionary")
    json_string = json.dumps([user.__dict__ for user in users]) 
    print(json_string)

    print("Test 4, make a dictionary")
    json_string = json.dumps([vars(user) for user in users]) 
    print(json_string)
Test 1, find user 1
* name: "Amitha", id: "amitha", psw: "sha256$Djj...", graduationclass: "2023", birth: "[2005, 10, 31]", age: "17"
name: "Joselyn", id: "joselyn", psw: "sha256$DRe...", graduationclass: "2024", birth: "[2006, 9, 15]", age: "16"
name: "Lina", id: "lina", psw: "sha256$2w2...", graduationclass: "2024", birth: "[2006, 2, 20]", age: "16"
name: "Naja", id: "naja", psw: "sha256$7W8...", graduationclass: "2025", birth: "[2007, 3, 8]", age: "15"
Test 2, change user 1 from Amitha to Lina
name: "Lina", id: "linaawad7", psw: "sha256$omN...", graduationclass: "2024", birth: "[2006, 2, 10]", age: "16"
name: "Joselyn", id: "joselyn", psw: "sha256$DRe...", graduationclass: "2024", birth: "[2006, 9, 15]", age: "16"
name: "Lina", id: "lina", psw: "sha256$2w2...", graduationclass: "2024", birth: "[2006, 2, 20]", age: "16"
name: "Naja", id: "naja", psw: "sha256$7W8...", graduationclass: "2025", birth: "[2007, 3, 8]", age: "15"
Test 3, make a dictionary
[{"_name": "Lina", "_uid": "linaawad7", "_password": "sha256$omNs5Mpp29gvzskL$1b09d570a37bf3cd5f960e4e263f4ddb829f0e123c01437c6f1406ca61327dc0", "_birth": [2006, 2, 10], "_age": 16, "_graduationclass": 2024}, {"_name": "Joselyn", "_uid": "joselyn", "_password": "sha256$DReJHaYWg6mj3Yp4$ed0b990471e155ba0082bb241286ebf050ec77c2c7fdbd549d7eff887000633d", "_birth": [2006, 9, 15], "_age": 16, "_graduationclass": 2024}, {"_name": "Lina", "_uid": "lina", "_password": "sha256$2w2pBxqzlB5Db8Cl$63fc5b25bf4a0faefebb5b893ee983d8022ee95e53b240722a40406a87fd0bcd", "_birth": [2006, 2, 20], "_age": 16, "_graduationclass": 2024}, {"_name": "Naja", "_uid": "naja", "_password": "sha256$7W8hlKk9XVNW80v1$f1dc8c8f037ccf3e705ee24554200ceb0da6c0ad63c527321cfbfc134d9ee0b1", "_birth": [2007, 3, 8], "_age": 15, "_graduationclass": 2025}]
Test 4, make a dictionary
[{"_name": "Lina", "_uid": "linaawad7", "_password": "sha256$omNs5Mpp29gvzskL$1b09d570a37bf3cd5f960e4e263f4ddb829f0e123c01437c6f1406ca61327dc0", "_birth": [2006, 2, 10], "_age": 16, "_graduationclass": 2024}, {"_name": "Joselyn", "_uid": "joselyn", "_password": "sha256$DReJHaYWg6mj3Yp4$ed0b990471e155ba0082bb241286ebf050ec77c2c7fdbd549d7eff887000633d", "_birth": [2006, 9, 15], "_age": 16, "_graduationclass": 2024}, {"_name": "Lina", "_uid": "lina", "_password": "sha256$2w2pBxqzlB5Db8Cl$63fc5b25bf4a0faefebb5b893ee983d8022ee95e53b240722a40406a87fd0bcd", "_birth": [2006, 2, 20], "_age": 16, "_graduationclass": 2024}, {"_name": "Naja", "_uid": "naja", "_password": "sha256$7W8hlKk9XVNW80v1$f1dc8c8f037ccf3e705ee24554200ceb0da6c0ad63c527321cfbfc134d9ee0b1", "_birth": [2007, 3, 8], "_age": 15, "_graduationclass": 2025}]

Hacks

  • Add new attributes/variables to the Class. Make class specific to your CPT work.

  • Add classOf attribute to define year of graduation

  • Add setter and getter for classOf
  • Add dob attribute to define date of birth This will require investigation into Python datetime objects as shown in example code below
  • Add setter and getter for dob
  • Add instance variable for age, make sure if dob changes age changes
  • Add getter for age, but don't add/allow setter for age
  • Update and format tester function to work with changes Start a class design for each of your own Full Stack CPT sections of your project

  • Use new code cell in this notebook

  • Define init and self attributes
  • Define setters and getters
  • Make a tester
from datetime import date

def calculate_age(born):
    today = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

dob = date(2005, 10, 31)
age = calculate_age(dob)
print(age)
17