def createGenericExpenseItems(request=None, datetimeTuple=None, rule=None):
<EOL>submissionUser = getattr(request, '<STR_LIT:user>', None)<EOL>generate_count = <NUM_LIT:0><EOL>rule_filters = Q(disabled=False) & Q(rentalRate__gt=<NUM_LIT:0>) &Q(genericrepeatedexpense__isnull=False)<EOL>if rule:<EOL><INDENT>rule_filters = rule_filters & Q(id=rule.id)<EOL><DEDENT>rulesToCheck = RepeatedExpenseRule.objects.filter(rule_filters).distinct()<EOL>if datetimeTuple and len(datetimeTuple) == <NUM_LIT:2>:<EOL><INDENT>timelist = list(datetimeTuple)<EOL>timelist.sort()<EOL><DEDENT>else:<EOL><INDENT>timelist = None<EOL><DEDENT>for rule in rulesToCheck:<EOL><INDENT>limits = timelist or [ensure_timezone(datetime.min), ensure_timezone(datetime.max)]<EOL>if rule.advanceDays:<EOL><INDENT>limits[<NUM_LIT:1>] = min(limits[<NUM_LIT:1>], timezone.now() + timedelta(days=rule.advanceDays))<EOL><DEDENT>if rule.priorDays:<EOL><INDENT>limits[<NUM_LIT:0>] = max(limits[<NUM_LIT:0>], timezone.now() - timedelta(days=rule.priorDays))<EOL><DEDENT>if rule.startDate:<EOL><INDENT>limits[<NUM_LIT:0>] = max(<EOL>limits[<NUM_LIT:0>],<EOL>timezone.now().replace(<EOL>year=rule.startDate.year, month=rule.startDate.month, day=rule.startDate.day,<EOL>hour=<NUM_LIT:0>, minute=<NUM_LIT:0>, second=<NUM_LIT:0>, microsecond=<NUM_LIT:0>,<EOL>)<EOL>)<EOL><DEDENT>if rule.endDate:<EOL><INDENT>limits[<NUM_LIT:1>] = min(<EOL>limits[<NUM_LIT:1>],<EOL>timezone.now().replace(<EOL>year=rule.endDate.year, month=rule.endDate.month, day=rule.endDate.day,<EOL>hour=<NUM_LIT:0>, minute=<NUM_LIT:0>, second=<NUM_LIT:0>, microsecond=<NUM_LIT:0>,<EOL>)<EOL>)<EOL><DEDENT>if rule.applyRateRule == RepeatedExpenseRule.RateRuleChoices.hourly:<EOL><INDENT>this_time = limits[<NUM_LIT:0>].replace(minute=<NUM_LIT:0>, second=<NUM_LIT:0>, microsecond=<NUM_LIT:0>)<EOL>if this_time < limits[<NUM_LIT:0>]:<EOL><INDENT>this_time += timedelta(hours=<NUM_LIT:1>)<EOL><DEDENT><DEDENT>elif rule.applyRateRule == RepeatedExpenseRule.RateRuleChoices.daily:<EOL><INDENT>this_time = limits[<NUM_LIT:0>].replace(hour=rule.dayStarts, minute=<NUM_LIT:0>, second=<NUM_LIT:0>, microsecond=<NUM_LIT:0>)<EOL>if this_time < limits[<NUM_LIT:0>]:<EOL><INDENT>this_time += timedelta(days=<NUM_LIT:1>)<EOL><DEDENT><DEDENT>elif rule.applyRateRule == RepeatedExpenseRule.RateRuleChoices.weekly:<EOL><INDENT>offset = limits[<NUM_LIT:0>].weekday() - rule.weekStarts<EOL>this_time = limits[<NUM_LIT:0>].replace(<EOL>day=limits[<NUM_LIT:0>].day - offset, hour=rule.dayStarts, minute=<NUM_LIT:0>, second=<NUM_LIT:0>, microsecond=<NUM_LIT:0><EOL>)<EOL>if this_time < limits[<NUM_LIT:0>]:<EOL><INDENT>this_time += timedelta(days=<NUM_LIT:7>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>this_time = limits[<NUM_LIT:0>].replace(<EOL>day=rule.monthStarts, hour=rule.dayStarts, minute=<NUM_LIT:0>, second=<NUM_LIT:0>, microsecond=<NUM_LIT:0><EOL>)<EOL>if this_time < limits[<NUM_LIT:0>]:<EOL><INDENT>this_time += relativedelta(months=<NUM_LIT:1>)<EOL><DEDENT><DEDENT>while this_time <= limits[<NUM_LIT:1>]:<EOL><INDENT>defaults_dict = {<EOL>'<STR_LIT>': rule.category,<EOL>'<STR_LIT:description>': rule.name,<EOL>'<STR_LIT>': submissionUser,<EOL>'<STR_LIT>': rule.rentalRate,<EOL>'<STR_LIT>': this_time,<EOL>'<STR_LIT>': rule.payTo,<EOL>}<EOL>item, created = ExpenseItem.objects.get_or_create(<EOL>expenseRule=rule,<EOL>periodStart=this_time,<EOL>periodEnd=this_time,<EOL>defaults=defaults_dict<EOL>)<EOL>if created:<EOL><INDENT>generate_count += <NUM_LIT:1><EOL><DEDENT>if rule.applyRateRule == RepeatedExpenseRule.RateRuleChoices.hourly:<EOL><INDENT>this_time += timedelta(hours=<NUM_LIT:1>)<EOL><DEDENT>elif rule.applyRateRule == RepeatedExpenseRule.RateRuleChoices.daily:<EOL><INDENT>this_time += timedelta(days=<NUM_LIT:1>)<EOL><DEDENT>elif rule.applyRateRule == RepeatedExpenseRule.RateRuleChoices.weekly:<EOL><INDENT>this_time += timedelta(days=<NUM_LIT:7>)<EOL><DEDENT>else:<EOL><INDENT>this_time += relativedelta(months=<NUM_LIT:1>)<EOL><DEDENT><DEDENT><DEDENT>rulesToCheck.update(lastRun=timezone.now())<EOL>return generate_count<EOL>
Generic repeated expenses are created by just entering an
expense at each exact point specified by the rule, without
regard for whether events are scheduled in the specified
window,