-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to database load, removing 2 new stocks.
- Loading branch information
1 parent
e7ac811
commit 16cb469
Showing
2 changed files
with
49 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
# home/management/commands/load_db_with_stocks.py | ||
from django.core.management.base import BaseCommand | ||
from home.views import load_db_with_stocks, load_db_with_recommendations | ||
from home.models import Stock | ||
from django.conf import settings | ||
import os | ||
import csv | ||
|
||
class Command(BaseCommand): | ||
help = 'Load initial stock and recommendation data into the database.' | ||
help = 'Load stocks from DJIAStockList.csv and update the database.' | ||
|
||
def handle(self, *args, **options): | ||
# Call the function to load stocks | ||
try: | ||
self.stdout.write('Loading stocks...') | ||
load_db_with_stocks() | ||
self.stdout.write(self.style.SUCCESS('Stocks have been loaded successfully.')) | ||
except Exception as e: | ||
self.stdout.write(self.style.ERROR(f'An error occurred while loading stocks: {e}')) | ||
# Define the path to your CSV file | ||
csv_file_path = os.path.join(settings.BASE_DIR, 'dataload', 'DJIAStockList.csv') | ||
|
||
# Get the current list of stocks from the database | ||
existing_stocks = set(Stock.objects.values_list('ticker', flat=True)) | ||
|
||
# Counter for statistics | ||
created_count = 0 | ||
updated_count = 0 | ||
removed_count = 0 | ||
|
||
# Call the function to load recommendations | ||
try: | ||
csvfilename = os.path.join(settings.BASE_DIR, 'dataload', 'COP_DJIA_Total_Dataset.csv') | ||
self.stdout.write('Loading recommendations...') | ||
load_db_with_recommendations(csvfilename) | ||
self.stdout.write(self.style.SUCCESS('Recommendations have been loaded successfully.')) | ||
# Use Python's built-in CSV reader to process the file | ||
with open(csv_file_path, mode='r', newline='', encoding='utf-8') as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
|
||
for row in reader: | ||
ticker = row['ticker'] | ||
company = row['company'] | ||
|
||
# Create a new stock entry or update the existing one | ||
stock, created = Stock.objects.update_or_create( | ||
ticker=ticker, # look up by ticker | ||
defaults={'company': company} # fields to update | ||
) | ||
|
||
# Update the counters | ||
if created: | ||
created_count += 1 | ||
else: | ||
updated_count += 1 | ||
|
||
# Remove the ticker from the existing stocks set | ||
if ticker in existing_stocks: | ||
existing_stocks.remove(ticker) | ||
|
||
# Remove stocks that are no longer in the CSV file | ||
removed_count = Stock.objects.filter(ticker__in=existing_stocks).delete()[0] | ||
|
||
# Print summary of the operation | ||
self.stdout.write(self.style.SUCCESS( | ||
f'Operation completed. {created_count} stocks created, {updated_count} stocks updated, {removed_count} stocks removed.' | ||
)) | ||
|
||
except Exception as e: | ||
self.stdout.write(self.style.ERROR(f'An error occurred while loading recommendations: {e}')) | ||
# If an error occurs during the process, print it to the console | ||
self.stdout.write(self.style.ERROR(f'An error occurred during the stock loading process: {e}')) |