# Path: operatingsystems/models.py
# class OS(models.Model):
# 
#     name = models.CharField(max_length=255, unique=True)
#     osgroup = models.ForeignKey(OSGroup, blank=True, null=True,
#                                 on_delete=models.SET_NULL)
# 
#     class Meta(object):
#         verbose_name = 'Operating System'
#         verbose_name_plural = 'Operating Systems'
#         ordering = ('name',)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('operatingsystems:os_detail', args=[str(self.id)])
# 
# class OSGroup(models.Model):
# 
#     name = models.CharField(max_length=255, unique=True)
#     repos = models.ManyToManyField(Repository, blank=True)
# 
#     class Meta(object):
#         verbose_name = 'Operating System Group'
#         verbose_name_plural = 'Operating System Groups'
#         ordering = ('name',)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('operatingsystems:osgroup_detail', args=[str(self.id)])
# 
# Path: operatingsystems/forms.py
# class AddOSToOSGroupForm(ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         super(AddOSToOSGroupForm, self).__init__(*args, **kwargs)
#         self.fields['osgroup'].label = 'OS Groups'
# 
#     class Meta(object):
#         model = OS
#         fields = ('osgroup',)
# 
# class AddReposToOSGroupForm(ModelForm):
# 
#     repos = ModelMultipleChoiceField(
#         queryset=Repository.objects.select_related(),
#         required=False,
#         label=None,
#         widget=FilteredSelectMultiple('Repos', False))
# 
#     def __init__(self, *args, **kwargs):
#         super(AddReposToOSGroupForm, self).__init__(*args, **kwargs)
#         self.fields['repos'].label = ''
# 
#     class Meta(object):
#         model = OSGroup
#         fields = ('repos',)
# 
# class CreateOSGroupForm(ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         super(CreateOSGroupForm, self).__init__(*args, **kwargs)
#         self.fields['name'].label = 'New OS Group'
# 
#     class Meta(object):
#         model = OSGroup
#         fields = ('name',)
# 
# Path: operatingsystems/serializers.py
# class OSSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta(object):
#         model = OS
#         fields = ('id', 'name', 'osgroup')
# 
# class OSGroupSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta(object):
#         model = OSGroup
#         fields = ('id', 'name', 'repos')


from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from django.contrib import messages
from django.urls import reverse
from rest_framework import viewsets, permissions
from operatingsystems.models import OS, OSGroup
from operatingsystems.forms import AddOSToOSGroupForm, \
    AddReposToOSGroupForm, CreateOSGroupForm
from operatingsystems.serializers import OSSerializer, \
    OSGroupSerializer
            else:
                if not oses:
                    text = 'There are no OS\'s with no Hosts'
                    messages.info(request, text)
                    return redirect(reverse('operatingsystems:os_list'))
                for os in oses:
                    os.delete()
                text = '{0!s} OS\'s have been deleted'.format(len(oses))
                messages.info(request, text)
                return redirect(reverse('operatingsystems:os_list'))
        elif 'cancel' in request.POST:
            if os_id == 'empty_oses':
                return redirect(reverse('operatingsystems:os_list'))
            else:
                return redirect(os.get_absolute_url())

    return render(request,
                  'operatingsystems/os_delete.html',
                  {'os': os, 'oses': oses}, )


@login_required
def osgroup_list(request):

    osgroups = OSGroup.objects.select_related()

    if 'search' in request.GET:
        terms = request.GET['search'].lower()
        query = Q()
        for term in terms.split(' '):

# Path: operatingsystems/models.py
# class OS(models.Model):
# 
#     name = models.CharField(max_length=255, unique=True)
#     osgroup = models.ForeignKey(OSGroup, blank=True, null=True,
#                                 on_delete=models.SET_NULL)
# 
#     class Meta(object):
#         verbose_name = 'Operating System'
#         verbose_name_plural = 'Operating Systems'
#         ordering = ('name',)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('operatingsystems:os_detail', args=[str(self.id)])
# 
# class OSGroup(models.Model):
# 
#     name = models.CharField(max_length=255, unique=True)
#     repos = models.ManyToManyField(Repository, blank=True)
# 
#     class Meta(object):
#         verbose_name = 'Operating System Group'
#         verbose_name_plural = 'Operating System Groups'
#         ordering = ('name',)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('operatingsystems:osgroup_detail', args=[str(self.id)])
# 
# Path: operatingsystems/forms.py
# class AddOSToOSGroupForm(ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         super(AddOSToOSGroupForm, self).__init__(*args, **kwargs)
#         self.fields['osgroup'].label = 'OS Groups'
# 
#     class Meta(object):
#         model = OS
#         fields = ('osgroup',)
# 
# class AddReposToOSGroupForm(ModelForm):
# 
#     repos = ModelMultipleChoiceField(
#         queryset=Repository.objects.select_related(),
#         required=False,
#         label=None,
#         widget=FilteredSelectMultiple('Repos', False))
# 
#     def __init__(self, *args, **kwargs):
#         super(AddReposToOSGroupForm, self).__init__(*args, **kwargs)
#         self.fields['repos'].label = ''
# 
#     class Meta(object):
#         model = OSGroup
#         fields = ('repos',)
# 
# class CreateOSGroupForm(ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         super(CreateOSGroupForm, self).__init__(*args, **kwargs)
#         self.fields['name'].label = 'New OS Group'
# 
#     class Meta(object):
#         model = OSGroup
#         fields = ('name',)
# 
# Path: operatingsystems/serializers.py
# class OSSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta(object):
#         model = OS
#         fields = ('id', 'name', 'osgroup')
# 
# class OSGroupSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta(object):
#         model = OSGroup
#         fields = ('id', 'name', 'repos')


# Path: operatingsystems/views.py
from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from django.contrib import messages
from django.urls import reverse
from rest_framework import viewsets, permissions
from operatingsystems.models import OS, OSGroup
from operatingsystems.forms import AddOSToOSGroupForm, \
    AddReposToOSGroupForm, CreateOSGroupForm
from operatingsystems.serializers import OSSerializer, \
    OSGroupSerializer

            else:
                if not oses:
                    text = 'There are no OS\'s with no Hosts'
                    messages.info(request, text)
                    return redirect(reverse('operatingsystems:os_list'))
                for os in oses:
                    os.delete()
                text = '{0!s} OS\'s have been deleted'.format(len(oses))
                messages.info(request, text)
                return redirect(reverse('operatingsystems:os_list'))
        elif 'cancel' in request.POST:
            if os_id == 'empty_oses':
                return redirect(reverse('operatingsystems:os_list'))
            else:
                return redirect(os.get_absolute_url())

    return render(request,
                  'operatingsystems/os_delete.html',
                  {'os': os, 'oses': oses}, )


@login_required
def osgroup_list(request):

    osgroups = OSGroup.objects.select_related()

    if 'search' in request.GET:
        terms = request.GET['search'].lower()
        query = Q()
        for term in terms.split(' '):

            q = Q(name__icontains=term)