Example #1
0
def remolt_molt(molt_ID):
    molt = api_utils.get_molt(molt_ID)
    if molt:
        auth = require_auth(request)
        if auth:
            crab = api_utils.get_crab(auth["crab_id"])
            if crab:
                if request.method == "POST":
                    if molt.author is not crab:
                        if not crab.has_remolted(molt):
                            molt.remolt(crab)
                            return "Remolted Molt.", 200
                        else:
                            return abort(
                                400,
                                description="Molt has already "
                                "been remolted by user.",
                            )
                    else:
                        return abort(400, description="Cannot remolt own Molt.")
                else:
                    remolt_shell = crab.has_remolted(molt)
                    if remolt_shell:
                        remolt_shell.delete()
                        return "Remolt successfully deleted.", 200
                    else:
                        return abort(400, description="No Remolt to delete.")
            else:
                return abort(400, description="The authorized user no longer exists.")
        else:
            return abort(401, description="This endpoint requires authentication.")
    else:
        return abort(404, description="No Molt with that ID.")
Example #2
0
def get_molt(molt_ID):
    molt = api_utils.get_molt(molt_ID)
    if molt:
        if request.method == "DELETE":
            auth = require_auth(request)
            if auth:
                crab = api_utils.get_crab(auth["crab_id"])
                if crab:
                    if molt.author is crab:
                        molt.delete()
                        return "Molt successfully deleted.", 200
                    else:
                        return abort(
                            400,
                            description="The authorized user "
                            "does not own this Molt.",
                        )
                else:
                    return abort(
                        400, description="The authorized user no longer exists."
                    )
            else:
                return abort(401, description="This endpoint requires authentication.")
        else:
            return api_utils.molt_to_json(molt)
    else:
        return abort(404, description="No Molt with that ID.")
Example #3
0
def unlike_molt(molt_ID):
    molt = api_utils.get_molt(molt_ID)
    if molt:
        auth = require_auth(request)
        if auth:
            crab = api_utils.get_crab(auth["crab_id"])
            if crab:
                if crab.has_liked(molt):
                    molt.unlike(crab)
                return "Unliked Molt.", 200
            else:
                return abort(400, description="The authorized user no longer exists.")
        else:
            return abort(401, description="This endpoint requires authentication.")
    else:
        return abort(404, description="No Molt with that ID.")
Example #4
0
def edit_molt(molt_ID):
    molt = api_utils.get_molt(molt_ID)
    if molt:
        auth = require_auth(request)
        if auth:
            crab = api_utils.get_crab(auth["crab_id"])
            if crab:
                molt_content = request.form.get("content")
                molt_image_link = request.form.get("image")
                molt_image = request.files.get("image")
                image_verified = False

                if molt_image_link:
                    return abort(400, "Images must be submitted as files, not text.")
                if molt_image:
                    if molt_image.filename != "":
                        if molt_image and utils.allowed_file(molt_image.filename):
                            image_verified = True
                        else:
                            return abort(
                                400, "There was a problem with the uploaded image."
                            )
                    else:
                        return abort(400, "Image filename is blank. Aborting.")
                if molt.editable:
                    if image_verified:
                        if molt.editable:
                            molt_image = utils.upload_image(molt_image)
                            molt.edit(content=molt_content, image=molt_image)
                    elif molt_content:
                        molt.edit(content=molt_content)
                    else:
                        return abort(400, description="Missing required content.")
                    # Return edited Molt
                    return api_utils.molt_to_json(molt), 201
                else:
                    return abort(400, description="Molt is not editable.")
            else:
                return abort(400, description="The authorized user no longer exists.")
        else:
            return abort(401, description="This endpoint requires authentication.")
    else:
        return abort(404, description="No Molt with that ID.")