Re: What hash algorithm does django auth use?

2009-03-31 Thread Matthew Somerville

Joshua Partogi wrote:
> # Process the data in form.cleaned_data
> user = User()
> new_member = form.save(commit=False)
> 
> new_member.password = user.set_password
> ( new_member.password )
> instance = new_member.save()
> 
> But it seems that user.set_password is not invoked and I get None.
> 
> What is the proper way of doing it?

You have to call set_password() on the user object you wish to set the 
password for, not a random new user object. So:

 # Process the data in form.cleaned_data
 new_member = form.save(commit=False)
 new_member.set_password( new_member.password )
 instance = new_member.save()

You could put that gubbins in your form's save() method, then it'd be 
taken care of wherever you might save the form (and probably use 
self.cleaned_data['password'] - see the UserCreationForm in 
django/contrib/auth/forms.py).

ATB,
Matthew

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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: error in response['Content-Type']

2009-03-31 Thread Matthew Somerville

剑韬 付 wrote:
> if response['Content-Type'].split(';')[0] not in safe_mime_types:
> AttributeError: 'tuple' object has no attribute 'split'

> here is the code snippets:
> resp = HttpResponse(S3FileIter(block_hash_list, data_start, 
> content_length),\
> mimetype = 
> mimetypes.guess_type(os.path.basename(request.REQUEST['srv_path'])))

> 1.what's the reason of problem

mimetypes.guess_type() returns a tuple as explained at
http://docs.python.org/library/mimetypes.html and HttpResponse expects a
string, as explained at
http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse.__init__

> 3.How to solve the problem

Pass in the first element of the tuple, not the tuple. Though you should
probably check that guess_type() is actually returning a valid content
type first, rather than just assuming it will.

ATB,
Matthew
http://www.dracos.co.uk/

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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: Like Statement in Raw SQL

2009-03-30 Thread Matthew Somerville

Francisco Rivas wrote:
> sql = sql + 'AND f.url like "%%%s%%"'  % (forge)
> 
>   cursor = connection.cursor()
>   cursor.execute(sql)
>   results = cursor.fetchall()

cursor.execute() expects placeholders, rather than direct parameters, so 
needs % to be escaped on input - your one level of escaping is removed 
when interpolating forge. So you /could/ change the one line above to be:
 sql = sql + 'AND f.url like "%s"'  % (forge)

but I would recommend instead something like:

 sql = sql + 'AND f.url like %s'
 cursor = connection.cursor()
 cursor.execute(sql, ['%'+forge+'%'])

which then means forge will be quoted/escaped for you as necessary 
automatically.

ATB,
Matthew

> not enough arguments for format string



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~--~~~~--~~--~--~---



MySQL group_concat with aggregates

2009-03-29 Thread Matthew Somerville

Hi,

I have the model described at 
http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany 
on which I have multiple rows in Membership with the same Person and 
Group (say they're a bit flaky, and leave and rejoin a few times ;) ). I 
wanted to print out a paginated list of groups someone is in, with all 
their joining dates in each group result.

I decided to try the new aggregate functionality. Here's my view:

from aggregates import Concatenate
groups = person.group_set
.annotate(Concatenate('membership__date_joined'))
.order_by('name')
page = Paginator(groups, 10).page(1)

And my Concatenate class looks like this:

from django.db.models import Aggregate
from django.db.models.sql.aggregates import Aggregate as AggregateSQL
from django.db.models import DecimalField

class ConcatenateSQL(AggregateSQL):
   sql_function = 'GROUP_CONCAT'
   def __init__(self, col, separator='|', source=None, **extra):
  self.sql_template = "%%(function)s(%%(field)s ORDER BY 
%%(field)s SEPARATOR '%s')" % separator
  c = DecimalField() # XXX
  super(ConcatenateSQL, self).__init__(col, source=c, **extra)

class Concatenate(Aggregate):
   name = 'Concatenate'
   def add_to_query(self, query, alias, col, source, is_summary):
  aggregate = ConcatenateSQL(col, separator=' / ', 
is_summary=is_summary)
  query.connection.ops.check_aggregate_support(aggregate)
  query.aggregates[alias] = aggregate

This works lovely, so the only issue I found was that I had to use a 
fake DecimalField() in order for the result from the database to get 
past the call to convert_values() in django/db/backends/__init__.py 
(called from django/db/models/sql/query.py in resolve_aggregate()). This 
function appears to only want numbers/datetimes to go in, and in this 
case I'm obviously returning text. Not sure what to suggest as a 
solution, as there are presumably other things going on of which I'm not 
aware, but the above works for me :)

ATB,
Matthew


P.S. If anyone's interested, prior to aggregate support, I was doing 
something like this to get the joining dates per group:

 groups = person.group_set.all().distinct().order_by('name')
 page = Paginator(groups, 10).page(1)
 ids = [ group.id for group in page.object_list ]
 members = Membership.objects.filter(group__in=ids, person=person)
 dates_joined = {}
 for m in members:
 dates_joined.setdefault(m.group, []).append(m.date_joined)
 for group in page.object_list:
 group.dates_joined = dates_joined[group]

