Example #1
0
def profile(request, username):
    
    user = get_object_or_404(User, username=username)
#    profile = Profile.objects.get_or_create(user=user)[0]
    profile = user.get_profile()

    if request.method == 'POST':
        if request.POST.has_key('change_rep'):
            if not request.user.is_staff:
                return HttpRepsonse("Unauthorized")
            rep_form = RepForm(request.POST)
            if rep_form.is_valid():
                profile.reputation = rep_form.cleaned_data['base_rep']
                profile.save()
                return HttpResponseRedirect(reverse('quanda_public_profile', args=[username]))
        elif request.POST.has_key('save_profile'):
            if not request.user == profile.user:
                return HttpResponse("Unauthorized")
            profile_form = ProfileForm(request.POST, instance=profile)
            if profile_form.is_valid():
                #return HttpResponse("ready to save")
                profile = profile_form.save()
                return HttpResponseRedirect(reverse('quanda_public_profile', args=[username]))
    else:
        profile_form = ProfileForm(instance=profile)
        rep_form = RepForm(initial={'base_rep': profile.reputation})
    
    return render_to_response("quanda/profile.html", {
        'rep_form': rep_form,
        'profile_form': profile_form,
        'questions': Question.objects.filter(author__username=username).order_by('-posted'),
        'answers': Answer.objects.filter(author__username=username).order_by('-posted'),
        'profile': profile,
        'tinymce': TINY_MCE_JS_LOCATION,
        }, context_instance=RequestContext(request))
Example #2
0
def profile_edit(request, **kwargs):
    
    template_name = kwargs.get("template_name", "profile/profile_edit.html")
    
    if request.is_ajax():
        template_name = kwargs.get(
            "template_name_facebox",
            "profile/profile_edit_facebox.html"
        )
    
    profile = request.user.get_profile()
    
    if request.method == "POST":
        if request.POST.has_key('change_rep'):
            if not request.user.is_staff:
                return HttpRepsonse("Unauthorized")
            rep_form = RepForm(request.POST)
            if rep_form.is_valid():
                profile.reputation = rep_form.cleaned_data['base_rep']
                profile.save()
                return HttpResponseRedirect(reverse("profile_detail", args=[request.user.username]))
        else:
            if not request.user == profile.user:
                return HttpResponse("Unauthorized")
            profile_form = ProfileForm(request.POST, instance=profile)
            if profile_form.is_valid():
                profile = profile_form.save()
                return HttpResponseRedirect(reverse("profile_detail", args=[request.user.username]))

    else:
        profile_form = ProfileForm(instance=profile)
        rep_form = RepForm(initial={'base_rep': profile.reputation})
    
    return render_to_response(template_name, {
        "profile": profile,
        "profile_form": profile_form,
        "rep_form": rep_form,
        'tinymce': TINY_MCE_JS_LOCATION,
    }, context_instance=RequestContext(request))