Ejemplo n.º 1
0
    def get(self, collection, id):
        """
        Route : GET /<collection>/<id>
        Description : Gets the chosen document.
        Returns the document.
        """

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

        if collection not in self.app.DOMAINS:
            abort(404)
        document = self.mongo.db[collection].find_one_or_404({"_id": id})
        document['etag'] = get_etag(document)
        return send_response(document, etag=document['etag'])
Ejemplo n.º 2
0
    def get(self, collection, id):
        """
        Route : GET /<collection>/<id>
        Description : Gets the chosen document.
        Returns the document.
        """

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

        if collection not in self.app.DOMAINS:
            abort(404)
        document = self.mongo.db[collection].find_one_or_404({"_id" : id})
        document['etag'] = get_etag(document)
        return send_response(document, etag=document['etag'])
Ejemplo n.º 3
0
    def post(self, collection):
        """
        Route : POST /<collection>/
        Description : Creates a list of documents in the database.
        Returns status and _id for each document.
        """

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

        if collection not in self.app.DOMAINS:
            abort(404)
        if request.mimetype != 'application/json':
            return send_error(415, "JSON_NEEDED",
                              "Accepted media type : application/json")
        data = request.data
        if not data:
            return send_error(400, "EMPTY_DATA")
        if isinstance(data, str) or isinstance(data, unicode):
            try:
                data = json.loads(data)
            except JSONDecodeError:
                return send_error(400, "BAD_JSON_FORMAT")
        if isinstance(data, dict):
            status = self.validate(data, collection)
            if status['created']:
                base_url = request.base_url
                response = {'title': "Document created", 'links': []}
                response['links'].append(
                    get_self_link(title=self.app.DOMAINS[collection]['title'],
                                  base_url=base_url,
                                  description='You are here.',
                                  methods=["GET", "POST", "DELETE"]))
                response['links'].append(
                    get_document_link(status['document'], base_url))
                return send_response(response, 201,
                                     get_etag(status['document']))
            else:
                return send_error(400, "VALIDATION_ERROR", status['issues'])
        return send_error(400, "BAD_DATA_FORMAT")
Ejemplo n.º 4
0
    def post(self, collection):
        """
        Route : POST /<collection>/
        Description : Creates a list of documents in the database.
        Returns status and _id for each document.
        """

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

        if collection not in self.app.DOMAINS:
            abort(404)
        if request.mimetype != 'application/json':
            return send_error(415, "JSON_NEEDED", "Accepted media type : application/json")
        data = request.data
        if not data:
            return send_error(400, "EMPTY_DATA")
        if isinstance(data, str) or isinstance(data, unicode):
            try:
                data = json.loads(data)
            except JSONDecodeError:
                return send_error(400, "BAD_JSON_FORMAT")
        if isinstance(data, dict):
            status = self.validate(data, collection)
            if status['created']:
                base_url = request.base_url
                response = {'title': "Document created", 'links': []}
                response['links'].append(get_self_link(
                    title=self.app.DOMAINS[collection]['title'],
                    base_url=base_url,
                    description='You are here.',
                    methods=["GET", "POST", "DELETE"]
                ))
                response['links'].append(get_document_link(status['document'], base_url))
                return send_response(response, 201, get_etag(status['document']))
            else:
                return send_error(400, "VALIDATION_ERROR", status['issues'])
        return send_error(400, "BAD_DATA_FORMAT")