Skip to content

Commit

Permalink
Merge pull request #5 from hivesolutions/dev_acaldas
Browse files Browse the repository at this point in the history
added model operations and links documentation
  • Loading branch information
tsilva committed Jan 18, 2016
2 parents f645bcf + 7814b5c commit 2fb2dae
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions readme.md
Expand Up @@ -35,3 +35,77 @@ Appier Extras is currently licensed under the [Apache License, Version 2.0](http
[![Build Status](https://travis-ci.org/hivesolutions/appier_extras.svg?branch=master)](https://travis-ci.org/hivesolutions/appier_extras)
[![Coverage Status](https://coveralls.io/repos/hivesolutions/appier_extras/badge.svg?branch=master)](https://coveralls.io/r/hivesolutions/appier_extras?branch=master)
[![PyPi Status](https://img.shields.io/pypi/v/appier_extras.svg)](https://pypi.python.org/pypi/appier_extras)

## Model Operations

To add an operation accessible from the the admin interface be executed on a model, add this to the model definition:

```python
class Cat:

@classmethod
@appier.operation(name = "Meow")
def meow(cls):
cats = cls.Cat.find()
for cat in cats: cat.meow()
```

To make the same operation be associated with a single instance, just to apply to an instance method instead:

```python
@appier.operation(name = "Meow")
def meow(self):
self.meow()
```

An operation can receive parameters that will be sent to the handler method:

```python
@appier.operation(
name = "Meow"
parameters = (
("Number of meows", "number_meows", int, 5)
)
)
def meow(self, number_meows):
for x in range(number_meows): self.meow()
```

## Model Links

To add a link from the model list page in the admin interface to anywhere else, add this to the model definition:

```python
class Cat

@classmethod
@appier.link(name = "Export Cats (CSV)")
def export_csv(cls):
return cls.get_app().url_for("cat.list_csv")
```

In the same way, if the link is just for a particular instance, just use an instance method:

```python
@appier.link(name = "Export Cat (CSV)")
def export_csv(self):
return self.get_app().url_for("cat.show_csv")
```

Links can receive parameters as well:

```python
@appier.link(
name = "Export Cats (CSV)"
parameters = (
("Start record", "start_record", int, 0),
("Number of records", "number_records", int, 10)
)
)
def export_csv(cls, start_record, number_records):
return self.get_app().url_for(
"cat.list_csv",
start_record = start_record,
number_records = number_records
)
```

0 comments on commit 2fb2dae

Please sign in to comment.