Help on using pyactiveresource

333 views
Skip to first unread message

jhernandez

unread,
Feb 17, 2010, 5:04:08 AM2/17/10
to pyactiveresource
Hi, i'm using pyactiveresource for a personal project and i'm a little
confused on how to use it. Project consists in a pygtk desktop client
for a rails application.

I'm not an expert in rails but i know ActiveResource's workflow and i
don't get the same feeling using pyactiveresource.

I'm retrieving data using pyactiveresource but i don't know how to
retrieve info from an url with this patterns: 'http://
www.railsapplication.org/entries/1234.xml' or 'http://
myrailsapplication.org/entries/1234.xml'

With a class defined as:
class Entries(ActiveResource):
_site = 'http://www.railsapplication.org'

When i retrieve an entry i do in this way:
entry = Entries.find(id_ = 1234)

This search retrieves info with given id from 'http://
www.railsapplication.org/entries.xml'

I have been reading a little of module's code, but i don't get still
make the patterns commented above.

Another due is when i want to update entry i get a 'True' but changes
doesn't occur in database.
I'm doing it like this:
entry.comment = 'New comment'
entry.update()

Thanks in advance!

Mark Roach

unread,
Feb 18, 2010, 10:51:45 PM2/18/10
to pyactive...@googlegroups.com
On Wed, Feb 17, 2010 at 2:04 AM, jhernandez <jav...@gmail.com> wrote:
Hi, i'm using pyactiveresource for a personal project and i'm a little
confused on how to use it. Project consists in a pygtk desktop client
for a rails application.

I'm not an expert in rails but i know ActiveResource's workflow and i
don't get the same feeling using pyactiveresource.

I'm retrieving data using pyactiveresource but i don't know how to
retrieve info from an url with this patterns: 'http://
www.railsapplication.org/entries/1234.xml' or 'http://
myrailsapplication.org/entries/1234.xml'

With a class defined as:
 class Entries(ActiveResource):
     _site = 'http://www.railsapplication.org'

When i retrieve an entry i do in this way:
 entry = Entries.find(id_ = 1234)

This search retrieves info with given id from 'http://
www.railsapplication.org/entries.xml'

That doesn't sound right. Try turning on some logging so you can see exactly what it's doing:

>>> import logging; logging.basicConfig(level=logging.DEBUG)

>>> class Entry(activeresource.ActiveResource):
... 
>>> Entry.find(1234)
INFO:pyactiveresource.connection:GET http://www.railsapplication.org/entries/1234.xml

(Note the url above)

 
I have been reading a little of module's code, but i don't get still
make the patterns commented above.

Another due is when i want to update entry i get a 'True' but changes
doesn't occur in database.
I'm doing it like this:
 entry.comment = 'New comment'
 entry.update()

I am guessing you mean save() here. I would suggest trying this with logging on as above to see what is really going on. The only way for save to be returning True is if the server is responding with a success message. If you send the logging output, that will help debug.
 
Thanks in advance!

--
You received this message because you are subscribed to the Google Groups "pyactiveresource" group.
To post to this group, send email to pyactive...@googlegroups.com.
To unsubscribe from this group, send email to pyactiveresour...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyactiveresource?hl=en.


javi hernandez

unread,
Feb 23, 2010, 5:12:13 AM2/23/10
to pyactive...@googlegroups.com
Mark Roach escribi�:

> On Wed, Feb 17, 2010 at 2:04 AM, jhernandez <jav...@gmail.com
> <mailto:jav...@gmail.com>> wrote:
>
> Hi, i'm using pyactiveresource for a personal project and i'm a little
> confused on how to use it. Project consists in a pygtk desktop client
> for a rails application.
>
> I'm not an expert in rails but i know ActiveResource's workflow and i
> don't get the same feeling using pyactiveresource.
>
> I'm retrieving data using pyactiveresource but i don't know how to
> retrieve info from an url with this patterns: 'http://
> www.railsapplication.org/entries/1234.xml
> <http://www.railsapplication.org/entries/1234.xml>' or 'http://
> myrailsapplication.org/entries/1234.xml
> <http://myrailsapplication.org/entries/1234.xml>'

>
> With a class defined as:
> class Entries(ActiveResource):
> _site = 'http://www.railsapplication.org'
>
> When i retrieve an entry i do in this way:
> entry = Entries.find(id_ = 1234)
>
> This search retrieves info with given id from 'http://
> www.railsapplication.org/entries.xml
> <http://www.railsapplication.org/entries.xml>'

>
>
> That doesn't sound right. Try turning on some logging so you can see
> exactly what it's doing:
>
> >>> import logging; logging.basicConfig(level=logging.DEBUG)
>
> >>> class Entry(activeresource.ActiveResource):
> ... _site = 'http://www.railsapplication.org'
> ...
> >>> Entry.find(1234)
> INFO:pyactiveresource.connection:GET
> http://www.railsapplication.org/entries/1234.xml
>
> (Note the url above)
lot of thanks, logging is very usefull for me.

