Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task 2: fixes to have kwargs #14

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions console.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,18 @@ def do_create(self, args):
try:
value = float(value)
params[key] = value
except Exception:
pass
print("params: ", end=' ')
print(params)
new_instance = HBNBCommand.classes[args[0]](params)
new_instance.save() # Save immediately after creation
print(new_instance.id)
elif '.' in value:
try:
value = float(value)
params[key] = value
except:
pass
print("params: ", end='') # DEBUG
print(params) # DEBUG
print(args[0]) # DEBUG
new_instance = HBNBCommand.classes[args[0]](**params)
new_instance.save() # Save immediately after creation
print(new_instance.id)

def help_create(self):
""" Help information for the create method """
Expand Down
32 changes: 23 additions & 9 deletions models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""This module defines a base class for all models in our hbnb clone."""

import uuid
from datetime import datetime
from datetime import datetime, timezone
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, DateTime

Expand All @@ -20,19 +20,33 @@ class BaseModel:
def __init__(self, *args, **kwargs):
"""Instantiates a new model."""
if not kwargs:
print("DEBUG: NOT KWARGS") # DEBUG
self.id = str(uuid.uuid4())
self.created_at = datetime.utcnow()
self.updated_at = datetime.utcnow()
self.created_at = datetime.now(timezone.utc)
self.updated_at = datetime.now(timezone.utc)
else:
# if not in
# else: create it for the above 3
if 'updated_at' in kwargs:
kwargs['updated_at'] = datetime.strptime(
kwargs['updated_at'], '%Y-%m-%dT%H:%M:%S.%f')
kwargs['updated_at'] = datetime.strptime(kwargs['updated_at'],
'%Y-%m-%dT%H:%M:%S.%f')
else:
self.updated_at = datetime.now(timezone.utc)
if 'created_at' in kwargs:
kwargs['created_at'] = datetime.strptime(
kwargs['created_at'], '%Y-%m-%dT%H:%M:%S.%f')
del kwargs['__class__']
kwargs['created_at'] = datetime.strptime(kwargs['created_at'],
'%Y-%m-%dT%H:%M:%S.%f')
else:
self.created_at = datetime.now(timezone.utc)
if 'id' not in kwargs:
self.id = str(uuid.uuid4())
if '__class__' in kwargs:
del kwargs['__class__']
print('DEBUG: BaseModel Else') # DEBUG
for key, val in kwargs.items():
setattr(self, key, val)
print('DEBUG: BaseModel: {}'.format(key)) # DEBUG
if key not in ['updated_at','created_at']:
setattr(self, key, val)
print('DEBUG: setattr') # DEBUG
self.__dict__.update(kwargs)

def __str__(self):
Expand Down
1 change: 1 addition & 0 deletions models/city.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ class City(BaseModel, Base):
# Relationship to Place
places = relationship("Place", back_populates="city",
cascade="all, delete-orphan")

4 changes: 2 additions & 2 deletions models/engine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os


if os.getenv('HBNB_MYSQL_STORAGE') == 'db':
storage_type = os.getenv('HBNB_MYSQL_STORAGE')
if storage_type == 'db':
from .db_storage import DBStorage
storage = DBStorage()
else:
Expand Down
22 changes: 17 additions & 5 deletions models/state.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
#!/usr/bin/python3
"""State Module for HBNB project"""
from models.base_model import BaseModel, Base
from sqlalchemy import Column, String
from datetime import datetime
from sqlalchemy import Column, ForeignKey, String


class State(BaseModel, Base):
"""State class"""
__tablename__ = 'states'
class State(BaseModel):
""" State class """
# if engine.storage_type == 'db':
# __tablename__ = 'states'
# name = Column(String(128), nullable=False)

# else:
# name = ''
name = ''

# class State(BaseModel, Base):
# """State class"""
# __tablename__ = 'states'

# name = Column(String(128), nullable=False)

name = Column(String(128), nullable=False)