Edgewall Software

Changes between Version 3 and Version 4 of TracDev/DataModels


Ignore:
Timestamp:
Oct 28, 2005, 4:44:33 AM (19 years ago)
Author:
Matthew Good
Comment:

more examples of the model api

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/DataModels

    v3 v4  
    66
    77Data Model - [source:/trunk/trac/ticket/model.py trac.ticket.model.Ticket]
     8
     9'''Example:'''
    810{{{
    911#!python
    10 29      class Ticket(object):
    11 30     
    12 31          def __init__(self, env, tkt_id=None, db=None):
     12from trac.env import Environment
     13from trac.ticket.model import Ticket
     14
     15env = Environment('/path/to/trac/env')
     16
     17# Create a new ticket:
     18tkt = Ticket(env)
     19tkt['reporter'] = 'me'
     20tkt['summary'] = 'my new ticket'
     21tkt['description'] = 'some bogus description'
     22tkt.insert()
     23
     24# To read an existing ticket pass its id to the constructor:
     25tkt = Ticket(env, 1)
     26print tkt['priority']
     27
     28# Update another ticket:
     29tkt = Ticket(env, 2)
     30tkt['status'] = 'closed'
     31tkt['resolution'] = 'fixed'
     32tkt.save_changes(author='me', comment='progammaticly closed a ticket')
     33
     34# And finally deleting:
     35tkt = Ticket(env, 3)
     36tkt.delete()
    1337}}}
    1438
    15 The fields on a model object can be edited by treating the object as a dictionary.  If you change any of the fields you must call save_changes() to have them persisted.
    16 
    17 To edit a ticket...
    18 {{{
    19 #!python
    20   import trac.env
    21   import trac.ticket.model
    22 
    23   # Open Trac environment
    24   t = trac.env.Environment("path/to/trac/env")
    25  
    26   # Get the ticket you are after (how can we query for tickets?)
    27   myticket = trac.ticket.model.Ticket(t, tkt_id=1)
    28 
    29   # Read a ticket field
    30   print "Changing description for ticket called %s" %myticket["summary"]
    31 
    32   #Write a ticket field
    33   myticket["description"] = "My ticket now has a new description."
    34 
    35   #Save your changes
    36   myticket.save_changes("myusername","Just changed the description")
    37 }}}
    38  
    39 For usage examples, see [source:/trunk/trac/ticket/web_ui.py trac.ticket.web_ui].
     39For more examples, see [source:/trunk/trac/ticket/web_ui.py trac.ticket.web_ui].
    4040
    4141== !WikiPage ==
    4242
    4343Data Model - [source:/trunk/trac/wiki/model.py trac.wiki.model.WikiPage]
     44
     45'''Example:'''
    4446{{{
    4547#!python
    46 26      class WikiPage(object):
    47 27          """Represents a wiki page (new or existing)."""
    48 28     
    49 29          def __init__(self, env, name=None, version=None, db=None):
     48from trac.env import Environment
     49from trac.ticket.model import Ticket
     50
     51env = Environment('/path/to/trac/env')
     52
     53# Read an existing or new WikiPage:
     54page = WikiPage(env, 'MyWikiPage')
     55# Check if this is a new page:
     56print page.exists
     57# Update the content:
     58page.text = 'page content goes here'
     59page.save(author='me', comment='I can edit the Wiki!', remote_addr='127.0.0.1')
     60
     61# Read a specific page version:
     62page = WikiPage(env, 'TracFaq', 1)
     63print page.text
     64
     65# Delete a page:
     66page = WikiPage(env, 'BadWikiPage')
     67page.delete()
     68
     69# Or delete only a specific version of a page:
     70page = WikiPage(env, 'AnotherPage')
     71page.delete(version=5)
     72
    5073}}}
    5174
    52 The !WikiPage has an optional parameter for the version, and you probably won't need to bother with the last parameter which is an optional db connection.
    53 
    54 For usage examples, see [source:/trunk/trac/wiki/web_ui.py trac.wiki.web_ui].
     75For more examples, see [source:/trunk/trac/wiki/web_ui.py trac.wiki.web_ui].
    5576
    5677----