Project

General

Profile

Rest api ẅith python » History » Version 9

Ian Epperson, 2013-02-07 20:50
The syntax highlight only works with the first div block, so rearranging so the larger block is colored properly - changes the top billing from the PyActiveResource to the pyRedmineWS

1 4 Javier Hernandez
h1. Using the REST API with Python
2
3 6 Javier Hernandez
Here is the two well-known options for using REST API with python. 
4 4 Javier Hernandez
5 9 Ian Epperson
# "Python library":https://github.com/ianepperson/pyredminews
6
# "PyActiveResource":http://code.google.com/p/pyactiveresource/
7
 
8 6 Javier Hernandez
9 1 Javier Hernandez
10 8 Ian Epperson
11
h2. *Python library* example:
12
13
Suppose Eric fell ill and was out for several days. You need to crawl through the project called Parrot and move any due date for issues assigned to Eric out by two more weeks.
14 1 Javier Hernandez
15
The dateutil library contains a handy method called reativedelta for calculating relative dates.
16
17
<pre>
18
<code class="python">
19 9 Ian Epperson
# Import the Redmine class
20 1 Javier Hernandez
from redmine import Redmine
21
from dateutil.relativedelta import relativedelta
22
23
server = Redmine('http://my-server.com', username='Me', password='seakrit')
24
project = server.projects['parrot']
25
26
# Find Eric in the user data
27
for u in server.users:
28
    if u.firstname == 'Eric' and u.lastname == 'Idle':
29
       user = u
30
       break
31
else:
32
    raise Exception("Didn't find Eric Idle in the user dateabase")
33
34
# Extend issues in project assigned to user by two weeks
35
for issue in project.issues(assigned_to_id=user.id):
36
    if issue.due_date is not None:
37
       issue.due_date += relativedelta(weeks=+2)
38 8 Ian Epperson
       issue.save('Giving Eric more time to complete - he was out ill')
39 9 Ian Epperson
40
</pre>
41
42
h2. *PyActiveResource* example:
43
44
<pre>
45
# Importing pyactiveresource
46
from pyactiveresource.activeresource import ActiveResource
47
48
class Issue(ActiveResource):
49
    _site = 'http://redmine.foo.org'
50
    _user = 'username'
51
    _password = 'password'
52
53
# Get issues
54
issues = Issue.find()
55
56
# Get a specific issue, from its id
57
issue = Issue.find(1345)
58
59
# Issue attributes
60
61
# Updating an attribute
62
63 6 Javier Hernandez
</pre>