forked from Comet-Robotics/club-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_csv.py
More file actions
81 lines (66 loc) · 2.82 KB
/
parse_csv.py
File metadata and controls
81 lines (66 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "clubManager.settings")
from clubManager import settings
import django
django.setup()
import csv
from datetime import datetime
from django.utils import timezone
from django.utils.timezone import make_aware
from payments.models import Payment, Product, Term
from django.contrib.auth.models import User
def parse_csv_and_store_data(file_path):
scanTime = timezone.now()
verifier = User.objects.get(username="osd220000")
with open(file_path, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
try:
payment_date = (
make_aware(datetime.strptime(row["Timestamp"], "%m/%d/%Y %H:%M:%S")) if row["Timestamp"] else None
)
first_name = row["First Name"].strip()
last_name = row["Last Name"].strip()
net_id = row["Net ID"].lower().strip()
payment_method = row["Which payment method will you use to pay your $10 member dues?"]
has_paid = row["Has Paid?"] == "Yes"
approval_date = (
make_aware(datetime.strptime(row["When?"], "%m/%d/%Y %I:%M %p")) if row["When?"] else None
)
if not has_paid:
continue
selected_term = Term.objects.get(id=1)
current_product = Product.objects.get(term=selected_term)
# TODO need to manually enter these payment methods
if payment_method == "Other Payment Method":
continue
# Create or get the user
user, created = User.objects.get_or_create(
defaults={
"first_name": first_name,
"last_name": last_name,
"username": net_id,
},
username=net_id,
)
methods = {
"Regular Cash": "cash",
"Cash App": "cashapp",
"PayPal": "paypal",
}
# Create the payment
Payment.objects.create(
user=user,
product=current_product, # Replace with actual product instance
amount_cents=1000, # Assuming $10 member dues
created_at=payment_date,
updated_at=approval_date,
method=methods[payment_method],
verified_by=verifier,
notes=f"{scanTime} - Imported from CSV",
)
except Exception as e:
print(f"Error processing row: {row}")
print(e)
# Call the function with the path to your CSV file
parse_csv_and_store_data("term1.csv")