# 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
        extra_kwargs = {
            'url': {'lookup_field': 'name'}
        }

    def create(self, validated_data):
        group_data = validated_data.pop('group')
        try:
            group = Group.objects.get(name=group_data.get('name'))
        except Group.DoesNotExist:
            group = Group.objects.create(**group_data)
        playbook = Playbook.objects.create(group=group, **validated_data)
        return playbook

    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.only_tags = validated_data.get('only_tags', instance.only_tags)
        instance.skip_tags = validated_data.get('skip_tags', instance.skip_tags)
        instance.extra_vars = validated_data.get('extra_vars', instance.extra_vars)
        instance.subset = validated_data.get('subset', instance.subset)
        group_data = validated_data.get('group', {'name': instance.group.name})
        try:
            group = Group.objects.get(name=group_data.get('name'))
            instance.group = group
        except Group.DoesNotExist:
            raise ObjectDoesNotExist
        return instance


class RunPlaybookSerializer(serializers.HyperlinkedModelSerializer):
    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

        extra_kwargs = {
            'url': {'lookup_field': 'name'}
        }

    def create(self, validated_data):
        group_data = validated_data.pop('group')
        try:
            group = Group.objects.get(name=group_data.get('name'))
        except Group.DoesNotExist:
            group = Group.objects.create(**group_data)
        playbook = Playbook.objects.create(group=group, **validated_data)
        return playbook

    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.only_tags = validated_data.get('only_tags', instance.only_tags)
        instance.skip_tags = validated_data.get('skip_tags', instance.skip_tags)
        instance.extra_vars = validated_data.get('extra_vars', instance.extra_vars)
        instance.subset = validated_data.get('subset', instance.subset)
        group_data = validated_data.get('group', {'name': instance.group.name})
        try:
            group = Group.objects.get(name=group_data.get('name'))
            instance.group = group
        except Group.DoesNotExist:
            raise ObjectDoesNotExist
        return instance


class RunPlaybookSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:

        model = Playbook