# Path: sshkm/models.py
# class Permission(models.Model):
#     group = models.ForeignKey(Group, on_delete=models.CASCADE)
#     host = models.ForeignKey(Host, on_delete=models.CASCADE)
#     osuser = models.ForeignKey(Osuser, on_delete=models.CASCADE)
# 
#     class Meta:
#         unique_together = ("group", "host", "osuser")
# 
# Path: sshkm/forms.py
# class PermissionForm(forms.ModelForm):
#     class Meta:
#         model = Permission
#         fields = ('host', 'group', 'osuser',)
# 
#     def __init__(self, *args, **kwargs):
#         super(PermissionForm, self).__init__(*args, **kwargs)
#         self.fields['host'] = ModelMultipleChoiceFieldNames(queryset=Host.objects.all().order_by('name'), label='Select host(s) on which you want to set perssion(s):')
#         self.fields['group'] = ModelMultipleChoiceFieldNames(queryset=Group.objects.all().order_by('name'), label='Select the group(s) which has/have permission on the selected host(s):')
#         self.fields['osuser'] = ModelMultipleChoiceFieldNames(queryset=Osuser.objects.all().order_by('name'), label='Select OS-User(s) on which the key(s) in the selected group(s) should be allowed to connect on the selected hosts(s):')


from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist
from django.core.cache import cache
from sshkm.models import Permission
from sshkm.forms import PermissionForm
    per_page = getattr(settings, "PAGINATION_PER_PAGE", 10)
    paginator = Paginator(permissions, per_page)
    page = request.GET.get('page')
    try:
        permissions = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        permissions = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        permissions = paginator.page(paginator.num_pages)

    context = {'permissions': permissions}
    return render(request, 'sshkm/permission/list.html', context)

@login_required
def PermissionCreate(request):
    permissionform = PermissionForm()

    return render(request, 'sshkm/permission/create.html', {
        'permissionform': permissionform,
    })

@login_required
def PermissionDelete(request):
    try:
        if request.POST.get('id_multiple') is not None:
            Permission.objects.filter(id__in=request.POST.getlist('id_multiple')).delete()
            messages.add_message(request, messages.SUCCESS, "Permissions deleted")
        else:

# Path: sshkm/models.py
# class Permission(models.Model):
#     group = models.ForeignKey(Group, on_delete=models.CASCADE)
#     host = models.ForeignKey(Host, on_delete=models.CASCADE)
#     osuser = models.ForeignKey(Osuser, on_delete=models.CASCADE)
# 
#     class Meta:
#         unique_together = ("group", "host", "osuser")
# 
# Path: sshkm/forms.py
# class PermissionForm(forms.ModelForm):
#     class Meta:
#         model = Permission
#         fields = ('host', 'group', 'osuser',)
# 
#     def __init__(self, *args, **kwargs):
#         super(PermissionForm, self).__init__(*args, **kwargs)
#         self.fields['host'] = ModelMultipleChoiceFieldNames(queryset=Host.objects.all().order_by('name'), label='Select host(s) on which you want to set perssion(s):')
#         self.fields['group'] = ModelMultipleChoiceFieldNames(queryset=Group.objects.all().order_by('name'), label='Select the group(s) which has/have permission on the selected host(s):')
#         self.fields['osuser'] = ModelMultipleChoiceFieldNames(queryset=Osuser.objects.all().order_by('name'), label='Select OS-User(s) on which the key(s) in the selected group(s) should be allowed to connect on the selected hosts(s):')


# Path: sshkm/views/permission.py
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist
from django.core.cache import cache
from sshkm.models import Permission
from sshkm.forms import PermissionForm

    per_page = getattr(settings, "PAGINATION_PER_PAGE", 10)
    paginator = Paginator(permissions, per_page)
    page = request.GET.get('page')
    try:
        permissions = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        permissions = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        permissions = paginator.page(paginator.num_pages)

    context = {'permissions': permissions}
    return render(request, 'sshkm/permission/list.html', context)

@login_required
def PermissionCreate(request):
    permissionform = PermissionForm()

    return render(request, 'sshkm/permission/create.html', {
        'permissionform': permissionform,
    })

@login_required
def PermissionDelete(request):
    try:
        if request.POST.get('id_multiple') is not None:
            Permission.objects.filter(id__in=request.POST.getlist('id_multiple')).delete()
            messages.add_message(request, messages.SUCCESS, "Permissions deleted")
        else:

            permission = Permission.objects.get(id=request.GET['id'])