2016-06-23 7 views
1

私は内部イントラネット用のアプリケーションポータルページで作業しています。このサイトはdjango-auth-ldapを使用してActive Directoryと統合されており、ユーザーが所属するグループに基づいてリンク項目のみを表示する必要があります。私のモデルの一環として、各リンクを表示するのに必要なグループの名前を含む 'required_group'フィールドがあります。しかし、私はリンクをループし、ユーザーグループのメンバーシップに応じてリストをフィルタリングするのに苦労しています。私はそれが理にかなったことを願うDjangoはユーザーグループメンバーシップに基づくテンプレート内のアイテムを表示します

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

私はそれを解決することができました!それは私が学ばなければならなかったQオブジェクトの使用を必要とします。これは私のviews.pyで、最後のQオブジェクトのクエリを示しています。

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) 

これは将来誰かに役立つことを願っています。

関連する問題