예제 #1
0
def edit_profile(request):
    user = request.user
    user_profile = user.user_profile

    if request.method == 'POST' and request.POST:
        user_form = UserEditForm(request.POST, instance=user)
        profile_form = UserProfileForm(request.POST, request.FILES, instance=user_profile)
        edu_formset = EducationDetailsFormSet(request.POST, instance=user_profile)
        emp_formset = EmploymentDetailsFormSet(request.POST, instance=user_profile)
        if user_form.is_valid() and profile_form.is_valid() and edu_formset.is_valid() and emp_formset.is_valid():
            if "url" in request.POST:
                user_profile.picture = cropAndSave(user, request.POST)
                user_profile.save()

            user_form.save()
            profile_form.save()
            emp_formset.save()
            edu_formset.save()
            return render(request, "mentor/edit_profile.html", locals())
            # return here if different behaviour desired
        else:
            print user_form.errors
            print profile_form.errors
            return render(request, "mentor/edit_profile.html", locals())
    else:
        user_form = UserEditForm(instance=user)
        profile_form = UserProfileForm(instance=user_profile)
        edu_formset = EducationDetailsFormSet(instance=user_profile)
        emp_formset = EmploymentDetailsFormSet(instance=user_profile)

    context = RequestContext(request)
    context_dict = {}

    categories = Business_categories.objects.all()

    dict_index = 0;
    for cat in categories:
        subcat_list = []
        subcategories = Business_subcategories.objects.filter(category_id=cat.id)
        subcat_list.append({'category_id': cat.id, 'category_name': cat.name, 'subcategories': subcategories})
        context_dict[dict_index] = subcat_list
        dict_index = dict_index + 1

    tags = Business_Mentor_Tags.objects.filter(mentor=user_profile)

    return render(request, "mentor/edit_profile.html", locals())
예제 #2
0
def register(request):
    # Like before, get the request's context.
    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        data = request.POST.copy()  # so we can manipulate data
        # random username
        data['username'] = data[
            'email']  # useless,rather keep email as data['email'] username ''.join([choice(letters) for i in xrange(30)])
        data['is_new'] = False
        user_form = UserForm(data)
        profile_form = UserProfileForm(data=request.POST)
        education_form = EducationForm(data=request.POST)
        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid() and education_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.save()
            '''
            we dont need it now
            #now delete the default mentee profile thats created
            UserProfile.objects.get(user=user).delete()
            '''
            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user
            cropAndSave(user, request.POST)
            if "url" in request.POST:
                profile.picture = request.POST['url']
            else:
                HttpResponse("Image URL missing :(")
            profile.is_new = False
            profile.is_mentor = True
            education = education_form.save(commit=False)
            education.parent = profile
            # employee = employment_form.save(commit=False)
            # employee.parent = profile

            profile.save()
            if education.institution != '':
                education.save()
                # if employee.organization != '':
                # employee.save()
            # Update our variable to tell the template registration was successful.
            registered = True
            return HttpResponseRedirect("/user/thank-you/")
        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print user_form.errors, profile_form.errors, education_form.errors
            return HttpResponse("Invalid user Input.")

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
        education_form = EducationForm()
        # employment_form = EmploymentForm()
    # Render the template depending on the context.
    return render_to_response(
        'mentor/register.html',
        {'user_form': user_form, 'profile_form': profile_form, 'education_form': education_form,
         'registered': registered},
        context)