Django根据用户组成员资格在模板中显示项目

时间:2016-06-23 12:46:39

标签: python django

我正在开发内部Intranet的应用程序门户页面。该站点使用django-auth-ldap与Active Directory集成,并且只应根据用户所属的组显示链接项。作为我的模型的一部分,有一个' required_group'包含显示每个链接所需的组名称的字段。但是,我正在努力遍历链接并根据用户组成员身份过滤列表。我希望这是有道理的!这是一些代码:

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from .models import Link


def index(request):
    # Check that user is authenticated
    if request.user.is_authenticated():
        # If user is authenticated and a member of "Domain Admins" show all of the links
        if request.user.groups.filter(name="Domain Admins").exists() or request.user.groups.filter(name="r-webapps-all").exists():
            links_to_display = Link.objects.all()
            context = {
                'links_to_display': links_to_display,
            }
        # Else loop through all links and only display links where link.required_group is in request.user.groups.all()
        # This is where I am stuck!
        else:
            links_to_display = Link.objects.all()
            for link in links_to_display:
                if request.user.groups.filter(name=link.required_group):
                    links_to_display = links_to_display.filter(required_group=link.required_group)

            context = {
                'links_to_display': links_to_display,
            }
    # If user is not authenticated only show links which have "Domain Users" as the required group
    else:
        links_to_display = Link.objects.filter(required_group="Domain Users")
        context = {
            'links_to_display': links_to_display,
        }
        # Login form POST
        if request.method == 'POST':
           username = request.POST['username']
           password = request.POST['password']
           user = authenticate(username=username, password=password)
           if user is not None:
                if user.is_active:
                    login(request, user)
                return redirect('/webapps/')
           else:
                return HttpResponse('ERROR')
    # Render web page
    return render(request, 'webapps/index.html', context)

models.py

class Link(models.Model):
    class Meta:
        ordering = ['display_name']
    link_target = models.CharField(max_length=200)
    display_name = models.CharField(max_length=200)
    required_group = models.CharField(max_length=200)
    image_file = models.FileField(upload_to='webapps')

的index.html

{% extends 'webapps/base.html' %}
{% block content %}
<div class="container">
    <div class="row">
        <div class="col-sm-12 text-center">
            <h2>Hospital Web Applications</h2>
        </div>
    </div>
</div>


<div class="container">
  <div class="row text-center">
          {% for link in links_to_display %}
              <div class="col-md-2 col-xs-2 link-div"><a href="{{ link.link_target }}" target="_blank"><img src="/media/{{ link.image_file }}"><br />{{ link.display_name }}</a></div>
          {% endfor %}

  </div>
    <hr>
</div>
{% endblock %}

非常感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:1)

我设法解决了!它需要使用我必须了解的Q对象。这是显示最终Q对象查询的views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.db.models import Q
from .models import Link 

def index(request):
    # Check that user is authenticated
    if request.user.is_authenticated():
        # If user is authenticated and a member of "Domain Admins" or "r-webapps-all" show all of the links
        if request.user.groups.filter(name="Domain Admins").exists() or request.user.groups.filter(name="r-webapps-all").exists():
            links_to_display = Link.objects.all()
            context = {
                'links_to_display': links_to_display,
            }
        # Else loop through all links and only display links which the user has access to.
        else:
            all_user_groups = request.user.groups.all()
            q_objects = Q()
            for group in all_user_groups:
                q_objects |= Q(required_group__contains=group)

            links_to_display = Link.objects.filter(q_objects)

            context = {
                'links_to_display': links_to_display,
            }
    # If user is not authenticated only show links which have "Domain Users" as the required group
    else:
        links_to_display = Link.objects.filter(required_group="Domain Users")
        context = {
            'links_to_display': links_to_display,
        }
        # Login form POST
        if request.method == 'POST':
           username = request.POST['username']
           password = request.POST['password']
           user = authenticate(username=username, password=password)
           if user is not None:
                if user.is_active:
                    login(request, user)
                return redirect('/webapps/')
           else:
                return HttpResponse('ERROR')
    # Render web page
    return render(request, 'webapps/index.html', context)

我希望将来可以帮助某人。