# 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: repos/models.py
# class Repository(models.Model):
# 
#     RPM = 'R'
#     DEB = 'D'
#     ARCH = 'A'
# 
#     REPO_TYPES = (
#         (RPM, 'rpm'),
#         (DEB, 'deb'),
#         (ARCH, 'arch'),
#     )
# 
#     name = models.CharField(max_length=255, unique=True)
#     arch = models.ForeignKey(MachineArchitecture, on_delete=models.CASCADE)
#     security = models.BooleanField(default=False)
#     repotype = models.CharField(max_length=1, choices=REPO_TYPES)
#     enabled = models.BooleanField(default=True)
#     repo_id = models.CharField(max_length=255, null=True, blank=True)
#     auth_required = models.BooleanField(default=False)
# 
#     class Meta(object):
#         verbose_name_plural = 'Repository'
#         verbose_name_plural = 'Repositories'
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('repos:repo_detail', args=[str(self.id)])
# 
#     def show(self):
#         """ Show info about this repo, including mirrors
#         """
#         text = '{0!s} : {1!s}\n'.format(self.id, self.name)
#         text += 'security: {0!s}    '.format(self.security)
#         text += 'arch: {0!s}\n'.format(self.arch)
#         text += 'Mirrors:'
# 
#         info_message.send(sender=None, text=text)
# 
#         for mirror in self.mirror_set.all():
#             mirror.show()
# 
#     def refresh(self, force=False):
#         """ Refresh all of a repos mirror metadata,
#             force can be set to force a reset of all the mirrors metadata
#         """
# 
#         if force:
#             for mirror in self.mirror_set.all():
#                 mirror.file_checksum = None
#                 mirror.save()
# 
#         if not self.auth_required:
#             if self.repotype == Repository.DEB:
#                 refresh_deb_repo(self)
#             elif self.repotype == Repository.RPM:
#                 refresh_rpm_repo(self)
#             elif self.repotype == Repository.ARCH:
#                 refresh_arch_repo(self)
#             else:
#                 text = 'Error: unknown repo type for repo '
#                 text += '{0!s}: {1!s}'.format(self.id, self.repotype)
#                 error_message.send(sender=None, text=text)
#         else:
#             text = 'Repo requires certificate authentication, not updating'
#             warning_message.send(sender=None, text=text)
# 
#     def disable(self):
#         """ Disable a repo. This involves disabling each mirror, which stops it
#             being considered for package updates, and disabling refresh for
#             each mirror so that it doesn't try to update its package metadata.
#         """
# 
#         self.enabled = False
#         for mirror in self.mirror_set.all():
#             mirror.enabled = False
#             mirror.refresh = False
#             mirror.save()
# 
#     def enable(self):
#         """ Enable a repo. This involves enabling each mirror, which allows it
#             to be considered for package updates, and enabling refresh for each
#             mirror so that it updates its package metadata.
#         """
# 
#         self.enabled = True
#         for mirror in self.mirror_set.all():
#             mirror.enabled = True
#             mirror.refresh = True
#             mirror.save()


from django.forms import ModelForm, ModelMultipleChoiceField
from django.contrib.admin.widgets import FilteredSelectMultiple
from operatingsystems.models import OS, OSGroup
from repos.models import Repository

    class Meta(object):
        model = OS
        fields = ('osgroup',)


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',)


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):

# 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: repos/models.py
# class Repository(models.Model):
# 
#     RPM = 'R'
#     DEB = 'D'
#     ARCH = 'A'
# 
#     REPO_TYPES = (
#         (RPM, 'rpm'),
#         (DEB, 'deb'),
#         (ARCH, 'arch'),
#     )
# 
#     name = models.CharField(max_length=255, unique=True)
#     arch = models.ForeignKey(MachineArchitecture, on_delete=models.CASCADE)
#     security = models.BooleanField(default=False)
#     repotype = models.CharField(max_length=1, choices=REPO_TYPES)
#     enabled = models.BooleanField(default=True)
#     repo_id = models.CharField(max_length=255, null=True, blank=True)
#     auth_required = models.BooleanField(default=False)
# 
#     class Meta(object):
#         verbose_name_plural = 'Repository'
#         verbose_name_plural = 'Repositories'
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('repos:repo_detail', args=[str(self.id)])
# 
#     def show(self):
#         """ Show info about this repo, including mirrors
#         """
#         text = '{0!s} : {1!s}\n'.format(self.id, self.name)
#         text += 'security: {0!s}    '.format(self.security)
#         text += 'arch: {0!s}\n'.format(self.arch)
#         text += 'Mirrors:'
# 
#         info_message.send(sender=None, text=text)
# 
#         for mirror in self.mirror_set.all():
#             mirror.show()
# 
#     def refresh(self, force=False):
#         """ Refresh all of a repos mirror metadata,
#             force can be set to force a reset of all the mirrors metadata
#         """
# 
#         if force:
#             for mirror in self.mirror_set.all():
#                 mirror.file_checksum = None
#                 mirror.save()
# 
#         if not self.auth_required:
#             if self.repotype == Repository.DEB:
#                 refresh_deb_repo(self)
#             elif self.repotype == Repository.RPM:
#                 refresh_rpm_repo(self)
#             elif self.repotype == Repository.ARCH:
#                 refresh_arch_repo(self)
#             else:
#                 text = 'Error: unknown repo type for repo '
#                 text += '{0!s}: {1!s}'.format(self.id, self.repotype)
#                 error_message.send(sender=None, text=text)
#         else:
#             text = 'Repo requires certificate authentication, not updating'
#             warning_message.send(sender=None, text=text)
# 
#     def disable(self):
#         """ Disable a repo. This involves disabling each mirror, which stops it
#             being considered for package updates, and disabling refresh for
#             each mirror so that it doesn't try to update its package metadata.
#         """
# 
#         self.enabled = False
#         for mirror in self.mirror_set.all():
#             mirror.enabled = False
#             mirror.refresh = False
#             mirror.save()
# 
#     def enable(self):
#         """ Enable a repo. This involves enabling each mirror, which allows it
#             to be considered for package updates, and enabling refresh for each
#             mirror so that it updates its package metadata.
#         """
# 
#         self.enabled = True
#         for mirror in self.mirror_set.all():
#             mirror.enabled = True
#             mirror.refresh = True
#             mirror.save()


# Path: operatingsystems/forms.py
from django.forms import ModelForm, ModelMultipleChoiceField
from django.contrib.admin.widgets import FilteredSelectMultiple
from operatingsystems.models import OS, OSGroup
from repos.models import Repository


    class Meta(object):
        model = OS
        fields = ('osgroup',)


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',)


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