def edit_profile(request): context = RequestContext(request) try: user_profile = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: user_profile = UserProfile(user=request.user) if request.method == 'POST': edit_form = ProfileForm(request.POST) if edit_form.is_valid(): data = edit_form.cleaned_data user_profile.full_name = data['full_name'] user_profile.bio = data['bio'] user_profile.save() return HttpResponseRedirect( reverse('userprofile.views.profile', args=(), kwargs={'username': request.user.username})) else: print edit_form.errors else: edit_form = ProfileForm({ 'full_name': user_profile.full_name, 'bio': user_profile.bio }) context_dict = {'edit_form': edit_form} return render_to_response('userprofile/edit.html', context_dict, context)
def edit_profile(request): context = RequestContext(request) try: user_profile = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: user_profile = UserProfile(user=request.user) if request.method == 'POST': edit_form = ProfileForm(request.POST) if edit_form.is_valid(): data = edit_form.cleaned_data user_profile.full_name = data['full_name'] user_profile.bio = data['bio'] user_profile.save() return HttpResponseRedirect( reverse('userprofile.views.profile', args=(), kwargs={'username': request.user.username})) else: print edit_form.errors else: edit_form = ProfileForm({ 'full_name': user_profile.full_name, 'bio': user_profile.bio }) context_dict = { 'edit_form': edit_form } return render_to_response('userprofile/edit.html', context_dict, context)
def add_employee(request): response = {} response.update({'employee_type_choices':employee_type_choices}) if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] try: User.objects.get(username__iexact=username) except: user = User() user.username = username user.set_password(password) user.save() else: response.update({'error':True}) response.update({'error_message':'Username already exists, please use another username'}) return render(request, 'userprofile/add_employee.html', response) # initialize userprofile object userprofile = UserProfile() userprofile.user = user userprofile.full_name = request.POST.get('full_name', '') userprofile.gender = request.POST.get('gender', '') dob = request.POST.get('dob', '') if dob: userprofile.dob = datetime.datetime.strptime(dob, '%m/%d/%Y') userprofile.father = request.POST.get('father', '') userprofile.mother = request.POST.get('mother', '') userprofile.email = request.POST.get('email', '') userprofile.present_address = request.POST.get('present_address', '') userprofile.permanent_address = request.POST.get('permanent_address', '') userprofile.mobile = request.POST.get('mobile', '') userprofile.landline = request.POST.get('landline', '') userprofile.maritial_status = request.POST.get('maritial_status', '') userprofile.user_type = 'staff' userprofile.save() try: userprofile.save() except: user.delete() response.update({'error':True}) return render(request, 'userprofile/add_employee.html', response) # initialize employee object employee = Employee() employee.userprofile = userprofile employee.employee_type = request.POST.get('employee_type', '') employee.date_joined = datetime.datetime.strptime(request.POST.get('date_joined', ''), '%m/%d/%Y') try: employee.save() except: user.delete() response.update({'error':True}) return render(request, 'userprofile/add_employee.html', response) response.update({'success':True}) return render(request, 'userprofile/add_employee.html', response)