10 | | 29 class Ticket(object): |
11 | | 30 |
12 | | 31 def __init__(self, env, tkt_id=None, db=None): |
| 12 | from trac.env import Environment |
| 13 | from trac.ticket.model import Ticket |
| 14 | |
| 15 | env = Environment('/path/to/trac/env') |
| 16 | |
| 17 | # Create a new ticket: |
| 18 | tkt = Ticket(env) |
| 19 | tkt['reporter'] = 'me' |
| 20 | tkt['summary'] = 'my new ticket' |
| 21 | tkt['description'] = 'some bogus description' |
| 22 | tkt.insert() |
| 23 | |
| 24 | # To read an existing ticket pass its id to the constructor: |
| 25 | tkt = Ticket(env, 1) |
| 26 | print tkt['priority'] |
| 27 | |
| 28 | # Update another ticket: |
| 29 | tkt = Ticket(env, 2) |
| 30 | tkt['status'] = 'closed' |
| 31 | tkt['resolution'] = 'fixed' |
| 32 | tkt.save_changes(author='me', comment='progammaticly closed a ticket') |
| 33 | |
| 34 | # And finally deleting: |
| 35 | tkt = Ticket(env, 3) |
| 36 | tkt.delete() |
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]. |
| 39 | For more examples, see [source:/trunk/trac/ticket/web_ui.py trac.ticket.web_ui]. |
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): |
| 48 | from trac.env import Environment |
| 49 | from trac.ticket.model import Ticket |
| 50 | |
| 51 | env = Environment('/path/to/trac/env') |
| 52 | |
| 53 | # Read an existing or new WikiPage: |
| 54 | page = WikiPage(env, 'MyWikiPage') |
| 55 | # Check if this is a new page: |
| 56 | print page.exists |
| 57 | # Update the content: |
| 58 | page.text = 'page content goes here' |
| 59 | page.save(author='me', comment='I can edit the Wiki!', remote_addr='127.0.0.1') |
| 60 | |
| 61 | # Read a specific page version: |
| 62 | page = WikiPage(env, 'TracFaq', 1) |
| 63 | print page.text |
| 64 | |
| 65 | # Delete a page: |
| 66 | page = WikiPage(env, 'BadWikiPage') |
| 67 | page.delete() |
| 68 | |
| 69 | # Or delete only a specific version of a page: |
| 70 | page = WikiPage(env, 'AnotherPage') |
| 71 | page.delete(version=5) |
| 72 | |