code repetition in views

2010-04-10 Thread ydjango
I find all my view method have identical code in start and in end:

anyway to avoid repetition...?

Example:

def typical_view_method(request):

   Check if user is authenticated.
   Get user and group
   Get some session variables

   try:
 Method specific logic
   except Exception, e:
 view_logger.error('error in typical_view_method:%s', e)

response = HttpResponse(json_data, mimetype = 'application/json')
response.__setitem__('Cache-Control', 'no-store,no-cache')
response.__setitem__('Pragma', 'no-cache')
response.__setitem__('Expires', '-1')
return response

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



Possible reasons why a session variable would be cleared out between POST requests?

2010-04-10 Thread Daniel
Hi there,

I have a request.session['querySoFar'] variable that I'm appending a Q
object to with each POST in order to build up a final query to run.

However, this session var is getting cleared out sometimes.  I have no
idea why this is happening.  I do not believe that I am resetting like
so:

 request.session['querySoFar'] = []

anywhere between POST requests.  When I append a Q object that
evaluates to no matches, this problem occurs.  When I append a Q
object that does result in matches, request.session['querySoFar'] does
indeed not get cleared out between POST requests.

Are there any general guesses?

Thank you

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



Re: ValidationError: ManagementForm data is missing or has been tampered with

2010-04-10 Thread Gramware
Try this for a solution 
http://stackoverflow.com/questions/2536285/django-formset-management-form-validation-error

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



LogEntry change messages do not do i18n properly

2010-04-10 Thread stereoit
Hi,

today I've noticed that my LogEntry change messages output the name of
the field and do not respect when it has verbose_name setup (which is
translated).

I've had a look at admin.options.construct_change_message(), the line
responsible is:

change_message.append(_('Changed %s.') %
get_text_list(form.changed_data, _('and')))

and form.changed_data contains the field names indeed. I've tried to
play with forms API, but the have 'label' for what models call
'verbose_name'.

Is this a feature of django or is there an easy way to use the
i18nlized names of attributes in admin log messages?

Thanks,

Robert

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



Re: Dynamic forms through a class factory; 'string index out of range'

2010-04-10 Thread Laereom
Scratch that.  I ended up doing it the other way.  I went back and
reviewed and it seemed less ugly now than it did at midnight last
night.  Funny, that.  Thanks for the advice.

On Apr 10, 10:45 am, Laereom  wrote:
> Honestly, I really don't like that method, mostly due to ugliness.
> I'm looking for a solution specifically to instantiating a form
> created via a class factory, unless that is some sort of Herculean
> task.
>
> On Apr 10, 10:13 am, Daniel Roseman  wrote:
>
>
>
> > On Apr 10, 4:40 pm, Laereom  wrote:
>
> > > As the title suggests, I'm creating a dynamic form via a class
> > > factory.
>
> > > Here is what I'm doing inside the class factory itself (as far as
> > > actually making the class goes):
> > >                 class _QuestionAnswerForm(forms.Form):
> > >                         response =
> > > forms.IntegerField(widget=RadioSelect(choices=choice_list))
> > >                 return _QuestionAnswerForm
>
> > > Here is what I'm doing outside the class factory:
>
> > >         _QuestionAnswerForm =
> > > question_manager.generate_question_form(1,question)
> > >         question_answer_form = _QuestionAnswerForm()
>
> > > I'm a bit ambiguous on the details of how class factories / etc work,
> > > so I may be doing that bit wrong.
>
> > > Either way, when I try to render this as either just a string or a
> > > form, I get the following error:t
>
> > > Caught an exception while rendering: string index out of range
>
> > > Peeking at the local variables, question_answer_form appears to be:
> > > 
>
> > > Note that it is an object rather than an instance.  I suspect that's
> > > my problem, due to some quirk of dynamic classes, but I have no idea
> > > how to fix it.
>
> > > Any advice?
>
> > Is the only reason you're doing a classfactory in order to set the
> > choices dynamically?  If so, a much easier way is to use a normal
> > class but override the __init__ method and set the value of
> > self['fieldname'].choices there.
>
> > --
> > DR.

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



Re: Dynamic forms through a class factory; 'string index out of range'

2010-04-10 Thread Laereom
Honestly, I really don't like that method, mostly due to ugliness.
I'm looking for a solution specifically to instantiating a form
created via a class factory, unless that is some sort of Herculean
task.

