class ModelResource(Resource): """ Model level resources - implements all REST methods for paths with no ID """ def get(self, model_name): """ Lists all instances of a given model :param model_name: the name of the model, as it appears in the config file :return: a list of instances of the model object """ self.model = Model(model_name, self.app) user = self.check_auth() return self.model.to_repr(**request.args) def post(self, model_name): """ Create an instance of a model :param model_name: the name of the model, as it appears in the config file :return: details of the created instance """ self.model = Model(model_name, self.app) user = self.check_auth() response = self.model.create(request.json) return response
def get(self, model_name): """ Lists all instances of a given model :param model_name: the name of the model, as it appears in the config file :return: a list of instances of the model object """ self.model = Model(model_name, self.app) user = self.check_auth() return self.model.to_repr(**request.args)
def post(self, model_name): """ Create an instance of a model :param model_name: the name of the model, as it appears in the config file :return: details of the created instance """ self.model = Model(model_name, self.app) user = self.check_auth() response = self.model.create(request.json) return response
def delete(self, model_name, _id): """ Delete a single instance by its ID :param model_name: the name of the model, as it appears in the config file :param _id: MongoDB _id string """ self.model = Model(model_name, self.app) user = self.check_auth() instance = self.model.delete(_id=_id) return instance
def get(self, model_name, _id): """ Retrieve a single instance by its ID :param model_name: the name of the model, as it appears in the config file :param _id: MongoDB _id string :return: the MongoDB document """ self.model = Model(model_name, self.app) user = self.check_auth() instance = self.model.retrieve(_id) return instance
def put(self, model_name, _id): """ Update a single instance by its ID :param model_name: the name of the model, as it appears in the config file :param _id: MongoDB _id string :return: the MongoDB document """ self.model = Model(model_name, self.app) user = self.check_auth() instance = self.model.update(_id=_id, data=request.json) return instance
def get_related(self, related_model_name): """ Returns the related model :param related_model_name: the name of another model in the app :rtype: crudcast.models.Model """ from crudcast.models import Model return Model(name=related_model_name, app=self.model.app)
class InstanceResource(Resource): """ Instance level resources - implements all REST methods for paths with an ID """ def get(self, model_name, _id): """ Retrieve a single instance by its ID :param model_name: the name of the model, as it appears in the config file :param _id: MongoDB _id string :return: the MongoDB document """ self.model = Model(model_name, self.app) user = self.check_auth() instance = self.model.retrieve(_id) return instance def put(self, model_name, _id): """ Update a single instance by its ID :param model_name: the name of the model, as it appears in the config file :param _id: MongoDB _id string :return: the MongoDB document """ self.model = Model(model_name, self.app) user = self.check_auth() instance = self.model.update(_id=_id, data=request.json) return instance def delete(self, model_name, _id): """ Delete a single instance by its ID :param model_name: the name of the model, as it appears in the config file :param _id: MongoDB _id string """ self.model = Model(model_name, self.app) user = self.check_auth() instance = self.model.delete(_id=_id) return instance