Django and Twitter

3 views
Skip to first unread message

matt....@gmail.com

unread,
Sep 18, 2007, 11:12:00 AM9/18/07
to Django users
I'm trying to marry up Django and a python wrapper to the Twitter API
called Python-Twitter (http://code.google.com/p/python-twitter/) to
trigger a status change on saving an item to a database. It looks
something like this:

def save(self):
if self.makepublic:
api = twitter.Api()
api = twitter.Api(username='username',
password='password')
status = api.PostUpdate('My update message here')
else:
pass
super(Modelname, self).save()

However, when I save the record in the Django admin, I get this:

[Errno 25] Inappropriate ioctl for device

Exception Location: build/bdist.linux-i686/egg/twitter.py in
_GetUsername, line 1498

The section of code mentioned in the error from twitter.py is here:

def _GetUsername(self):
'''Attempt to find the username in a cross-platform fashion.'''
return os.getenv('USER') or \
os.getenv('LOGNAME') or \
os.getenv('USERNAME') or \
os.getlogin() or \
'nobody'

Anyone got any guesses? I'm so much of a python noob that I can't even
spell half these things. Any suggestions are most appreciated.

Matt

Matthew Waite
politifact.com | mattwaite.com

Tim Chase

unread,
Sep 18, 2007, 12:10:58 PM9/18/07
to django...@googlegroups.com
> However, when I save the record in the Django admin, I get this:
>
> [Errno 25] Inappropriate ioctl for device
>
> Exception Location: build/bdist.linux-i686/egg/twitter.py in
> _GetUsername, line 1498

A couple pieces of information would be helpful:

Which OS?

Could you provide the full traceback (redacted for
usernames/passwords if needed, but at least the full callstack)?

As which user is the python process running, and how is it
running (dev server, apache, fast-cgi)?

If you pull up a raw python prompt and execute the following two
lines, what do you get:

>>> import os
>>> os.getlogin()

Also at the raw python prompt, what happens if you do

>>> import twitter
>>> twitter._GetUsername()

That should provide enough information to at least try and
replicate the problem, and get things a step closer to finding an
answer.

-tim

matt....@gmail.com

unread,
Sep 18, 2007, 12:36:18 PM9/18/07
to Django users
Tim,

Thanks for taking a look. Still feeling my way on what I need to post
to be most helpful.

The OS is Ubuntu 7.04 (Feisty).

Here's the full traceback:

Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py"
in get_response
77. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/
decorators.py" in _checklogin
55. return view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/views/decorators/
cache.py" in _wrapped_view_func
39. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/
main.py" in change_stage
329. new_object = manipulator.save(new_data)
File "/usr/lib/python2.5/site-packages/django/db/models/
manipulators.py" in save
108. new_object.save()
File "/home/mwaite/django-projects/data/app/models.py" in save
170. api = twitter.Api()
File "build/bdist.linux-i686/egg/twitter.py" in __init__
900. self._cache = _FileCache()
File "build/bdist.linux-i686/egg/twitter.py" in __init__
1451. self._InitializeRootDirectory(root_directory)
File "build/bdist.linux-i686/egg/twitter.py" in
_InitializeRootDirectory
1508. root_directory = self._GetTmpCachePath()
File "build/bdist.linux-i686/egg/twitter.py" in _GetTmpCachePath
1502. username = self._GetUsername()
File "build/bdist.linux-i686/egg/twitter.py" in _GetUsername
1498. os.getlogin() or \

OSError at /url/of/record/in/admin/


[Errno 25] Inappropriate ioctl for device

Here's where I expose my rank noobishness: I'm not sure what user the
python process is running as. It's a local install, running on Apache/
mod_python so I'm guessing that it's running as me. Tell me how I can
figure it out and I'll tell you for sure.

As for executing on the python prompt: os.getlogin() gets my login.
The twitter._GetUsername() doesn't work however:

>>> twitter._GetUsername()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '_GetUsername'

Again, thank you for taking a look at this.

Matt Waite

Tim Chase

unread,
Sep 18, 2007, 1:01:25 PM9/18/07
to django...@googlegroups.com
> Thanks for taking a look. Still feeling my way on what I need to post
> to be most helpful.

I'd say you did correctly, describing the problem and not
flooding the list with 20 diff. config files and code...if the
list needs more info, we usually ask for it :) However, for
future reference, full tracebacks (redacted for volumnous or
confidential data) and exact error messages can be helpful.

The OS/shell info was to ensure that for some reason your
os.getlogin() call wasn't borked for some reason.

> File "/home/mwaite/django-projects/data/app/models.py" in save
> 170. api = twitter.Api()

it looks like the problem resides here. From what I see (not
knowing anything about the twitter API, this answer may register
a 10 on the bogosity meter), it looks like you're instantiating
the Api() object with no parameters, and then on your following
line, throwing away your previous instance ("api") and then
*re*-instantiating it with parameters.

api = twitter.Api()
api = twitter.Api(username='username',password='password')


status = api.PostUpdate('My update message here')

It seems as though the twitter API is kind enough to try and find
your username via shell variable and system calls if you don't
pass it in, but it chokes on something. As far as I can tell,
you can simply delete the first line.

> The twitter._GetUsername() doesn't work however:
>
>>>> >>> twitter._GetUsername()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute '_GetUsername'

It failed because, not surprisingly, the "twitter" module doesn't
have "_GetUsername". The above traceback indicates that it
should be the Api() object that likely has the _GetUsername
method. Thus, from a raw python prompt, I'd try just what you
intend to do in code:

>>> import twitter
>>> t = twitter.Api(username='username', password='password')
>>> status = t.PostUpdate('Testing from python shell')
>>> print status

to ensure it works as expected, and then, if curious, you can try

>>> print t._GetUsername()

> I'm not sure what user the python process is running as. It's
> a local install, running on Apache/ mod_python so I'm guessing
> that it's running as me. Tell me how I can figure it out and
> I'll tell you for sure.

usually in this setup, apache runs as a web-user, usually
something like "www", "www-data", "_www", or "apache". However,
for the time being, I'd say this issue is semi-moot.

-tim


Reply all
Reply to author
Forward
0 new messages