Skip to content

Commit

Permalink
Use uploadv2 API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraust committed Sep 2, 2024
1 parent e8e6c62 commit f58d680
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 46 deletions.
59 changes: 41 additions & 18 deletions OSCRUI/leagueconnector.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Backend inteface to the OSCR web server"""

import gzip
import json
import os
import tempfile
import json

import OSCR_django_client
from OSCR.utilities import logline_to_str
from OSCR_django_client.api import CombatlogApi, LadderApi, LadderEntriesApi, VariantApi
from OSCR_django_client.api import (CombatlogApi, LadderApi, LadderEntriesApi,
VariantApi)
from PySide6.QtWidgets import QMessageBox

from .datafunctions import CustomThread, analyze_log_callback
Expand All @@ -17,6 +18,9 @@
from .subwindows import show_warning, uploadresult_dialog
from .textedit import format_datetime_str

# OSCR_SERVER_BACKEND = "https://oscr.stobuilds.com/"
OSCR_SERVER_BACKEND = "http://127.0.0.1:8000"


def establish_league_connection(self, translate):
"""
Expand All @@ -27,7 +31,9 @@ def establish_league_connection(self, translate):
"""
if self.league_api is None:
self.league_api = OSCRClient(translate)
map_fetch_thread = CustomThread(self.window, lambda: fetch_and_insert_maps(self))
map_fetch_thread = CustomThread(
self.window, lambda: fetch_and_insert_maps(self)
)
map_fetch_thread.start()


Expand Down Expand Up @@ -184,7 +190,9 @@ def extend_ladder(self):
row.get("build", "Unknown"),
)
)
self.widgets.ladder_table.model().sourceModel().extend_data(table_index, table_data, logfile_ids)
self.widgets.ladder_table.model().sourceModel().extend_data(
table_index, table_data, logfile_ids
)


def download_and_view_combat(self, translation):
Expand All @@ -203,7 +211,9 @@ def download_and_view_combat(self, translation):
mode="w", encoding="utf-8", dir=self.config["templog_folder_path"], delete=False
) as file:
file.write(result.decode())
analyze_log_callback(self, translation, path=file.name, parser_num=1, hidden_path=True)
analyze_log_callback(
self, translation, path=file.name, parser_num=1, hidden_path=True
)
self.switch_overview_tab(0)
self.switch_main_tab(0)

Expand All @@ -212,15 +222,20 @@ def upload_callback(self, translate):
"""
Helper function to grab the current combat and upload it to the backend.
"""
if self.parser1.active_combat is None or self.parser1.active_combat.log_data is None:
if (
self.parser1.active_combat is None
or self.parser1.active_combat.log_data is None
):
show_warning(self, "OSCR - Logfile Upload", self._("No data to upload."))
return

establish_league_connection(self, translate)

with tempfile.NamedTemporaryFile(delete=False) as file:
data = gzip.compress(
"".join([logline_to_str(line) for line in self.parser1.active_combat.log_data]).encode()
"".join(
[logline_to_str(line) for line in self.parser1.active_combat.log_data]
).encode()
)
file.write(data)
file.flush()
Expand All @@ -246,12 +261,10 @@ def populate_variants(self):


class OSCRClient:
def __init__(self, translate, address=None):
def __init__(self, translate):
"""Initialize an instance of the OSCR backlend client"""

if not address:
self.address = "https://oscr.stobuilds.com"

self.address = OSCR_SERVER_BACKEND
self._ = translate

self.api_client = OSCR_django_client.api_client.ApiClient()
Expand All @@ -270,13 +283,15 @@ def upload(self, filename):
"""Upload a combat log located at path for analysis"""

try:
return self.api_combatlog.combatlog_upload(file=filename)
return self.api_combatlog.combatlog_uploadv2(file=filename)
except OSCR_django_client.exceptions.ServiceException as e:
reply = QMessageBox()
reply.setWindowTitle("Open Source Combatlog Reader")
try:
data = json.loads(e.body)
reply.setText(data.get("detail", self._("Failed to parse error from server")))
reply.setText(
data.get("detail", self._("Failed to parse error from server"))
)
except Exception as e:
reply.setText(self._("Failed to parse error from server"))
reply.exec()
Expand All @@ -290,7 +305,9 @@ def download(self, id):
reply.setWindowTitle("Open Source Combatlog Reader")
try:
data = json.loads(e.body)
reply.setText(data.get("detail", self._("Failed to parse error from server")))
reply.setText(
data.get("detail", self._("Failed to parse error from server"))
)
except Exception as e:
reply.setText(self._("Failed to parse error from server"))
reply.exec()
Expand All @@ -306,7 +323,9 @@ def ladders(self, **kwargs):
reply.setWindowTitle("Open Source Combatlog Reader")
try:
data = json.loads(e.body)
reply.setText(data.get("detail", self._("Failed to parse error from server")))
reply.setText(
data.get("detail", self._("Failed to parse error from server"))
)
except Exception as e:
reply.setText(self._("Failed to parse error from server"))
reply.exec()
Expand All @@ -327,7 +346,9 @@ def ladder_entries(self, id, page=1):
reply.setWindowTitle("Open Source Combatlog Reader")
try:
data = json.loads(e.body)
reply.setText(data.get("detail", self._("Failed to parse error from server")))
reply.setText(
data.get("detail", self._("Failed to parse error from server"))
)
except Exception as e:
reply.setText(self._("Failed to parse error from server"))
reply.exec()
Expand All @@ -344,9 +365,11 @@ def variants(self, **kwargs):
reply.setWindowTitle("Open Source Combatlog Reader")
try:
data = json.loads(e.body)
reply.setText(data.get("detail", self._("Failed to parse error from server")))
reply.setText(
data.get("detail", self._("Failed to parse error from server"))
)
except Exception as e:
reply.setText(self._("Failed to parse error from server"))
reply.exec()

return None
return None
74 changes: 46 additions & 28 deletions OSCRUI/subwindows.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from PySide6.QtCore import QPoint, QSize, Qt
from PySide6.QtGui import QIntValidator, QMouseEvent
from PySide6.QtCore import QPoint, QSize, Qt, QUrl
from PySide6.QtGui import QIntValidator, QMouseEvent, QDesktopServices
from PySide6.QtWidgets import QAbstractItemView, QDialog
from PySide6.QtWidgets import QGridLayout, QHBoxLayout, QLineEdit
from PySide6.QtWidgets import QMessageBox, QSpacerItem, QSplitter, QTableView
Expand Down Expand Up @@ -212,6 +212,18 @@ def split_dialog(self, translate):
dialog.setSizePolicy(SMAXMAX)
dialog.exec()

def uploadresult_view_result(result):
"""
Open the result up in the user's web browser.
Parameters:
- :param result: The Combat Log Upload Response
"""
from .leagueconnector import OSCR_SERVER_BACKEND

url = QUrl(f"{OSCR_SERVER_BACKEND}/ui/combatlog/{result.combatlog}/")
QDesktopServices.openUrl(url)


def uploadresult_dialog(self, result, translate):
"""
Expand All @@ -234,34 +246,40 @@ def uploadresult_dialog(self, result, translate):
content_layout.setContentsMargins(thick, thick, thick, thick)
content_layout.setSpacing(0)
margin = {'margin-bottom': self.theme['defaults']['isp']}
title_label = create_label(self, self._('Upload Results:'), 'label_heading', style_override=margin)
title_label = create_label(self, f"{result.detail}", 'label_heading', style_override=margin)
content_layout.addWidget(title_label, 0, 0, 1, 4, alignment=ALEFT)
view_button = create_button(self, 'View Online', style_override=margin)
view_button.clicked.connect(lambda: uploadresult_view_result(result))
if result.results:
content_layout.addWidget(view_button, 0, 0, 1, 4, alignment=ARIGHT)
icon_size = QSize(self.config['icon_size'] / 1.5, self.config['icon_size'] / 1.5)
for row, line in enumerate(result, 1):
if row % 2 == 1:
table_style = {'background-color': '@mbg', 'padding': (5, 3, 3, 3), 'margin': 0}
icon_table_style = {'background-color': '@mbg', 'padding': (3, 3, 3, 3), 'margin': 0}
else:
table_style = {'background-color': '@bg', 'padding': (5, 3, 3, 3), 'margin': 0}
icon_table_style = {'background-color': '@bg', 'padding': (3, 3, 3, 3), 'margin': 0}
if line.updated:
icon = self.icons['check'].pixmap(icon_size)
else:
icon = self.icons['dash'].pixmap(icon_size)
status_label = create_label(self, '', style_override=icon_table_style)
status_label.setPixmap(icon)
status_label.setSizePolicy(SMINMIN)
content_layout.addWidget(status_label, row, 0)
name_label = create_label(self, line.name, style_override=table_style)
name_label.setSizePolicy(SMINMAX)
content_layout.addWidget(name_label, row, 1)
value_label = create_label(self, str(line.value), style_override=table_style)
value_label.setSizePolicy(SMINMAX)
value_label.setAlignment(ARIGHT)
content_layout.addWidget(value_label, row, 2)
detail_label = create_label(self, line.detail, style_override=table_style)
detail_label.setSizePolicy(SMINMAX)
content_layout.addWidget(detail_label, row, 3)
row = 0
if (result.results):
for row, line in enumerate(result.results, 1):
if row % 2 == 1:
table_style = {'background-color': '@mbg', 'padding': (5, 3, 3, 3), 'margin': 0}
icon_table_style = {'background-color': '@mbg', 'padding': (3, 3, 3, 3), 'margin': 0}
else:
table_style = {'background-color': '@bg', 'padding': (5, 3, 3, 3), 'margin': 0}
icon_table_style = {'background-color': '@bg', 'padding': (3, 3, 3, 3), 'margin': 0}
if line.updated:
icon = self.icons['check'].pixmap(icon_size)
else:
icon = self.icons['dash'].pixmap(icon_size)
status_label = create_label(self, '', style_override=icon_table_style)
status_label.setPixmap(icon)
status_label.setSizePolicy(SMINMIN)
content_layout.addWidget(status_label, row, 0)
name_label = create_label(self, line.name, style_override=table_style)
name_label.setSizePolicy(SMINMAX)
content_layout.addWidget(name_label, row, 1)
value_label = create_label(self, str(line.value), style_override=table_style)
value_label.setSizePolicy(SMINMAX)
value_label.setAlignment(ARIGHT)
content_layout.addWidget(value_label, row, 2)
detail_label = create_label(self, line.detail, style_override=table_style)
detail_label.setSizePolicy(SMINMAX)
content_layout.addWidget(detail_label, row, 3)
top_margin = {'margin-top': self.theme['defaults']['isp']}
close_button = create_button(self, 'Close', style_override=top_margin)
close_button.clicked.connect(dialog.close)
Expand Down

0 comments on commit f58d680

Please sign in to comment.