From bdbf21f75a1add92cf6d30ed7098dc5a9d51cbe4 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 4 Jun 2020 02:24:00 +0000 Subject: [PATCH] Add script to import old card scan counts --- apiserver/.gitignore | 1 + apiserver/apiserver/api/models.py | 4 +++ apiserver/import_card_scans.py | 43 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 apiserver/import_card_scans.py diff --git a/apiserver/.gitignore b/apiserver/.gitignore index cd3de99..9ba3c0e 100644 --- a/apiserver/.gitignore +++ b/apiserver/.gitignore @@ -114,3 +114,4 @@ missing_paypal/ backups/ secrets.py old_counts.csv +scans.csv diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index 983c8d9..449b797 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -141,6 +141,10 @@ class StatsSignupCount(models.Model): retain_count = models.IntegerField(default=0) vetted_count = models.IntegerField(default=0) +class StatsSpaceActivity(models.Model): + date = models.DateField(default=today_alberta_tz) + card_scans = models.IntegerField() + class HistoryIndex(models.Model): content_type = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL) object_id = models.PositiveIntegerField() diff --git a/apiserver/import_card_scans.py b/apiserver/import_card_scans.py new file mode 100644 index 0000000..9774eaf --- /dev/null +++ b/apiserver/import_card_scans.py @@ -0,0 +1,43 @@ +# Expects a scans.csv of the historical scans in format: +# date,card_number + +import django, sys, os +os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings' +django.setup() + +import csv +from datetime import datetime +from apiserver.api import models +from django.utils.timezone import now, pytz + +days = {} + +with open('scans.csv', newline='') as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + datetime_obj = datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S") + datetime_obj_utc = datetime_obj.replace(tzinfo=pytz.timezone('UTC')) + date = datetime_obj_utc.astimezone(pytz.timezone('America/Edmonton')) + + card = row['card_number'] + + print('Processing', date, card) + day = str(date.date()) + + if day not in days: + days[day] = set() + + days[day].add(card) + +print(days) + +for day, cards in days.items(): + print(day, len(cards)) + + models.StatsSpaceActivity.objects.update_or_create( + date=day, + defaults=dict(card_scans=len(cards)), + ) + +print('Done.') +