Ejemplo n.º 1
0
def _edit_profile(request, new_account):
    ldap = UserSession.connect(request)
    unique_id = request.user.unique_id
    try:
        person = ldap.get_by_unique_id(unique_id, use_master=True)
    except NO_SUCH_PERSON:
        log.info('profile_uid Sending 404 for [%s]' % unique_id)
        raise Http404

    del_form = forms.DeleteForm(initial=dict(unique_id=unique_id))

    if not person:
        raise Http404

    if request.user.unique_id != person.unique_id:
        return HttpResponseForbidden()

    profile = request.user.get_profile()
    user_groups = stringify_groups(profile.groups.all().order_by('name'))

    if request.method == 'POST':
        form = forms.ProfileForm(request.POST, request.FILES)
        if form.is_valid():
            # Save both LDAP and RDBS data via our ProfileForm
            ldap.update_person(unique_id, form.cleaned_data)
            ldap.update_profile_photo(unique_id, form.cleaned_data)

            form.save(request, ldap)

            return redirect(
                reverse('confirm_register')
                if new_account else reverse('profile', args=[unique_id]))
    else:
        initial = dict(first_name=person.first_name,
                       last_name=person.last_name,
                       biography=person.biography,
                       website=profile.website,
                       groups=user_groups)

        initial.update(_get_services_fields(ldap, unique_id, use_master=True))
        form = forms.ProfileForm(initial=initial)

    d = dict(
        form=form,
        edit_form_action=reverse('phonebook.edit_profile'),
        delete_form=del_form,
        person=person,
        email=person.username,
        registration_flow=new_account,
        user_groups=user_groups,
        photo=ldap.profile_photo(unique_id, use_master=True),
    )
    return render(request, 'phonebook/edit_profile.html', d)
Ejemplo n.º 2
0
def edit_profile(request):
    profile = request.user.get_profile()
    user_groups = stringify_groups(profile.groups.all().order_by('name'))
    user_skills = stringify_groups(profile.skills.all().order_by('name'))

    if request.method == 'POST':
        form = forms.ProfileForm(
            request.POST,
            request.FILES,
            instance=profile,
        )
        if form.is_valid():
            old_username = request.user.username
            form.save(request)

            # Notify the user that their old profile URL won't work.
            if (not profile.is_vouched
                    and request.user.username != old_username):
                messages.info(
                    request,
                    _(u'You changed your username; please '
                      'note your profile URL has also '
                      'changed.'))

            return redirect(reverse('profile', args=[request.user.username]))
    else:
        initial = dict(first_name=request.user.first_name,
                       last_name=request.user.last_name,
                       bio=profile.bio,
                       website=profile.website,
                       irc_nickname=profile.ircname,
                       groups=user_groups,
                       skills=user_skills)

        if not request.user.username.startswith('u/'):
            initial.update(username=request.user.username)

        form = forms.ProfileForm(
            instance=profile,
            initial=initial,
        )

    # When changing this keep in mind that the same view is used for
    # user.register.
    d = dict(form=form,
             mode='edit',
             user_groups=user_groups,
             my_vouches=UserProfile.objects.filter(vouched_by=profile),
             profile=profile)
    return render(request, 'phonebook/edit_profile.html', d)
Ejemplo n.º 3
0
def edit_profile(request):
    profile = request.user.get_profile()
    user_groups = stringify_groups(profile.groups.all().order_by('name'))

    if request.method == 'POST':
        form = forms.ProfileForm(request.POST, request.FILES, instance=profile)
        if form.is_valid():
            form.save(request)
            return redirect(reverse('profile', args=[request.user.username]))
    else:
        initial = dict(first_name=request.user.first_name,
                       last_name=request.user.last_name,
                       bio=profile.bio,
                       website=profile.website,
                       irc_nickname=profile.ircname,
                       groups=user_groups)

        form = forms.ProfileForm(instance=profile, initial=initial)

    # When changing this keep in mind that the same view is used for
    # user.register.
    d = dict(form=form, mode='edit', user_groups=user_groups, profile=profile)
    return render(request, 'phonebook/edit_profile.html', d)
Ejemplo n.º 4
0
def edit_profile(request):
    COUNTRIES = zip(
        product_details.get_regions(request.locale).values(),
        product_details.get_regions(request.locale).values())
    COUNTRIES = sorted(COUNTRIES, key=lambda country: country[1])
    COUNTRIES.insert(0, ('', '----'))

    profile = request.user.get_profile()
    user_groups = stringify_groups(profile.groups.all().order_by('name'))
    user_skills = stringify_groups(profile.skills.all().order_by('name'))
    user_languages = stringify_groups(profile.languages.all().order_by('name'))

    if request.method == 'POST':
        form = forms.ProfileForm(
            request.POST,
            request.FILES,
            instance=profile,
        )
        form.fields['region'].choices = COUNTRIES

        if 'reset_api_key' in request.POST:
            # The rest of the form is irrelevant.
            try:
                request.user.api_key.delete()
            except ApiKey.DoesNotExist:
                pass
            return redirect(urlparams(reverse('profile.edit'), 'services'))

        if form.is_valid():
            old_username = request.user.username
            form.save(request)

            # Notify the user that their old profile URL won't work.
            if (not profile.is_vouched
                    and request.user.username != old_username):
                messages.info(
                    request,
                    _(u'You changed your username; please '
                      'note your profile URL has also '
                      'changed.'))

            return redirect(reverse('profile', args=[request.user.username]))
    else:
        initial = dict(first_name=request.user.first_name,
                       last_name=request.user.last_name,
                       username=request.user.username,
                       bio=profile.bio,
                       website=profile.website,
                       irc_nickname=profile.ircname,
                       groups=user_groups,
                       skills=user_skills,
                       languages=user_languages)

        form = forms.ProfileForm(
            instance=profile,
            initial=initial,
        )
        form.fields['country'].choices = COUNTRIES

        if not request.user.username.startswith('u/'):
            initial.update(username=request.user.username)

    # When changing this keep in mind that the same view is used for
    # user.register.
    d = dict(form=form,
             mode='edit',
             user_groups=user_groups,
             my_vouches=UserProfile.objects.filter(vouched_by=profile),
             profile=profile)
    # If there are form errors, don't send a 200 OK.
    status = 400 if form.errors else 200
    return render(request, 'phonebook/edit_profile.html', d, status=status)