Esempio n. 1
0
 def delete(self):
     if "id" in request.args:
         idImg = ObjectId(request.args["id"])
         result = connection().images.delete_one({"_id": idImg})
         images = connection().images.find()
         if result.deleted_count > 0:
             images = connection().images.find()
             return jsonify(headers={
                 "Content-Type": "application/json",
                 "Access-Control-Allow-Origin": "*"
             },
                            statusCode=200,
                            data=json.loads(dumps(list(images))),
                            count=images.count())
         else:
             return jsonify(statusCode=500,
                            headers={
                                "Content-Type": "application/json",
                                "Access-Control-Allow-Origin": "*"
                            },
                            error={
                                "message":
                                "Error deleting image! Try again later."
                            })
     else:
         return jsonify(
             statusCode=400,
             headers={
                 "Content-Type": "application/json",
                 "Access-Control-Allow-Origin": "*"
             },
             error={
                 message:
                 "Error: No ID field provided. Please specify an id."
             })
    def update(self):
        if "id" in request.args:
            idNote = ObjectId(request.args["id"])

            title = request.form.get("title")
            description = request.form.get("description")
            newData = {}
            if title is not None:
                newData["title"] = title
            if description is not None:
                newData["description"] = description
            result = connection().notes.update_one({"_id": idNote},
                                                   {"$set": newData},
                                                   upsert=False)

            if result.modified_count > 0:
                note = connection().notes.find({"_id": idNote})
                return jsonify(headers={
                    "Content-Type": "application/json",
                    "Access-Control-Allow-Origin": "*"
                },
                               statusCode=200,
                               data=json.loads(dumps(list(note))))
            else:
                return jsonify(
                    statusCode=500,
                    headers={
                        "Content-Type": "application/json",
                        "Access-Control-Allow-Origin": "*"
                    },
                    error={"message": "Error updating note! Try again later."})
        else:
            return jsonify(
                statusCode=400,
                headers={
                    "Content-Type": "application/json",
                    "Access-Control-Allow-Origin": "*"
                },
                error={
                    message:
                    "Error: No ID field provided. Please specify an id."
                })
Esempio n. 3
0
    def create(self, req):
        imageFile = request.files["file"]
        if "files" in req.files or imageFile.filename != "":
            if imageFile and self.allowed_file(imageFile.filename):
                url = self.upload_cloudinary(imageFile)
                payload = {"url": url}

                result = connection().images.insert_one(payload)
                if result.inserted_id:
                    image = connection().images.find(
                        {"_id": ObjectId(result.inserted_id)})
                    return jsonify(headers={
                        "Content-Type": "application/json",
                        "Access-Control-Allow-Origin": "*"
                    },
                                   statusCode=200,
                                   data=json.loads(dumps(list(image))))
                else:
                    return jsonify(statusCode=500,
                                   headers={
                                       "Content-Type": "application/json",
                                       "Access-Control-Allow-Origin": "*"
                                   },
                                   error={
                                       "message":
                                       "Error saving file! Try again later."
                                   })
            else:
                return jsonify(statusCode=400,
                               headers={
                                   "Content-Type": "application/json",
                                   "Access-Control-Allow-Origin": "*"
                               },
                               error={"message": "File not supported!"})
        else:
            return jsonify(statusCode=400,
                           headers={
                               "Content-Type": "application/json",
                               "Access-Control-Allow-Origin": "*"
                           },
                           error={"message": "Select at least one file!"})
Esempio n. 4
0
 def get_images(self):
     if "id" in request.args:
         idImg = ObjectId(request.args["id"])
         image = connection().images.find({"_id": idImg})
         return jsonify(
             headers={
                 "Content-Type": "application/json",
                 "Access-Control-Allow-Origin": "*"
             },
             statusCode=200,
             data=json.loads(dumps(list(image))),
         )
     else:
         images = connection().images.find()
         return jsonify(headers={
             "Content-Type": "application/json",
             "Access-Control-Allow-Origin": "*"
         },
                        statusCode=200,
                        data=json.loads(dumps(list(images))),
                        count=images.count())
 def create(self, form):
     note = {
         "title": request.form.get("title"),
         "description": request.form.get("description")
     }
     result = connection().notes.insert_one(note)
     if result.inserted_id:
         note = connection().notes.find(
             {"_id": ObjectId(result.inserted_id)})
         return jsonify(headers={
             "Content-Type": "application/json",
             "Access-Control-Allow-Origin": "*"
         },
                        statusCode=200,
                        data=json.loads(dumps(list(note))))
     else:
         return jsonify(
             statusCode=500,
             headers={
                 "Content-Type": "application/json",
                 "Access-Control-Allow-Origin": "*"
             },
             error={"message": "Error creating new note! Try again later."})