Add script to import old card scan counts

This commit is contained in:
Tanner Collin 2020-06-04 02:24:00 +00:00
parent ea3f1fc6e4
commit bdbf21f75a
3 changed files with 48 additions and 0 deletions

View File

@ -114,3 +114,4 @@ missing_paypal/
backups/ backups/
secrets.py secrets.py
old_counts.csv old_counts.csv
scans.csv

View File

@ -141,6 +141,10 @@ class StatsSignupCount(models.Model):
retain_count = models.IntegerField(default=0) retain_count = models.IntegerField(default=0)
vetted_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): class HistoryIndex(models.Model):
content_type = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL) content_type = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL)
object_id = models.PositiveIntegerField() object_id = models.PositiveIntegerField()

View File

@ -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.')