On Apr 10, 10:13 am, Daniel Roseman  wrote:
> On Apr 10, 4:40 pm, Laereom  wrote:
>
>
>
>
>
> > As the title suggests, I'm creating a dynamic form via a class
> > factory.
>
> > Here is what I'm doing inside the class factory itself (as far as
> > actually making the class goes):
> >                 class _QuestionAnswerForm(forms.Form):
> >                         response =
> > forms.IntegerField(widget=RadioSelect(choices=choice_list))
> >                 return _QuestionAnswerForm
>
> > Here is what I'm doing outside the class factory:
>
> >         _QuestionAnswerForm =
> > question_manager.generate_question_form(1,question)
> >         question_answer_form = _QuestionAnswerForm()
>
> > I'm a bit ambiguous on the details of how class factories / etc work,
> > so I may be doing that bit wrong.
>
> > Either way, when I try to render this as either just a string or a
> > form, I get the following error:t
>
> > Caught an exception while rendering: string index out of range
>
> > Peeking at the local variables, question_answer_form appears to be:
> > 
>
> > Note that it is an object rather than an instance.  I suspect that's
> > my problem, due to some quirk of dynamic classes, but I have no idea
> > how to fix it.
>
> > Any advice?
>
> Is the only reason you're doing a classfactory in order to set the
> choices dynamically?  If so, a much easier way is to use a normal
> class but override the __init__ method and set the value of
> self['fieldname'].choices there.
>
> --
> DR.

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



Re: Baffled by self.client.login(...) not working in unit tests

2010-04-10 Thread adambossy
I forgot to mention: I'm using Django 1.0 on Ubuntu.


On Apr 9, 10:41 pm, adambossy  wrote:
> I have created users for my unit tests in two ways:
>
> 1) Create a fixture for "auth.user" that looks roughly like this:
>
>     {
>         "pk": 1,
>         "model": "auth.user",
>         "fields": {
>             "username": "homer",
>             "is_active": 1,
>             "password":
> "sha1$72cd3$4935449e2cd7efb8b3723fb9958fe3bb100a30f2",
>             ...
>         }
>     }
>
> I've left out the seemingly unimportant parts.
>
> 2) Use 'create_user' in the setUp function (although I'd rather keep
> everything in my fixtures class):
>
>       def
> setUp(self):
>               User.objects.create_user('homer', 'ho...@simpson.net',
> 'simpson')
>
> Note that the password is simpson in both cases.
>
> I've verified that this info is correctly being loaded into the test
> database time and time again. I can grab the User object using
> User.objects.get. I can verify the password is correct using
> 'check_password.' The user is active.
>
> Yet, invariably, self.client.login(username='homer',
> password='simpson') FAILS. I'm baffled as to why. I think I've read
> every single Internet discussion pertaining to this. Can anybody help?
> My login code looks like this:
>
>                 login = self.client.login(username='homer',
> password='simpson')
>                 self.assertTrue(login)
>
> Thanks,
>
> Adam

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



Re: Dynamic forms through a class factory; 'string index out of range'

2010-04-10 Thread Daniel Roseman


On Apr 10, 4:40 pm, Laereom  wrote:
> As the title suggests, I'm creating a dynamic form via a class
> factory.
>
> Here is what I'm doing inside the class factory itself (as far as
> actually making the class goes):
>                 class _QuestionAnswerForm(forms.Form):
>                         response =
> forms.IntegerField(widget=RadioSelect(choices=choice_list))
>                 return _QuestionAnswerForm
>
> Here is what I'm doing outside the class factory:
>
>         _QuestionAnswerForm =
> question_manager.generate_question_form(1,question)
>         question_answer_form = _QuestionAnswerForm()
>
> I'm a bit ambiguous on the details of how class factories / etc work,
> so I may be doing that bit wrong.
>
> Either way, when I try to render this as either just a string or a
> form, I get the following error:t
>
> Caught an exception while rendering: string index out of range
>
> Peeking at the local variables, question_answer_form appears to be:
> 
>
> Note that it is an object rather than an instance.  I suspect that's
> my problem, due to some quirk of dynamic classes, but I have no idea
> how to fix it.
>
> Any advice?

Is the only reason you're doing a classfactory in order to set the
choices dynamically?  If so, a much easier way is to use a normal
class but override the __init__ method and set the value of
self['fieldname'].choices there.

--
DR.

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



Re: Giving up PHP. ...Can't decide between Django & Rails

2010-04-10 Thread Torsten Bronger
Hallöchen!

Wiiboy writes:

