Re: Generate a unique username for django.contrib.auth

2011-01-12 Thread marco carminati
Hi Micah

On Jan 12, 10:11 pm, Micah Carrick  wrote:
> I've got my site's authentication working with and email and password
> only--no username (thanks to Shawn Milochik for helping me with that).
> However, I still need to put in a username to make the User model happy. I
> was hoping to have "user" as a prefix and then some unique number.
>

couldn't you use the user.id after creating it?
i.e.

prefix = 'user'
###some code to save user###

user.your_field =  prefix + str(user.id)
user.save()

if you don't neet this value in the database you could put in your
mail directly the user.id
hello {{ user.id }}

unfortunately I cannot test this solution now (sorry, i've not django
in this machine)

hoping this help,
marco

-- 
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: Generate a unique username for django.contrib.auth

2011-01-12 Thread Eric Chamberlain
We use a base64 or base36 (if you want compatibility with the contrib.admin) 
encoded UUID, or generate a random 30-character string, the odds of a collision 
is quite low.

On Jan 12, 2011, at 1:11 PM, Micah Carrick wrote:

> I've got my site's authentication working with and email and password 
> only--no username (thanks to Shawn Milochik for helping me with that). 
> However, I still need to put in a username to make the User model happy. I 
> was hoping to have "user" as a prefix and then some unique number.
> 
> I cannot simply copy the email to the username because the username must be 
> less than 30 characters and, after looking into my database, many email 
> addresses go over that.
> I cannot generate a random number because there could be a collision.
> I cannot use uuid4().hex because that's 32 characters... I need <30.
> I cannot use User.objects.count() because that could result in a collision if 
> 2 users register at the same time.
> 
> Thoughts?
> 
> 
> 
> 
> -- 
> 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.

-- 
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: Generate a unique username for django.contrib.auth

2011-01-12 Thread Acorn
Why not just use incremental numeric user IDs?

On 12 January 2011 21:23, Eric Chamberlain  wrote:
> We use a base64 or base36 (if you want compatibility with the contrib.admin) 
> encoded UUID, or generate a random 30-character string, the odds of a 
> collision is quite low.
>
> On Jan 12, 2011, at 1:11 PM, Micah Carrick wrote:
>
>> I've got my site's authentication working with and email and password 
>> only--no username (thanks to Shawn Milochik for helping me with that). 
>> However, I still need to put in a username to make the User model happy. I 
>> was hoping to have "user" as a prefix and then some unique number.
>>
>> I cannot simply copy the email to the username because the username must be 
>> less than 30 characters and, after looking into my database, many email 
>> addresses go over that.
>> I cannot generate a random number because there could be a collision.
>> I cannot use uuid4().hex because that's 32 characters... I need <30.
>> I cannot use User.objects.count() because that could result in a collision 
>> if 2 users register at the same time.
>>
>> Thoughts?
>>
>>
>>
>>
>> --
>> 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.
>
> --
> 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.
>
>

-- 
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: Generate a unique username for django.contrib.auth

2011-01-12 Thread Micah Carrick
Thanks folks.

Here's what I have now. Not the most elegant but it's the best I can come up
with so far. First, I'm generating a random string for the username so that
the form will validate. After the record is saved I change the username to
"user_" and the user ID. This way it looks a little less scary if it pops up
on an admin form somewhere or something.

# in the view for my sign up form
if request.method == 'POST':
data = request.POST.copy()
randstr = ''.join([choice(string.letters) for i in xrange(30)])
data['username'] = 'user_' + randstr
form = SignUpForm(data) # subclass of UserCreationForm
if form.is_valid():
form.save()

# in the save method of SignUpForm which is subclassed from UserCreationForm
user.save()
user.username = 'user_' + str(user.id)
user.save()

What I still hate about this one is that I'm hitting the database twice for
a single registration. But, it seems to work.

On Wed, Jan 12, 2011 at 1:23 PM, Eric Chamberlain  wrote:

> We use a base64 or base36 (if you want compatibility with the
> contrib.admin) encoded UUID, or generate a random 30-character string, the
> odds of a collision is quite low.
>
> On Jan 12, 2011, at 1:11 PM, Micah Carrick wrote:
>
> > I've got my site's authentication working with and email and password
> only--no username (thanks to Shawn Milochik for helping me with that).
> However, I still need to put in a username to make the User model happy. I
> was hoping to have "user" as a prefix and then some unique number.
> >
> > I cannot simply copy the email to the username because the username must
> be less than 30 characters and, after looking into my database, many email
> addresses go over that.
> > I cannot generate a random number because there could be a collision.
> > I cannot use uuid4().hex because that's 32 characters... I need <30.
> > I cannot use User.objects.count() because that could result in a
> collision if 2 users register at the same time.
> >
> > Thoughts?
> >
> >
> >
> >
> > --
> > 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.
>
> --
> 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.
>
>


-- 
Micah Carrick, Founder

*Green Tackle* - *Environmentally Friendly Fishing Tackle*
www.GreenTackle.com 

 Email: mi...@greentackle.com
 Phone: 971.270.2206
 Toll Free: 877.580.9165
 Fax: 503.946.3106

-- 
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: Generate a unique username for django.contrib.auth

2011-01-12 Thread Eric Chamberlain
Because the id is not known until after the record is saved, so you'd have to 
generate some non-colliding filler value anyway.  Using incremental numbers can 
also leak usage information to the users.


On Jan 12, 2011, at 1:52 PM, Acorn wrote:

> Why not just use incremental numeric user IDs?
> 
> On 12 January 2011 21:23, Eric Chamberlain  wrote:
>> We use a base64 or base36 (if you want compatibility with the contrib.admin) 
>> encoded UUID, or generate a random 30-character string, the odds of a 
>> collision is quite low.
>> 
>> On Jan 12, 2011, at 1:11 PM, Micah Carrick wrote:
>> 
>>> I've got my site's authentication working with and email and password 
>>> only--no username (thanks to Shawn Milochik for helping me with that). 
>>> However, I still need to put in a username to make the User model happy. I 
>>> was hoping to have "user" as a prefix and then some unique number.
>>> 
>>> I cannot simply copy the email to the username because the username must be 
>>> less than 30 characters and, after looking into my database, many email 
>>> addresses go over that.
>>> I cannot generate a random number because there could be a collision.
>>> I cannot use uuid4().hex because that's 32 characters... I need <30.
>>> I cannot use User.objects.count() because that could result in a collision 
>>> if 2 users register at the same time.
>>> 
>>> Thoughts?
>>> 
> 

-- 
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: Generate a unique username for django.contrib.auth

2011-01-14 Thread Ian Clelland
On Wed, Jan 12, 2011 at 1:11 PM, Micah Carrick  wrote:
> I cannot use uuid4().hex because that's 32 characters... I need <30.

uuid4().hex[:30] is almost as random as uuid4().hex

There are no timers, MAC addresses, or other non-random sections in a
UUID4, so taking any 30-character slice from it should get you 120
bits of pure random goodness.

-- 
Regards,
Ian Clelland


-- 
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: Generate a unique username for django.contrib.auth

2011-01-15 Thread Martin J. Laubach
> I cannot simply copy the email to the username because the username must be
> less than 30 characters and, after looking into my database, many email
> addresses go over that.

  Note that you can fix that quite easily by putting something like
this

for f in User._meta.fields:
if f.name == "username":
f.max_length=75

  somewhere in your code. Easiest in your models.py.

mjl

-- 
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.