uncloud-mravi/uncloud_pay/management/commands/generate-bills.py

36 lines
1,000 B
Python
Raw Normal View History

2020-02-29 08:08:30 +00:00
import logging
from django.core.management.base import BaseCommand
from uncloud_auth.models import User
from uncloud_pay.models import Order, Bill
from django.core.exceptions import ObjectDoesNotExist
2020-02-29 08:08:30 +00:00
from datetime import timedelta, date
from django.utils import timezone
from uncloud_pay.models import Bill
2020-02-29 08:08:30 +00:00
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Generate bills and charge customers if necessary.'
def add_arguments(self, parser):
pass
2020-02-29 08:08:30 +00:00
# TODO: use logger.*
def handle(self, *args, **options):
2020-02-29 08:08:30 +00:00
# Iterate over all 'active' users.
# TODO: filter out inactive users.
users = User.objects.all()
print("Processing {} users.".format(users.count()))
for user in users:
2020-02-29 08:08:30 +00:00
now = timezone.now()
Bill.generate_for(
2020-02-29 08:08:30 +00:00
year=now.year,
month=now.month,
2020-03-05 10:27:43 +00:00
user=user)
# We're done for this round :-)
print("=> Done.")