Esempio n. 1
0
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)
Esempio n. 2
0
    def add_actor(data):
        """
        Add new actor
        """
        #data = get_request_data()
        ### YOUR CODE HERE ###

        # use this for 200 response code
        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)
Esempio n. 3
0
def add_actor():
    """
    Add new actor
    """
    ### YOUR CODE HERE ###
    data = get_request_data()
    if 'name' in data.keys():
        if 'date_of_birth' in data.keys():
            if 'gender' in data.keys():
                try:
                    data['date_of_birth'] = dt.strptime(
                        data['date_of_birth'], '%d.%m.%Y').date()
                except:
                    err = 'Wrong data format'
                    return make_response(jsonify(error=err), 400)
                if data['gender'].isalpha():
                    new_record = Actor.create(**data)
                    try:
                        new_actor = {
                            k: v
                            for k, v in new_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(new_actor), 200)
                else:
                    err = 'Wrong gender format'
                    return make_response(jsonify(error=err), 400)
            else:
                err = 'No gender specified'
                return make_response(jsonify(error=err), 400)
        else:
            err = 'No date_of_birth specified'
            return make_response(jsonify(error=err), 400)
    else:
        err = 'No name specified'
        return make_response(jsonify(error=err), 400)