# Path: sshkm/models.py
# 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 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)
# 
# Path: sshkm/forms.py
# class KeyForm(forms.ModelForm):
#     class Meta:
#         model = Key
#         fields = ('name', 'email', 'description', 'firstname', 'lastname', 'publickey', 'member_of',)
#         labels = {
#             'name': _('Name (unique)'),
#             'email': _('Email (optional)'),
#             'description': _('Description (optional)'),
#             'firstname': _('Firstname (optional)'),
#             'lastname': _('Lastname (optional)'),
#             'publickey': _('Public Key'),
#         }
# 
#     def __init__(self, *args, **kwargs):
#         super(KeyForm, self).__init__(*args, **kwargs)
#         self.fields['member_of'] = ModelMultipleChoiceFieldNames(queryset=Group.objects.all(), required=False, label='Member of Group(s)')
#         self.fields['name'].widget.attrs['placeholder'] = ''
#         self.fields['email'].widget.attrs['placeholder'] = ''
#         self.fields['firstname'].widget.attrs['placeholder'] = ''
#         self.fields['lastname'].widget.attrs['placeholder'] = ''
#         self.fields['description'].widget.attrs['placeholder'] = ''
#         self.fields['publickey'].widget.attrs['placeholder'] = 'ssh-rsa AAAAB3NzaC1yc......== user@example.com'


from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
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 Key, KeyGroup, Group
from sshkm.forms import KeyForm
    return render(request, 'sshkm/key/list.html', context)

@login_required
def KeyDetail(request):
    if request.method == 'GET' and 'id' in request.GET:
        key = get_object_or_404(Key, pk=request.GET['id'])
        groups = Group.objects.all()
        groups_selected = KeyGroup.objects.all().filter(key_id=request.GET['id'])
        ids_selected = []
        
        for group_selected in groups_selected:
            ids_selected.append(group_selected.group_id)

        groups_not_selected = groups.exclude(id__in=ids_selected)
        
        return render(request, 'sshkm/key/detail.html', {
            'keyform': KeyForm(instance=key),
            'groups': groups,
            'groups_selected': groups_selected,
            'groups_not_selected': groups_not_selected,
        })
    else:
        return render(request, 'sshkm/key/detail.html', {
            'keyform': KeyForm(),
            'groups_not_selected': Group.objects.all(),
        })

@login_required
def KeyDelete(request):
    try:

# Path: sshkm/models.py
# 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 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)
# 
# Path: sshkm/forms.py
# class KeyForm(forms.ModelForm):
#     class Meta:
#         model = Key
#         fields = ('name', 'email', 'description', 'firstname', 'lastname', 'publickey', 'member_of',)
#         labels = {
#             'name': _('Name (unique)'),
#             'email': _('Email (optional)'),
#             'description': _('Description (optional)'),
#             'firstname': _('Firstname (optional)'),
#             'lastname': _('Lastname (optional)'),
#             'publickey': _('Public Key'),
#         }
# 
#     def __init__(self, *args, **kwargs):
#         super(KeyForm, self).__init__(*args, **kwargs)
#         self.fields['member_of'] = ModelMultipleChoiceFieldNames(queryset=Group.objects.all(), required=False, label='Member of Group(s)')
#         self.fields['name'].widget.attrs['placeholder'] = ''
#         self.fields['email'].widget.attrs['placeholder'] = ''
#         self.fields['firstname'].widget.attrs['placeholder'] = ''
#         self.fields['lastname'].widget.attrs['placeholder'] = ''
#         self.fields['description'].widget.attrs['placeholder'] = ''
#         self.fields['publickey'].widget.attrs['placeholder'] = 'ssh-rsa AAAAB3NzaC1yc......== user@example.com'


# Path: sshkm/views/key.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
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 Key, KeyGroup, Group
from sshkm.forms import KeyForm

    return render(request, 'sshkm/key/list.html', context)

@login_required
def KeyDetail(request):
    if request.method == 'GET' and 'id' in request.GET:
        key = get_object_or_404(Key, pk=request.GET['id'])
        groups = Group.objects.all()
        groups_selected = KeyGroup.objects.all().filter(key_id=request.GET['id'])
        ids_selected = []
        
        for group_selected in groups_selected:
            ids_selected.append(group_selected.group_id)

        groups_not_selected = groups.exclude(id__in=ids_selected)
        
        return render(request, 'sshkm/key/detail.html', {
            'keyform': KeyForm(instance=key),
            'groups': groups,
            'groups_selected': groups_selected,
            'groups_not_selected': groups_not_selected,
        })
    else:
        return render(request, 'sshkm/key/detail.html', {
            'keyform': KeyForm(),
            'groups_not_selected': Group.objects.all(),
        })

@login_required
def KeyDelete(request):
    try:

        keys = Key.objects.filter(id__in=request.GET.getlist('id', request.POST.getlist('id'))) # calls getlist on POST only if getlist of GET is None