def test_fetching_event_metadata_timeout(self, mock): "Ensure 'fetch_workshop_metadata' reacts to timeout." website_url = "https://pbanaszkiewicz.github.io/workshop" mock.register_uri( "GET", website_url, exc=requests.exceptions.ConnectTimeout, ) with self.assertRaises(requests.exceptions.ConnectTimeout): fetch_workshop_metadata(website_url)
def test_fetching_event_metadata_html(self, mock): "Ensure 'fetch_workshop_metadata' works correctly with HTML metadata provided." website_url = 'https://pbanaszkiewicz.github.io/workshop' repo_url = ('https://raw.githubusercontent.com/pbanaszkiewicz/' 'workshop/gh-pages/index.html') mock.get(website_url, text=self.html_content, status_code=200) mock.get(repo_url, text='', status_code=200) metadata = fetch_workshop_metadata(website_url) self.assertEqual(metadata['slug'], '2015-07-13-test')
def test_fetching_event_metadata_yaml(self, mock): "Ensure 'fetch_workshop_metadata' works correctly with YAML metadata provided." website_url = "https://pbanaszkiewicz.github.io/workshop" repo_url = ("https://raw.githubusercontent.com/pbanaszkiewicz/" "workshop/gh-pages/index.html") mock.get(website_url, text="", status_code=200) mock.get(repo_url, text=self.yaml_content, status_code=200) metadata = fetch_workshop_metadata(website_url) self.assertEqual(metadata["slug"], "workshop")
def get_form_kwargs(self): """Extend form kwargs with `initial` values. The initial values are read from SelfOrganisedSubmission request object, and from corresponding workshop page (if it's possible).""" kwargs = super().get_form_kwargs() # no matter what, don't show "lessons" field; previously they were shown # when mix&match was selected kwargs["show_lessons"] = False url = self.other_object.workshop_url.strip() data = { "url": url, "host": self.other_object.host_organization() or self.other_object.institution, "administrator": Organization.objects.get(domain="self-organized"), } try: metadata = fetch_workshop_metadata(url) parsed_data = parse_workshop_metadata(metadata) except (AttributeError, HTTPError, RequestException, WrongWorkshopURL): # ignore errors, but show warning instead messages.warning( self.request, "Cannot automatically fill the form " "from provided workshop URL.", ) else: # keep working only if no exception occurred try: lang = parsed_data["language"].lower() parsed_data["language"] = Language.objects.get(subtag=lang) except (KeyError, ValueError, Language.DoesNotExist): # ignore non-existing messages.warning(self.request, "Cannot automatically fill language.") # it's easier to remove bad value than to leave # 500 Server Error when the form is rendered if "language" in parsed_data: del parsed_data["language"] data.update(parsed_data) if "instructors" in data or "helpers" in data: instructors = data.get("instructors") or ["none"] helpers = data.get("helpers") or ["none"] data["comment"] = (f"Instructors: {','.join(instructors)}\n\n" f"Helpers: {','.join(helpers)}") initial = super().get_initial() initial.update(data) kwargs["initial"] = initial return kwargs
def test_fetching_event_metadata_timeout(self, mock): "Ensure 'fetch_workshop_metadata' reacts to timeout." website_url = 'https://pbanaszkiewicz.github.io/workshop' repo_url = ('https://raw.githubusercontent.com/pbanaszkiewicz/' 'workshop/gh-pages/index.html') mock.register_uri( "GET", website_url, exc=requests.exceptions.ConnectTimeout, ) with self.assertRaises(requests.exceptions.ConnectTimeout): metadata = fetch_workshop_metadata(website_url)
def get_event_metadata(self, event_url): """Get metadata from event (location, instructors, helpers, etc.).""" metadata = fetch_workshop_metadata(event_url) # normalize the metadata metadata = parse_workshop_metadata(metadata) return metadata