def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST, request.FILES) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit = False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] print "Profile photo uploaded." profile.save() registered = True else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response('ca/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, }, context)
def edit_profile(request): context = RequestContext(request) user = request.user if user.is_authenticated(): profile = UserProfile.objects.get(user = user) if request.method == "POST": print request.FILES update_user_form = UserForm(request.POST, instance = user) update_profile_form = UserProfileForm(request.POST, request.FILES, instance = profile) if update_user_form.is_valid(): if update_profile_form.is_valid(): user = update_user_form.save() user.set_password(user.password) profile = update_profile_form.save(commit = False) selected_fav_programs = update_profile_form.cleaned_data['fav_program'] selected_packages = update_profile_form.cleaned_data['packages'] # TODO: remove tracking for not_selected_packages # TODO: highlight packages, fav_programs that are already selected before profile.fav_program = selected_fav_programs if 'picture' in request.FILES: profile.picture = request.FILES['picture'] print "Profile photo updated." # Manually save M2M through relationship for package in selected_packages: tracking = Tracking.objects.get_or_create(package = package, user = profile) print tracking tracking[0].save() user.save() profile.save() print "Update profile successful" # Redirect to profile page return render_to_response("ca/profile.html", { 'user': user, 'profile': profile, }, context) else: update_user_form = UserForm(instance = user) update_profile_form = UserProfileForm(instance = profile, initial = {'packages': [p.id for p in profile.packages.all()]}) return render_to_response('ca/edit_profile.html', { 'profile': profile, 'user': user, 'edituser': update_user_form, 'editprofile': update_profile_form, }, context) else: # TODO: Your session has timed out! pass