Skip to content

Commit

Permalink
Merge pull request #190 from matematikk-mooc/KURSP-616-refactor-to-dj…
Browse files Browse the repository at this point in the history
…ango-orm

KURSP-616: refactor statistics api
  • Loading branch information
theahthodesen authored Mar 25, 2024
2 parents 6de3ad0 + a42cc75 commit 82fdf78
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@

from statistics_api.course_info.models import School
from django.utils import timezone
from datetime import datetime

from statistics_api.definitions import ROOT_DIR
from statistics_api.utils.utils import parse_year_from_data_file_name, get_primary_school_data_file_paths


logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
logger = logging.getLogger()
class Command(BaseCommand):
help = "Imports a CSV file with school teacher counts, downloaded from Skoleporten Rapportbygger to the database"

def handle(self, *args, **options):
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
logger = logging.getLogger()
logger.info("Starting importing primary school teacher counts from CSV file...")
primary_school_data_file_paths = get_primary_school_data_file_paths(f"{ROOT_DIR}data/")

organization_numbers_and_teacher_counts: List[Tuple[str, int]] = []
for csv_file_path in primary_school_data_file_paths:
organization_numbers_and_teacher_counts: List[Tuple[str, int]] = []

logger.info(f"Opening file {csv_file_path}...")
with open(csv_file_path, encoding='utf-8') as csvfile:
row_iterator = csv.reader(csvfile, delimiter="\t")
Expand All @@ -36,20 +34,20 @@ def handle(self, *args, **options):
number_of_teachers = number_of_teachers.replace(" ", "")
organization_numbers_and_teacher_counts.append((organizational_number, int(number_of_teachers)))

year_of_data = parse_year_from_data_file_name(csv_file_path)
self.insert_schools(organization_numbers_and_teacher_counts, year_of_data)
logger.info(f"Finished importing primary school teacher counts from CSV file.")
year_of_data = parse_year_from_data_file_name(csv_file_path)
self.insert_schools(organization_numbers_and_teacher_counts, year_of_data)
logger.info(f"Finished importing primary school teacher counts from CSV file.")



def insert_schools(self, organization_numbers_and_teacher_counts: List[Tuple[str, int]], year_of_data: int) -> int:

logger.info(len(organization_numbers_and_teacher_counts))
for organizational_number, teacher_count in organization_numbers_and_teacher_counts:
obj, created = School.objects.update_or_create(
organization_number=organizational_number,
year=year_of_data,
defaults={
'number_of_teachers': teacher_count,
'updated_date': timezone.now(),
'updated_date': str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
}
)

0 comments on commit 82fdf78

Please sign in to comment.