# Path: rolf/rolf_main/models.py
# class Deployment(models.Model):
#     name = models.CharField(max_length=256, default="prod")
#     application = models.ForeignKey(Application)
#     deprecated = models.BooleanField(
#         default=False,
#         help_text=("Check this box if you don't want this deployment "
#                    "to show up by default."))
# 
#     class Meta:
#         order_with_respect_to = "application"
# 
#     def get_absolute_url(self):
#         return "/deployment/%d/" % self.id
# 
#     def __unicode__(self):
#         return self.name
# 
#     def all_categories(self):
#         return Category.objects.all()
# 
#     def new_push(self, user, comment=""):
#         return Push.objects.create(deployment=self, user=user, comment=comment)
# 
#     def env(self):
#         d = dict(DEPLOYMENT_ID=str(self.id))
#         for s in self.setting_set.all():
#             d[s.name] = s.value
#         return d
# 
#     def status(self):
#         if self.push_set.count() > 0:
#             return self.most_recent_push().status
#         else:
#             return "unknown"
# 
#     def last_message(self):
#         if self.push_set.count() > 0:
#             return self.most_recent_push().comment
#         else:
#             return None
# 
#     def most_recent_push(self):
#         return self.push_set.all()[0]
# 
#     def all_recipes(self):
#         """ helper to make this available in generic templates """
#         return Recipe.objects.all().exclude(name="")
# 
#     def add_editor(self, user):
#         for group in user.groups.all():
#             Permission.objects.create(deployment=self,
#                                       group=group,
#                                       capability="edit")
# 
#     def can_edit(self, user):
#         edit_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="edit")])
#         user_groups = set([g.id for g in user.groups.all()])
#         return not edit_groups.isdisjoint(user_groups)
# 
#     def can_push(self, user):
#         edit_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="edit")])
#         push_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="push")])
#         user_groups = set([g.id for g in user.groups.all()])
#         return not user_groups.isdisjoint(edit_groups | push_groups)
# 
#     def can_view(self, user):
#         user_groups = set([g.id for g in user.groups.all()])
#         edit_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="edit")])
#         push_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="push")])
#         view_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="view")])
#         return not user_groups.isdisjoint(edit_groups | push_groups |
#                                           view_groups)
# 
#     def add_permission_form(self, request_vars=None):
#         class AddPermissionForm(ModelForm):
#             class Meta:
#                 model = Permission
#                 exclude = ('deployment',)
# 
#         if request_vars:
#             return AddPermissionForm(request_vars)
#         else:
#             return AddPermissionForm()
# 
#     def add_flag_form(self, request_vars=None):
#         class AddFlagForm(ModelForm):
#             class Meta:
#                 model = Flag
#                 exclude = ('deployment',)
# 
#         if request_vars:
#             return AddFlagForm(request_vars)
#         else:
#             return AddFlagForm()
# 
#     def last_push_date(self):
#         if self.push_set.count() == 0:
#             return None
#         else:
#             return self.push_set.all()[0].start_time
# 
#     def clone_settings(self, new_deployment):
#         for setting in self.setting_set.all():
#             Setting.objects.create(deployment=new_deployment,
#                                    name=setting.name,
#                                    value=setting.value)
# 
#     def clone_stages(self, new_deployment):
#         for stage in self.stage_set.all():
#             recipe = stage.recipe
#             r = recipe
#             if recipe.name == "":
#                 # not a cookbook recipe, so we clone it
#                 r = Recipe.objects.create(name="",
#                                           language=recipe.language,
#                                           code=recipe.code)
#             Stage.objects.create(deployment=new_deployment,
#                                  name=stage.name, recipe=r)
# 
#     def clone_permissions(self, new_deployment):
#         for perm in self.permission_set.all():
#             Permission.objects.create(deployment=new_deployment,
#                                       group=perm.group,
#                                       capability=perm.capability)
# 
#     def clone_flags(self, new_deployment):
#         for flag in self.flag_set.all():
#             Flag.objects.create(deployment=new_deployment,
#                                 name=flag.name,
#                                 varname=flag.varname,
#                                 boolean=flag.boolean,
#                                 default=flag.default,
#                                 description=flag.description)
# 
#     def clear_old_pushes(self, keep=20):
#         deleted = 0
#         if self.push_set.all().count() < keep:
#             return deleted
#         for push in self.push_set.all().order_by(
#                 '-start_time')[keep:]:
#             push.delete()
#             deleted += 1
#         return deleted


from django.core.management.base import BaseCommand
from rolf.rolf_main.models import Deployment

KEEP = 20


class Command(BaseCommand):
    def handle(self, **options):
        count = Deployment.objects.all().count()
        idx = 1
        deleted = 0

