Skip to content

Commit

Permalink
Merge branch 'oaobranch'
Browse files Browse the repository at this point in the history
  • Loading branch information
OluwaninsolaAO committed Mar 21, 2023
2 parents 94210cb + b8b27da commit ca5d84c
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
"""This module defines a base class for all models in our hbnb clone"""
import uuid
from datetime import datetime
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, DateTime

Base = sqlalchemy.orm.declarative_base()


class BaseModel:
"""A base class for all hbnb models"""
id = Column(String(60), nullable=False, 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):
"""Instatntiates a new model"""
if not kwargs:
from models import storage
self.id = str(uuid.uuid4())
self.created_at = datetime.now()
self.updated_at = datetime.now()
storage.new(self)
else:
kwargs['updated_at'] = datetime.strptime(kwargs['updated_at'],
'%Y-%m-%dT%H:%M:%S.%f')
Expand All @@ -31,6 +37,7 @@ 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 to_dict(self):
Expand All @@ -42,3 +49,9 @@ def to_dict(self):
dictionary['created_at'] = self.created_at.isoformat()
dictionary['updated_at'] = self.updated_at.isoformat()
return dictionary


def delete(self):
"""Delete the class instance"""
from models import storage
del storage.all()[self.to_dict()['__class__'] + '.' + self.id]

0 comments on commit ca5d84c

Please sign in to comment.