# 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
                  {'page': page, 'terms': terms, 'empty_oses': empty_oses}, )


@login_required
def os_detail(request, os_id):

    os = get_object_or_404(OS, id=os_id)

    if request.method == 'POST':
        create_form = CreateOSGroupForm(request.POST, prefix='create')
        if create_form.is_valid():
            osgroup = create_form.save()
            os.osgroup = osgroup
            os.save()
            text = 'Created OS Group {0!s} '.format(osgroup)
            text += 'and added OS {0!s} to it'.format(os)
            messages.info(request, text)
            return redirect(os.get_absolute_url())
        add_form = AddOSToOSGroupForm(request.POST, instance=os, prefix='add')
        if add_form.is_valid():
            add_form.save()
            text = 'OS {0!s} added to OS Group {1!s}'.format(os, os.osgroup)
            messages.info(request, text)
            return redirect(os.get_absolute_url())
    else:
        add_form = AddOSToOSGroupForm(instance=os, prefix='add')
        create_form = CreateOSGroupForm(prefix='create')

    return render(request,
                  'operatingsystems/os_detail.html',

# 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

                  {'page': page, 'terms': terms, 'empty_oses': empty_oses}, )


@login_required
def os_detail(request, os_id):

    os = get_object_or_404(OS, id=os_id)

    if request.method == 'POST':
        create_form = CreateOSGroupForm(request.POST, prefix='create')
        if create_form.is_valid():
            osgroup = create_form.save()
            os.osgroup = osgroup
            os.save()
            text = 'Created OS Group {0!s} '.format(osgroup)
            text += 'and added OS {0!s} to it'.format(os)
            messages.info(request, text)
            return redirect(os.get_absolute_url())
        add_form = AddOSToOSGroupForm(request.POST, instance=os, prefix='add')
        if add_form.is_valid():
            add_form.save()
            text = 'OS {0!s} added to OS Group {1!s}'.format(os, os.osgroup)
            messages.info(request, text)
            return redirect(os.get_absolute_url())
    else:
        add_form = AddOSToOSGroupForm(instance=os, prefix='add')
        create_form = CreateOSGroupForm(prefix='create')

    return render(request,
                  'operatingsystems/os_detail.html',

                  {'os': os,