view apps/profile/views.py @ 130:5ab229c9d348

Add back ability to edit account settings * Account/profile editing is now done with a ModelForm, instead of processing the data manually. Help text and verbose names have been moved to the Profile model and to the UserForm (in the latter case, it's because we can't edit the User model directly). * The profile page now shows pygments style information and sections of the profile that are not filled are now hidden. * Some parts of apps/profiles/views.py have been rewritten (the getprofile() method has been removed, as it's no longer necessary, and the editprofile() view was rewritten in accordance with the switch to using ModelForms). * The styling for forms has been modified slightly.
author dellsystem <ilostwaldo@gmail.com>
date Sat, 22 Sep 2012 11:18:23 -0400
parents a8da60d611f7
children c7be7def8b57
line wrap: on
line source

from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

from agora.apps.free_license.models import FreeLicense
from agora.apps.bundle.models import Bundle
from agora.apps.snippet.models import Snippet
from agora.apps.profile.models import Profile
from agora.apps.profile.forms import UserForm, ProfileForm


def showprofile(request, username):
    user = get_object_or_404(User, username=username)
    profile = user.get_profile()

    if user.first_name or user.last_name:
        name = user.get_full_name()
    else:
        name = user.username

    b = Bundle.objects.filter(uploader=user)
    s = Snippet.objects.filter(author=user)

    context = {
        'profile': user.get_profile,
        'name': name,
        'bundles': Bundle.objects.filter(uploader=user),
        'snippets': Snippet.objects.filter(author=user),
    }

    return render(request, 'profile/user.djhtml', context)


@login_required
def editprofile(request):
    user = request.user
    profile = user.get_profile()

    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=user)
        profile_form = ProfileForm(request.POST, instance=profile)

        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return redirect(user)
    else:
        user_form = UserForm(instance=user)
        profile_form = ProfileForm(instance=profile)

    context = {
        'profile': profile,
        'user_form': user_form,
        'profile_form': profile_form,
    }

    return render(request, 'profile/edit-user.djhtml', context)