def actor_clear_relations(): """ Clear all relations by id """ data = get_request_data() if 'id' in data.keys(): try: row_id = int(data['id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) try: actor = Actor.query.filter_by( id=row_id).first() # clear relations here rel_actor = { k: v for k, v in actor.__dict__.items() if k in ACTOR_FIELDS } rel_actor['filmography'] = str(actor.filmography) return make_response(jsonify(rel_actor), 200) except: err = 'Record with such id does not exist' return make_response(jsonify(error=err), 400) else: err = 'Id not specified' return make_response(jsonify(error=err), 400)
def actor_add_relation(): """ Add a movie to actor's filmography """ data = get_request_data() if 'id' in data.keys(): try: row_id = int(data['id']) relation_id = int(data['relation_id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) obj = Movie.query.filter_by(id=relation_id).first() try: actor = Actor.add_relation(row_id, obj) rel_actor = {k: v for k, v in actor.__dict__.items() if k in ACTOR_FIELDS} rel_actor['filmography'] = str(actor.filmography) except: err = 'Record with such id does not exist' return make_response(jsonify(error=err), 400) return make_response(jsonify(rel_actor), 200) else: err = 'No id specified' return make_response(jsonify(error=err), 400)
def get_actor_by_id(): """ Get record by id """ data = get_request_data() if 'id' in data.keys(): try: row_id = int(data['id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) obj = Actor.query.filter_by(id=row_id).first() try: actor = { k: v for k, v in obj.__dict__.items() if k in ACTOR_FIELDS } except: err = 'Record with such id does not exist' return make_response(jsonify(error=err), 400) return make_response(jsonify(actor), 200) else: err = 'No id specified' return make_response(jsonify(error=err), 400)
def actor_add_relation(): """ Add a movie to actor's filmography """ data = get_request_data() KEY_DICT = ['id', 'relation_id'] for dat in data: if dat not in set(KEY_DICT): err = 'Wrong key' return make_response(jsonify(error=err), 400) ### YOUR CODE HERE ### if data.get('id'): try: row_id = int(data['id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) obj = Actor.query.filter_by(id=data['id']).first() try: try_actor = { k: v for k, v in obj.__dict__.items() if k in ACTOR_FIELDS } except: err = 'Actor with such id does not exist' return make_response(jsonify(error=err), 400) if data.get('relation_id'): try: row_id = int(data['relation_id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) obj = Movie.query.filter_by(id=data['relation_id']).first() try: try_movie = { k: v for k, v in obj.__dict__.items() if k in MOVIE_FIELDS } except: err = 'Movie with such id does not exist' return make_response(jsonify(error=err), 400) else: err = 'No related_id specified' return make_response(jsonify(error=err), 400) related_movie = Movie.query.filter_by(id=data['relation_id']).first() actor = Actor.add_relation(data['id'], related_movie) # add relation here rel_actor = { k: v for k, v in actor.__dict__.items() if k in ACTOR_FIELDS } rel_actor['filmography'] = str(actor.filmography) return make_response(jsonify(rel_actor), 200) else: err = 'No id specified' return make_response(jsonify(error=err), 400)
def actor_add_relation(): """ Add a movie to actor's filmography """ data = get_request_data() obj = Movie.query.filter_by(id=data['relation_id']).first() actor = Actor.add_relation(data['id'], obj) rel_actor = {k: v for k, v in actor.__dict__.items() if k in ACTOR_FIELDS} rel_actor['filmography'] = str(actor.filmography) return make_response(jsonify(rel_actor), 200)
def add_actor(): """ Add new actor """ data = get_request_data() # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! for dat in data: if dat not in set(ACTOR_FIELDS): err = 'Wrong key' return make_response(jsonify(error=err), 400) # # if 'name' in data.keys(): # if data['name'].isdigit(): # err = 'Name must be string' # return make_response(jsonify(error=err), 400) # # if (len(data['name']) > 50): # err = 'Name must be less than 50 characters' # return make_response(jsonify(error=err), 400) # # if not data['name']: # err = 'Name cannot be null' # return make_response(jsonify(error=err), 400) # # if 'gender' in data.keys(): # if (data['gender'] != "male") and (data['gender'] != "female"): # !!!!!!!!!!!!!!!!!!!!!!!! # err = 'There are only two genders' # return make_response(jsonify(error=err), 400) # if not data['date_of_birth']: err = 'enter date' return make_response(jsonify(error=err), 400) if 'date_of_birth' in data.keys(): try: dt.strptime(data['date_of_birth'], DATE_FORMAT) except: err = 'incorrect date format(d.m.y)' return make_response(jsonify(error=err), 400) # act_dict = get_dict_of_actors() # for a in act_dict: # if (a['name'] == data['name']): # err = 'actor with that name is already exist' # return make_response(jsonify(error=err), 400) new_record = data new_record['date_of_birth'] = dt.strptime(data['date_of_birth'], '%d.%m.%Y') new_actor = Actor.create(**new_record) # !!!!!!!!!!!!!!!!!!!!!!!! new_record['id'] = new_actor.id return make_response(jsonify(new_record), 200)
def update_actor(): """ Update actor record by id """ data = get_request_data() upd_record = Actor.update(**data) upd_actor = { k: v for k, v in upd_record.__dict__.items() if k in ACTOR_FIELDS } return make_response(jsonify(upd_actor), 200)
def add_actor(): """ Add new actor """ data = get_request_data() new_record = Actor.create(**data) new_actor = { k: v for k, v in new_record.__dict__.items() if k in ACTOR_FIELDS } return make_response(jsonify(new_actor), 200)
def delete_actor(): """ Delete actor by id """ data = get_request_data() if 'id' in data.keys(): try: row_id = int(data['id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) if Actor.delete(row_id): msg = f'Record successfully deleted' return make_response(jsonify(message=msg), 200) else: err = 'Record with such id does not exist' return make_response(jsonify(error=err), 400) else: err = 'No id specified' return make_response(jsonify(error=err), 400)
def update_actor(): """ Update actor record by id """ data = get_request_data() if 'id' in data.keys(): try: row_id = int(data['id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) if 'date_of_birth' in data.keys(): try: from settings.constants import DATE_FORMAT dt.strptime(data['date_of_birth'], DATE_FORMAT) except ValueError: err = "This is the incorrect date string format" return make_response(jsonify(error=err), 400) for k in data.keys(): if k not in ACTOR_FIELDS: err = 'more then need' return make_response(jsonify(error=err), 400) try: upd_record = Actor.update(row_id, **data) upd_actor = {k: v for k, v in upd_record.__dict__.items() if k in ACTOR_FIELDS} except: err = 'Record with such id does not exist' return make_response(jsonify(error=err), 400) return make_response(jsonify(upd_actor), 200) else: err = 'No id specified' return make_response(jsonify(error=err), 400)
def add_actor(): """ Add new actor """ data = get_request_data() if 'date_of_birth' in data.keys(): try: from settings.constants import DATE_FORMAT dt.strptime(data['date_of_birth'], DATE_FORMAT) except ValueError: err = "This is the incorrect date string format" return make_response(jsonify(error=err), 400) else: err = 'No date specified' return make_response(jsonify(error=err), 400) try: new_record = Actor.create(**data) new_actor = {k: v for k, v in new_record.__dict__.items() if k in ACTOR_FIELDS} except: err = 'Input wrong fields' return make_response(jsonify(error=err), 400) return make_response(jsonify(new_actor), 200)
def del_actor(): data = get_request_data() id_del = data['id'] if id_del and 'id' in data.keys(): try: row_id = int(data['id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) obj = Actor.query.filter_by(id=row_id).first() try: actor = { k: v for k, v in obj.__dict__.items() if k in ACTOR_FIELDS } except: err = 'Record with such id does not exist' return make_response(jsonify(error=err), 400) Actor.delete(id_del) message = "row with id has been succsessfuly deleted" return make_response(jsonify(message), 200)
def upd_actor(): data = get_request_data() new_record = data for dat in data: if dat not in set(ACTOR_FIELDS): err = 'Wrong key' return make_response(jsonify(error=err), 400) if data.get('id'): try: row_id = int(data['id']) except: err = 'Id must be integer' return make_response(jsonify(error=err), 400) obj = Actor.query.filter_by(id=row_id).first() try: try_actor = { k: v for k, v in obj.__dict__.items() if k in MOVIE_FIELDS } except: err = 'Record with such id does not exist' return make_response(jsonify(error=err), 400) if data.get('gender'): if (data['gender'] != "male") and ( data['gender'] != "female") and (data['gender'] != "Female") and ( data['gender'] != "Male"): # !!!!!!!!!!!!!!!!!!!!!!!! err = 'There are only two genders' return make_response(jsonify(error=err), 400) else: new_record.pop('date_of_birth', None) if data.get('date_of_birth'): try: dt.strptime(data['date_of_birth'], DATE_FORMAT).date() new_record['date_of_birth'] = dt.strptime( data['date_of_birth'], DATE_FORMAT).date() except: err = 'incorrect date format(d.m.y)' return make_response(jsonify(error=err), 400) else: new_record.pop('date_of_birth', None) if data.get('name'): obj = Actor.query.filter_by(name=new_record['name']).first() if obj: err = 'Actor with that name already exist' return make_response(jsonify(error=err), 400) if data['name'].isdigit(): err = 'Name must be string' return make_response(jsonify(error=err), 400) if (len(data['name']) > 50): err = 'Name must be less than 50 characters' return make_response(jsonify(error=err), 400) else: new_record.pop('data', None) Actor.update(data['id'], **new_record) actor = Actor.query.filter_by(id=data['id']).first() print(actor) if actor: return_var = { k: v for k, v in actor.__dict__.copy().items() if not k.startswith('_') } else: err = f'Could not find an actor with ID {data.get("id")}' return make_response(jsonify(error=err), 400) return make_response(jsonify(return_var), 200) else: err = 'No id specified' return make_response(jsonify(error=err), 400)