From 511dbc1eaf7b54825bb878d5174bee1ed7422fa5 Mon Sep 17 00:00:00 2001 From: Rebecca Xu Date: Tue, 20 Nov 2018 18:26:54 -0800 Subject: [PATCH] added parser function and replaced rsplit with url_parser --- kerckhoff/packages/models.py | 4 ++-- kerckhoff/packages/utils.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/kerckhoff/packages/models.py b/kerckhoff/packages/models.py index 02dc6e7..387617b 100644 --- a/kerckhoff/packages/models.py +++ b/kerckhoff/packages/models.py @@ -31,7 +31,7 @@ def as_dict(self): } def save(self, *args, **kwargs): - self.drive_folder_id = self.drive_folder_url.rsplit('/', 1)[-1] + self.drive_folder_id = url_parser(self.drive_folder_url) super().save(*args, **kwargs) def populate(self, user): @@ -140,7 +140,7 @@ def setup_and_save(self, user, pset_slug): self.drive_folder_id = drive_id self.drive_folder_url = url else: - folder_id = self.drive_folder_url.rsplit('/', 1)[-1] + folder_id = url_parser(self.drive_folder_url) details = get_file(google, folder_id) if details.get("mimeType") != "application/vnd.google-apps.folder": raise ValidationError({"drive_folder_url" : ["The Google drive link must be a link to an existing folder!"]}) diff --git a/kerckhoff/packages/utils.py b/kerckhoff/packages/utils.py index bcdc09d..dd87fb6 100644 --- a/kerckhoff/packages/utils.py +++ b/kerckhoff/packages/utils.py @@ -30,6 +30,13 @@ def replace_url(fn): text = IMAGE_REGEX.sub(replace_url,package.cached_article_preview) return text +def url_parser(url_string): + domain = url_string.split("?") + separate = domain[0].split('/') + url_string = separate[len(separate) - 1] + return url_string + + def transfer_to_s3(session: OAuth2Session, package): if package.images.get("s3") is None: package.images["s3"] = {}