Exemplo n.º 1
0
 def validate(self, item, collection):
     date_utc = datetime.utcnow()
     v = Validator()
     if v.validate(item, self.app.DOMAINS[collection]['schema']):
         item['created'] = item['updated'] = date_utc
         id = self.mongo.db[collection].insert(item)
         item = self.mongo.db[collection].find_one({"_id": id})
         response = ({'created': True, 'document': item})
     else:
         response = ({'created': False, 'issues': v.error})
     return response
Exemplo n.º 2
0
 def validate(self, item, collection):
     date_utc = datetime.utcnow()
     v = Validator()
     if v.validate(item, self.app.DOMAINS[collection]['schema']):
         item['created'] = item['updated'] = date_utc
         id = self.mongo.db[collection].insert(item)
         item = self.mongo.db[collection].find_one({"_id" : id})
         response = ({'created' : True, 'document' : item})
     else:
         response = ({'created' : False, 'issues' : v.error})
     return response
Exemplo n.º 3
0
    def put(self, collection, id):
        """
        Route : PUT /<collection>/<id>
        Description : Updates the document.
        Returns the status.
        """

        if not self.app.config['ITEM_PUT']:
            abort(405)

        if collection not in self.app.DOMAINS:
            abort(404)
        document = self.mongo.db[collection].find_one_or_404({"_id": id})
        date_utc = datetime.utcnow()
        if request.mimetype != 'application/json':
            return send_error(415, "JSON_NEEDED",
                              "Accepted media type : application/json")
        data = request.data
        if isinstance(data, str) or isinstance(data, unicode):
            try:
                data = json.loads(data)
            except JSONDecodeError:
                return send_error(400, "BAD_JSON_FORMAT")
        for key in data:
            document[key] = data[key]
        copy = {}
        for key in document:
            if key == "created": continue
            if key == "updated": continue
            if key == "_id": continue
            copy[key] = document[key]
        v = Validator()
        if v.validate(copy, self.app.DOMAINS[collection]['schema']):
            document['updated'] = date_utc
            self.mongo.db[collection].save(document)
            return send_response(status=200)
        return send_error(400, "VALIDATION_ERROR", v.error)
Exemplo n.º 4
0
    def put(self, collection, id):
        """
        Route : PUT /<collection>/<id>
        Description : Updates the document.
        Returns the status.
        """

        if not self.app.config['ITEM_PUT']:
            abort(405)

        if collection not in self.app.DOMAINS:
            abort(404)
        document = self.mongo.db[collection].find_one_or_404({"_id" : id})
        date_utc = datetime.utcnow()
        if request.mimetype != 'application/json':
            return send_error(415, "JSON_NEEDED", "Accepted media type : application/json")
        data = request.data
        if isinstance(data, str) or isinstance(data, unicode):
            try:
                data = json.loads(data)
            except JSONDecodeError:
                return send_error(400, "BAD_JSON_FORMAT")
        for key in data:
            document[key] = data[key]
        copy = {}
        for key in document:
            if key == "created": continue
            if key == "updated": continue
            if key == "_id": continue
            copy[key] = document[key]
        v = Validator()
        if v.validate(copy, self.app.DOMAINS[collection]['schema']):
            document['updated'] = date_utc
            self.mongo.db[collection].save(document)
            return send_response(status=200)
        return send_error(400, "VALIDATION_ERROR", v.error)