request token takes username or email now

This commit is contained in:
j 2010-12-25 15:23:10 +05:30
parent 657ecdf9f0
commit 258675f9eb

View file

@ -226,66 +226,68 @@ def resetPassword(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(resetPassword) actions.register(resetPassword)
class RecoverForm(forms.Form):
username_or_email = forms.TextInput()
def requestToken(request): def requestToken(request):
''' '''
param data { param data {
username_or_email: username username: username,
email: email
} }
return { return {
status: {'code': int, 'text': string} status: {'code': int, 'text': string}
data: { data: {
errors: { errors: {
username_or_email: 'Username or email address not found' username: 'Unknown Username'
email: 'Unknown Email'
} }
username: user username: user
} }
} }
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
form = RegisterForm(data, request.FILES) user = None
if form.is_valid(): if 'username' in data:
username = data['username_or_email'] try:
user = None user = models.User.objects.get(username=data['username'])
q = models.User.objects.filter(username=username) except models.User.DoesNotExist:
if q.count() > 0: user = None
user = q[0] elif 'email' in data:
else: try:
q = models.User.objects.filter(email=username) user = models.User.objects.get(email=data['email'])
if q.count() > 0: except models.User.DoesNotExist:
user = q[0] user = None
if user: if user:
while True: while True:
token = ox.to32(random.randint(32768, 1048575)) token = ox.to32(random.randint(32768, 1048575))
if models.UserProfile.objects.filter(reset_token=token).count() == 0: if models.UserProfile.objects.filter(reset_token=token).count() == 0:
break break
user_profile = user.get_profile() user_profile = user.get_profile()
user_profile.reset_token = token user_profile.reset_token = token
user_profile.save() user_profile.save()
template = loader.get_template('password_reset_email.txt') template = loader.get_template('password_reset_email.txt')
context = RequestContext(request, { context = RequestContext(request, {
'url': request.build_absolute_uri("/"), 'url': request.build_absolute_uri("/"),
'token': token, 'token': token,
'sitename': settings.SITENAME, 'sitename': settings.SITENAME,
}) })
message = template.render(context) message = template.render(context)
subject = '%s password reset' % settings.SITENAME subject = '%s password reset' % settings.SITENAME
user.email_user(subject, message) user.email_user(subject, message)
response = json_response({ response = json_response({
'username': user.username 'username': user.username
}, text='recover email sent') }, text='password reset email sent')
else:
response = json_response({
'errors': {
'username_or_email': 'Username or email address not found'
}
})
else: else:
response = json_response(status=400, text='invalid data') response = json_response({
'errors': {
}
})
if 'username' in data:
response['data']['errors']['username'] = 'Unknown Username'
elif 'email' in data:
response['data']['errors']['email'] = 'Unknown Email'
else:
response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(requestToken) actions.register(requestToken)