Example #1
0
    def post(cls):
        # POST /category
        data = cls.parser.parse_args()  

        category = CategoryModel(**data)
        try:
            category.save_to_db()
        except:
             return {"Messege": "An error occured inserting the category."}, 500
        return category.json(), 201
Example #2
0
    def post(self, name):
        if CategoryModel.find_by_name(name):
            return {'message': "A category with name '{}' already exists.".format(name)}, 400

        category = CategoryModel(name)
        try:
            category.save_to_db()
        except:
            return {"message": "An error occurred creating the category."}, 500

        return category.json(), 201
Example #3
0
    def put(self):
        data = _category_parser.parse_args()

        category = CategoryModel(**data)

        try:
            category.update_to_db()
        except:
            return {"message": "An error occurred updating the category"}, 500

        return category.json(), 200
Example #4
0
    def post(self, name):
        if CategoryModel.find_by_name(name):
            return {'message': 'Category already exists'}, 400
        category = CategoryModel(name)

        try:
            category.save_to_db()
        except Exception:
            return {'message': 'Error occured while creating category'}, 500

        return category.json(), 201
Example #5
0
    def post(self):
        data = self.parser.parse_args()
        new_cat = CategoryModel(**data)

        try:
            db.session.add(new_cat)
            db.session.commit()
        except exc.IntegrityError as e:
            return {"message": e.args[0]}, 500
        except:
            return {"message": "Something went wrong"}, 500

        return {'data': new_cat.json()}
Example #6
0
    def post(self, name):
        category = CategoryModel.find_by_name(name)
        if category:
            return {
                "message": f"A category with name {name} already exists"
            }, 400

        category = CategoryModel(name)
        try:
            category.save_to_db()
        except:
            return {"message": "An error occured while creating category"}, 500

        return category.json(), 201
Example #7
0
    def post(self):
        data = parser.parse_args()

        if CategoryModel.query.filter_by(name=data['name']).first():
            msg = "A category with name:'{}' already exists.".format(
                data['name'])
            return {"message": msg}, 400

        category = CategoryModel(**data)
        try:
            category.save()
        except:
            return {"message": "An error occurred while inserting Category"}, 500

        return category.json(), 201
Example #8
0
    def post(cls, name):
        category = CategoryModel.find_existing_by_name(name)
        if category:
            return {"message": "Category with name '{}' already exists".format(name)}, 400

        data = cls.parser.parse_args()

        category = CategoryModel(name, **data)
        try:
            category.save_to_db()
        except IntegrityError as e:
            return {"database_exception": str(e)}, 400
        except:
            return {"message": "Internal error occurred during insertion."}, 500

        return category.json(), 201
Example #9
0
    def put(self, category_name):

        data = Category.parser.parse_args()
        category = CategoryModel.find_by_name(category_name)

        if category is None:
            category = CategoryModel(category_name, data['category_value'])
        else:
            category.category_value = data['category_value']

        try:
            category.save_to_db()
        except:
            return {"message": " unable to save /update category in DB"}, 500

        return category.json(), 200
Example #10
0
    def post(self):
        data = Category.parser.parse_args()
        if CategoryModel.find_by_name(data['name']):
            return {
                'message':
                "A category with name '{}' already exists.".format(
                    data['name'])
            }, 400

        category = CategoryModel(uuid.uuid4().hex, data['name'])
        try:
            category.save_entity()
        except:
            return {
                'message': "An error occurred while creating the category."
            }, 500

        return category.json(), 201
Example #11
0
    def put(cls, name):
        category = CategoryModel.find_existing_by_name(name)

        data = cls.parser.parse_args()

        if not category:
            category = CategoryModel(name, **data)
        else:
            category.update(data)

        try:
            category.save_to_db()
        except IntegrityError as e:
            return {"database_exception": str(e)}, 400
        except:
            return {"message": "Internal error occurred during the update."}, 500

        return category.json(), 201
Example #12
0
    def post(self):
        _category_parser = reqparse.RequestParser(bundle_errors=True)
        _category_parser.add_argument('name',
                                      type=non_empty_string,
                                      required=True,
                                      help="The name field is required!"
                                      )
        data = _category_parser.parse_args()
        if CategoryModel.find_by_name(data['name']):
            return {'message': "A category with name {} already exists".format(data['name'])}, 400

        article = CategoryModel(**data)
        try:
            article.save_to_db()
        except Exception as e:
            return {'message': 'An error occurred while creating the category.'}, 500

        return article.json(), 201
