def edit_recommender_info(request, recommender_id): try: request.user.applicant except: return HttpResponseRedirect(reverse('index')) user = User.objects.get(id = request.user.id) recommender = Recommender.objects.get(id = recommender_id) if request.method == "POST": form = EditRecommenderForm(data=request.POST, request=request) if form.is_valid(): recommender.user.first_name = form.cleaned_data.get('first_name') recommender.user.last_name = form.cleaned_data.get('last_name') recommender.user.email = form.cleaned_data.get('email') recommender.relationship = form.cleaned_data.get('relationship') recommender.save() recommender.user.save() return HttpResponseRedirect(reverse('applicant_index')) else: recommender_user_info = model_to_dict(recommender.user) recommender_info = model_to_dict(recommender) allinfo = dict(recommender_user_info.items() + recommender_info.items()) form = EditRecommenderForm(request=request, initial=allinfo) form.fields['first_name'].widget.attrs['readonly'] = True form.fields['last_name'].widget.attrs['readonly'] = True form.fields['email'].widget.attrs['readonly'] = True form.fields['relationship'].widget.attrs['readonly'] = True return render(request, "edit_recommender_info.html", {'form': form, 'recommender': recommender})
def replace_recommender(request, recommender_id): try: request.user.applicant except: return HttpResponseRedirect(reverse('index')) user = User.objects.get(id = request.user.id) applicant = user.applicant recommender = Recommender.objects.get(id = recommender_id) if request.method == "POST": form = EditRecommenderForm(data=request.POST, request=request) if form.is_valid(): recommendation = Recommendation.objects.filter(recommender = recommender, applicant = applicant).first() recommendation.delete() first_name = form.cleaned_data.get('first_name') last_name = form.cleaned_data.get('last_name') email = form.cleaned_data.get('email') relationship = form.cleaned_data.get('relationship') add_recommender(email, first_name, last_name, user.applicant) new_rec_user = User.objects.filter(email = email).first() new_recommender = Recommender.objects.filter(user = new_rec_user).first() recommendation_request_sent(user.id, new_recommender.id) return HttpResponseRedirect(reverse('applicant_index')) else: form = EditRecommenderForm(request=request) return render(request, "replace_recommender.html", {'form' : form, 'recommender' : recommender})