diff --git a/console.py b/console.py index e003cfc..7c4ea16 100755 --- a/console.py +++ b/console.py @@ -114,7 +114,7 @@ def do_destroy(self, arg): if len(args) > 1: key = args[0] + "." + args[1] if key in models.storage.all(): - models.storage.all().pop(key) + models.storage.all()[key].delete() models.storage.save() else: print("** no instance found **") diff --git a/models/base_model.py b/models/base_model.py index 2f020d9..5d2ee72 100755 --- a/models/base_model.py +++ b/models/base_model.py @@ -51,15 +51,20 @@ def save(self): models.storage.new(self) models.storage.save() - def to_dict(self): + def to_dict(self, save_to_disk=False): """returns a dictionary containing all keys/values of the instance""" new_dict = self.__dict__.copy() if "created_at" in new_dict: new_dict["created_at"] = new_dict["created_at"].isoformat() if "updated_at" in new_dict: new_dict["updated_at"] = new_dict["updated_at"].isoformat() + if '_password' in new_dict: + new_dict['password'] = new_dict['_password'] + new_dict.pop('_password', None) new_dict["__class__"] = self.__class__.__name__ new_dict.pop('_sa_instance_state', None) + if not save_to_disk: + new_dict.pop('password', None) return new_dict def delete(self): diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index 3c62050..d084ae6 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -43,7 +43,7 @@ def save(self): """serializes __objects to the JSON file (path: __file_path)""" json_objects = {} for key in self.__objects: - json_objects[key] = self.__objects[key].to_dict() + json_objects[key] = self.__objects[key].to_dict(save_to_disk=True) with open(self.__file_path, 'w') as f: json.dump(json_objects, f) diff --git a/models/user.py b/models/user.py index 33f7755..c6d63ce 100755 --- a/models/user.py +++ b/models/user.py @@ -1,5 +1,7 @@ #!/usr/bin/python3 """ holds class User""" +from base64 import b64encode +import hashlib import models from models.base_model import BaseModel, Base from os import getenv @@ -13,8 +15,9 @@ class User(BaseModel, Base): __tablename__ = 'users' email = Column(String(128), nullable=False) - password = Column(String(128), - nullable=False) + _password = Column('password', + String(128), + nullable=False) first_name = Column(String(128), nullable=True) last_name = Column(String(128), @@ -27,10 +30,23 @@ class User(BaseModel, Base): cascade="all, delete-orphan") else: email = "" - password = "" + _password = "" first_name = "" last_name = "" def __init__(self, *args, **kwargs): """initializes user""" + if 'password' in kwargs: + pwd = hashlib.md5(b64encode(kwargs['password'].encode('ascii'))) + kwargs['password'] = pwd.hexdigest() super().__init__(*args, **kwargs) + + @property + def password(self): + return self._password + + @password.setter + def password(self, pwd): + """hashing password values""" + self._password = hashlib.md5(b64encode(pwd + .encode('ascii'))).hexdigest()