Skip to content

Commit

Permalink
refactor: flat is better than nested
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Jan 24, 2024
1 parent 2a2d8f8 commit 897fd7f
Showing 1 changed file with 64 additions and 66 deletions.
130 changes: 64 additions & 66 deletions web/b3desk/models/bbb.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,76 +190,74 @@ def create(self):
)

params = self.get_params_with_checksum(action, params)
if current_app.config["FILE_SHARING"]:
# ADDING DEFAULT FILE TO MEETING
SECRET_KEY = current_app.config["SECRET_KEY"]
xml_beg = "<?xml version='1.0' encoding='UTF-8'?> <modules> <module name='presentation'> "
xml_end = " </module></modules>"
xml_mid = ""
if not current_app.config["FILE_SHARING"]:
response = requests.post(self.get_url(action), params=params)
data = {c.tag: c.text for c in ElementTree.fromstring(response.content)}
return data

if self.meeting.default_file:
meeting_file = self.meeting.default_file
if meeting_file.url:
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{meeting_file.url}' filename='{meeting_file.title}' />"
else: # file is not URL nor NC hence it was uploaded
filehash = hashlib.sha1(
f"{SECRET_KEY}-0-{meeting_file.id}-{SECRET_KEY}".encode()
).hexdigest()
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{current_app.config['SERVER_FQDN']}/ncdownload/0/{meeting_file.id}/{filehash}' filename='{meeting_file.title}' />"
xml = xml_beg + xml_mid + xml_end
response = requests.post(
self.get_url(action),
params=params,
headers={"Content-Type": "application/xml"},
data=xml,
)
## BEGINNING OF TASK CELERY - aka background_upload for meeting_files
params = {}
xml = ""
# ADDING ALL FILES EXCEPT DEFAULT
SERVER_FQDN = current_app.config["SERVER_FQDN"]
BIGBLUEBUTTON_ENDPOINT = current_app.config["BIGBLUEBUTTON_ENDPOINT"]
BIGBLUEBUTTON_SECRET = current_app.config["BIGBLUEBUTTON_SECRET"]
# ADDING DEFAULT FILE TO MEETING
SECRET_KEY = current_app.config["SECRET_KEY"]
xml_beg = "<?xml version='1.0' encoding='UTF-8'?> <modules> <module name='presentation'> "
xml_end = " </module></modules>"
xml_mid = ""

insertAction = "insertDocument"
xml_beg = "<?xml version='1.0' encoding='UTF-8'?> <modules> <module name='presentation'> "
xml_end = " </module></modules>"
xml_mid = ""
for meeting_file in self.meeting.files:
if meeting_file.is_default:
continue
elif meeting_file.url:
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{meeting_file.url}' filename='{meeting_file.title}' />"
else: # file is not URL nor NC hence it was uploaded
filehash = hashlib.sha1(
f"{SECRET_KEY}-0-{meeting_file.id}-{SECRET_KEY}".encode()
).hexdigest()
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{SERVER_FQDN}/ncdownload/0/{meeting_file.id}/{filehash}' filename='{meeting_file.title}' />"
if self.meeting.default_file:
meeting_file = self.meeting.default_file
if meeting_file.url:
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{meeting_file.url}' filename='{meeting_file.title}' />"
else: # file is not URL nor NC hence it was uploaded
filehash = hashlib.sha1(
f"{SECRET_KEY}-0-{meeting_file.id}-{SECRET_KEY}".encode()
).hexdigest()
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{current_app.config['SERVER_FQDN']}/ncdownload/0/{meeting_file.id}/{filehash}' filename='{meeting_file.title}' />"
xml = xml_beg + xml_mid + xml_end
response = requests.post(
self.get_url(action),
params=params,
headers={"Content-Type": "application/xml"},
data=xml,
)
## BEGINNING OF TASK CELERY - aka background_upload for meeting_files
params = {}
xml = ""
# ADDING ALL FILES EXCEPT DEFAULT
SERVER_FQDN = current_app.config["SERVER_FQDN"]
BIGBLUEBUTTON_ENDPOINT = current_app.config["BIGBLUEBUTTON_ENDPOINT"]
BIGBLUEBUTTON_SECRET = current_app.config["BIGBLUEBUTTON_SECRET"]

xml = xml_beg + xml_mid + xml_end
params = {"meetingID": self.meeting.meetingID}
request = requests.Request(
"POST",
f"{BIGBLUEBUTTON_ENDPOINT}/{insertAction}",
params=params,
)
pr = request.prepare()
bigbluebutton_secret = BIGBLUEBUTTON_SECRET
secret = "{}{}".format(
pr.url.replace("?", "").replace(f"{BIGBLUEBUTTON_ENDPOINT}/", ""),
bigbluebutton_secret,
)
params["checksum"] = hashlib.sha1(secret.encode("utf-8")).hexdigest()
background_upload.delay(
f"{BIGBLUEBUTTON_ENDPOINT}/{insertAction}", xml, params
)
insertAction = "insertDocument"
xml_beg = "<?xml version='1.0' encoding='UTF-8'?> <modules> <module name='presentation'> "
xml_end = " </module></modules>"
xml_mid = ""
for meeting_file in self.meeting.files:
if meeting_file.is_default:
continue
elif meeting_file.url:
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{meeting_file.url}' filename='{meeting_file.title}' />"
else: # file is not URL nor NC hence it was uploaded
filehash = hashlib.sha1(
f"{SECRET_KEY}-0-{meeting_file.id}-{SECRET_KEY}".encode()
).hexdigest()
xml_mid += f"<document downloadable='{'true' if meeting_file.is_downloadable else 'false'}' url='{SERVER_FQDN}/ncdownload/0/{meeting_file.id}/{filehash}' filename='{meeting_file.title}' />"

data = {c.tag: c.text for c in ElementTree.fromstring(response.content)}
return data
else:
response = requests.post(self.get_url(action), params=params)
data = {c.tag: c.text for c in ElementTree.fromstring(response.content)}
return data
xml = xml_beg + xml_mid + xml_end
params = {"meetingID": self.meeting.meetingID}
request = requests.Request(
"POST",
f"{BIGBLUEBUTTON_ENDPOINT}/{insertAction}",
params=params,
)
pr = request.prepare()
bigbluebutton_secret = BIGBLUEBUTTON_SECRET
secret = "{}{}".format(
pr.url.replace("?", "").replace(f"{BIGBLUEBUTTON_ENDPOINT}/", ""),
bigbluebutton_secret,
)
params["checksum"] = hashlib.sha1(secret.encode("utf-8")).hexdigest()
background_upload.delay(f"{BIGBLUEBUTTON_ENDPOINT}/{insertAction}", xml, params)

data = {c.tag: c.text for c in ElementTree.fromstring(response.content)}
return data

def delete_recordings(self, recording_ids):
"""DeleteRecordings BBB API: https://docs.bigbluebutton.org/dev/api.html#deleterecordings"""
Expand Down

0 comments on commit 897fd7f

Please sign in to comment.