Hello All,
I am working on integration of django application with facebook and i did almost but I want to give an facility to the user to select his/her username after he/she successfully logged in facebook on my django site here is the code :
form for usename :
class RegisterUsernameForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_(u'username'))
def clean_username(self):
"""
Validate that the username is alphanumeric and is not already
in use.
"""
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
made an template against it as well
here is the view which i used to use the from
def register_username(request, form_class=RegisterUsernameForm, template_name='registration/register_username_form.html', extra_context=None):
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid():
username = form['username'].data
return HttpResponseRedirect(reverse('facebook_settings'))
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{ 'form': form },
context_instance=context)
in which i am just getting the username from the from...
I have one fb_authenticate on my django application in which i am check whether user exist in my db or not if not then create new one at that point of time i need to redirect that form "username" which i have above mention . so user can select it and then will register him in dgango app
class FbBackend:
def fb_authenticate(self, request, facebook_id=None):
if not facebook_id: return None
try:
profile = UserProfile.objects.get(facebook_id=facebook_id)
#To Check whether the same user is loggin in to facebook if not then logout the perivous one and login new one.
if request.user != profile.user :
from django.contrib.auth import logout
logout(request)
request.user=profile.user
UpdateFbUserDetails(request, profile.user, facebook_id)
return profile.user
except UserProfile.DoesNotExist:
# No user. Create one.
pass
#Get user facebook account details like first name, last name and email address.
username = request.facebook.users.getInfo([request.facebook.uid], ['name'])[0]['name']
firstname = request.facebook.users.getInfo([request.facebook.uid], ['first_name'])[0]['first_name']
lastname = request.facebook.users.getInfo([request.facebook.uid], ['last_name'])[0]['last_name']
emailaddress = request.facebook.users.getInfo([request.facebook.uid], ['email'])[0]['email']
# Create the username for facebook logged in user
fbusername = (emailaddress.rsplit ('@')[0]).replace ('.','') + "_"+((emailaddress.rsplit ('@')[1])[0]).capitalize()
try:
user = User.objects.get(email=emailaddress)
# This shouldn't really happen. Log an error.
# logging.error('Strange: user %s already exists.' % username)
except User.DoesNotExist:
user = User.objects.create_user(fbusername,emailaddress,'xxxx')#('fb_%s' % facebook_id, '')
user.first_name = firstname
user.last_name = lastname
if not UpdateFbUserDetails(request, user, facebook_id):
return None
user.save()
profile, created = UserProfile.objects.get_or_create(user=user)
profile.facebook_id = facebook_id
new_basics_info = UserBasicInfo()
new_basics_info.full_name = username
new_basics_info.save()
profile.basics_info = new_basics_info
new_personal_info = UserPersonalInfo()
new_personal_info.save()
profile.personal_info = new_personal_info
profile.save()
return user
Please help me how i can achieve it......
Thanks
Ansh J