Skip to content

Latest commit

 

History

History
128 lines (88 loc) · 3.7 KB

uses_cases.md

File metadata and controls

128 lines (88 loc) · 3.7 KB

Uses cases

  • Create an object containing references
  • Passing reference from one model to another
  • Change the order of attributes displayed in the list
  • Rename the attributes displayed in the list
  • Change the main attribute of the class 'id' by other
  • Formatting attributes in the list

Create an object containing references

In this example shown as Province object has a reference to the object Country through the attribute Province.country

from storm.locals import *
from country import Country

class Province (object):

    __storm_table__ = "states"

    id = Int(primary = True)
	name = Unicode()
	country_id = Int()
	country = Reference(country_id, Country.id)

Passing reference from one model to another

Several times we need to interact with data from another model. Here is an example:

from book.manager import BookManager
from book.gui import BookGUI
from library.manager import LibraryManager
from library.gui import LibraryGUI

book_mgr = BookManager(store, reset)
library_mgr = LibraryManager(store, reset)

books_gui = BookGUI(manager = book_mgr)
# here is passed by reference 'books_gui' to 'library_gui'
library_gui = LibraryGUI(manager = library_mgr, managers = [books_gui])

Change the order of attributes displayed in the list

In the gui.py, only to change the order in which they are defined attributes list, changes already be reflected in the table.

class PersonsGUI( BaseGUI ):

    def __init__(self, manager, managers = []):
        BaseGUI.__init__(self, manager, managers)

        [...]

        self.addTableColumn(u'#', Person.id)
        self.addTableColumn(u'Name', Person.name)
        self.addTableColumn(u'Last name', Person.last_name)
        self.addTableColumn(u'E-mail', Person.email)

        self._start_operations()

Rename the attributes displayed in the list

Only renaming the keys in the call to the 'addTableColumn' function, changes will be reflected in the table.

...
self.addTableColumn(u'#', Person.id)
self.addTableColumn(u'ChangeMe', Person.name)
...

Change the main attribute of the class 'id' by other

Always default id attribute to identify objects in the database is used.

Here is an example of how to change the attribute id by isbn in the case of a book:

# class with 'ide' attribute
class Book (object):

	id = Int(primary = True)

# class with changed 'isbn' attribute
class Book (object):

	isbn = Unicode(primary = True)

Formatting attributes in the list

Sometimes we need to format the attributes displayed in the list.

See an example of how:

  • give the format 'dd/mm/yyyy' to date column
  • add '$' sign to 'amount' column
def fnParseDate(self, row, value):
	return value.strftime("%d/%m/%Y")

def fnParseAmount(self, row, value):
	return "$ %8.2f" % value

# using the 'fnParse' option
self.addTableColumn(u'Date', InvoiceRow.date, fnParse=self.fnParseDate) # type date
self.addTableColumn(u'Supplier', InvoiceRow.supplier) # type unicode
self.addTableColumn(u'Amount', InvoiceRow.amount, fnParse=self.fnParse) # type float

How to center a column?

  • 'C' for center
  • 'L' for left
  • 'R' for right
# using the 'alignment' option
self.addTableColumn(u'Amount', InvoiceRow.amount, alignment='C')

Go > Index | Go > Install | Go > Getting started | Go > Use cases | Go > Example Apps