I’m applying for my first position as a Python/Django developer, even my first position in tech, after having had a career in a different industry.
For the application I’m asked to create a small Django app that takes it data from external csv files. For this I have written a custom command that gets run by a cronjob periodically:
from django.core.management.base import BaseCommand import requests from requests.auth import HTTPBasicAuth import csv from hotels.models import Cities, Hotels """ This command handles the logic for importing the csv files into the database. It connects to the csv url's by using HHTPBasicAuth abd parses to a csv reader. It then iterates over the reader and creates a model instance for each row in the csv. """ class Command(BaseCommand): help = 'Imports data from CSV files through a url' def handle(self, *args, **kwargs): csv_city = 'http://xxxxxx/djangocase/city.csv' csv_hotel = 'http://xxxxxxl/djangocase/hotel.csv' credentials = HTTPBasicAuth('xxxxxx', 'xxxxxx') # check if we are able to establish a HTTP connection response = [ requests.get(csv_city, auth=credentials), requests.get(csv_hotel, auth=credentials) ] def create_csvreader_object(response_index): data = response[response_index] lines = data.text.splitlines() reader = csv.reader(lines, delimiter=';',) return reader if all(connection.status_code == 200 for connection in response): # clear the databases before importing the csv data Cities.objects.all().delete() Hotels.objects.all().delete() # create instances for the Cities model in the database id_counter = 1 for row in create_csvreader_object(0): Cities.objects.create( id = id_counter, city_abbreviation = row, city_name = row ) id_counter += 1 """ To create the instances in the Hotel model we need to get all objects in the Cities model so we can match the city_abbreviation in the Cities model to the corresponding field in the Hotel model in order to establish a Foreign relationship. """ id_counter = 1 all_cities = Cities.objects.all() for row in create_csvreader_object(1): for city in all_cities: if city.city_abbreviation == row: Hotels.objects.create( id = id_counter, city = city, city_abbreviation = row, hotel_id = row, hotel_name = row ) id_counter += 1 return 'Data fetched' else: return 'Unable to establish connection'
I was wondering if my approach to this is the right way to go or if there are smarter ways to do this? Are there built-in Django tools that can handle this kind of syncing?
Any feedback is welcome.