# Path: rolf/rolf_main/models.py
# class Deployment(models.Model):
#     name = models.CharField(max_length=256, default="prod")
#     application = models.ForeignKey(Application)
#     deprecated = models.BooleanField(
#         default=False,
#         help_text=("Check this box if you don't want this deployment "
#                    "to show up by default."))
# 
#     class Meta:
#         order_with_respect_to = "application"
# 
#     def get_absolute_url(self):
#         return "/deployment/%d/" % self.id
# 
#     def __unicode__(self):
#         return self.name
# 
#     def all_categories(self):
#         return Category.objects.all()
# 
#     def new_push(self, user, comment=""):
#         return Push.objects.create(deployment=self, user=user, comment=comment)
# 
#     def env(self):
#         d = dict(DEPLOYMENT_ID=str(self.id))
#         for s in self.setting_set.all():
#             d[s.name] = s.value
#         return d
# 
#     def status(self):
#         if self.push_set.count() > 0:
#             return self.most_recent_push().status
#         else:
#             return "unknown"
# 
#     def last_message(self):
#         if self.push_set.count() > 0:
#             return self.most_recent_push().comment
#         else:
#             return None
# 
#     def most_recent_push(self):
#         return self.push_set.all()[0]
# 
#     def all_recipes(self):
#         """ helper to make this available in generic templates """
#         return Recipe.objects.all().exclude(name="")
# 
#     def add_editor(self, user):
#         for group in user.groups.all():
#             Permission.objects.create(deployment=self,
#                                       group=group,
#                                       capability="edit")
# 
#     def can_edit(self, user):
#         edit_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="edit")])
#         user_groups = set([g.id for g in user.groups.all()])
#         return not edit_groups.isdisjoint(user_groups)
# 
#     def can_push(self, user):
#         edit_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="edit")])
#         push_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="push")])
#         user_groups = set([g.id for g in user.groups.all()])
#         return not user_groups.isdisjoint(edit_groups | push_groups)
# 
#     def can_view(self, user):
#         user_groups = set([g.id for g in user.groups.all()])
#         edit_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="edit")])
#         push_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="push")])
#         view_groups = set([p.group.id for p
#                            in self.permission_set.filter(capability="view")])
#         return not user_groups.isdisjoint(edit_groups | push_groups |
#                                           view_groups)
# 
#     def add_permission_form(self, request_vars=None):
#         class AddPermissionForm(ModelForm):
#             class Meta:
#                 model = Permission
#                 exclude = ('deployment',)
# 
#         if request_vars:
#             return AddPermissionForm(request_vars)
#         else:
#             return AddPermissionForm()
# 
#     def add_flag_form(self, request_vars=None):
#         class AddFlagForm(ModelForm):
#             class Meta:
#                 model = Flag
#                 exclude = ('deployment',)
# 
#         if request_vars:
#             return AddFlagForm(request_vars)
#         else:
#             return AddFlagForm()
# 
#     def last_push_date(self):
#         if self.push_set.count() == 0:
#             return None
#         else:
#             return self.push_set.all()[0].start_time
# 
#     def clone_settings(self, new_deployment):
#         for setting in self.setting_set.all():
#             Setting.objects.create(deployment=new_deployment,
#                                    name=setting.name,
#                                    value=setting.value)
# 
#     def clone_stages(self, new_deployment):
#         for stage in self.stage_set.all():
#             recipe = stage.recipe
#             r = recipe
#             if recipe.name == "":
#                 # not a cookbook recipe, so we clone it
#                 r = Recipe.objects.create(name="",
#                                           language=recipe.language,
#                                           code=recipe.code)
#             Stage.objects.create(deployment=new_deployment,
#                                  name=stage.name, recipe=r)
# 
#     def clone_permissions(self, new_deployment):
#         for perm in self.permission_set.all():
#             Permission.objects.create(deployment=new_deployment,
#                                       group=perm.group,
#                                       capability=perm.capability)
# 
#     def clone_flags(self, new_deployment):
#         for flag in self.flag_set.all():
#             Flag.objects.create(deployment=new_deployment,
#                                 name=flag.name,
#                                 varname=flag.varname,
#                                 boolean=flag.boolean,
#                                 default=flag.default,
#                                 description=flag.description)
# 
#     def clear_old_pushes(self, keep=20):
#         deleted = 0
#         if self.push_set.all().count() < keep:
#             return deleted
#         for push in self.push_set.all().order_by(
#                 '-start_time')[keep:]:
#             push.delete()
#             deleted += 1
#         return deleted


# Path: rolf/rolf_main/management/commands/delete_old_logs.py
from django.core.management.base import BaseCommand
from rolf.rolf_main.models import Deployment


KEEP = 20


class Command(BaseCommand):
    def handle(self, **options):
        count = Deployment.objects.all().count()
        idx = 1
        deleted = 0

        for deployment in Deployment.objects.all():