2

I'm fairly new to Django and recently created a website to group and display COVID-19 data in my home country.

I am recording daily new cases, deaths, and recoveries by city using a CaseDeclaration model:

class CaseDeclaration(models.Model):
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    date = models.DateField(default=lambda: CaseDeclaration.latest_date())
    new_cases = models.IntegerField(default=0)
    new_deaths = models.IntegerField(default=0)
    new_recoveries = models.IntegerField(default=0)
    source = models.URLField(max_length=200, blank=True)

It is working properly but I am now writing a script that will automate the process of creating CaseDeclaration objects off of publications made by a local news outlet on Facebook. The script is in a text_extractor.py file next to models.py and views.py. At this point it takes a txt file and extracts the data into a dictionary like so (keys are the city names):

>>> print(extract_data())
{'non répertorié': {'new_deaths': 6, 'new_recoveries': 344}, 'Antananarivo': {'new_cases': 388}, 'Ambatondrazaka': {'new_cases': 13}, 'Toliara': {'new_cases': 9}, 'Fianarantsoa': {'new_cases': 7}, 'Mahajanga': {'new_cases': 6}, 'Ambositra': {'new_cases': 3}, 'Miarinarivo': {'new_cases': 2}, 'Toamasina': {'new_cases': 2}, 'Sambava': {'new_cases': 1}}

And the date extract_date() returns the format 8-11-2020 as a str.

I now need a way to take that data and create model instances for each city, all without me having to use the python shell or admin panel. And finally program the script to execute daily.

What I have tried so far:

  1. import models into my text_extractor.py file and use Model().save() from there, but my file could not import models, and I could not figure out how to run the script from the server from there.
  2. create a Model Manager for CaseDeclaration and give it a create_from_dict() function
  3. create a function that will create the model instances and run it every time the home page is loaded *and the latest CaseDeclaration object is not from today. Also, here my custom .py file isn't recognized as a module and can't be imported.

Whichever way would eventually work I am not capable to figure it out by myself and online resources have been really limited, so I am hoping you can help out. Thank you so much in advance!

EDIT: Current code text_extractor.py

def extract_data():
    return find_data(region_to_cities(join_words(cleanup_covid19(extracted_data))))

def extract_date():
    temp = find_date(region_to_cities(join_words(cleanup_covid19(extracted_data))))
    date = '-'.join(str(e) for e in temp)
    return date

views.py

def extractCaseDeclaration():
    from .text_extractor import extract_data, extract_date
    data = extract_data()
    date = datetime.datetime.strptime(extract_date(), '%m-%d-%Y')

    for key, values in data.items():
        city = ''
        if "-" in key:
            city = string.capwords(key, '-')
        else:
            city = string.capwords(key)
        print(city)
        city = City.objects.get(name = city)

extractCaseDeclaration()

1 Answers1

1

You can run the python script in the manage.py shell just start the shell:

python manage.py shell

and run the code snippet below

>>>exec(open('/python_automate_script.py').read())

This will run the python script.

There are other ways, you can find more details in this website

Gift.py
  • 11
  • 3