Skip to main content

Standard helper for shopinvader routers

Project description

Odoo Community Association

Shopinvader Router Helper

Alpha License: AGPL-3 shopinvader/odoo-shopinvader

Base helper for all shopinvader routers.

Simple Usage

The helper can be used as-is for a simple inheritable router logic:

@router.get("/name-type/{name}/{type}")
async def name_type(
    name: str, type: str, env: Annotated[Environment, Depends(odoo_env)]
  ):
  helper = env["shopinvader.router.name_type.helper"].new(dict(
      name=name, type=type
  ))
  return helper.fetch_data()


class RouterHelperNameType(VirtualModel):
    _inherit = "shopinvader.router.helper"
    _name = "shopinvader.router.name_type.helper"

    name = fields.Char()
    type = fields.Selection(
        selection=[("type_1", "Type 1"), ("type_2", "Type 2")],
        default="type_1",
    )

    def fetch_data(self):
      return requests.get("https://my.api/{}/{}".format(self.name, self.type))

CRUD Usage

The helper can also be used to implement a full CRUD logic. For that, you need to define the model to use and the domain to restrict the records operations on.

@router.post("/items/")
async def create_item(
    item: ItemCreate, env: Annotated[Environment, Depends(odoo_env)]
  ):
  helper = env["shopinvader.router.item.helper"].new()
  return helper.create(item.dict())

@router.get("/items/")
async def read_items(
    env: Annotated[Environment, Depends(odoo_env)]
  ):
  helper = env["shopinvader.router.item.helper"].new()
  return helper.search()

@router.get("/items/{item_id}")
async def read_item(
    item_id: int, env: Annotated[Environment, Depends(odoo_env)]
  ):
  helper = env["shopinvader.router.item.helper"].new()
  return helper.get(item_id)

@router.put("/items/{item_id}")
async def update_item(
    item_id: int, item: ItemUpdate, env: Annotated[Environment, Depends(odoo_env)]
  ):
  helper = env["shopinvader.router.item.helper"].new()
  return helper.write(item_id, item.dict())

@router.delete("/items/{item_id}")
async def delete_item(
    item_id: int, env: Annotated[Environment, Depends(odoo_env)]
  ):
  helper = env["shopinvader.router.item.helper"].new()
  return helper.unlink(item_id)

class RouterHelperItem(VirtualModel):
    _inherit = "shopinvader.router.helper"
    _name = "shopinvader.router.item.helper"

    _model = "my.item.model"

    def _domain(self):
        domain = [('api_available', '=', True)]
        domain.append(('partner_id', '=', self.partner_id.id))
        return domain

TODO

  • Check helper usage with fastapi dependency injection system for cleaner code.

  • More utils (pdf export, file handling, …).

Table of contents

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed feedback.

Do not contact contributors directly about support or help with technical issues.

Credits

Authors

  • Akretion

Contributors

Maintainers

This module is part of the shopinvader/odoo-shopinvader project on GitHub.

You are welcome to contribute.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file odoo_addon_shopinvader_router_helper-18.0.1.0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for odoo_addon_shopinvader_router_helper-18.0.1.0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4d17b0ce81070399e3f0d155105e45a76a1a976f014426d6a228218983f7f998
MD5 0641bc64de367322ca3f71c46bec5997
BLAKE2b-256 9cc6e37194572dcbbb9cccbe471e0b05edce7abc1c3c44226923c800c35799eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page