# Path: scm/models.py
# class Installation(models.Model):
#     installed_set = models.ForeignKey(InstallableSet, verbose_name='livraison appliquée ', on_delete=models.CASCADE)
#     asked_in_ticket = models.CharField(max_length=10, verbose_name='ticket lié ', blank=True, null=True)
#     install_date = models.DateTimeField(verbose_name='date d\installation ')
# 
#     def __str__(self):
#         return '%s sur %s  le %s' % (self.installed_set.name, [i.component_instance.environments.all() for i in self.modified_components.all()], self.install_date)
# 
# class ComponentInstanceConfiguration(models.Model):
#     component_instance = models.ForeignKey(ComponentInstance, related_name='configurations', on_delete=models.CASCADE)
#     result_of = models.ForeignKey(InstallableItem, on_delete=models.CASCADE)
#     part_of_installation = models.ForeignKey(Installation, related_name='modified_components', on_delete=models.CASCADE)
#     created_on = models.DateTimeField()
#     install_failure = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return u'At %s, component %s was at version %s' % (self.created_on, self.component_instance.name, self.result_of.what_is_installed.version)
# 
# Path: ref/models/parameters.py
# def getParam(key, **others):
#     """
#         Retrieves a parameter.
#         This function hits the database, so it should be called as little as possible.
#         
#         @return: the parameter value as a string (unicode).
#         @raise ParamNotFound: if the param cannot be found, or if multiple params were found. 
#     """
#     if others and 'app' in others: app = others['app']
#     else: app = sys._getframe(1).f_globals['__name__'].split('.')[0]
#     filters = others or {}
#     filters['app'] = app
#     filters['key'] = key
# 
#     try:
#         return MageParam.objects.get(**filters).value
#     except (MageParam.DoesNotExist, MageParam.MultipleObjectsReturned):
#         raise ParamNotFound(filters)


from datetime import timedelta
from django.utils.timezone import now
from scm.exceptions import MageScmError, MageScmCallerError,\
    MageScmFailedEnvironmentDependencyCheck
from scm.models import Installation, ComponentInstanceConfiguration
from ref.models.parameters import getParam
        for ii in ii_list:
            install_detail.append((compo, ii))
            
    ## Registration
    i = Installation(installed_set = iset, install_date = install_date, asked_in_ticket = ticket)
    i.save()
    
    for duet in install_detail:
        compo = duet[0]
        ii = duet[1]
        cic = ComponentInstanceConfiguration(component_instance = compo, result_of = ii, part_of_installation = i, created_on = install_date)
        cic.save() 
        
def install_ii_single_target_envt(ii, instance, envt, force_prereqs = False, install_date = None, ticket = None, install = None):
    iset = ii.belongs_to_set
    if install_date is None:
        install_date = now()
    
    ## Check prerequisites
    try:
        iset.check_prerequisites(envt.name)
    except MageScmFailedEnvironmentDependencyCheck as e:
        if not force_prereqs:
            raise e

    if install is None:
        ## Check if an install is in progress
        limit = int(getParam('APPLY_MERGE_LIMIT'))
        tlimit1 = install_date - timedelta(minutes=limit)
        tlimit2 = install_date + timedelta(minutes=limit)

# Path: scm/models.py
# class Installation(models.Model):
#     installed_set = models.ForeignKey(InstallableSet, verbose_name='livraison appliquée ', on_delete=models.CASCADE)
#     asked_in_ticket = models.CharField(max_length=10, verbose_name='ticket lié ', blank=True, null=True)
#     install_date = models.DateTimeField(verbose_name='date d\installation ')
# 
#     def __str__(self):
#         return '%s sur %s  le %s' % (self.installed_set.name, [i.component_instance.environments.all() for i in self.modified_components.all()], self.install_date)
# 
# class ComponentInstanceConfiguration(models.Model):
#     component_instance = models.ForeignKey(ComponentInstance, related_name='configurations', on_delete=models.CASCADE)
#     result_of = models.ForeignKey(InstallableItem, on_delete=models.CASCADE)
#     part_of_installation = models.ForeignKey(Installation, related_name='modified_components', on_delete=models.CASCADE)
#     created_on = models.DateTimeField()
#     install_failure = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return u'At %s, component %s was at version %s' % (self.created_on, self.component_instance.name, self.result_of.what_is_installed.version)
# 
# Path: ref/models/parameters.py
# def getParam(key, **others):
#     """
#         Retrieves a parameter.
#         This function hits the database, so it should be called as little as possible.
#         
#         @return: the parameter value as a string (unicode).
#         @raise ParamNotFound: if the param cannot be found, or if multiple params were found. 
#     """
#     if others and 'app' in others: app = others['app']
#     else: app = sys._getframe(1).f_globals['__name__'].split('.')[0]
#     filters = others or {}
#     filters['app'] = app
#     filters['key'] = key
# 
#     try:
#         return MageParam.objects.get(**filters).value
#     except (MageParam.DoesNotExist, MageParam.MultipleObjectsReturned):
#         raise ParamNotFound(filters)


# Path: scm/install.py
from datetime import timedelta
from django.utils.timezone import now
from scm.exceptions import MageScmError, MageScmCallerError,\
    MageScmFailedEnvironmentDependencyCheck
from scm.models import Installation, ComponentInstanceConfiguration
from ref.models.parameters import getParam

        for ii in ii_list:
            install_detail.append((compo, ii))
            
    ## Registration
    i = Installation(installed_set = iset, install_date = install_date, asked_in_ticket = ticket)
    i.save()
    
    for duet in install_detail:
        compo = duet[0]
        ii = duet[1]
        cic = ComponentInstanceConfiguration(component_instance = compo, result_of = ii, part_of_installation = i, created_on = install_date)
        cic.save() 
        
def install_ii_single_target_envt(ii, instance, envt, force_prereqs = False, install_date = None, ticket = None, install = None):
    iset = ii.belongs_to_set
    if install_date is None:
        install_date = now()
    
    ## Check prerequisites
    try:
        iset.check_prerequisites(envt.name)
    except MageScmFailedEnvironmentDependencyCheck as e:
        if not force_prereqs:
            raise e

    if install is None:
        ## Check if an install is in progress
        limit = int(getParam('APPLY_MERGE_LIMIT'))
        tlimit1 = install_date - timedelta(minutes=limit)
        tlimit2 = install_date + timedelta(minutes=limit)

        installs = Installation.objects.filter(installed_set = iset, modified_components__component_instance__environments = envt, 