Skip to content

Commit bfccebc

Browse files
Clean up GUI code and remove app freeze on voting
1 parent 7ebfa70 commit bfccebc

2 files changed

Lines changed: 84 additions & 95 deletions

File tree

LDJAM_API/LDJAM_API.py

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,89 +8,90 @@
88

99

1010
def get_cookie_header():
11-
# load data from local config
12-
cookie_data = load_config()
11+
# load data from local config
12+
cookie_data = load_config()
1313

14-
cookie1 = cookie_data.get('SIDS')
14+
cookie1 = cookie_data.get('SIDS')
1515

16-
# build header in correct format
17-
header = {'Cookie': f'SIDS={cookie1}'}
16+
# build header in correct format
17+
header = {'Cookie': f'SIDS={cookie1}'}
1818

19-
return header
19+
return header
2020

2121

2222
def get_event_themes(event_id: int):
23-
# call ldjam API to get all running events themes
24-
header = get_cookie_header()
25-
call_url = f'https://api.ldjam.com/vx/theme/idea/vote/get/{event_id}'
23+
# call ldjam API to get all running events themes
24+
header = get_cookie_header()
25+
call_url = f'https://api.ldjam.com/vx/theme/idea/vote/get/{event_id}'
2626

27-
request = get(call_url, headers=header)
27+
request = get(call_url, headers=header)
2828

29-
return request
29+
return request
3030

3131

3232
def get_current_event_id():
33-
# call ldjam API to get running events ID (this call doesnt need auth headers)
34-
call_url = 'https://api.ldjam.com/vx/node2/what/1'
35-
request = get(call_url)
33+
# call ldjam API to get running events ID (this call doesnt need auth headers)
34+
call_url = 'https://api.ldjam.com/vx/node2/what/1'
35+
request = get(call_url)
3636

37-
request_json = json.loads(request.text)
37+
request_json = json.loads(request.text)
3838

39-
# if we got an answer, extract event ID and return, otherwise abort
40-
if request_json['status'] == 200:
41-
event_id = request_json.get('featured').get('id')
39+
# if we got an answer, extract event ID and return, otherwise abort
40+
if request_json['status'] == 200:
41+
event_id = request_json.get('featured').get('id')
4242

43-
print(f'done. ID: {event_id}')
44-
return event_id
45-
else:
46-
print('ERROR! Could not fetch current LDJAM ID. Aborting...')
47-
exit(0)
43+
print(f'done. ID: {event_id}')
44+
return event_id
45+
else:
46+
print('ERROR! Could not fetch current LDJAM ID. Aborting...')
47+
exit(0)
4848

4949

5050
def get_user_votes(event_id: int):
51-
# call ldjam API to get all themes the user already voted on
52-
header = get_cookie_header()
53-
call_url = f'https://api.ldjam.com/vx/theme/idea/vote/getmy/{event_id}'
51+
# call ldjam API to get all themes the user already voted on
52+
header = get_cookie_header()
53+
call_url = f'https://api.ldjam.com/vx/theme/idea/vote/getmy/{event_id}'
5454

55-
request = get(call_url, headers=header)
55+
request = get(call_url, headers=header)
5656

57-
request_json = json.loads(request.text)
57+
request_json = json.loads(request.text)
5858

59-
# if we got an answer, extract voted themes and return, otherwise abort
60-
if request_json['status'] == 200:
61-
user_votes = request_json.get('votes')
59+
# if we got an answer, extract voted themes and return, otherwise abort
60+
if request_json['status'] == 200:
61+
user_votes = request_json.get('votes')
6262

63-
return user_votes
64-
else:
65-
try:
66-
if request_json['message'] == 'Event is not Slaughtering':
67-
print('Theme slaughter has not begun yet! Please try again later.')
68-
exit(0)
63+
return user_votes
64+
else:
65+
try:
66+
if request_json['message'] == 'Event is not Slaughtering':
67+
print('Theme slaughter has not begun yet! Please try again later.')
68+
exit(0)
6969

70-
except KeyError:
71-
print('ERROR! Could not fetch user votes. Aborting...')
72-
exit(0)
70+
except KeyError:
71+
print('ERROR! Could not fetch user votes. Aborting...')
72+
exit(0)
7373

74-
print('ERROR! Could not fetch user votes. Aborting...')
75-
exit(0)
74+
print('ERROR! Could not fetch user votes. Aborting...')
75+
exit(0)
7676

7777

7878
def vote_theme(theme_id: int, voting: str):
79-
# call ldjam API to vote on given theme ID with the given voting string
80-
header = get_cookie_header()
79+
# call ldjam API to vote on given theme ID with the given voting string
8180

82-
try:
83-
request = get(f'https://api.ldjam.com/vx/theme/idea/vote/{voting}/{theme_id}', headers=header)
81+
header = get_cookie_header()
8482

85-
except RequestConnectionError:
86-
return -1
87-
except NewConnectionError:
88-
return -1
83+
try:
84+
request = get(f'https://api.ldjam.com/vx/theme/idea/vote/{voting}/{theme_id}', headers=header)
8985

90-
request_json = json.loads(request.text)
86+
except RequestConnectionError:
87+
return -1
88+
except NewConnectionError:
89+
return -1
9190

92-
# return whether vote was successful
93-
if request_json['status'] == 200:
94-
return 0
91+
request_json = json.loads(request.text)
9592

96-
return -1
93+
# return whether vote was successful
94+
if request_json['status'] == 200:
95+
return 0
96+
97+
return -1

main_gui.py

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import json
22
import sys
3-
from threading import Thread
4-
from time import sleep
3+
import time
54

6-
from PySide2 import QtGui
5+
from PySide2 import QtCore
76
from PySide2.QtCore import Qt
87
from PySide2.QtGui import QPalette
98
from PySide2.QtWidgets import QMainWindow, QApplication, QAbstractItemView
@@ -38,8 +37,9 @@ def __init__(self):
3837

3938
self.ui.text_theme_search.textChanged.connect(self.search_term_changed)
4039

41-
self.ui.btn_vote_yes.clicked.connect(lambda: self.start_vote_thread("yes"))
42-
self.ui.btn_vote_no.clicked.connect(lambda: self.start_vote_thread("no"))
40+
self.ui.btn_vote_yes.clicked.connect(lambda: self.vote_selected_themes("yes"))
41+
self.ui.btn_vote_no.clicked.connect(lambda: self.vote_selected_themes("no"))
42+
self.ui.btn_flag.clicked.connect(lambda: self.vote_selected_themes("flag"))
4343

4444
self.ui.btn_vote_yes.setEnabled(False)
4545
self.ui.btn_vote_no.setEnabled(False)
@@ -67,6 +67,8 @@ def search_term_changed(self):
6767
current_theme = self.theme_library['ideas'].get(theme)
6868
self.ui.list_themes.addItem(current_theme)
6969

70+
self.ui.list_themes.sortItems()
71+
7072
if len(self.ui.text_theme_search.text()) > 0:
7173
self.ui.label_status.setText(
7274
f"{len(self.theme_library['ideas'])} themes loaded, {len(self.theme_library['ideas']) - len(self.voted_themes)} unvoted ({self.ui.list_themes.count()} filtered)")
@@ -98,54 +100,40 @@ def get_theme_id(self, theme_name: str) -> int:
98100

99101
return -1
100102

101-
def vote_theme(self, themes: list, voting: str) -> None:
102-
for theme in themes:
103-
theme_id = self.get_theme_id(theme.text())
104-
print(
105-
f"Vote: {voting}, Theme ID: {theme_id} (To be clear, that is '{self.theme_library['ideas'].get(theme_id)}')")
106-
voting_success = LDJAM_API.vote_theme(theme_id, voting)
107-
108-
if voting_success != 0:
109-
print("Error voting")
103+
def vote_theme(self, theme: str, voting: str) -> None:
104+
theme_id = self.get_theme_id(theme)
105+
print(
106+
f"Vote: {voting}, Theme ID: {theme_id} (To be clear, that is '{self.theme_library['ideas'].get(theme_id)}')")
107+
voting_success = LDJAM_API.vote_theme(theme_id, voting)
110108

111-
sleep(1)
109+
if voting_success != 0:
110+
print("Error voting")
112111

113-
def vote_yes(self):
112+
def vote_selected_themes(self, voting: str):
114113
self.ui.btn_vote_yes.setEnabled(False)
115114
self.ui.btn_vote_no.setEnabled(False)
116115
self.ui.btn_flag.setEnabled(False)
117116

118-
for theme in self.ui.list_themes.selectedItems():
119-
print(f"Voting yes on '{theme.text()}'")
120-
self.vote_theme(theme.text(), "yes")
121-
122-
self.ui.text_theme_search.clear()
123-
self.refresh_theme_list()
124-
125-
self.ui.btn_vote_yes.setEnabled(True)
126-
self.ui.btn_vote_no.setEnabled(True)
127-
self.ui.btn_flag.setEnabled(True)
128-
129-
def start_vote_thread(self, voting: str):
130-
voting_thread = Thread(target=self.vote_theme, args=(self.ui.list_themes.selectedItems(), voting,), daemon=True)
131-
132-
voting_thread.start()
117+
self.ui.list_themes.setEnabled(False)
133118

134-
self.ui.btn_vote_yes.setEnabled(False)
135-
self.ui.btn_vote_no.setEnabled(False)
136-
self.ui.btn_flag.setEnabled(False)
119+
counter = 0
120+
theme_count = len(self.ui.list_themes.selectedItems())
121+
for theme in self.ui.list_themes.selectedItems():
122+
self.vote_theme(theme.text(), voting)
137123

138-
selected_amount = len(self.ui.list_themes.selectedItems())
139-
self.ui.label_status.setText(
140-
f"Applying votes. The app will appear frozen for {selected_amount} "
141-
f"{'seconds' if selected_amount != 1 else 'second'}.")
124+
counter += 1
125+
theme.setText(f"{theme.text()} ({'Voted ' + voting.upper() if voting != 'flag' else 'FLAGGED'})")
126+
self.ui.label_status.setText(
127+
f"Voting... {round((counter / theme_count) * 100)}% done ({counter} / {theme_count})")
128+
QtCore.QCoreApplication.processEvents()
142129

143-
QtGui.QGuiApplication.processEvents()
144-
voting_thread.join()
130+
time.sleep(1)
145131

146132
self.ui.text_theme_search.clear()
147133
self.refresh_theme_list()
148134

135+
self.ui.list_themes.setEnabled(True)
136+
149137
def refresh_theme_list(self):
150138
request = LDJAM_API.get_event_themes(self.event_id)
151139
self.theme_library = json.loads(request.text)

0 commit comments

Comments
 (0)