Custom Resources

Sometimes there is a need to redefine a resource behaviour to achieve the needed goal. Python-Redmine provides a feature for such a case called custom resources. Basically this is just a normal class inheritance made specifically for Python-Redmine.

Existing Resources

The list of existing resource class names that can be inherited from is available here.

Creation

To create a custom resource choose which resource behavior you want to change, e.g. WikiPage:

from redminelib.resources import WikiPage

class CustomWikiPage(WikiPage):
    pass

Name

Python-Redmine converts underscore to camelcase when it tries to import the resource, which means that it is important to follow this convention to make everything work properly, e.g when you do:

custom_wiki_page = redmine.custom_wiki_page.get('Foo')

Python-Redmine is searching for a resource class named CustomWikiPage. The location of the class doesn’t matter since all classes that inherit from any Python-Redmine resource class are automatically added to the special resource registry.

Methods and Attributes

All existing resources are derived from a BaseResource class which you usually won’t inherit from directly unless you want to add support for a new resource which Python-Redmine doesn’t support. Below you will find methods and attributes which can be redefined in your custom resource:

class redminelib.resources.BaseResource(manager, attributes)

Implementation of Redmine resource.

__getattr__(attr)

Returns the requested attribute and makes a conversion if needed.

__setattr__(attr, value)

Sets the requested attribute.

classmethod decode(attr, value, manager)

Decodes a single attr, value pair from Python representation to the needed Redmine representation.

Parameters:
  • attr (string) – (required). Attribute name.

  • value (any) – (required). Attribute value.

  • manager (managers.ResourceManager) – (required). Manager object.

classmethod encode(attr, value, manager)

Encodes a single attr, value pair retrieved from Redmine to the needed Python representation.

Parameters:
  • attr (string) – (required). Attribute name.

  • value (any) – (required). Attribute value.

  • manager (managers.ResourceManager) – (required). Manager object.

classmethod bulk_decode(attrs, manager)

Decodes resource data from Python representation to the needed Redmine representation.

Parameters:
  • attrs (dict) – (required). Attributes in the form of key, value pairs.

  • manager (managers.ResourceManager) – (required). Manager object.

classmethod bulk_encode(attrs, manager)

Encodes resource data retrieved from Redmine to the needed Python representation.

Parameters:
  • attrs (dict) – (required). Attributes in the form of key, value pairs.

  • manager (managers.ResourceManager) – (required). Manager object.

raw()

Returns resource data as it was received from Redmine.

refresh(itself=True, **params)

Reloads resource data from Redmine.

Parameters:
  • itself (bool) – (optional). Whether to refresh itself or return a new resource.

  • params (dict) – (optional). Parameters used for resource retrieval.

pre_create()

Tasks that should be done before creating the Resource.

post_create()

Tasks that should be done after creating the Resource.

pre_update()

Tasks that should be done before updating the Resource.

post_update()

Tasks that should be done after updating the Resource.

pre_delete()

Tasks that should be done before deleting the Resource.

post_delete()

Tasks that should be done after deleting the Resource.

save(**attrs)

Creates or updates a Resource.

Parameters:

attrs (dict) – (optional). Attrs to be set for a resource before create/update operation.

delete(**params)

Deletes Resource from Redmine.

Parameters:

params (dict) – (optional). Parameters used for resource deletion.

export(fmt, savepath=None, filename=None)

Exports Resource to requested format if Resource supports that.

Parameters:
  • fmt (string) – (required). Format to use for export, e.g. atom, csv, txt, pdf, html etc.

  • savepath (string) – (optional). Path where to save the file.

  • filename (string) – (optional). Name that will be used for the file.

export_url(fmt)

Returns export URL for the Resource according to format.

Parameters:

fmt (string) – (required). Export format, e.g. atom, csv, txt, pdf, html etc.

is_new()

Checks if Resource was just created and not yet saved to Redmine, or it is an existing Resource.