Example #13
0
    def post(self, id):
        """POST request that deals with creation of a category provided an id"""

        # Call the model in order to find the category provided its id
        if CategoryModel.find_by_id(id):
            return {'message': "A Category with id '{}' already exists.".format(id)}, 400
    
        # Push the data to the model
        Category = CategoryModel(id)
        try:
            # Try to save
            Category.save_to_db()
        except:
            # If error
            return {"message": "An error occurred creating the Category."}, 500

        # once done we return the object as json
        return Category.json(), 201
Example #14
0
    def post(self):
        """
		Agregar una nueva categoría (Solo los admin pueden hacerlo).
		"""
        if current_identity.user_type != user_types['admin']:
            return {
                "message": "No tiene permitido crear nuevas categorías."
            }, 401

        data = CategoryList.parser.parse_args()
        if CategoryModel.find_by_name(data['name']):
            return {
                "message": f"La categoría {data['name']!r} ya existe."
            }, 400

        new_category = CategoryModel(**data)
        new_category.save_to_db()

        return new_category.json(), 201
Example #15
0
    def post(self):
        data = CreateCategory.parser.parse_args()
        if CategoryModel.find_by_name(data['name']):
            return {
                'message':
                "An category with name '{}' already exists.".format(
                    data['name'])
            }, 400

        category = CategoryModel(**data)

        try:
            category.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the category."
            }, 500

        return category.json(), 201
Example #16
0
    def post(self, name):
        """POST request that deals with creation of a category provided a name"""

        # Call the model to look for a category provided a name
        if CategoryModel.find_by_name(name):
            return {'message': "A Category with name '{}' already exists.".format(name)}, 400

        # We create the category with the given name      
        Category = CategoryModel(name)

        try:
            # try to save and commit
            Category.save_to_db()
        except:
            # if error during saving and committing
            return {"message": "An error occurred creating the Category."}, 500

        # we return the json of the category
        return Category.json(), 201
Example #17
0
    def put(self, id):
        data = EditCategory.parser.parse_args()
        ex_category = CategoryModel.find_by_name(data['name'])

        if ex_category and ex_category.id != id:
            return {
                'message':
                "An category with name '{}' already exists.".format(
                    data['name'])
            }, 400

        category = CategoryModel.find_by_id(id)

        if category:
            category.name = data['name']
        else:
            category = CategoryModel(**data)

        category.save_to_db()
        return category.json()
Example #18
0
    def post(self, category_name):

        category = CategoryModel.find_by_name(category_name)
        if category:
            return {
                'message': " category {} already exists".format(category_name)
            }, 400

        data = Category.parser.parse_args()
        category = CategoryModel(category_name, data['category_value'])

        try:
            category.save_to_db()  # passing product object to insert
            # print(category)
        except:
            return {
                "message": "error occured while loading category data into DB "
            }, 400

        return category.json(), 201
Example #19
0
    def put(self, category_id):
        data = parser.parse_args()

        category = CategoryModel.find_by_id(category_id)

        if category is None:
            new_category = CategoryModel(**data)

            try:
                new_category.save()

                return new_category.json(), 201
            except:
                return {"message": "An error occurred while inserting Category"}, 500

        try:
            category.update(**data)
        except:
            return {"message": "An error occurred while updating Category."}, 500

        return category.json(), 200
Example #20
0
    def put(self, category_id):
        _category_parser = reqparse.RequestParser(bundle_errors=True)
        _category_parser.add_argument('name',
                                      type=non_empty_string,
                                      required=False,
                                      help="The name field is required!"
                                      )
        data = _category_parser.parse_args()
        category = CategoryModel.find_by_id(category_id)

        if category is None:
            category = CategoryModel(**data)
        else:
            category.name = data['name'] if data['name'] is not None else category.name

        try:
            category.save_to_db()
        except Exception as e:
            return {'message': 'An error occurred while creating the category.'}, 500

        return category.json(), 201
    def put(self):
        data = Category.parser.parse_args()
        data = {
            "name": data['name'].lower(),
            "type": data['type'].lower(),
            "budget": data['budget']
        }

        category = CategoryModel.find_by_name(data['name'].lower())

        if category is None:
            category = CategoryModel(**data)
        else:
            if data['budget'] is not None:
                category.budget = data['budget']
            if not category.type == data['type']:
                category.type = data['type']

        TransactionModel.update_prices(**data)

        category.save_to_db()
        return category.json()
