Beispiel #1
0
    def post(self, category_name):

        db.create_all()
        db.session.commit()
        if BrandCategoryModel.find_by_name(category_name):
            return {
                'message':
                "An Record with name '{}' already exists.".format(
                    category_name)
            }, 400

        data = self.parser.parse_args()

        brandcategory = BrandCategoryModel(
            category_name,
            **data,
        )
        brandcategory.created_by = 1
        brandcategory.created_on = datetime.now()
        brandcategory.modified_by = 0
        brandcategory.deleted_by = 0
        try:
            brandcategory.save_to_db()
        except Exception as e:
            return {
                "message":
                "An error occurred while inserting the Record.'{}'".format(e)
            }

        return brandcategory.json(), 201
Beispiel #2
0
    def put(self, token):
        try:
            data = self.parser.parse_args()
            validateObj = BrandCategoryModel.validateData(data, request)
            if type(validateObj) is dict:
                return {"success": False, "errors": validateObj}, 400

            category_id = decodeID(token)
            category = BrandCategoryModel.find_by_id(category_id)
            if not category or category.token != token:
                return {"success": False, 'message': 'Record Not Found'}, 404

            category.category_name = data['category_name']
            category.modified_on = datetime.now()
            category.modified_by = 1
            category.logo = data['logo']
            name = data['category_name']
            if name.strip():
                category.save_to_db()
            else:
                return {
                    "success": False,
                    "message": "String Should not be empty"
                }
            return {
                "success": True,
                "message": "Record updated successfully."
            }, 200
        except Exception as e:
            return {"success": False, "message": str(e)}
Beispiel #3
0
 def get(self, category_name):
     brandcategory = BrandCategoryModel.find_by_name(category_name)
     try:
         if brandcategory:
             return brandcategory.json()
     except Exception as e:
         return {"message": "Record not found'{}'".format(e)}, 404
Beispiel #4
0
 def get(cls, token):
     try:
         category_id = decodeID(token)
         category = BrandCategoryModel.find_by_id(category_id)
         if not category or category.token != token:
             return {"success": False, 'message': 'Record Not Found'}, 404
         return category.json(), 200
     except Exception as e:
         return {"success": False, "message": str(e)}
Beispiel #5
0
    def delete(cls, category_name):
        brandcategory = BrandCategoryModel.find_by_name(category_name)
        if brandcategory:
            brandcategory.deleted_by = 1
            brandcategory.deleted_on = datetime.now()
            brandcategory.save_to_db()

            return {'message': 'Record deleted'}

        else:
            return {"Message": "Record Not FOUND"}
Beispiel #6
0
 def put(self, category_name):
     data = self.parser.parse_args()
     brandcategory = BrandCategoryModel.find_by_name(category_name)
     if brandcategory:
         brandcategory.modified_on = datetime.now()
         brandcategory.modified_by = 1
         brandcategory.logo = data['logo']
         brandcategory.save_to_db()
     else:
         return {"Message": "Record Not FOUND"}
     return brandcategory.json()
Beispiel #7
0
    def delete(cls, token):
        try:
            category_id = decodeID(token)
            category = BrandCategoryModel.find_by_id(category_id)
            if not category or category.token != token:
                return {"success": False, 'message': 'Record Not Found'}, 404

            category.deleted_by = 1
            category.deleted_on = datetime.now()
            category.save_to_db()
            #category.delete_from_db()
            return {"success": True, 'message': 'Record deleted.'}, 200
        except Exception as e:
            return {"success": False, "message": str(e)}
Beispiel #8
0
    def post(self):
        data = self.parser.parse_args()
        category = BrandCategoryModel(**data)
        category.created_by = 1
        category.created_on = datetime.now()
        category.modified_by = 0
        category.deleted_by = 0
        category.modified_on = None
        category.deleted_on = None

        try:
            validateObj = BrandCategoryModel.validateData(data, request)

            if type(validateObj) is dict:
                return {"success": False, "errors": validateObj}, 400

            if BrandCategoryModel.find_by_name(data['category_name']):
                return {
                    "success": False,
                    "message":
                    "A category with that Record Name already exists"
                }, 400
            name = data['category_name']
            if name.strip():
                category.save_to_db()
                category.token = encodeID(category.category_id)
                category.save_to_db()
            else:
                return {
                    "success": False,
                    "message": "String Should not be empty"
                }
        except Exception as e:
            return {
                "message":
                "An error occurred creating the Record.'{}'".format(e)
            }, 500
        return category.json(), 201