def invite(request): profile = request.user.userprofile invite_form = None vouch_form = None if profile.can_vouch: invite_form = forms.InviteForm(request.POST or None, instance=Invite(inviter=profile)) vouch_form = forms.VouchForm(request.POST or None) if invite_form and vouch_form and invite_form.is_valid() and vouch_form.is_valid(): invite_form.instance.reason = vouch_form.cleaned_data['description'] invite = invite_form.save() invite.send(sender=profile, personal_message=invite_form.cleaned_data['message']) msg = _(u"%s has been invited to Mozillians. They'll receive an email " u"with instructions on how to join. You can " u"invite another Mozillian if you like.") % invite.recipient messages.success(request, msg) return redirect('phonebook:invite') return render(request, 'phonebook/invite.html', { 'invite_form': invite_form, 'vouch_form': vouch_form, 'invites': profile.invites.all(), 'vouch_threshold': settings.CAN_VOUCH_THRESHOLD, })
def view_profile(request, username): """View a profile by username.""" data = {} privacy_mappings = {'anonymous': PUBLIC, 'mozillian': MOZILLIANS, 'employee': EMPLOYEES, 'privileged': PRIVILEGED, 'myself': None} privacy_level = None if (request.user.is_authenticated() and request.user.username == username): # own profile view_as = request.GET.get('view_as', 'myself') privacy_level = privacy_mappings.get(view_as, None) profile = UserProfile.objects.privacy_level(privacy_level).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, login_url=reverse('phonebook:home')) (request, username)) if not request.user.userprofile.is_vouched: # you have to be vouched to continue messages.error(request, GET_VOUCHED_MESSAGE) return redirect('phonebook:home') if not profile_exists or not profile_complete: raise Http404 profile = UserProfile.objects.get(user__username=username) profile.set_instance_privacy_level(PUBLIC) if request.user.is_authenticated(): profile.set_instance_privacy_level( request.user.userprofile.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 data['groups'] = profile.get_annotated_groups() data['locale'] = request.locale # Only show pending groups if user is looking at their own profile, # or current user is a superuser if not (request.user.is_authenticated() and (request.user.username == username or request.user.is_superuser)): data['groups'] = [grp for grp in data['groups'] if not grp.pending] return render(request, 'phonebook/profile.html', data)
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('phonebook:home') if not profile_exists or not profile_complete: raise Http404 profile = UserProfile.objects.get(user__username=username) profile.set_instance_privacy_level(PUBLIC) if request.user.is_authenticated(): profile.set_instance_privacy_level( request.user.userprofile.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)
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.userprofile) # 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('phonebook:profile_view', p.user.username) return HttpResponseBadRequest()
def view_profile(request, username): """View a profile by username.""" data = {} privacy_mappings = {'anonymous': PUBLIC, 'mozillian': MOZILLIANS, 'employee': EMPLOYEES, 'privileged': PRIVILEGED, 'myself': None} privacy_level = None abuse_form = None if (request.user.is_authenticated() and request.user.username == username): # own profile view_as = request.GET.get('view_as', 'myself') privacy_level = privacy_mappings.get(view_as, None) profile = UserProfile.objects.privacy_level(privacy_level).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, login_url=reverse('phonebook:home')) (request, username)) if not request.user.userprofile.is_vouched: # you have to be vouched to continue messages.error(request, GET_VOUCHED_MESSAGE) return redirect('phonebook:home') if not profile_exists or not profile_complete: raise Http404 profile = UserProfile.objects.get(user__username=username) profile.set_instance_privacy_level(PUBLIC) if request.user.is_authenticated(): profile.set_instance_privacy_level( request.user.userprofile.privacy_level) if (request.user.is_authenticated() and request.user.userprofile.is_vouched and not profile.can_vouch): abuse_report = get_object_or_none(AbuseReport, reporter=request.user.userprofile, profile=profile) if not abuse_report: abuse_report = AbuseReport(reporter=request.user.userprofile, profile=profile) abuse_form = forms.AbuseReportForm(request.POST or None, instance=abuse_report) if abuse_form.is_valid(): abuse_form.save() msg = _(u'Thanks for helping us improve mozillians.org!') messages.info(request, msg) return redirect('phonebook:profile_view', profile.user.username) if (request.user.is_authenticated() and profile.is_vouchable(request.user.userprofile)): vouch_form = forms.VouchForm(request.POST or None) data['vouch_form'] = vouch_form if vouch_form.is_valid(): # We need to re-fetch profile from database. profile = UserProfile.objects.get(user__username=username) profile.vouch(request.user.userprofile, vouch_form.cleaned_data['description']) # 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('phonebook:profile_view', profile.user.username) data['shown_user'] = profile.user data['profile'] = profile data['groups'] = profile.get_annotated_groups() data['abuse_form'] = abuse_form # Only show pending groups if user is looking at their own profile, # or current user is a superuser if not (request.user.is_authenticated() and (request.user.username == username or request.user.is_superuser)): data['groups'] = [grp for grp in data['groups'] if not (grp.pending or grp.pending_terms)] return render(request, 'phonebook/profile.html', data)