# Path: feature_toggle/models.py
# class FeatureToggleAttribute(TimeStampedModel):
#     feature_toggle = models.ForeignKey(FeatureToggle, related_name='attributes', on_delete=models.CASCADE)
#     key = models.CharField(max_length=ATTRIB_KEY_MAX_LENGTH, choices=ATTRIB_CHOICES)
#     value = models.TextField(max_length=ATTRIB_VAL_MAX_LENGTH, null=True, blank=True)
# 
#     class Meta:
#         unique_together = ('feature_toggle', 'key')
#         managed = False
# 
#     def __str__(self):
#         return "{ft}: {k} - {v}".format(ft=str(self.feature_toggle), k=self.key, v=self.value)
# 
#     def __unicode__(self):
#         return self.__str__()
# 
# class FeatureToggle(TimeStampedModel):
#     uid = models.CharField(max_length=20)
#     name = models.CharField(max_length=20)
#     environment = models.CharField(max_length=50, choices=ENV_CHOICES)
#     is_active = models.BooleanField(default=True)
#     start_date_time = models.DateTimeField(auto_now_add=True)
#     end_date_time = models.DateTimeField(auto_now_add=True)  # todo: add 3 months from now by default
#     time_bomb = models.BooleanField(default=False)
# 
#     class Meta:
#         unique_together = (('name', 'environment'), ('uid',))
#         managed = True
# 
#     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
#         if not self.uid:
#             self.uid = make_meanigful_id(self.name, length=5)
#         super().save(force_insert, force_update, using, update_fields)
# 
#     def set_attribute(self, key, value=None, update_if_existing=True):
#         attrib, created = self.attributes.get_or_create(key=key)
#         if not created and not update_if_existing:
#             raise FeatureToggleAttributeAlreadyExists(**dict(key=key))
# 
#         attrib.value = value
#         attrib.save()
# 
#     def __str__(self):
#         return "{e}: {n}({u})".format(e=self.environment, n=self.name, u=self.uid)
# 
#     def __unicode__(self):
#         return self.__str__()


from django.contrib import admin
from .models import FeatureToggleAttribute, FeatureToggle
# -*- coding: utf-8 -*-





class FeatureToggleAttributesAdminInline(admin.TabularInline):
    model = FeatureToggleAttribute
    extra = 0
    min_num = 1


class FeatureToggleAdmin(admin.ModelAdmin):
    inlines = [FeatureToggleAttributesAdminInline]

    def get_readonly_fields(self, request, obj=None):
        readonly_fields = super().get_readonly_fields(request, obj)
        if obj:
            readonly_fields += ('uid',)
        return readonly_fields



# Path: feature_toggle/models.py
# class FeatureToggleAttribute(TimeStampedModel):
#     feature_toggle = models.ForeignKey(FeatureToggle, related_name='attributes', on_delete=models.CASCADE)
#     key = models.CharField(max_length=ATTRIB_KEY_MAX_LENGTH, choices=ATTRIB_CHOICES)
#     value = models.TextField(max_length=ATTRIB_VAL_MAX_LENGTH, null=True, blank=True)
# 
#     class Meta:
#         unique_together = ('feature_toggle', 'key')
#         managed = False
# 
#     def __str__(self):
#         return "{ft}: {k} - {v}".format(ft=str(self.feature_toggle), k=self.key, v=self.value)
# 
#     def __unicode__(self):
#         return self.__str__()
# 
# class FeatureToggle(TimeStampedModel):
#     uid = models.CharField(max_length=20)
#     name = models.CharField(max_length=20)
#     environment = models.CharField(max_length=50, choices=ENV_CHOICES)
#     is_active = models.BooleanField(default=True)
#     start_date_time = models.DateTimeField(auto_now_add=True)
#     end_date_time = models.DateTimeField(auto_now_add=True)  # todo: add 3 months from now by default
#     time_bomb = models.BooleanField(default=False)
# 
#     class Meta:
#         unique_together = (('name', 'environment'), ('uid',))
#         managed = True
# 
#     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
#         if not self.uid:
#             self.uid = make_meanigful_id(self.name, length=5)
#         super().save(force_insert, force_update, using, update_fields)
# 
#     def set_attribute(self, key, value=None, update_if_existing=True):
#         attrib, created = self.attributes.get_or_create(key=key)
#         if not created and not update_if_existing:
#             raise FeatureToggleAttributeAlreadyExists(**dict(key=key))
# 
#         attrib.value = value
#         attrib.save()
# 
#     def __str__(self):
#         return "{e}: {n}({u})".format(e=self.environment, n=self.name, u=self.uid)
# 
#     def __unicode__(self):
#         return self.__str__()


# Path: feature_toggle/admin.py
from django.contrib import admin
from .models import FeatureToggleAttribute, FeatureToggle

# -*- coding: utf-8 -*-





class FeatureToggleAttributesAdminInline(admin.TabularInline):
    model = FeatureToggleAttribute
    extra = 0
    min_num = 1


class FeatureToggleAdmin(admin.ModelAdmin):
    inlines = [FeatureToggleAttributesAdminInline]

    def get_readonly_fields(self, request, obj=None):
        readonly_fields = super().get_readonly_fields(request, obj)
        if obj:
            readonly_fields += ('uid',)
        return readonly_fields



admin.site.register(FeatureToggle, FeatureToggleAdmin)