def test_is_officer(self): # Test that failing to provide a user results in False self.assertFalse(is_officer(None)) # Test with non-officer self.assertFalse(is_officer(self.user)) # Test with officer Group.objects.create(name="Officer").user_set.add(self.user) self.assertTrue(is_officer(self.user))
def index(request): """Landing Page""" context = {} is_off = is_officer(request.user) context['is_officer'] = is_off return render(request, 'index.html', context)
def index(request): """Landing Page""" context = RequestContext(request) is_off = is_officer(request.user) context["is_officer"] = is_off return render_to_response("index.html", context)
def officer_photos(request, pk=None): """Update officer headshot (displayed on the main LNL website about page)""" context = {} if pk is None: pk = request.user.pk officer = get_object_or_404(get_user_model(), pk=pk) if not challenges.is_officer(officer): messages.add_message(request, messages.ERROR, 'This feature is not available for this user.') return HttpResponseRedirect(reverse("home")) context['officer'] = officer img = OfficerImg.objects.filter(officer=officer).first() form = forms.OfficerPhotoForm(instance=img) if request.method == "POST": form = forms.OfficerPhotoForm(request.POST, request.FILES, instance=img) if request.POST['save'] == "Remove": img = OfficerImg.objects.filter(officer=officer).first() if img is not None: img.delete() messages.success( request, "Your profile photo was removed successfully!", extra_tags='success') return HttpResponseRedirect( reverse("accounts:detail", args=[officer.pk])) if form.is_valid(): form.instance.officer = officer form.save() messages.success(request, "Your profile photo was updated successfully!", extra_tags='success') return HttpResponseRedirect( reverse("accounts:detail", args=[officer.pk])) else: context['form'] = form else: context['form'] = form return render(request, 'form_crispy.html', context)