django-simplekeys/simplekeys/management/commands/exportkeys.py

27 lines
828 B
Python
Raw Normal View History

2024-12-17 05:04:51 +00:00
import csv
from django.core.management.base import BaseCommand
from ...models import Key
class Command(BaseCommand):
help = 'Export API user information.'
def add_arguments(self, parser):
parser.add_argument('--since', dest='since', default=False,
help='only dump users since a given date')
def handle(self, *args, **options):
fields = ('key', 'status', 'tier', 'email', 'name', 'organization', 'usage', 'website',
'created_at', 'updated_at')
dw = csv.DictWriter(self.stdout, fields)
dw.writeheader()
qs = Key.objects.all()
if options['since']:
qs = qs.filter(created_at__gte=options['since'])
for key in qs.order_by('created_at'):
dw.writerow({f: getattr(key, f) for f in fields})