Example #22
0
    def post(self):
        data = Category.parser.parse_args()

        name = data['name']

        category = CategoryModel.find_by_name(name)

        if category:
            return {
                "message": "That category, {}, already exists.".format(name)
            }, 400

        category = CategoryModel(name, current_identity.id)

        try:
            category.save_to_db()
        except:
            return {
                "message": "An error occured while creating the category."
            }, 500

        return category.json(), 201
Example #23
0
    def post(self):
        _parser = reqparse.RequestParser()
        _parser.add_argument('name',
                             type=str,
                             required=True,
                             help="This field cannot be blank.")
        _parser.add_argument('urgency',
                             type=int,
                             required=True,
                             help="This field cannot be blank.")
        data = _parser.parse_args()
        category = CategoryModel.find_by_name(data['name'])
        if category:
            return {"message": "Category with name already exists"}, 400

        category = CategoryModel(name=data['name'], urgency=data['urgency'])
        category.save_to_db()

        return {
            "message": "Category created successfully",
            "category": category.json()
        }, 201
    def post(self):
        data = Category.parser.parse_args()
        data = {
            "name": data['name'].lower(),
            "type": data['type'].lower(),
            "budget": data['budget']
        }

        if CategoryModel.find_by_name(data['name'].lower()):
            return {
                "message": "A category with that name already exists."
            }, 400

        category = CategoryModel(**data)

        try:
            category.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the category."
            }, 500

        return category.json(), 201
Example #25
0
    def put(self, id):
        """PUT request that deals with the edition or a creation of a category with at a certain id"""

        # Parse the application/json data
        data = Category.parser.parse_args()

        # Call the model
        category = CategoryModel.find_by_id(id)

        # if the category already exists
        if category:

            # we update the field
            category.name = data['name']
        else:
            # Else we create it
            category = CategoryModel(**data)

        # then we save and commit
        category.save_to_db()

        # We return the category as json
        return category.json()
Example #26
0
    def put(self, name):
        """PUT request that deals with the edition or a creation of a category with at a certain name"""

        # we parse the arguments of the JSON
        data = Category.parser.parse_args()

        # Call the model to find the category entry with a specific name
        category = CategoryModel.find_by_name(name)

        # if the category exists
        if category:

            # then we update
            category.name = data['name']
        else:

            # If doesn't exist we create it
            category = CategoryModel(**data)
        
        # we save and commit
        category.save_to_db()

        # Return a json of the object
        return category.json()
Example #27
0
    def post(self):
        """POST request create a category, provided a name"""

        # we parse the args
        data = Category.parser.parse_args()

        # we call the model and find the category by name
        if CategoryModel.find_by_name(data['name']):

            # We return if the category already exist
            return {'message': "An category with name '{}' already exists.".format(data['name'])}, 400

        # Ifnot we create the category
        category = CategoryModel(**data)

        try:
            # We try to save and commit
            category.save_to_db()
        except:
            # In case of error we return an error
            return {"message": "An error occurred inserting the category."}, 500

        # Return a json of the object
        return category.json(), 201
Example #28
0
    def post(self, businessId):
        claims = get_jwt_claims()
        if not claims['is_admin']:
            return {'message': 'admin previlege required'}, 400

        approved_zid_list = VbusinessModel.find_all_business_list()

        current_user = UserModel.find_by_user(get_jwt_identity())

        if (current_user.businessId
                not in approved_zid_list) or (businessId
                                              not in approved_zid_list):
            return {
                'message':
                'Error # 180 in Product Resource, You have not been authorized to use this business'
            }, 400

        json_data = request.get_json()

        if not json_data:
            return {
                'message':
                'Error # 186 in Product Resource, No input data provided'
            }, 400

        try:
            data = categorySchema.load(json_data).data
        except ValidationError as err:
            return err.messages, 400

        data['approvedCategory'] = html.unescape(data['approvedCategory'])
        if not CaitemModel.find_by_zid_category([businessId],
                                                [data['approvedCategory']]):
            return {
                'message':
                'Error # 131 in Product Resources, this category or business ID does not exist in our System'
            }, 400

        if CategoryModel.find_by_zid_category(current_user.businessId,
                                              data['approvedCategory']):
            return {
                'message':
                'Error # 194 in Product Resources, this category has already been approved'
            }, 400

        categoryDetail = CategoryModel(
            zid=businessId,
            approvedCategory=data['approvedCategory'],
            xtra1=None,
            xtra2=None,
            xtra3=None,
            xtra4=None,
            xtra5=None)

        try:
            categoryDetail.save_to_db()
        except Exception as e:
            print(e)
            return {
                "message":
                "Error # 205 in Product Resource, An error occured while saving the product category"
            }, 400

        return categoryDetail.json(), 200