# Path: cyclosible/playbook/models.py
# class Playbook(models.Model):
#     class Meta:
#         app_label = 'playbook'
#         permissions = (
#             ("view_playbook", "Can view the playbook"),
#             ("can_override_skip_tags", 'Can override skip_tags'),
#             ("can_override_only_tags", 'Can override only_tags'),
#             ("can_override_extra_vars", 'Can override extra_vars'),
#             ("can_override_subset", 'Can override subset'),
#             ("can_run_playbook", 'Can run the playbook')
#         )
# 
#     name = models.CharField(max_length=100, unique=True, db_index=True)
#     only_tags = models.CharField(max_length=1024, default='', blank=True)
#     skip_tags = models.CharField(max_length=1024, default='', blank=True)
#     extra_vars = models.CharField(max_length=1024, default='', blank=True)
#     subset = models.CharField(max_length=1024, default='', blank=True)
#     group = models.ForeignKey(Group, null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class PlaybookRunHistory(models.Model):
#     class Meta:
#         app_label = 'playbook'
# 
#     STATUS = (
#         ('SUCCESS', 'Success'),
#         ('RUNNING', 'Running'),
#         ('FAILURE', 'Failure'),
#     )
# 
#     playbook = models.ForeignKey(Playbook, db_index=True, related_name='history')
#     date_launched = models.DateTimeField(blank=True)
#     date_finished = models.DateTimeField(blank=True, null=True)
#     status = models.CharField(max_length=1024, default='RUNNING')
#     task_id = models.CharField(max_length=1024, default='', blank=True)
#     log_url = models.CharField(max_length=1024, default='', blank=True)
#     launched_by = models.ForeignKey(User)
# 
# Path: cyclosible/Cyclosible/serializers.py
# class GroupSerializerNoValidator(serializers.HyperlinkedModelSerializer):
#     name = serializers.SlugField(validators=[])
# 
#     class Meta:
#         model = Group
#         lookup_field = 'name'
#         fields = ('url', 'name')
#         extra_kwargs = {
#             'url': {'lookup_field': 'name'}
#         }


from .models import Playbook, PlaybookRunHistory
from django.contrib.auth.models import Group
from rest_framework import serializers
from ..Cyclosible.serializers import GroupSerializerNoValidator
from django.core.exceptions import ObjectDoesNotExist


class PlaybookRunHistorySerializer(serializers.HyperlinkedModelSerializer):
    playbook = serializers.SlugRelatedField(
        read_only=True,
        slug_field='name'
    )

    launched_by = serializers.SlugRelatedField(
        read_only=True,
        slug_field='username'
    )

    class Meta:
        model = PlaybookRunHistory
        fields = ('url', 'playbook', 'date_launched', 'date_finished', 'status', 'task_id', 'launched_by', 'log_url')


class FilteredHistorySerializer(serializers.ListSerializer):

    def to_representation(self, data):
        data = data.order_by('-date_launched')[:10]
        return super(FilteredHistorySerializer, self).to_representation(data)


class PlaybookRunHistoryLastSerializer(PlaybookRunHistorySerializer):
    class Meta:

# Path: cyclosible/playbook/models.py
# class Playbook(models.Model):
#     class Meta:
#         app_label = 'playbook'
#         permissions = (
#             ("view_playbook", "Can view the playbook"),
#             ("can_override_skip_tags", 'Can override skip_tags'),
#             ("can_override_only_tags", 'Can override only_tags'),
#             ("can_override_extra_vars", 'Can override extra_vars'),
#             ("can_override_subset", 'Can override subset'),
#             ("can_run_playbook", 'Can run the playbook')
#         )
# 
#     name = models.CharField(max_length=100, unique=True, db_index=True)
#     only_tags = models.CharField(max_length=1024, default='', blank=True)
#     skip_tags = models.CharField(max_length=1024, default='', blank=True)
#     extra_vars = models.CharField(max_length=1024, default='', blank=True)
#     subset = models.CharField(max_length=1024, default='', blank=True)
#     group = models.ForeignKey(Group, null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class PlaybookRunHistory(models.Model):
#     class Meta:
#         app_label = 'playbook'
# 
#     STATUS = (
#         ('SUCCESS', 'Success'),
#         ('RUNNING', 'Running'),
#         ('FAILURE', 'Failure'),
#     )
# 
#     playbook = models.ForeignKey(Playbook, db_index=True, related_name='history')
#     date_launched = models.DateTimeField(blank=True)
#     date_finished = models.DateTimeField(blank=True, null=True)
#     status = models.CharField(max_length=1024, default='RUNNING')
#     task_id = models.CharField(max_length=1024, default='', blank=True)
#     log_url = models.CharField(max_length=1024, default='', blank=True)
#     launched_by = models.ForeignKey(User)
# 
# Path: cyclosible/Cyclosible/serializers.py
# class GroupSerializerNoValidator(serializers.HyperlinkedModelSerializer):
#     name = serializers.SlugField(validators=[])
# 
#     class Meta:
#         model = Group
#         lookup_field = 'name'
#         fields = ('url', 'name')
#         extra_kwargs = {
#             'url': {'lookup_field': 'name'}
#         }


# Path: cyclosible/playbook/serializers.py
from .models import Playbook, PlaybookRunHistory
from django.contrib.auth.models import Group
from rest_framework import serializers
from ..Cyclosible.serializers import GroupSerializerNoValidator
from django.core.exceptions import ObjectDoesNotExist



class PlaybookRunHistorySerializer(serializers.HyperlinkedModelSerializer):
    playbook = serializers.SlugRelatedField(
        read_only=True,
        slug_field='name'
    )

    launched_by = serializers.SlugRelatedField(
        read_only=True,
        slug_field='username'
    )

    class Meta:
        model = PlaybookRunHistory
        fields = ('url', 'playbook', 'date_launched', 'date_finished', 'status', 'task_id', 'launched_by', 'log_url')


class FilteredHistorySerializer(serializers.ListSerializer):

    def to_representation(self, data):
        data = data.order_by('-date_launched')[:10]
        return super(FilteredHistorySerializer, self).to_representation(data)


class PlaybookRunHistoryLastSerializer(PlaybookRunHistorySerializer):
    class Meta:

        model = PlaybookRunHistory