# Path: sshkm/models.py
# class Osuser(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     home = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
# 
# 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 OsuserForm(forms.ModelForm):
#     class Meta:
#         model = Osuser
#         fields = ('name', 'home', 'description',)
#         labels = {
#             'name': _('Username'),
#             'home': _('Home Directory (optional)'),
#             'description': _('Description (optional)'),
#         }
# 
#     def __init__(self, *args, **kwargs):
#         super(OsuserForm, self).__init__(*args, **kwargs)
#         self.fields['name'].widget.attrs['placeholder'] = 'root, oracle, john, ...'
#         self.fields['home'].widget.attrs['placeholder'] = '/root, /home/oracle, ...'
#         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 Osuser, Permission
from sshkm.forms import OsuserForm



@login_required
def OsuserList(request):
    osusers = Osuser.objects.order_by('name')
    context = {'osusers': osusers}
    return render(request, 'sshkm/osuser/list.html', context)

@login_required
def OsuserDetail(request):
    if request.method == 'GET' and 'id' in request.GET:
        osuser = get_object_or_404(Osuser, pk=request.GET['id'])
        osuserform = OsuserForm(instance=osuser)
        permissions = Permission.objects.filter(osuser_id=request.GET['id'])
        return render(request, 'sshkm/osuser/detail.html', {
            'osuserform': osuserform,
            'permissions': permissions,
        })
    else:

# Path: sshkm/models.py
# class Osuser(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     home = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
# 
# 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 OsuserForm(forms.ModelForm):
#     class Meta:
#         model = Osuser
#         fields = ('name', 'home', 'description',)
#         labels = {
#             'name': _('Username'),
#             'home': _('Home Directory (optional)'),
#             'description': _('Description (optional)'),
#         }
# 
#     def __init__(self, *args, **kwargs):
#         super(OsuserForm, self).__init__(*args, **kwargs)
#         self.fields['name'].widget.attrs['placeholder'] = 'root, oracle, john, ...'
#         self.fields['home'].widget.attrs['placeholder'] = '/root, /home/oracle, ...'
#         self.fields['description'].widget.attrs['placeholder'] = ''


# Path: sshkm/views/osuser.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 Osuser, Permission
from sshkm.forms import OsuserForm




@login_required
def OsuserList(request):
    osusers = Osuser.objects.order_by('name')
    context = {'osusers': osusers}
    return render(request, 'sshkm/osuser/list.html', context)

@login_required
def OsuserDetail(request):
    if request.method == 'GET' and 'id' in request.GET:
        osuser = get_object_or_404(Osuser, pk=request.GET['id'])
        osuserform = OsuserForm(instance=osuser)
        permissions = Permission.objects.filter(osuser_id=request.GET['id'])
        return render(request, 'sshkm/osuser/detail.html', {
            'osuserform': osuserform,
            'permissions': permissions,
        })
    else:

        osuserform = OsuserForm()