コード例 #1
0
def view_profile(request, username):
    """View a profile by username."""
    data = {}
    if (request.user.is_authenticated() and request.user.username == username):
        # own profile
        view_as = request.GET.get('view_as', 'myself')
        profile = request.user.userprofile
        if view_as == 'anonymous':
            profile = (UserProfile.objects.privacy_level(PUBLIC).get(
                user__username=username))
        elif view_as == 'mozillian':
            profile = (UserProfile.objects.privacy_level(MOZILLIANS).get(
                user__username=username))
        elif view_as == 'employee':
            profile = (UserProfile.objects.privacy_level(EMPLOYEES).get(
                user__username=username))
        elif view_as == 'privileged':
            profile = (UserProfile.objects.privacy_level(PRIVILEGED).get(
                user__username=username))
        data['privacy_mode'] = view_as
    else:
        userprofile_query = UserProfile.objects.filter(user__username=username)
        public_profile_exists = userprofile_query.public().exists()
        profile_exists = userprofile_query.exists()
        profile_complete = userprofile_query.exclude(full_name='').exists()

        if not public_profile_exists:
            if not request.user.is_authenticated():
                # you have to be authenticated to continue
                messages.warning(request, LOGIN_MESSAGE)
                return login_required(view_profile)(request, username)
            if not request.user.userprofile.is_vouched:
                # you have to be vouched to continue
                messages.error(request, GET_VOUCHED_MESSAGE)
                return redirect('home')

        if not profile_exists or not profile_complete:
            raise Http404

        profile = UserProfile.objects.get(user__username=username)
        privacy_level = get_privacy_level(request.user)
        profile.set_instance_privacy_level(privacy_level)

        if (not profile.is_vouched and request.user.is_authenticated()
                and request.user.userprofile.is_vouched):
            data['vouch_form'] = (forms.VouchForm(
                initial={'vouchee': profile.pk}))

    data['shown_user'] = profile.user
    data['profile'] = profile
    return render(request, 'phonebook/profile.html', data)
コード例 #2
0
def vouch(request):
    """Vouch a user."""
    form = forms.VouchForm(request.POST)

    if form.is_valid():
        p = UserProfile.objects.get(pk=form.cleaned_data.get('vouchee'))
        p.vouch(request.user.get_profile())

        # Notify the current user that they vouched successfully.
        msg = _(u'Thanks for vouching for a fellow Mozillian! '
                'This user is now vouched!')
        messages.info(request, msg)
        return redirect(reverse('profile', args=[p.user.username]))

    return HttpResponseForbidden
コード例 #3
0
def profile(request, username):
    """View a profile by username."""
    user = get_object_or_404(User, username=username)
    vouch_form = None
    profile = user.get_profile()

    if not request.user.userprofile.is_vouched and request.user != user:
        log.warning('vouch_required forbidding access')
        return HttpResponseForbidden(_('You must be vouched to do this.'))

    if not profile.is_vouched and request.user.get_profile().is_vouched:
        vouch_form = forms.VouchForm(initial=dict(vouchee=profile.pk))

    data = dict(shown_user=user, profile=profile, vouch_form=vouch_form)
    return render(request, 'phonebook/profile.html', data)