Which worked fine, but I felt was a bit fiddly.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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: Problem with Field errors

2009-03-29 Thread Matthew Somerville

Jack Orenstein wrote:
> On Mar 29, 2009, at 9:35 AM, Matthew Somerville wrote:

>> For more information, see
>> http://docs.djangoproject.com/en/dev/ref/forms/validation/
> 
> Thanks, that's really useful to know about. This works for most of  
> the additional validation I need to do, but doesn't fit so well for  
> the cross-field validation, (e.g. password and confirm_password  
> fields match). Or can I do that by overriding Form.is_valid?

 From the page I provided a link to: "* The Form subclass’s clean() 
method. This method can perform any validation that requires access to 
multiple fields from the form at once. This is where you might put in 
things to check that if field A is supplied, field B must contain a 
valid e-mail address and the like."

ATB,
Matthew

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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: Problem with Field errors

2009-03-29 Thread Matthew Somerville

Jack Orenstein wrote:
> On Mar 28, 2009, at 12:26 PM, Daniel Roseman wrote:
> 
>> On Mar 28, 4:14 pm, Jack Orenstein  wrote:
>>> My application needs to validate data from a from beyond the
>>> validation of Fields done by django. So in my form handler, I check
>>> Form.is_valid, and if that returns true, then I do my own validation.

If you have a field you need to perform more validation on, what you 
should do is give your Form subclass a clean_() method which 
will be called automatically, and should raise a ValidationError if the 
data doesn't validate (which will then put the error in the right place 
for you). Then is_valid() will do Django and your validation together. 
For more information, see 
http://docs.djangoproject.com/en/dev/ref/forms/validation/

>> Try self.form._errors['foobar'] instead.
> 
> That works, thank you.
> 
> Why does it work?

http://docs.djangoproject.com/en/dev/ref/forms/validation/#form-subclasses-and-modifying-field-errors
 
explains the _errors variable, but I don't think you need it in this case.

ATB,
Matthew

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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: django.contrib.auth.models.User User model can't be saved

2009-03-29 Thread Matthew Somerville

Joshua Partogi wrote:
> Yes you're right. In my template I only have username, first_name,
> last_name, email and password. I also print out the errors too.

Nowhere are you printing out non-field specific errors - please read 
about non_field_errors at 
http://docs.djangoproject.com/en/dev/ref/forms/validation/

 > Are you saying that I should have all the other fields from
 > django.contrib.auth.models.User too?

No, that would be silly. :) You have created a Form that wants every 
field from User and are not supplying them all. All you have to do is 
specify the fields you want to use from User using the Meta class as 
described at 
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form

Then you can just display the form with form.as_p and it will know it 
only cares about those fields.

ATB,
Matthew

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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: django.contrib.auth.models.User User model can't be saved

2009-03-28 Thread Matthew Somerville

Joshua Partogi wrote:
> That is exactly the problem. No exception is thrown. But I guess it
> didn't pass the validation because it wasn't redirecting to another
> page. Which is funny because all the required field in
> django.contrib.auth.models.User object is filled in.

How are you printing out the form in the template? If it's not 
redirecting, it's not validating, so there's an error - make sure you're 
printing out all the errors in the template. Without any more clues, it 
could be something as simple as your form has method GET rather than 
POST. ;) But I'm guessing you don't want your registration form to have 
all the fields from the User model (such as is_superuser), and you're 
printing out only those ones you do want in the template, rather than 
restricting the form with the fields Meta variable, and so aren't 
printing out the error messages correctly. That's just a guess.

> Am I doing a good practice anyway for doing registration using this 
> django.contrib.auth.models.User object?

I believe that's what it's for, yes :) The code you give looks fine in 
its structure.

ATB,
Matthew

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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: Contrib comments and removing fields for private comments

2009-03-27 Thread Matthew Somerville

coulix wrote:
> however i feel like i am heading the wrong way, i saw that if a user
> si logged in the email and user field of comment model will be
> automatically assigned.

Yes, they are.

> Therefore maybe i just have to custom the form template instead of
> customising the forms.py and models.py since i want to remove not add
> fields ?

That's what I do, and it seems to work :-) I simply have:
 {% if user.is_authenticated %}
 {% get_comment_form for production as form %}
 
 ...
 
 {% endif %}

where ... includes (apart from HTML text) form.comment, 
form.honeypot.label_tag, form.honeypot, form.content_type, 
form.object_pk, form.timestamp, form.security_hash and a hidden next.

I also have a wrapper around post_comment in order for only logged-in 
users to post:
 def post_comment_wrapper(request):
 if not request.user.is_authenticated():
 return HttpResponseRedirect()
 return post_comment(request)

ATB,
Matthew


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~--~~~~--~~--~--~---