>
>
>
> I have been reading a little of module's code, but i don't get still
> make the patterns commented above.
>
> Another due is when i want to update entry i get a 'True' but changes
> doesn't occur in database.
> I'm doing it like this:
> entry.comment = 'New comment'
> entry.update()
>
>
> I am guessing you mean save() here. I would suggest trying this with
> logging on as above to see what is really going on. The only way for
> save to be returning True is if the server is responding with a
> success message. If you send the logging output, that will help debug.
Yes, i use save() for save element, but it still problems updating, i
get 'True' as response but changes does not occurs.

I think it could be that i'm not getting all data correctly because
there are so many xml fields like this:

<author id='12' name='jimmy neutron' />

When i get an entry, entry.author = None

If i make a 'PUT/POST' method, entry.author = None, probabaly, the error
is here.

In ruby, i can get author's id like this:
'entry.author.name'

So, how can i easy access/modify this attribute entry?

Thanks again!


>
>
> Thanks in advance!
>
> --
> You received this message because you are subscribed to the Google
> Groups "pyactiveresource" group.
> To post to this group, send email to
> pyactive...@googlegroups.com

> <mailto:pyactive...@googlegroups.com>.


> To unsubscribe from this group, send email to
> pyactiveresour...@googlegroups.com

> <mailto:pyactiveresource%2Bunsu...@googlegroups.com>.

Mark Roach

unread,
Feb 23, 2010, 1:53:12 PM2/23/10
to pyactive...@googlegroups.com
On Tue, Feb 23, 2010 at 2:12 AM, javi hernandez <jav...@gmail.com> wrote:
> Mark Roach escribió:

> Yes, i use save() for save element, but it still problems updating, i get
> 'True' as response but changes does not occurs.
>
> I think it could be that i'm not getting all data correctly because there
> are so many xml fields like this:
>
> <author id='12' name='jimmy neutron' />

I'm not sure how you're ending up with xml like that. Is that xml
generated by the server or by pyactiveresource?

> When i get an entry, entry.author = None
>
> If i make a 'PUT/POST' method, entry.author = None, probabaly, the error is
> here.
>
> In ruby, i can get author's id like this:
> 'entry.author.name'

On the server, or via ActiveResource? Seeing a comparison of what
ActiveResource is doing vs. what pyActiveResource is doing would be
helpful. A snippet of your client side code and the related xml is
probably what is needed.

jhernandez

unread,
Feb 23, 2010, 3:28:05 PM2/23/10
to pyactiveresource
On Feb 23, 7:53 pm, Mark Roach <mrro...@google.com> wrote:

> On Tue, Feb 23, 2010 at 2:12 AM, javi hernandez <javi...@gmail.com> wrote:
> > Mark Roach escribió:
> > Yes, i use save() for save element, but it still problems updating, i get
> > 'True' as response but changes does not occurs.
>
> > I think it could be that i'm not getting all data correctly because there
> > are so many xml fields like this:
>
> > <author id='12' name='jimmy neutron' />
>
> I'm not sure how you're ending up with xml like that. Is that xml
> generated by the server or by pyactiveresource?

First, sorry for give you only examples, but i think that was enough.

That xml was generated by the server, a real 'entry', seen with a web
browser, is like that

<issue>
<id>12571</id>
<project name="REST API Tests" id="9883"/>
<tracker name="Bug" id="1"/>
<status name="In Progress" id="2"/>
<priority name="Normal" id="4"/>
<author name="javier hernandez" id="20366"/>
<assigned_to name="javier hernandez" id="20366"/>
<subject>primera peticion</subject>
<description>descripcion \n</description>
<start_date>2010-02-20</start_date>
<due_date></due_date>
<done_ratio>40</done_ratio>
<estimated_hours>5.0</estimated_hours>
<spent_hours>0.0</spent_hours>
<custom_fields>
<custom_field name="Has been tested" id="1">0</custom_field>
</custom_fields>
<created_on>Sat Feb 20 18:28:16 -0800 2010</created_on>
<updated_on>Mon Feb 22 15:06:48 -0800 2010</updated_on>
</issue>

>
> > When i get an entry, entry.author = None

In pyactiveresource's client code


>
> > If i make a 'PUT/POST' method, entry.author = None, probabaly, the error is
> > here.
>
> > In ruby, i can get author's id like this:
> > 'entry.author.name'

In ActiveResouce's client code


>
> On the server, or via ActiveResource? Seeing a comparison of what
> ActiveResource is doing vs. what pyActiveResource is doing would be
> helpful. A snippet of your client side code and the related xml is
> probably what is needed.

