from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django import forms

from foundry.forms import JoinForm 

from neo import api


'''
A token generator to be used in password_reset and password_reset_confirm
forms - the token can then be used to change the user's password locally
and on Neo
'''
class NeoTokenGenerator(PasswordResetTokenGenerator):
    def make_token(self, user):
        return api.get_forgot_password_token(user.username).TempToken
        
    def check_token(self, user, token):
        user.forgot_password_token = token
        return True


'''
Overrides the Django password change form so that the the old password
is stored in clear text on the user object, thus making it accessible
to Neo
'''
class NeoPasswordChangeForm(PasswordChangeForm):
    def clean_new_password1(self):
        new = self.cleaned_data['new_password1']
        self.user.set_password(new, old_password=self.cleaned_data['old_password'])
        self.user.full_clean()
        return new

    def save(self, commit=True):
        if commit:
            self.user.save()
        return self.user


'''
Patch the foundry JoinForm
'''
def clean_join_form(form):
    from foundry.forms import JoinForm
    from django.forms.models import construct_instance
    cleaned_data = super(JoinForm, form).clean()
    opts = form._meta
    # Update the model instance with cleaned_data.
    member = construct_instance(form, form.instance, opts.fields, opts.exclude)
    member.set_password(form.cleaned_data["password1"])
    member.full_clean()
    return cleaned_data

def save_join_form(form, commit=True):
    if commit:
        form.instance.save()
    return form.instance

JoinForm.clean = clean_join_form
JoinForm.save = save_join_form
@@ -121,192 +121,167 @@
 orm%0A
+%0A
 from 
-django.contrib.auth.models import User%0Afrom django.core.exceptions import ValidationError%0Afrom django import forms%0A%0Afrom foundry.forms import JoinForm %0A%0Afrom neo import api%0A%0A%0A'''%0A
+foundry.forms import JoinForm%0A%0Afrom neo import api%0Afrom neo.models import NeoProfile%0A%0A%0Aclass NeoTokenGenerator(PasswordResetTokenGenerator):%0A    '''%0A    
 A to
@@ -350,16 +350,20 @@
 confirm%0A
+    
 forms - 
@@ -427,16 +427,20 @@
 locally%0A
+    
 and on N
@@ -446,65 +446,15 @@
 Neo%0A
-'''%0Aclass NeoTokenGenerator(PasswordResetTokenGenerator):
+    '''
 %0A   
@@ -478,24 +478,41 @@
 elf, user):%0A
+        try:%0A    
         retu
@@ -553,36 +553,143 @@
 ser.
-username).TempToken%0A        
+neoprofile.login_alias).TempToken%0A        except NeoProfile.DoesNotExist:%0A            return super(NeoTokenGenerator).make_token(user)%0A
 %0A   
@@ -725,16 +725,61 @@
 token):%0A
+        try:%0A            user.neoprofile%0A    
         
@@ -817,24 +817,28 @@
 ken%0A        
+    
 return True%0A
@@ -837,22 +837,194 @@
 rn True%0A
-%0A%0A'''%0A
+        except NeoProfile.DoesNotExist:%0A            return super(NeoTokenGenerator, self).check_token(user, token)%0A%0A%0Aclass NeoPasswordChangeForm(PasswordChangeForm):%0A    '''%0A    
 Override
@@ -1086,16 +1086,20 @@
 assword%0A
+    
 is store
@@ -1164,67 +1164,26 @@
 ble%0A
+    
 to Neo%0A
-'''%0Aclass NeoPasswordChangeForm(PasswordChangeForm):
+    '''
 %0A   
@@ -1541,16 +1541,18 @@
 orm%0A'''%0A
+%0A%0A
 def clea
@@ -1971,16 +1971,17 @@
 d_data%0A%0A
+%0A
 def save
@@ -2132,20 +2132,21 @@
 ave = save_join_form
+%0A

fix password reset token generator so that it works for non-Neo users