> [...]
>
> 2. Regarding documentation, the docs at
> http://docs.djangoproject.com are, in my opinion, simply amazing.
> By far the best documentation for a free project that I've seen
> (but then again, I haven't seen much).  [...]

They are great.  Really great.  And I've seen many open-source
projects.

> [...]
>
> 4. I find the error handling in Django (basically just Python
> error handling) works quite well.  I sometimes get rather
> ambiguous errors, but their solutions become easier to find as you
> encounter them.  [...]

Well, you have to learn to read the tracebacks, that's for sure.
But this can't be made easier I think.

Sometimes, an exception is raised but the error was completely
elsewhere.  This can be rather tricky.  I can't give an example but
I think it bit me quite often when a module import had failed.

Moreover, manage.py often suppresses the traceback and prints its
own error message.  This can make debugging harder.

But these are fringe cases.

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
   Jabber ID: torsten.bron...@jabber.rwth-aachen.de
  or http://bronger-jmp.appspot.com

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



Dynamic forms through a class factory; 'string index out of range'

2010-04-10 Thread Laereom
As the title suggests, I'm creating a dynamic form via a class
factory.

Here is what I'm doing inside the class factory itself (as far as
actually making the class goes):
class _QuestionAnswerForm(forms.Form):
response =
forms.IntegerField(widget=RadioSelect(choices=choice_list))
return _QuestionAnswerForm

Here is what I'm doing outside the class factory:

_QuestionAnswerForm =
question_manager.generate_question_form(1,question)
question_answer_form = _QuestionAnswerForm()


I'm a bit ambiguous on the details of how class factories / etc work,
so I may be doing that bit wrong.

Either way, when I try to render this as either just a string or a
form, I get the following error:

Caught an exception while rendering: string index out of range

Peeking at the local variables, question_answer_form appears to be:


Note that it is an object rather than an instance.  I suspect that's
my problem, due to some quirk of dynamic classes, but I have no idea
how to fix it.

Any advice?

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



Re: Giving up PHP. ...Can't decide between Django & Rails

2010-04-10 Thread Wiiboy
I'm a bit of a noob as well, and know nothing about Rails, but I'd
like to comment on a few issues you mentioned.

1.  Non-existent template variables are replaced with the value of the
TEMPLATE_STRING_IF_INVALID setting.  Set that to something besides its
default (an empty string) to make non-existent variables easier to
find.

2. Regarding documentation, the docs at http://docs.djangoproject.com
are, in my opinion, simply amazing.  By far the best documentation for
a free project that I've seen (but then again, I haven't seen much).
And that's just a matter of opinion I guess =)

3.I have yet to encounter any problem with the dev server, although I
remember issues like that from App Engine.  Irritating as hell.

4. I find the error handling in Django (basically just Python error
handling) works quite well.  I sometimes get rather ambiguous errors,
but their solutions become easier to find as you encounter them.  One
error I always hated with PHP was when you forgot to put a semicolon
on the end of a line.  PHP said the first thing on the _next_ line was
unexpected.  It seems ambiguous at first, but after you see it a few
times, you know exactly what it means.  Same with Python/Django.

That's all I have to say.  Hope I helped. =)

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



Re: Is the DoesNotExist exception the only way to check if an object exists?

2010-04-10 Thread greatlemer
This can work when you know you want to use the object once it's been
retrieved.  If all you want to do is check for existence then it's
better to do the check with a:

if Class.objects.filter(pk=pk).count() == 0:
# Do object doesn't exist stuff
else:
# Do object does exist stuff

--
G

On Apr 9, 11:44 pm, johan sommerfeld 
wrote:
> If you don't want to use exception (which I do when doing something
> similar). You can use filter and then check the length of the array
> returned.
>
> def my_view( request , pk ):
>     obj = Class.objects.filter( pk = pk)
>     if len(obj) != 1:
>         return bad_key_view
>     obj = obj[0]
>     # Do something with obj and return a suitable response.
>
> /J
>
>
>
> On Fri, Apr 9, 2010 at 11:54 PM, Joakim Hove  wrote:
> > Hello,
>
> > I have something I would presume was a very common pattern. I have a
> > view which gets a primary-key (from the user) as second argument:
>
> > def my_view( request , pk ):
> >     obj = Class.objects.get( pk = pk)
> >     # Do something with obj and return a suitable response.
>
> > Now, of course I would like to check whether the object identified by
> > 'pk' is in the database, and return a suitable error message if that
> > fails; I was halfway expecting to find a "has_key() / exists() / ..."
> > method, but it seems the only way to handle this gracefully is by
> > catching the DoesNotExist exception?
>
> > I have never really got very friendly with exceptions, I tend to
> > consider them as something exceptional which "should not" happen,
> > whereas the fact that the database does not contain a particular key
> > is in my opinion something quite ordinary and not by any means
> > "exceptional".
>
> > Or maybe I am misunderstanding roally here?
>
> > Joakim
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users+unsubscr...@googlegroups.com > groups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.

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



[django] dumping data in utf-8

2010-04-10 Thread Andreo
I dump datga:
./manage.py dumpdata --format=yaml appname > initial_data.yaml

