def notify_expiration(): """ From the configured models, find items that will expire within the configured time period and notify whomever is onfigured for that model """ for app_model, config in EXPIRATION.items(): app, model_name = app_model.split('.') func = config['notify_func'] if isinstance(func, basestring): func = get_class(func) end = datetime.timedelta(days=config['notify_lead_time']) start = datetime.timedelta(days=config['notify_lead_time'] - config['notify_time_delta']) for license in GrantedLicense.objects.filter( agreement_expires__gt=datetime.date.today() + start, agreement_expires__lte=datetime.date.today() + end ).exclude( content_expiration=1 ): if license.content_expiration == 2: # Expires with agreement, so all related items are gathered objs = func(getattr(license, config['related_name']).all()) elif license.content_expiration == 3: # Expires after duration, so need the date_field if not license.content_expires_after: license.content_expires_after = 0 tdelta = datetime.datetime.now() - datetime.timedelta(days=license.content_expires_after) lookup = {'%s__lt' % config['date_field']: tdelta} objs = func(getattr(license, config['related_name']).filter(**lookup)) notify(config['notify_recipients'], objs, license)
def expire_items(): """ From the configured models, find expired items and execute the expire function """ for app_model, config in EXPIRATION.items(): app, model_name = app_model.split('.') func = config['expire_func'] if isinstance(func, basestring): func = get_class(func) for license in GrantedLicense.objects.filter( agreement_expires__lt=datetime.datetime.now() ).exclude( content_expiration=1 ): if license.content_expiration == 2: # Expires with agreement, so all related items are gathered objs = getattr(license, config['related_name']).all() func(objs) elif license.content_expiration == 3: # Expires after duration, so need the date_field if not license.content_expires_after: license.content_expires_after = 0 tdelta = datetime.date.today() - datetime.timedelta(days=license.content_expires_after) lookup = {'%s__lt' % config['date_field']: tdelta} objs = getattr(license, config['related_name']).filter(**lookup) func(objs)