Ejemplo n.º 1
0
    async def create_view(cls, request):
        if not has_required_scope(request, cls.permission_scopes):
            raise HTTPException(403)

        if not cls.create_form:
            raise MissingFormError()

        context = cls.get_context(request)

        if request.method == "GET":
            form = cls.get_form(cls.create_form)
            context.update({"form": form})
            return config.templates.TemplateResponse(cls.create_template,
                                                     context)

        data = await request.form()
        form = cls.get_form(cls.create_form, formdata=data)

        if not form.validate():
            context.update({"form": form})
            return config.templates.TemplateResponse(cls.create_template,
                                                     context)

        await cls.do_create(form, request)

        message(request, "Created successfully", "success")

        return RedirectResponse(url=request.url_for(cls.url_names()["list"]),
                                status_code=302)
Ejemplo n.º 2
0
    async def update_view(cls, request):
        if not has_required_scope(request, cls.permission_scopes):
            raise HTTPException(403)

        if not cls.update_form:
            raise MissingFormError()

        instance = await cls.get_object(request)
        context = cls.get_context(request)
        form_kwargs = {
            "form_cls": cls.update_form,
            "data": instance,
            "obj": instance if not isinstance(instance, dict) else None,
        }

        if request.method == "GET":
            form = cls.get_form(**form_kwargs)
            context.update({"form": form, "object": instance})
            return config.templates.TemplateResponse(cls.update_template,
                                                     context)

        data = await request.form()
        form = cls.get_form(**form_kwargs, formdata=data)

        if not form.validate():
            context.update({"form": form, "object": instance})
            return config.templates.TemplateResponse(cls.update_template,
                                                     context)

        await cls.do_update(instance, form, request)

        message(request, "Updated successfully", "success")

        return RedirectResponse(url=request.url_for(cls.url_names()["list"]),
                                status_code=302)
Ejemplo n.º 3
0
    async def delete_view(cls, request):
        if not await cls.has_required_scope(request):
            raise HTTPException(403)

        if not cls.delete_form:
            raise MissingFormError()

        instance = await cls.get_object(request)
        context = await cls.get_context(request)
        form_kwargs = {
            "form_cls": cls.delete_form,
            "data": instance if isinstance(instance, dict) else None,
            "obj": instance if not isinstance(instance, dict) else None,
        }

        if request.method == "GET":
            form = cls.get_form(**form_kwargs)
            context.update({"form": form, "object": instance})
            return config.templates.TemplateResponse(cls.delete_template,
                                                     context)

        data = await request.form()
        form = cls.get_form(**form_kwargs, formdata=data)

        if not form.validate():
            context.update({"form": form, "object": instance})
            return config.templates.TemplateResponse(cls.delete_template,
                                                     context)

        try:
            await cls.do_delete(instance, form, request)
            message(request, "Deleted successfully", "success")
        except IntegrityError:
            message(
                request,
                "Could not be deleted due to being referenced by a related object",
                "error",
            )

        return RedirectResponse(url=request.url_for(cls.url_names()["list"]),
                                status_code=302)
Ejemplo n.º 4
0
def add_message(request):
    message(request, "Hello World")
    return RedirectResponse("/view", status_code=302)