The result is:
-   fields: {description: "\u0423\u043A\u0440\u0430\u0438\u0301\u043D
\u0430 (\u0443\
\u043A\u0440. \u0423\u043A\u0440\u0430\u0457\u043D\u0430
[ukr\u0251\u02C8\

How to configure django to save data in utf-8?

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



[django] automatically loading initial data fixtures for third party apps

2010-04-10 Thread Andreo
Hi all,

I want to provide automatically loaded initial data for the third
party app - 'django-tagging'. There is a special place for internal
apps:  /fixtures/initial_data.yaml, but not for a third
party.

I want data to bi initialized on ./manage.py syncdb

There are many similar questions:
http://groups.google.com/group/django-users/browse_thread/thread/16a6bda3f0491f80/f5edd5c0bb0992d7?lnk=gst=initial+data#f5edd5c0bb0992d7

ps
As a solution i see to keep initial data in the folder: fixture/
/initial_data.yaml, the same way as templates are overloaded.

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



the most efficient way to remove item from a table with self referencing foreign key

2010-04-10 Thread mouadino
Hello everybody

I have a model with self reference foreign key :

class Categories(Model.model):

category = Model.CharField(...)
parent_category = Model.ForeignKey(self, ...)
url = Model.CharField()

an i want to remove items from this table given the url of a category
this is a peace of the table :

IDcategory   id_parent_category url
1  blabla   NULLNULL   #this
is a root category
2  blala1 http://www.blabla

know for removing the category of the url http://www.blabla i do like
this

category = Categories.objects.get(url = "http://www.blabla;)
category.delete()

but i want to remove also the parent category so i used the the
cascade deleting by default and i write in the model a function
delete_all() :

def delete_all():
 category = Categories.objects.get(url = "")
 while category.parent_category :
   category = category.parent_category

 category.delete()

and i called like this :

category = Categories.objects.get(url = "http://www.blabla;)
category.delete_all()

the problem now it's that i have lot of categories in the table and
with this way it's take lot of time to remove them.

someone have a better idea to remove them , and sorry for this long
post

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



filter records of one table depending on other table

2010-04-10 Thread Jitesh
Hi all,
I am having two tables say TableOne and TableTwo and below are some of
the fields of these tables
TableOne:
companyName

Tabletwo:
companyid (i am saving key().id() of TableOne in it)


now i have to filter TableOne contents which are not in TableTwo.
i.e. i want list of companies which does not have its corresponding
companyid in TableTwo
I am using django with app-engine

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



Re: Is the DoesNotExist exception the only way to check if an object exists?

2010-04-10 Thread Daniel Roseman
On Apr 9, 10:54 pm, Joakim Hove  wrote:
> Hello,
>
> I have something I would presume was a very common pattern. I have a
> view which gets a primary-key (from the user) as second argument:
>
> def my_view( request , pk ):
>      obj = Class.objects.get( pk = pk)
>      # Do something with obj and return a suitable response.
>
> Now, of course I would like to check whether the object identified by
> 'pk' is in the database, and return a suitable error message if that
> fails; I was halfway expecting to find a "has_key() / exists() / ..."
> method, but it seems the only way to handle this gracefully is by
> catching the DoesNotExist exception?
>
> I have never really got very friendly with exceptions, I tend to
> consider them as something exceptional which "should not" happen,
> whereas the fact that the database does not contain a particular key
> is in my opinion something quite ordinary and not by any means
> "exceptional".
>
> Or maybe I am misunderstanding roally here?
>
> Joakim

Although I agree with the other posts that there isn't anything
exceptional about exceptions in Python, there is a shortcut for
exactly this pattern: get_object_or_404. See the documentation:
http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#get-object-or-404
--
DR.

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



Re: Is the DoesNotExist exception the only way to check if an object exists?

2010-04-10 Thread Matt Schinckel
On Apr 10, 8:30 am, Kevin Teague  wrote:
> There is nothing exceptional about exceptions in Python. McDonc does a
> good job at explaining how to think about exceptions as less than
> exceptional:
>
> http://plope.com/Members/chrism/exceptions_arent_errors/view
>
> The only arguably exceptional part is that ideally the ORM would raise
> a plain KeyError exception instead of a custom exception when a key
> look-up fails ...

It makes a difference if you are likely to find an object or not - at
least in terms of performance.

If you are expecting to find an object, (and only one), the setup of a
try: except: clause is minimal, but if you are not finding an object
more often than not, it turns out to be much slower to have the
exception raised.

Having said that, the cost of having two DB queries may overcome that
anyway: len(queryset) followed by queryset.get() is probably going to
hit the database twice. A better solution would be to do a
queryset.all(), see if the length is 1, and then get the first value
by index 0.

Matt.

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



Re: sorting by date – problem handling/ordering nu ll values

2010-04-10 Thread ARie
okay, it actually wasn't that hard.

just in case anyone faces the same situation here the solution that
works for me:
tickets.extra(select={'has_deadline': "CASE WHEN myDeadline IS NULL
THEN 1 ELSE 0 END"}).order_by('has_deadline','myDeadline')



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