doc/book/en/development/devcore/appobject.rst
branchtls-sprint
changeset 1714 a721966779be
child 2172 cf8f9180e63e
equal deleted inserted replaced
1499:fd8751c3f3ee 1714:a721966779be
       
     1 
       
     2   
       
     3 The `AppObject` class
       
     4 ~~~~~~~~~~~~~~~~~~~~~
       
     5 
       
     6 In general:
       
     7 
       
     8 * we do not inherit directly from this class but from a more specific
       
     9   class such as `AnyEntity`, `EntityView`, `AnyRsetView`,
       
    10   `Action`...
       
    11 
       
    12 * to be recordable, a subclass has to define its own register (attribute
       
    13   `__registry__`) and its identifier (attribute `id`). Usually we do not have
       
    14   to take care of the register, only the identifier `id`.
       
    15 
       
    16 We can find a certain number of attributes and methods defined in this class
       
    17 and common to all the application objects.
       
    18 
       
    19 At the recording, the following attributes are dynamically added to
       
    20 the *subclasses*:
       
    21 
       
    22 * `vreg`, the `vregistry` of the application
       
    23 * `schema`, the application schema
       
    24 * `config`, the application configuration
       
    25 
       
    26 We also find on instances, the following attributes:
       
    27 
       
    28 * `req`, `Request` instance
       
    29 * `rset`, the *result set* associated to the object if necessary
       
    30 * `cursor`, rql cursor on the session
       
    31 
       
    32 
       
    33 :URL handling:
       
    34   * `build_url(method=None, **kwargs)`, returns an absolute URL based on
       
    35     the given arguments. The *controller* supposed to handle the response,
       
    36     can be specified through the special parameter `method` (the connection
       
    37     is theoretically done automatically :).
       
    38 
       
    39   * `datadir_url()`, returns the directory of the application data
       
    40     (contains static files such as images, css, js...)
       
    41 
       
    42   * `base_url()`, shortcut to `req.base_url()`
       
    43 
       
    44   * `url_quote(value)`, version *unicode safe* of the function `urllib.quote`
       
    45 
       
    46 :Data manipulation:
       
    47 
       
    48   * `etype_rset(etype, size=1)`, shortcut to `vreg.etype_rset()`
       
    49 
       
    50   * `eid_rset(eid, rql=None, descr=True)`, returns a *result set* object for
       
    51     the given eid
       
    52   * `entity(row, col=0)`, returns the entity corresponding to the data position
       
    53     in the *result set* associated to the object
       
    54 
       
    55   * `complete_entity(row, col=0, skip_bytes=True)`, is equivalent to `entity` but
       
    56     also call the method `complete()` on the entity before returning it
       
    57 
       
    58 :Data formatting:
       
    59   * `format_date(date, date_format=None, time=False)` returns a string for a
       
    60     mx date time according to application's configuration
       
    61   * `format_time(time)` returns a string for a mx date time according to
       
    62     application's configuration
       
    63 
       
    64 :And more...:
       
    65 
       
    66   * `external_resource(rid, default=_MARKER)`, access to a value defined in the
       
    67     configuration file `external_resource`
       
    68 
       
    69   * `tal_render(template, variables)`, renders a precompiled page template with
       
    70     variables in the given dictionary as context
       
    71 
       
    72 .. note::
       
    73   When we inherit from `AppObject` (even not directly), you *always* have to use
       
    74   **super()** to get the methods and attributes of the superclasses, and not
       
    75   use the class identifier.
       
    76   For example, instead of writting: ::
       
    77 
       
    78       class Truc(PrimaryView):
       
    79           def f(self, arg1):
       
    80               PrimaryView.f(self, arg1)
       
    81 
       
    82   You'd better write: ::
       
    83 
       
    84       class Truc(PrimaryView):
       
    85           def f(self, arg1):
       
    86               super(Truc, self).f(arg1)