Skip to content

Instantly share code, notes, and snippets.

@EdwardIII
Created September 23, 2012 19:10
Show Gist options
  • Select an option

  • Save EdwardIII/3772709 to your computer and use it in GitHub Desktop.

Select an option

Save EdwardIII/3772709 to your computer and use it in GitHub Desktop.
def new(request):
if request.method == 'POST':
print request.POST
form = PersonForm(request.POST, request.FILES)
if form.is_valid():
from django.contrib.auth.models import User
person = form.save(commit=False)
#TODO: Could this be encapsulated inside the Person object? *Should* it be?
#temp_password = User.objects.make_random_password()
auto_username = hashlib.md5(person.email.lower().strip()).hexdigest()[:30]
user = User.objects.create_user(auto_username,
person.email,
form.cleaned_data['password']
)
user.save()
person.user = user
user_token = authenticate(username=user.username, password=form.cleaned_data['password'])
login(request, user_token)
person.save()
#TODO: override save() and on first save launch the two behaviours below. See http://stackoverflow.com/questions/907695/in-a-django-model-custom-save-method-how-should-you-identify-a-new-object
person.notify_admin()
person.send_welcome_email()
if 'next' in request.GET:
return HttpResponseRedirect(request.GET['next'])
else:
return redirect('people.views.thanks', person.id,)
else:
form = PersonForm()
return render_to_response('people/new.html', {'form': form}, context_instance=RequestContext(request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment