Adminschedule Py

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

adminSchedule.

py

import sys
from PyQt5.QtWidgets import QDialog, QApplication, QMessageBox, QTableWidgetItem
from PyQt5.uic import loadUi
import psycopg2
import random

class AdminSchedule(QDialog):
def __init__(self):
super(AdminSchedule, self).__init__()
loadUi("adminSchedule.ui", self)
self.setFixedSize(1261, 900)
self.schedulebtn.clicked.connect(self.adminsched)
self.savebtn_2.clicked.connect(self.randommatch)

self.random.clicked.connect(self.randommatch)
self.tabWidget.currentChanged.connect(self.handleTabChange)

def handleTabChange(self, index):


if index == 1:
# Perform actions for the first tab
self.ViewSchedule()
elif index == 0:
# Perform actions for the second tab
self.randommatch()
def adminsched(self):
sender = self.sender()
sender.setStyleSheet("background-color: rgb(255, 188, 4);")

def randommatch(self):
selected_sport = self.cbsport.currentText()
game_type = self.cbsport_2.currentText()
date = self.dateEdit.date().toString("yyyy-MM-dd")
time = self.timeEdit.time().toString("hh:mm")
remaining_teams = self.teamnum.text()
db_host = 'localhost'
db_name = 'sport'
db_user = 'postgres'
db_password = '123456'

try:
conn = psycopg2.connect(
host=db_host,
database=db_name,
user=db_user,
password=db_password
)

cursor = conn.cursor()

# Fetch teams specific to the selected sport from the database


teams_query = "SELECT TEAM_NAME FROM TEAM WHERE TEAM_LEAGUE = %s"
cursor.execute(teams_query, (selected_sport,))
teams_result = cursor.fetchall()
teams = [team[0] for team in teams_result]

home_team = random.choice(teams)
against_team = random.choice([team for team in teams if team !=
home_team])

# Set the teams as options in the home and against ComboBoxes


self.home.clear()
self.home.addItem(home_team)
self.home.addItems([team for team in teams if team != home_team])

self.against.clear()
self.against.addItem(against_team)
self.against.addItems([team for team in teams if team != against_team])
location = self.cblocation.currentText()

match_des = f"{home_team} vs {against_team}"

sched = random.randint(1111, 1999)


while True:
cursor.execute("SELECT 1 FROM SCHEDULE WHERE SCHED_ID = %s",
(sched,))
if cursor.fetchone() is None:
break

querysched = """INSERT INTO SCHEDULE (SCHED_ID, SCHED_GAMEDATE,


SCHED_GAMETIME, SCHED_LOCVENUE)
VALUES (%s, %s, %s, %s)"""
cursor.execute(querysched, (sched, date, time, location))

# Select random home and against teams


home_team = random.choice(teams)
against_team = random.choice(teams)

match = random.randint(100, 999)


# Fetch the TEAM_ID based on the home_team from the TEAM table
team_id_query = "SELECT TEAM_ID FROM TEAM WHERE TEAM_NAME = %s"
cursor.execute(team_id_query, (home_team,))
team_id = cursor.fetchone()[0] # Assuming the first column is TEAM_ID

# Check if the team already has an upcoming match


team_match_query = "SELECT 1 FROM MATCH WHERE TEAM_ID = %s AND
MATCH_STATUS = 'Upcoming'"
cursor.execute(team_match_query, (team_id,))
team_match_result = cursor.fetchone()

if team_match_result:
# Display error message
QMessageBox.warning(self, "Error", "The team already has an
upcoming match.")
return

res_match = random.randint(999, 9999)


queryres = "INSERT INTO RESULT (RES_ID,RES_SCORE,RES_DESCRIPTION)
VALUES (%s, %s,%s)"
cursor.execute(queryres, (res_match, 0, 0))
# Insert data into the MATCH table with the retrieved TEAM_ID
querymatch = "INSERT INTO MATCH (MATCH_ID, MATCH_GAME_TYPE,
MATCH_STATUS, TEAM_ID, MATCH_DESCRIPTION, SCHED_ID, RES_ID) VALUES (%s, %s, %s, %s,
%s, %s, %s)"
cursor.execute(querymatch, (match, game_type, 'Upcoming', team_id,
match_des, sched, res_match))

conn.commit()
conn.close()

QMessageBox.information(self, "Success", "Match scheduled


successfully.")

except (Exception, psycopg2.Error) as error:


print("Error while connecting to PostgreSQL:", error)
def ViewSchedule(self):
# Connection details
db_host = 'localhost'
db_name = 'sport'
db_user = 'postgres'
db_password = '123456'

try:
conn = psycopg2.connect(
host=db_host,
database=db_name,
user=db_user,
password=db_password
)

cursor = conn.cursor()

# Base query to retrieve all data


query = """
SELECT MATCH_DESCRIPTION, SCHED_ID
FROM MATCH
"""

selected_sport = self.cbsportsmatch.currentText()

# Modify the query if a specific sport is selected


if selected_sport != "All":
query += " WHERE TEAM_LEAGUE = %s"
cursor.execute(query, (selected_sport,))
else:
cursor.execute(query)

rows = cursor.fetchall()

# Set the number of rows and columns in the table widget


self.tableWidget.setRowCount(len(rows))
self.tableWidget.setColumnCount(len(cursor.description))

# Set the headers


headers = ["DATE", "TIME", "LOCATION", "Description", "Sport"]
self.tableWidget.setHorizontalHeaderLabels(headers)

# Iterate over the rows and populate the table widget


for i, row in enumerate(rows):
sched_id = row[1]
query = "SELECT sched_gametime, sched_gamedate, sched_locvenue FROM
sched WHERE sched_id = %s"
cursor.execute(query, (sched_id,))
schedule_data = cursor.fetchone()
if schedule_data:
sched_gametime, sched_gamedate, sched_locvenue = schedule_data

# Create table items and set the data


item1 = QTableWidgetItem(sched_gamedate)
item2 = QTableWidgetItem(sched_gametime)
item3 = QTableWidgetItem(sched_locvenue)
item4 = QTableWidgetItem(row[0]) # MATCH_DESCRIPTION
item5 = QTableWidgetItem(selected_sport)

# Add the items to the table widget


self.tableWidget.setItem(i, 0, item1)
self.tableWidget.setItem(i, 1, item2)
self.tableWidget.setItem(i, 2, item3)
self.tableWidget.setItem(i, 3, item4)
self.tableWidget.setItem(i, 4, item5)

except (Exception, psycopg2.Error) as error:


print("Error while connecting to PostgreSQL:", error)

finally:
# Close the database connection
if conn:
conn.close()

if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = AdminSchedule()
mainwindow.show()
sys.exit(app.exec_())

You might also like