forked from RoBorregos/CandidatesDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
153 lines (114 loc) · 3.68 KB
/
upload.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Install the required packages
# !pip install pandas sqlalchemy psycopg2-binary
from sqlalchemy import create_engine, Column, Integer, String, Float
from sqlalchemy.orm import sessionmaker, declarative_base
from dotenv import load_dotenv
import os
import pandas as pd
import typer
from sqlalchemy.dialects.postgresql import UUID
import uuid
app = typer.Typer()
load_dotenv() # Make sure to have a .env file in the same directory as this script
Base = declarative_base()
class AdminTable(Base):
__tablename__ = 'Admin'
email = Column(String, primary_key=True)
class JudgeTable(Base):
__tablename__ = 'Judge'
email = Column(String, primary_key=True)
class EmailTeam(Base):
__tablename__ = 'EmailTeam'
email = Column(String, primary_key=True)
team = Column(String)
class Team(Base):
__tablename__ = 'Team'
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, primary_key=True)
def getSession():
# Database URL
DATABASE_URL = os.getenv("DATABASE_URL")
# Set up SQLAlchemy Base and engine
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
return Session()
@app.command()
def admin_table(file_name: str, col_name: str = 'email'):
df = pd.read_excel(file_name)
session = getSession()
try:
for _, row in df.iterrows():
record = AdminTable(
email=row[col_name],
)
session.add(record)
# Commit records
session.commit()
print("Data successfully inserted!")
except Exception as e:
session.rollback() # Rollback
print(f"Error occurred: {e}")
finally:
session.close() # Close session
@app.command()
def judge_table(file_name: str, col_name: str = 'email'):
df = pd.read_excel(file_name)
session = getSession()
try:
for _, row in df.iterrows():
record = JudgeTable(
email=row[col_name],
)
session.add(record)
# Commit records
session.commit()
print("Data successfully inserted!")
except Exception as e:
session.rollback() # Rollback
print(f"Error occurred: {e}")
finally:
session.close() # Close session
@app.command()
def email_team_table(file_name: str, col_name: str = 'email', col_name2: str = 'team'):
df = pd.read_excel(file_name)
session = getSession()
try:
for _, row in df.iterrows():
record = EmailTeam(
email=row[col_name],
team=row[col_name2]
)
session.add(record)
# Commit records
session.commit()
print("Data successfully inserted!")
except Exception as e:
session.rollback() # Rollback
print(f"Error occurred: {e}")
finally:
session.close() # Close session
@app.command()
def team_table(file_name: str, col_name: str = 'name'):
df = pd.read_excel(file_name)
session = getSession()
try:
for _, row in df.iterrows():
record = Team(
name=row[col_name],
)
session.add(record)
# Commit records
session.commit()
print("Data successfully inserted!")
except Exception as e:
session.rollback() # Rollback
print(f"Error occurred: {e}")
finally:
session.close() # Close session
if __name__ == "__main__":
app()
# Run with typer:
# python3 upload.py admin-table example_upload/test.xlsx
# python3 upload.py judge-table example_upload/test.xlsx
# python3 upload.py email-team-table example_upload/email_team.xlsx
# python3 upload.py team-table example_upload/teams.xlsx