wuttafarm.web.views.farmos.structures

View for farmOS Structures

class wuttafarm.web.views.farmos.structures.StructureView(request, context=None)[source]

View for farmOS Structures

configure_form(form)[source]

Configure the given model form, as needed.

This is called by make_model_form() - for multiple CRUD views (create, view, edit, delete, possibly others).

The default logic here does just one thing: when “editing” (i.e. in edit() view) then all fields which are part of the model_key will be marked via set_readonly() so the user cannot change primary key values for a record.

Subclass may override as needed. The form param will already be “complete” and ready to use as-is, but this method can further modify it based on request details etc.

configure_grid(grid)[source]

Configure the grid for the index() view.

This is called by make_model_grid().

There is minimal default logic here; subclass should override as needed. The grid param will already be “complete” and ready to use as-is, but this method can further modify it based on request details etc.

get_grid_data(columns=None, session=None)[source]

Returns the grid data for the index() view.

This is called by make_model_grid(); in the resulting Grid instance, this becomes data.

Default logic will call get_query() and if successful, return the list from query.all(). Otherwise returns an empty list. Subclass should override as needed.

get_instance()[source]

This should return the appropriate model instance, based on the matchdict of model keys.

Normally this is called with no arguments, in which case the pyramid:pyramid.request.Request.matchdict is used, and will return the “current” model instance based on the request (route/params).

If a matchdict is provided then that is used instead, to obtain the model keys. In the simple/common example of a “native” model in WuttaWeb, this would look like:

keys = {'uuid': '38905440630d11ef9228743af49773a4'}
obj = self.get_instance(matchdict=keys)

Although some models may have different, possibly composite key names to use instead. The specific keys this logic is expecting are the same as returned by get_model_key().

If this method is unable to locate the instance, it should raise a 404 error, i.e. notfound().

Default implementation of this method should work okay for views which define a model_class. For other views however it will raise NotImplementedError, so subclass may need to define.

Warning

If you are defining this method for a subclass, please note this point regarding the 404 “not found” logic.

It is not enough to simply return this 404 response, you must explicitly raise the error. For instance:

def get_instance(self, **kwargs):

    # ..try to locate instance..
    obj = self.locate_instance_somehow()

    if not obj:

        # NB. THIS MAY NOT WORK AS EXPECTED
        #return self.notfound()

        # nb. should always do this in get_instance()
        raise self.notfound()

This lets calling code not have to worry about whether or not this method might return None. It can safely assume it will get back a model instance, or else a 404 will kick in and control flow goes elsewhere.

get_instance_title(structure)[source]

Return the human-friendly “title” for the instance, to be used in the page title when viewing etc.

Default logic returns the value from str(instance); subclass may override if needed.

get_xref_buttons(structure)[source]

Should return a list of “cross-reference” buttons to be shown when viewing the given object.

Default logic always returns empty list; subclass can override as needed.

If applicable, this method should do its own permission checks and only include the buttons current user should be allowed to see/use.

See also make_button() - example:

def get_xref_buttons(self, product):
    buttons = []
    if self.request.has_perm('external_products.view'):
        url = self.request.route_url('external_products.view',
                                     id=product.external_id)
        buttons.append(self.make_button("View External", url=url))
    return buttons