# 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
    except EmptyPage:
        page = paginator.page(paginator.num_pages)

    return render(request,
                  'operatingsystems/osgroup_list.html',
                  {'page': page, 'terms': terms}, )


@login_required
def osgroup_detail(request, osgroup_id):

    osgroup = get_object_or_404(OSGroup, id=osgroup_id)

    if request.method == 'POST':
        repos_form = AddReposToOSGroupForm(request.POST, instance=osgroup)
        if repos_form.is_valid():
            repos_form.save()
            messages.info(request, 'Modified Repositories')
            return redirect(osgroup.get_absolute_url())

    repos_form = AddReposToOSGroupForm(instance=osgroup)

    return render(request,
                  'operatingsystems/osgroup_detail.html',
                  {'osgroup': osgroup, 'repos_form': repos_form}, )


@login_required
def osgroup_delete(request, osgroup_id):


# 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

    except EmptyPage:
        page = paginator.page(paginator.num_pages)

    return render(request,
                  'operatingsystems/osgroup_list.html',
                  {'page': page, 'terms': terms}, )


@login_required
def osgroup_detail(request, osgroup_id):

    osgroup = get_object_or_404(OSGroup, id=osgroup_id)

    if request.method == 'POST':
        repos_form = AddReposToOSGroupForm(request.POST, instance=osgroup)
        if repos_form.is_valid():
            repos_form.save()
            messages.info(request, 'Modified Repositories')
            return redirect(osgroup.get_absolute_url())

    repos_form = AddReposToOSGroupForm(instance=osgroup)

    return render(request,
                  'operatingsystems/osgroup_detail.html',
                  {'osgroup': osgroup, 'repos_form': repos_form}, )


@login_required
def osgroup_delete(request, osgroup_id):


    osgroup = get_object_or_404(OSGroup, id=osgroup_id)