# Path: sshkm/models.py
# class Group(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     members = models.ManyToManyField('Key', through='KeyGroup', blank=True)
# 
# class Key(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     firstname = models.CharField(max_length=191, null=True, blank=True)
#     lastname = models.CharField(max_length=191, null=True, blank=True)
#     email = models.EmailField(max_length=191, null=True, blank=True)
#     publickey = models.TextField(null=True, blank=True)
#     member_of = models.ManyToManyField('Group', through='KeyGroup', blank=True)
# 
# class KeyGroup(models.Model):
#     key = models.ForeignKey('Key', on_delete=models.CASCADE)
#     group = models.ForeignKey('Group', on_delete=models.CASCADE)
# 
# 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 GroupForm(forms.ModelForm):
#     class Meta:
#         model = Group
#         fields = ('name', 'description', 'members',)
#         labels = {
#             'name': _('Groupname (department, external company, remote hostgroup, ...)'),
#             'description': _('Description (optional)'),
#         }
# 
#     def __init__(self, *args, **kwargs):
#         super(GroupForm, self).__init__(*args, **kwargs)
#         self.fields['members'] = ModelMultipleChoiceFieldNames(queryset=Key.objects.all(), required=False, label='Members (Key(s))')
#         self.fields['name'].widget.attrs['placeholder'] = ''
#         self.fields['description'].widget.attrs['placeholder'] = ''


from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
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 sshkm.models import Group, Key, KeyGroup, Permission
from sshkm.forms import GroupForm



@login_required
def GroupList(request):
    groups = Group.objects.order_by('name')
    context = {'groups': groups}
    return render(request, 'sshkm/group/list.html', context)

@login_required
def GroupDetail(request):
    if request.method == 'GET' and 'id' in request.GET:

# Path: sshkm/models.py
# class Group(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     members = models.ManyToManyField('Key', through='KeyGroup', blank=True)
# 
# class Key(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     firstname = models.CharField(max_length=191, null=True, blank=True)
#     lastname = models.CharField(max_length=191, null=True, blank=True)
#     email = models.EmailField(max_length=191, null=True, blank=True)
#     publickey = models.TextField(null=True, blank=True)
#     member_of = models.ManyToManyField('Group', through='KeyGroup', blank=True)
# 
# class KeyGroup(models.Model):
#     key = models.ForeignKey('Key', on_delete=models.CASCADE)
#     group = models.ForeignKey('Group', on_delete=models.CASCADE)
# 
# 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 GroupForm(forms.ModelForm):
#     class Meta:
#         model = Group
#         fields = ('name', 'description', 'members',)
#         labels = {
#             'name': _('Groupname (department, external company, remote hostgroup, ...)'),
#             'description': _('Description (optional)'),
#         }
# 
#     def __init__(self, *args, **kwargs):
#         super(GroupForm, self).__init__(*args, **kwargs)
#         self.fields['members'] = ModelMultipleChoiceFieldNames(queryset=Key.objects.all(), required=False, label='Members (Key(s))')
#         self.fields['name'].widget.attrs['placeholder'] = ''
#         self.fields['description'].widget.attrs['placeholder'] = ''


# Path: sshkm/views/group.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
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 sshkm.models import Group, Key, KeyGroup, Permission
from sshkm.forms import GroupForm




@login_required
def GroupList(request):
    groups = Group.objects.order_by('name')
    context = {'groups': groups}
    return render(request, 'sshkm/group/list.html', context)

@login_required
def GroupDetail(request):
    if request.method == 'GET' and 'id' in request.GET:

        group = get_object_or_404(Group, pk=request.GET['id'])