Пример #1
0
    def execute(self, data_dict):
        """Inspect the data_dict to determine which CRUD operation to perform"""

        process = data_dict.get("process", None)  # Get the requested process
        data = data_dict.get("data", None)  # Get the request data

        if process == "create":
            return self.create(data)  # Dispatch to the create function
        elif process == "add":
            return self.add(data)
        elif process == "read":
            if data.get("id", None) == None:  # 400 if there is no ID given
                raise toolkit.ValidationError({},
                                              toolkit._("No ID was given."))
            else:
                return self.read(data)  # Dispatch to the read function
        elif process == "update":
            if data.get("id", None) == None:  # 400 if there is no ID given
                raise toolkit.ValidationError({},
                                              toolkit._("No ID was given."))
            else:
                return self.update(data)  # Dispatch to the update function
        elif process == "delete":
            if data.get("id", None) == None:  # 400 if there is no ID given
                raise toolkit.ValidationError({},
                                              toolkit._("No ID was given."))
            else:
                return self.delete(data)  # Dispatch to the delete function
        else:  # 400 if the request didn't contain an appropriate process
            raise toolkit.ValidationError(
                {},
                toolkit.
                _("Please supply a 'process' attribute in the POST body. Value can be one of: create, read, update, delete"
                  ))
Пример #2
0
def dispatch(context, data_dict):
    """
    Send the action request to the correct place, based on the POST body
    
    Body should contain JSON data as follows:
    {
      "model": One of HarvestNode, HarvestedRecord
      "process": One of "create", "read", "update", "delete"
      "data": An object containing the data to act on
    }
    
    Requests are inspected and passed on to model-specific controllers, defined below
    
    """

    # Determine the correct controller by inspecting the data_dict
    request_model = data_dict.get("model", None)
    controller = None
    if request_model == "HarvestNode":
        controller = HarvestNodeController(context)
    elif request_model == "HarvestedRecord":
        controller = HarvestedRecordController(context)
    else:
        raise toolkit.ValidationError(
            {},
            "Please supply a 'model' attribute in the POST body. Value can be one of: HarvestNode, HarvestedRecord"
        )

    # execute method inspects POST body and runs the correct functions
    return controller.execute(data_dict)
Пример #3
0
def dispatch(context, data_dict):
    """
    Send the action request to the correct place, based on the POST body
    
    Body should contain JSON data as follows:
    {
      "model": One of BulkUpload
      "process": One of "create", "read", "update", "delete"
      "data": An object containing the data to act on
    }
    
    Requests are inspected and passed on to model-specific controllers, defined below
    
    """
    
    # Determine the correct controller by inspecting the data_dict
    request_model = data_dict.get("model", None)
    controller = None
    print "request_model: ",request_model
    if request_model == "BulkUpload":
        controller = BulkUploadController(context)
    elif request_model == "BulkUpload_Package":
        controller = BulkUpload_PackageController(context)
    elif request_model == "StandingData":
        controller = StandingDataController(context)
    elif request_model == "UserSearch":
        controller = UserSearchController(context)
    elif request_model == "DocumentIndex":
        controller = DocumentIndexController(context)
    else:
        raise toolkit.ValidationError({}, "Please supply a 'model' attribute in the POST body. Value can be one of: BulkUpload,BulkUpload_Package,StandingData,UserSearch,DocumentIndex")
    
    # execute method inspects POST body and runs the correct functions
    return controller.execute(data_dict)
Пример #4
0
 def update(self, data):
     """Update an existing object with new data"""
     pk = data.get("id")
     instance = self.model.by_id(
         pk
     )  # by_id() defined by ckanext.ngds.model.ngds_db_object:NgdsDataObject
     if instance:
         result = instance.as_dict(
         )  # as_dict() defined by ckan.model.domain_object:DomainObject
         keys = result.keys(
         )  # Grab the keys from as_dict(). These represent the update-able attributes of the instance
         result.update(
             data
         )  # generate a dict with updates applied to the existing object
         if self.valid_data(result):  # Check that the update is feasible
             for key in keys:  # Cycle through the update-able attributes of the instance
                 if key in data.keys(
                 ):  # If the attribute is defined in the incoming data...
                     setattr(
                         instance, key,
                         data[key])  # Update the attribute of the instance
             instance.save(
             )  # Done with the loop, save the instance to update the object
             return instance.as_dict()  # Return it
         else:  # Update does not produce a valid object, 400
             raise toolkit.ValidationError(
                 {},
                 toolkit.
                 _("The content supplied in the 'data' attribute would create an invalid %s instance."
                   ) % self.model.__name__)
     else:  # ID did not correspond to an existing object, 400
         raise toolkit.ObjectNotFound()
Пример #5
0
 def add(self, data):
     """Save a new object to the database"""
     if self.valid_data(data):
         instance = self.model(**data)
         instance.add()  # No Commit.
         return instance.as_dict(
         )  # as_dict() defined by ckan.model.domain_object:DomainObject
     else:  # 400 if the data is not valid
         raise toolkit.ValidationError(
             {},
             toolkit.
             _("Please supply a 'data' attribute containing the appropriate content for a %s instance."
               ) % self.model.__name__)
Пример #6
0
    def create(self, data):
        """Save a new object to the database"""
        if self.valid_data(data):
            try:
                instance = self.model(**data)
                instance.save(
                )  # Automatically commits, save() defined by ckan.model.domain_object:DomainObject
                print instance.as_dict()
                return instance.as_dict(
                )  # as_dict() defined by ckan.model.domain_object:DomainObject
            except Exception as ex:  #
                if 'duplicate' in ex.__str__():
                    raise toolkit.ValidationError(
                        {}, toolkit._("Duplicate Entry: %s" % ex.__str__()))
                else:
                    raise toolkit.ValidateError({}, toolkit._(ex.__str__()))

        else:  # 400 if the data is not valid
            raise toolkit.ValidationError(
                {},
                toolkit.
                _("Please supply a 'data' attribute containing the appropriate content for a %s instance."
                  ) % self.model.__name__)
Пример #7
0
def do_harvest(context, data_dict):
    """
    Perform a harvest
    
    POST body should identify the ID of a HarvestNode
    { "id": "harvest-node-id" }
    
    """
    the_id = data_dict.get("id", None)
    if the_id == None:
        raise toolkit.ValidationError(
            {},
            "Please supply the ID of a HarvestNode that you want to harvest")
    else:
        node = context['model'].HarvestNode.by_id(the_id)
        node.do_harvest()