def test_is_admin(self): """ Verifies the check for a user having Administrator privileges. """ self.assertTrue(auth.is_admin(self.admin)) self.assertFalse(auth.is_admin(self.moderator)) self.assertFalse(auth.is_admin(self.user))
def add_forum(request, section_id): """ Adds a Forum to a Section. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) forums = list(section.forums.all()) if app_settings.USE_REDIS: redis.seen_user(request.user, "Adding a new forum") if request.method == "POST": form = forms.AddForumForm(forums, request.POST) if form.is_valid(): if not form.cleaned_data["forum"]: # Add to the end order = len(forums) + 1 else: # Insert before an existing Forum order = Forum.objects.get(pk=form.cleaned_data["forum"]).order Forum.objects.increment_orders(section.id, order) forum = Forum.objects.create( name=form.cleaned_data["name"], section=section, order=order, description=form.cleaned_data["description"], ) return HttpResponseRedirect(forum.get_absolute_url()) else: form = forms.AddForumForm(forums) return render( request, "forum/add_forum.html", {"form": form, "section": section, "title": "Add Forum to %s" % section.name} )
def add_forum(request, section_id): """ Adds a Forum to a Section. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) forums = list(section.forums.all()) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Adding a new forum') if request.method == 'POST': form = forms.AddForumForm(forums, request.POST) if form.is_valid(): if not form.cleaned_data['forum']: # Add to the end order = len(forums) + 1 else: # Insert before an existing Forum order = Forum.objects.get(pk=form.cleaned_data['forum']).order Forum.objects.increment_orders(section.id, order) forum = Forum.objects.create( name=form.cleaned_data['name'], section=section, order=order, description=form.cleaned_data['description']) return HttpResponseRedirect(forum.get_absolute_url()) else: form = forms.AddForumForm(forums) return render( request, 'forum/add_forum.html', { 'form': form, 'section': section, 'title': 'Add Forum to %s' % section.name, })
def add_forum(request, section_id): """ Adds a Forum to a Section. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) forums = list(section.forums.all()) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Adding a new forum') if request.method == 'POST': form = forms.AddForumForm(forums, request.POST) if form.is_valid(): if not form.cleaned_data['forum']: # Add to the end order = len(forums) + 1 else: # Insert before an existing Forum order = Forum.objects.get(pk=form.cleaned_data['forum']).order Forum.objects.increment_orders(section.id, order) forum = Forum.objects.create(name=form.cleaned_data['name'], section=section, order=order, description=form.cleaned_data['description']) return HttpResponseRedirect(forum.get_absolute_url()) else: form = forms.AddForumForm(forums) return render(request, 'forum/add_forum.html', { 'form': form, 'section': section, 'title': 'Add Forum to %s' % section.name, })
def add_section(request): """ Adds a Section. """ if not auth.is_admin(request.user): return permission_denied(request) sections = list(Section.objects.all()) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Adding a new section') if request.method == 'POST': form = forms.AddSectionForm(sections, request.POST) if form.is_valid(): if not form.cleaned_data['section']: # Add to the end order = len(sections) + 1 else: # Insert before an existing Section order = Section.objects.get(pk=form.cleaned_data['section']).order Section.objects.increment_orders(order) section = Section.objects.create(name=form.cleaned_data['name'], order=order) return HttpResponseRedirect(section.get_absolute_url()) else: form = forms.AddSectionForm(sections) return render(request, 'forum/add_section.html', { 'form': form, 'title': 'Add Section', })
def add_section(request): """ Adds a Section. """ if not auth.is_admin(request.user): return permission_denied(request) sections = list(Section.objects.all()) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Adding a new section') if request.method == 'POST': form = forms.AddSectionForm(sections, request.POST) if form.is_valid(): if not form.cleaned_data['section']: # Add to the end order = len(sections) + 1 else: # Insert before an existing Section order = Section.objects.get( pk=form.cleaned_data['section']).order Section.objects.increment_orders(order) section = Section.objects.create(name=form.cleaned_data['name'], order=order) return HttpResponseRedirect(section.get_absolute_url()) else: form = forms.AddSectionForm(sections) return render(request, 'forum/add_section.html', { 'form': form, 'title': 'Add Section', })
def is_admin(user): """ Returns ``True`` if the given user has admin permissions, ``False`` otherwise. """ return user.is_authenticated() and \ auth.is_admin(user)
def edit_section(request, section_id): """ Edits a Section. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) if app_settings.USE_REDIS: redis.seen_user(request.user, "Editing a section") if request.method == "POST": form = forms.EditSectionForm(request.POST, instance=section) if form.is_valid(): form.save(commit=True) return HttpResponseRedirect(section.get_absolute_url()) else: form = forms.EditSectionForm(instance=section) return render(request, "forum/edit_section.html", {"form": form, "section": section, "title": "Edit Section"})
def delete_section(request, section_id): """ Deletes a Section after confirmation is made via POST. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Deleting a section') if request.method == 'POST': section.delete() return HttpResponseRedirect(reverse('forum_index')) else: return render(request, 'forum/delete_section.html', { 'section': section, 'forum_list': section.forums.all(), 'title': 'Delete Section', })
def delete_section(request, section_id): """ Deletes a Section after confirmation is made via POST. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) if app_settings.USE_REDIS: redis.seen_user(request.user, "Deleting a section") if request.method == "POST": section.delete() return HttpResponseRedirect(reverse("forum_index")) else: return render( request, "forum/delete_section.html", {"section": section, "forum_list": section.forums.all(), "title": "Delete Section"}, )
def delete_section(request, section_id): """ Deletes a Section after confirmation is made via POST. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Deleting a section') if request.method == 'POST': section.delete() return HttpResponseRedirect(reverse('forum_index')) else: return render( request, 'forum/delete_section.html', { 'section': section, 'forum_list': section.forums.all(), 'title': 'Delete Section', })
def delete_forum(request, forum_id): """ Deletes a Forum after confirmation is made via POST. """ if not auth.is_admin(request.user): return permission_denied(request) forum = get_object_or_404(Forum.objects.select_related(), pk=forum_id) section = forum.section if app_settings.USE_REDIS: redis.seen_user(request.user, "Deleting a forum") if request.method == "POST": forum.delete() return HttpResponseRedirect(section.get_absolute_url()) else: return render( request, "forum/delete_forum.html", {"section": section, "forum": forum, "topic_count": forum.topics.count(), "title": "Delete Forum"}, )
def edit_forum(request, forum_id): """ Edits a Forum. """ if not auth.is_admin(request.user): return permission_denied(request) forum = get_object_or_404(Forum.objects.select_related(), pk=forum_id) if app_settings.USE_REDIS: redis.seen_user(request.user, "Editing a forum") if request.method == "POST": form = forms.EditForumForm(request.POST, instance=forum) if form.is_valid(): form.save(commit=True) return HttpResponseRedirect(forum.get_absolute_url()) else: form = forms.EditForumForm(instance=forum) return render( request, "forum/edit_forum.html", {"form": form, "forum": forum, "section": forum.section, "title": "Edit Forum"}, )
def delete_forum(request, forum_id): """ Deletes a Forum after confirmation is made via POST. """ if not auth.is_admin(request.user): return permission_denied(request) forum = get_object_or_404(Forum.objects.select_related(), pk=forum_id) section = forum.section if app_settings.USE_REDIS: redis.seen_user(request.user, 'Deleting a forum') if request.method == 'POST': forum.delete() return HttpResponseRedirect(section.get_absolute_url()) else: return render( request, 'forum/delete_forum.html', { 'section': section, 'forum': forum, 'topic_count': forum.topics.count(), 'title': 'Delete Forum', })
def edit_section(request, section_id): """ Edits a Section. """ if not auth.is_admin(request.user): return permission_denied(request) section = get_object_or_404(Section, pk=section_id) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Editing a section') if request.method == 'POST': form = forms.EditSectionForm(request.POST, instance=section) if form.is_valid(): form.save(commit=True) return HttpResponseRedirect(section.get_absolute_url()) else: form = forms.EditSectionForm(instance=section) return render(request, 'forum/edit_section.html', { 'form': form, 'section': section, 'title': 'Edit Section', })
def delete_forum(request, forum_id): """ Deletes a Forum after confirmation is made via POST. """ if not auth.is_admin(request.user): return permission_denied(request) forum = get_object_or_404(Forum.objects.select_related(), pk=forum_id) section = forum.section if section.is_managed(): if not section.is_corp_authed(request.user): raise Http404 if app_settings.USE_REDIS: redis.seen_user(request.user, 'Deleting a forum') if request.method == 'POST': forum.delete() return HttpResponseRedirect(section.get_absolute_url()) else: return render(request, 'forum/delete_forum.html', { 'section': section, 'forum': forum, 'topic_count': forum.topics.count(), 'title': 'Delete Forum', })
def edit_forum(request, forum_id): """ Edits a Forum. """ if not auth.is_admin(request.user): return permission_denied(request) forum = get_object_or_404(Forum.objects.select_related(), pk=forum_id) if app_settings.USE_REDIS: redis.seen_user(request.user, 'Editing a forum') if request.method == 'POST': form = forms.EditForumForm(request.POST, instance=forum) if form.is_valid(): form.save(commit=True) return HttpResponseRedirect(forum.get_absolute_url()) else: form = forms.EditForumForm(instance=forum) return render( request, 'forum/edit_forum.html', { 'form': form, 'forum': forum, 'section': forum.section, 'title': 'Edit Forum', })