In pyactiveresource, my code is something like this:

from pyactiveresource.activeresource import ActiveResource

class Issues(ActiveResource):
"""Represents Issues object"""

_site = HOST
_user = USER
_password = PASSWORD

I create the class, and using ipython, i do:

issue = Issues.find(12571)

I get the issue successfully as you can see in debug lines

INFO:pyactiveresource.connection:GET http://demo.redmine.org/issues/12571.xml
DEBUG:pyactiveresource.connection:request-headers:Authorization:Basic
amF2aXViZTpyZWRtaW5ldGVzdHM=
INFO:pyactiveresource.connection:--> 200 OK 960b

now i have issue's attributes as a dict object:

issue.attributes

and i get:

{'assigned_to': None,
'author': None,
'created_on': 'Sat Feb 20 18:28:16 -0800 2010',
'custom_fields': custom_fields(None),
'description': 'descripcion \n',
'done_ratio': '40',
'due_date': None,
'estimated_hours': '5.0',
'id': '12571',
'priority': None,
'project': None,
'spent_hours': '0.0',
'start_date': '2010-02-20',
'status': None,
'subject': 'primera peticion',
'tracker': None,
'updated_on': 'Mon Feb 22 17:08:29 -0800 2010'}


This issue is what pyactiveresource gets from the server, corresponds
with xml posted before.
Note that there are attributes with 'None' as value like 'project' or
'author'.
You can check in xml that 'project' field has two properties, both
with its values, 'name' and 'id'.
I tried to get this values using pyactiveresource's in many ways, but
with no successfull.

Maybe i'm doing something wrong?

Using ruby, i get this values without problems, doing it like that:

require 'rubygems'
require 'active_resource'

# Issue model on the client side
class Issue < ActiveResource::Base
self.site = 'http://redmine.server/'
self.user = 'foo'
self.password = 'bar'
end


# Retrieving an issue
issue = Issue.find(1)

# Print author's name
puts issue.author.name


Hope this time you've got detailed info!

Lot of thanks again!

Mark Roach

unread,
Feb 24, 2010, 4:24:55 PM2/24/10
to pyactive...@googlegroups.com
Hi again. That helps very much. You have some interesting xml going on there.

It looks as though the xml -> dictionary converter is not handling
that method of attribute specification (it generally assumes that it's
getting the output from ActiveRecord::Base#to_xml)

I have submitted a fix for this that seems to do the trick, can you
please give it a try and let me know if it fixes your issue?
http://code.google.com/p/pyactiveresource/source/detail?r=65

Thanks,

Mark

jhernandez

unread,
Feb 25, 2010, 9:43:09 AM2/25/10
to pyactiveresource
It worked for me, lot of thanks!

On Feb 24, 10:24 pm, Mark Roach <mrro...@google.com> wrote:
> Hi again. That helps very much. You have some interesting xml going on there.
>
> It looks as though the xml -> dictionary converter is not handling
> that method of attribute specification (it generally assumes that it's
> getting the output from ActiveRecord::Base#to_xml)
>
> I have submitted a fix for this that seems to do the trick, can you

> please give it a try and let me know if it fixes your issue?http://code.google.com/p/pyactiveresource/source/detail?r=65

> > INFO:pyactiveresource.connection:GEThttp://demo.redmine.org/issues/12571.xml

jhernandez

unread,
Feb 25, 2010, 8:48:56 PM2/25/10
to pyactiveresource
One more thing.

Why you replace all '_' to '-' when you convert a dictionary to an
xml?
In my case i don't need that at all. I have removed all replaces and
now it fits my needs.

FYI

Mark Roach

unread,
Feb 25, 2010, 10:18:38 PM2/25/10
to pyactive...@googlegroups.com
Just because that's what activeresource does and the goal was to be as
compatible as possible. It's not particularly efficient though is it?
ActiveRecord's to_xml is not very efficient either though, so *shrug*

jhernandez

unread,
Feb 26, 2010, 4:48:29 AM2/26/10
to pyactiveresource
Ok, i've been reading ActiveResource's documentation, and, 'to_xml'
method do that replaces, but you can disable that feature by setting
'dasherize' to 'false'.

I read this in http://api.rubyonrails.org/classes/ActiveResource/Base.html#M000914

It is implemented in pyActiveResource? If not, can you implement this?
I pretend to distribute my code and i want my application works using
'official' pyactiveresource's code.

Mark Roach

unread,
Mar 3, 2010, 11:57:39 PM3/3/10
to pyactive...@googlegroups.com
It wasn't implemented, but I went ahead and added it so
resource.to_xml(dasherize=False) should do the trick now.
Reply all
Reply to author
Forward
0 new messages