コード例 #1
0
def create_account(request):
    if (request.method == 'POST'):
        user_form = UserForm(prefix="user_form", data=request.POST)
        user_creation_form = UserCreationForm(prefix="user_creation_form",
                                              data=request.POST)
        personal_detail_form = PersonalDetailForm(
            prefix='personal_detail_form', data=request.POST)
        education_detail_form = EducationDetailForm(
            prefix='education_detail_form',
            data=request.POST,
            files=request.FILES)

        #print (user_form.errors.as_data())
        #print (user_creation_form.errors.as_data())
        #print (personal_detail_form.errors.as_data())
        #print (education_detail_form.errors.as_data())

        if (user_form.is_valid() and user_creation_form.is_valid()
                and personal_detail_form.is_valid()
                and education_detail_form.is_valid()):
            user = user_creation_form.save()
            user_form_obj = user_form.save(commit=False)
            user.email = user_form_obj.email
            user.first_name = user_form_obj.first_name.title()
            user.last_name = user_form_obj.last_name.title()
            user.username = user.username.upper()
            user.save()

            personal_detail = personal_detail_form.save(commit=False)
            personal_detail.user = user
            personal_detail.save()

            education_detail = education_detail_form.save(commit=False)
            education_detail.user = user
            education_detail.roll_number = user.username
            education_detail.save()

            cgpas = request.POST.getlist('cgpa')
            sem = 1
            for cgpa in cgpas:
                if (cgpa):
                    CGPA.objects.create(person=education_detail,
                                        semester=sem,
                                        cgpa=cgpa)
                    sem += 1
                else:
                    break

        else:
            error_list = []
            print(user_form.errors.as_data())
            errors_dict = user_form.errors.as_data()
            for key, value in errors_dict.items():
                print(key, value)
                error_list.append(value)

            errors_dict = user_creation_form.errors.as_data()
            print(user_creation_form.errors.as_data())
            for key, value in errors_dict.items():
                for item in value:
                    print(item)
                    error_list.extend(item)

            errors_dict = personal_detail_form.errors.as_data()
            print(personal_detail_form.errors.as_data())
            for key, value in errors_dict.items():
                for item in value:
                    print(item)
                    error_list.extend(item)

            errors_dict = education_detail_form.errors.as_data()
            print(education_detail_form.errors.as_data())
            for key, value in errors_dict.items():
                for item in value:
                    print(item)
                    error_list.extend(item)

            print('\n')
            print(error_list)
            return render(
                request, 'consent/create_account.html', {
                    'user_form': user_form,
                    'user_creation_form': user_creation_form,
                    'personal_detail_form': personal_detail_form,
                    'education_detail_form': education_detail_form,
                    'error_list': error_list,
                })

        return HttpResponseRedirect('/consent/home')
        # return HttpResponse('Form successfully submitted.')

    else:
        user_form = UserForm(prefix='user_form', label_suffix='')
        user_creation_form = UserCreationForm(prefix='user_creation_form',
                                              label_suffix='')
        personal_detail_form = PersonalDetailForm(
            prefix='personal_detail_form', label_suffix='')
        education_detail_form = EducationDetailForm(
            prefix='education_detail_form', label_suffix='')

        return render(
            request, 'consent/create_account.html', {
                'user_form': user_form,
                'user_creation_form': user_creation_form,
                'personal_detail_form': personal_detail_form,
                'education_detail_form': education_detail_form,
            })
コード例 #2
0
def edit_profile(request):
    personal_detail = PersonalDetail.objects.get(user=request.user)
    education_detail = EducationDetail.objects.get(user=request.user)
    cgpa = CGPA.objects.filter(person=education_detail)

    if (request.method == 'POST'):
        personal_detail_form = PersonalDetailForm(
            prefix='personal_detail_form',
            data=request.POST,
            instance=personal_detail,
        )
        education_detail_form = EducationDetailForm(
            prefix='education_detail_form',
            data=request.POST,
            files=request.FILES,
            instance=education_detail,
        )

        user_form = UserForm(
            prefix='user_form',
            data=request.POST,
            instance=request.user,
        )

        print(user_form.errors.as_data())
        print(personal_detail_form.errors.as_data())
        print(education_detail_form.errors.as_data())

        if (user_form.is_valid() and personal_detail_form.is_valid()
                and education_detail_form.is_valid()):
            user_obj = user_form.save()
            personal_detail = personal_detail_form.save()
            education_detail = education_detail_form.save()

            cgpas = request.POST.getlist('cgpa')

            sem = 1
            for cgpa in cgpas:
                new_cgpa = None
                if (cgpa):
                    new_cgpa = cgpa

                cgpa_qs = CGPA.objects.update_or_create(
                    person=education_detail,
                    semester=sem,
                    defaults={'cgpa': new_cgpa},
                )
                sem += 1

            messages.success(request, 'Your profile was successfully updated!')
            return HttpResponseRedirect('/consent/profile/view')

        else:
            error_list = []

            errors_dict = personal_detail_form.errors.as_data()
            print(personal_detail_form.errors.as_data())
            for key, value in errors_dict.items():
                for item in value:
                    print(item)
                    error_list.extend(item)

            errors_dict = education_detail_form.errors.as_data()
            print(education_detail_form.errors.as_data())
            for key, value in errors_dict.items():
                for item in value:
                    print(item)
                    error_list.extend(item)

            print('\n')
            print(error_list)
            semester_var = {}
            for semesters in cgpa:
                semester_var[semesters.semester] = semesters.cgpa

            return render(
                request, 'consent/edit_profile.html', {
                    'user_form': user_form,
                    'personal_detail_form': personal_detail_form,
                    'education_detail_form': education_detail_form,
                    'error_list': error_list,
                    'semester_var': semester_var,
                })

        return HttpResponseRedirect('/consent/home')

    else:
        user_form = UserForm(prefix='user_form', instance=request.user)

        personal_detail_form = PersonalDetailForm(
            prefix='personal_detail_form', instance=personal_detail)

        education_detail_form = EducationDetailForm(
            prefix='education_detail_form', instance=education_detail)

        semester_var = {}

        for semesters in cgpa:
            semester_var[semesters.semester] = semesters.cgpa

        return render(
            request, 'consent/edit_profile.html', {
                'user_form': user_form,
                'personal_detail_form': personal_detail_form,
                'education_detail_form': education_detail_form,
                'cgpa': cgpa,
                'semester_var': semester_var,
            })