-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbase_model.py
executable file
·80 lines (70 loc) · 2.85 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
"""
Contains class BaseModel
"""
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
import models
import uuid
time = "%Y-%m-%dT%H:%M:%S.%f"
if models.storage_type == "db":
Base = declarative_base()
else:
class Base:
pass
class BaseModel:
"""The BaseModel class from which future classes will be derived"""
if models.storage_type == "db":
id = Column(String(60),
unique=True,
primary_key=True)
created_at = Column(DateTime,
nullable=False,
default=datetime.utcnow)
updated_at = Column(DateTime,
nullable=False,
default=datetime.utcnow)
def __init__(self, *args, **kwargs):
"""Initialization of the base model"""
if kwargs:
for key, value in kwargs.items():
if key != "__class__":
setattr(self, key, value)
if hasattr(self, "created_at") and type(self.created_at) is str:
self.created_at = datetime.strptime(kwargs["created_at"], time)
if hasattr(self, "updated_at") and type(self.updated_at) is str:
self.updated_at = datetime.strptime(kwargs["updated_at"], time)
if kwargs.get("id") is None:
self.id = str(uuid.uuid4())
else:
self.id = str(uuid.uuid4())
self.created_at = datetime.now()
self.updated_at = self.created_at
def __str__(self):
"""String representation of the BaseModel class"""
return "[{:s}] ({:s}) {}".format(self.__class__.__name__, self.id,
self.__dict__)
def __repr__(self):
"""String representation of the BaseModel class"""
return "[{:s}] ({:s}) {}".format(self.__class__.__name__, self.id,
self.__dict__)
def save(self):
"""updates the attribute 'updated_at' with the current datetime"""
self.updated_at = datetime.now()
models.storage.new(self)
models.storage.save()
def delete(self):
"""Deletes itself from the file storage"""
models.storage.delete(self)
def to_dict(self):
"""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"].strftime(time)
if "updated_at" in new_dict:
new_dict["updated_at"] = new_dict["updated_at"].strftime(time)
new_dict["__class__"] = self.__class__.__name__
if "_sa_instance_state" in new_dict:
del new_dict["_sa_instance_state"]
return new_dict