-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_model.py
80 lines (69 loc) · 2.83 KB
/
base_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python3
"""This module defines a base class for all models in our hbnb clone"""
import uuid
from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from os import getenv
storage_type = getenv("HBNB_TYPE_STORAGE")
Base = declarative_base()
class BaseModel:
"""A base class for all hbnb models"""
id = Column(String(60), unique=True, nullable=False, primary_key=True)
created_at = Column(DateTime, nullable=False, default=datetime.utcnow())
updated_at = Column(DateTime, nullable=True, default=datetime.utcnow())
def __init__(self, *args, **kwargs):
"""Instantiation of base model class
Args:
args: it won't be used
kwargs: arguments for the constructor of the BaseModel
Attributes:
id: unique id generated
created_at: creation date
updated_at: updated date
"""
if kwargs:
for key, value in kwargs.items():
if key == "created_at" or key == "updated_at":
value = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
if key != "__class__":
setattr(self, key, value)
if "id" not in kwargs:
self.id = str(uuid.uuid4())
if "created_at" not in kwargs:
self.created_at = datetime.now()
if "updated_at" not in kwargs:
self.updated_at = datetime.now()
else:
self.id = str(uuid.uuid4())
self.created_at = self.updated_at = datetime.now()
def __str__(self):
"""Returns a string representation of the instance"""
cls = (str(type(self)).split(".")[-1]).split("'")[0]
return "[{}] ({}) {}".format(cls, self.id, self.to_dict())
def save(self):
"""Updates updated_at with current time when instance is changed"""
from models import storage
self.updated_at = datetime.now()
storage.new(self)
storage.save()
def delete(self):
"""
Delete the current instance from the storage (models.storage)
by calling the method delete
"""
from models import storage
storage.delete(self)
def to_dict(self):
"""Convert instance into dict format"""
dictionary = {}
dictionary.update(self.__dict__)
dictionary.update({"__class__": (str(type(self))
.split(".")[-1]).split("'")[0]})
dictionary["created_at"] = self.created_at.isoformat()
dictionary["updated_at"] = self.updated_at.isoformat()
if "_sa_instance_state" in dictionary.keys():
del dictionary["_sa_instance_state"]
if 'password' in dictionary:
del dictionary['password']
return dictionary