Table Of Contents

Previous topic

Getting Started with YARHP

Next topic

Glossary

This Page

Design and Conventions

Models

  1. Always use common names.
  2. Always singular.
  3. Use composite names weak entities, i.e.: entities that cannot exist on their own (without the parents model). A foreign key that’s part of the composite primary key is a hint that we should use a composite name for the model which represents this table.

URLs

  1. Always use the collection name of the resource (if any)
  2. Avoid using verbs.
  3. Minimize the use of query string.

Important

Always use route names using helpers to generate URLs. Hard-coded URLs must not be found in the code whether relative, or absolute, in templates, models or views. To generate route path, you must use the route_path() method from the Request class. It takes a route name and resource IDs and generates the URL:

>>> route_path('user_transactions', user_id=3, id=7)
'/users/3/transactions/7'

Route names

  1. Always lower cased, underscore separated.
  2. Collection name is used for routes trying to access multiple instances of a resource, e.g: users is the route name of /users.
  3. Member name is used for routes accessing a single instance or the only instance of the resource, e.g.:
    • user is the route name of /users/2
    • system is the route name of /system
  4. parent resource member name is always a prefix for route names of a nested resource, e.g.:
    • order_detail_items is the route name for /orders/123/details/12/items/7

Views

Follows same conventions as route names, with the following changes:

  1. Resource name use CamelCase.

  2. All views are suffixed with View.

  3. The collection name of the served resource (the leaf) is always used, e.g.:

    URL

    Route name

    View

    /users/12/transactions/14

    user_transactions

    UserTransactionsView

    /users

    users

    UsersView

A view class may define the following methods:

  • index
  • show
  • create
  • update
  • delete
  • new
  • edit

See API Documentation for the relation between HTTP verbs and Python view methods.

Notes on View methods and HTTP methods.

If derived from:

  1. BaseView Using include param in resource.add() will tell yarhp which actions must be implemented. AttributeError will be raised if one of the icnluded actions are not defined in your view. Using exclude will tell yarhp that the current resource does not support excluded actions and thus 405 Method Not Allowed will be raised instead of AttributeError.

  2. SQLAView default index, show and delete methods are implemented and can be overwritten in your view. Note that the defaults work currently only for top level resources.

  3. DynamoDBView default create update index show delete methods are implemented. Works only for top level resources.

    Using include/exlude params in resource.add() will have similar to 1) behavior, that is raise AttributeError if action is in include list but not implemented (except the default methods) and raise 405 if action is in exclude list.

By default all methods listed above are included.