def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG EXAMPLE: create City City.create() """ arg = arg.split() error = self.__class_err(arg) temp_dict = {} for p in range(1, len(arg)): temp = arg[p].split("=") key = temp[0] value = temp[1].strip('"').strip("'").replace("_", " ").replace("\\", "") try: value = int(value) except: try: value = float(value) except: pass PARAM[key] = value temp_dict[key] = value PARAM['class'] = arg[0] if not error: for k, v in CNC.items(): if k == arg[0]: if (temp_dict): my_obj = v(**temp_dict) else: my_obj = v() my_obj.save() print(my_obj.id)
def cities_per_state(state_id=None): """ cities route to handle http method for requested cities by state """ state_obj = storage.get('State', state_id) if state_obj is None: abort(404, 'Not found') if request.method == 'GET': all_cities = storage.all('City') state_cities = [obj.to_json() for obj in all_cities.values() if obj.state_id == state_id] return jsonify(state_cities) if request.method == 'POST': req_data = request.get_json() if req_data is None: abort(400, 'Not a JSON') if req_data.get("name") is None: abort(400, 'Missing name') City = CNC.get("City") req_data['state_id'] = state_id new_object = City(**req_data) new_object.save() return jsonify(new_object.to_json()), 201
def users_no_id(user_id=None): """ users route that handles http requests with no ID given """ auth_header = request.headers.get('Authorization') if auth_header: try: auth_token = auth_header.split(" ")[1] except IndexError: abort(400, 'Bearer token malformed.') else: abort(400, 'Provide a valid auth token.') resp = User.decode_auth_token(auth_token) if 'Please log in again.' in resp: abort(400, resp) if request.method == 'GET': all_users = storage.all('User') all_users = [obj.to_json() for obj in all_users.values()] return jsonify(all_users) if request.method == 'POST': req_data = request.get_json() if req_data is None: abort(400, 'Not a JSON') if req_data.get('email') is None: abort(400, 'Missing email') if req_data.get('password') is None: abort(400, 'Missing password') User = CNC.get('User') new_object = User(**req_data) new_object.save() return jsonify(new_object.to_json()), 201
def reviews_per_place(place_id=None): """ reviews route to handle http method for requested reviews by place """ place_obj = storage.get('Place', place_id) if request.method == 'GET': if place_obj is None: abort(404, 'Not found') all_reviews = storage.all('Review') place_reviews = [ obj.to_json() for obj in all_reviews.values() if obj.place_id == place_id ] return jsonify(place_reviews) if request.method == 'POST': if place_obj is None: abort(404, 'Not found') req_json = request.get_json() if req_json is None: abort(400, 'Not a JSON') user_id = req_json.get("user_id") if user_id is None: abort(400, 'Missing user_id') user_obj = storage.get('User', user_id) if user_obj is None: abort(404, 'Not found') if req_json.get('text') is None: abort(400, 'Missing text') Review = CNC.get("Review") req_json['place_id'] = place_id new_object = Review(**req_json) new_object.save() return jsonify(new_object.to_json()), 201
def places_per_city(city_id=None): """ places route to handle http method for requested places by city """ city_obj = storage.get('City', city_id) if city_obj is None: abort(404, 'Not found') if request.method == 'GET': all_places = storage.all('Place') city_places = [ obj.to_json() for obj in all_places.values() if obj.city_id == city_id ] return jsonify(city_places) if request.method == 'POST': req_data = request.get_json() if req_data is None: abort(400, 'Not a JSON') user_id = req_data.get("user_id") if user_id is None: abort(400, 'Missing user_id') user_obj = storage.get('User', user_id) if user_obj is None: abort(404, 'Not found') if req_data.get("name") is None: abort(400, 'Missing name') Place = CNC.get("Place") req_data['city_id'] = city_id new_object = Place(**req_data) new_object.save() return jsonify(new_object.to_json()), 201
def post(self): all_users = storage.all('User').values() req_data = request.get_json() if req_data is None: abort(400, 'Not a JSON') email = req_data.get('email') if email is None: abort(400, 'Missing email') password = req_data.get('password') if password is None: abort(400, 'Missing password') for user in all_users: if user.email == email: abort(400, 'User already exists. Please Log in.') User = CNC.get('User') new_user = User(**req_data) new_user.save() auth_token = new_user.encode_auth_token(new_user.id) responseObject = { 'status': 'success', 'message': 'Successfully registered.', 'user': new_user.to_json(), 'auth_token': auth_token.decode() } return make_response(jsonify(responseObject)), 201
def bikes_per_city(city_id=None): ''' Bikes route to handle http method for requested bikes by city ''' city_obj = storage.get('City', city_id) if city_obj is None: abort(404, 'Not found') if request.method == 'GET': all_bikes = storage.all('Bike') city_bikes = [ obj.to_dict() for obj in all_bikes.values() if obj.city_id == city_id ] return jsonify(city_bikes) if request.method == 'POST': req_json = request.get_json() if req_json is None: abort(400, 'Not a JSON') user_id = req_json.get('user_id') if user_id is None: abort(400, 'Missing user_id') user_obj = storage.get('User', user_id) if user_obj is None: abort(404, 'Not found') if req_json.get('name') is None: abort(400, 'Missing name') Bike = CNC.get('Bike') req_json['city_id'] = city_id new_object = Bike(**req_json) new_object.save() return jsonify(new_object.to_json()), 201
def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG EXAMPLE: create City City.create() """ arg_list = arg.split() error = self.__class_err(arg_list) if not error: ''' instantiates object called at arg[0]''' ''' below this line code handles arguments after arg[0] arguments update dictionary with new key-pair values ''' if os.getenv('HBNB_TYPE_STORAGE', 'fs') == 'db': for k, v in DNC.items(): if k == arg_list[0]: my_obj = v() new_dict = self._parse_args(arg_list) # updates dictionary with new values pairs my_obj.__dict__.update(new_dict) my_obj.save() BaseModel(**my_obj.__dict__) print(my_obj.id) else: for k, v in CNC.items(): if k == arg_list[0]: my_obj = v() new_dict = self._parse_args(arg_list) my_obj.__dict__.update(new_dict) my_obj.save() BaseModel(**my_obj.__dict__) print(my_obj.id)
def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG""" arg = arg.split() error = self.__class_err(arg) if not error: for k, v in CNC.items(): if k == arg[0]: my_obj = v() my_obj.save() print(my_obj.id)
def __class_err(self, arg): ''' Checks for missing class or unknown class. ''' error = 0 if len(arg) == 0: print(Console.ERR[0]) error = 1 else: if isinstance(arg, list): arg = arg[0] if arg not in CNC.keys(): print(Console.ERR[1]) error = 1 return error
def __class_err(self, arg): """ private: checks for missing class or unknown class """ error = 0 if len(arg) == 0: print(HBNBCommand.ERR[0]) error = 1 else: if isinstance(arg, list): arg = arg[0] if arg not in CNC.keys(): print(HBNBCommand.ERR[1]) error = 1 return error
def states_no_id(): ''' States route to handle http method for requested states no id provided. ''' if request.method == 'GET': all_states = storage.all('State') all_states = list(obj.to_dict() for obj in all_states.values()) return jsonify(all_states) if request.method == 'POST': req_json = request.get_json() if req_json is None: abort(400, 'Not a JSON') if req_json.get('name') is None: abort(400, 'Missing name') State = CNC.get('State') new_object = State(**req_json) new_object.save() return jsonify(new_object.to_json()), 201
def categories_no_id(category_id=None): ''' Categories route that handles http requests no ID given ''' if request.method == 'GET': all_categories = storage.all('Category') all_categories = [obj.to_dict() for obj in all_categories.values()] return jsonify(all_categories) if request.method == 'POST': req_json = request.get_json() if req_json is None: abort(400, 'Not a JSON') if req_json.get('name') is None: abort(400, 'Missing name') Category = CNC.get('Category') new_object = Category(**req_json) new_object.save() return jsonify(new_object.to_json()), 201
def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG EXAMPLE: create City City.create() """ d = {} arg = arg.split() if len(arg) > 1: d = self.create_kwargs(arg) error = self.__class_err(arg) if not error: for k, v in CNC.items(): if k == arg[0]: my_obj = v(**d) my_obj.save() print(my_obj.id)
def amenities_no_id(amenity_id=None): """ amenities route that handles http requests no ID given """ if request.method == 'GET': all_amenities = storage.all('Amenity') all_amenities = [obj.to_json() for obj in all_amenities.values()] return jsonify(all_amenities) if request.method == 'POST': req_data = request.get_json() if req_data is None: abort(400, 'Not a JSON') if req_data.get('name') is None: abort(400, 'Missing name') Amenity = CNC.get('Amenity') new_object = Amenity(**req_data) new_object.save() return jsonify(new_object.to_json()), 201
def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG EXAMPLE: create City City.create() """ arg_dict = {} arg = arg.split() error = self.__class_err(arg) if not error: for k, v in CNC.items(): if k == arg[0]: my_obj = v() for ele in arg[1:]: ele[1].replace("_", " ").replace('"', '\"') if "=" not in ele: try: if ele[0].isdigit(): ele[0] = int(ele[0]) except: pass try: ele[0] = float(ele[0]) except: pass arg_dict[ele[0]] = None ele = ele.split('=') try: if ele[1].isdigit(): ele[1] = int(ele[0]) except: pass try: ele[0] = float(ele[0]) print(ele[0]) except: pass arg_dict[ele[0]] = ele[1] merger = my_obj.__dict__.update(arg_dict) my_obj.save() print(my_obj.id)
def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG EXAMPLE: create City City.create() """ arg = arg.split() error = self.__class_err(arg) """ CNC - this variable is a dictionary with: keys: Class Names values: Class type (used for instantiation) """ if not error: for k, v in CNC.items(): if k == arg[0]: if len(arg) > 1: newArgs = arg[1:] dictParam = {} for newArg in newArgs: params = newArg.split('=') try: params[1] = int(params[1]) pass except ValueError: try: params[1] = float(params[1]) pass except ValueError: try: str(params[1]) params[1] = params[1].replace('"', '') params[1] = params[1].replace('_', ' ') except: pass dictParam[params[0]] = params[1] my_obj = v(**dictParam) else: my_obj = v() my_obj.save() print(my_obj.id)
def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG EXAMPLE: create City City.create() """ arg = arg.split() error = self.__class_err(arg) if not error: for k, v in CNC.items(): if k == arg[0]: my_obj = v() """ my_obj.save() """ """ print(my_obj.id) """ """ Go through command line arguments and separate each key value pair - store in new dictionary """ new_dict = {} for s in arg: if '=' in s: index = s.index('=') key = s[:index] value = s[index + 1:] new_dict[key] = value """ Iterate through newly created dictionary and do string formatting """ for key, value in new_dict.items(): new_val = value if value.startswith('"') and value.endswith('"'): new_val = value[1:-1] if '_' in value: new_val = new_val.replace('_', ' ') new_dict[key] = new_val my_obj.__dict__.update(new_dict) my_obj.save() ''' BaseModel(**my_obj.__dict__) ''' print(my_obj.id)
def do_create(self, arg): """create <Class name> <param 1> <param 2> <param 3>... PARAM: <key name>=<value> ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG""" arg = arg.split() validated_args = [] for item in arg[1:]: if "=" not in item: continue validated_args.append(item) args_dict = HBNBCommand.marshal_dict(validated_args) for k, v in CNC.items(): if k == arg[0]: my_obj = v(**args_dict) my_obj.save() print(my_obj.id) return print(HBNBCommand.ERR[1])
def users_no_id(user_id=None): ''' Users route that handles http requests with no ID given ''' if request.method == 'GET': all_users = storage.all('User') all_users = [obj.to_json() for obj in all_users.values()] return jsonify(all_users) if request.method == 'POST': req_json = request.get_json() if req_json is None: abort(400, 'Not a JSON') if req_json.get('email') is None: abort(400, 'Missing email') if req_json.get('password') is None: abort(400, 'Missing password') User = CNC.get('User') new_object = User(**req_json) new_object.save() return jsonify(new_object.to_json()), 201
def do_create(self, arg): """create: create [ARG] ARG = Class Name SYNOPSIS: Creates a new instance of the Class from given input ARG""" arg = arg.split() error = self.__class_err(arg) if not error: for k, v in CNC.items(): if k == arg[0]: my_obj = v() for param in arg[1:]: attribute = param.split('=') value = attribute[1] if value[0] == '"' and value[-1] == '"': value = self.__parse_string(value) else: value = self.__parse_number(value) my_obj.bm_update(attribute[0], value) my_obj.save() print(my_obj.id)