def login(email, password, url, auth_user=None, auth_pass=None): """ Log in to the edX application via HTTP and parse sessionid from cookie. Args: email: Email address of edX user password: Password for the edX user url: Url of the edX application auth_user (Optional): Basic auth username auth_pass (Optional): Basic auth password Returns: A dictionary with the data needed to create a headers file for sitespeed.io, that will access the edX application as a logged-in user. { 'session_key': name of the session key cookie, 'session_id': the sessionid on the edx platform 'csrf_token': the csrf token } Raises: RuntimeError: If the login page is not accessible or the login fails. """ if (auth_user and auth_pass): auth = (auth_user, auth_pass) else: auth = None base_url = get_base_url(url) resp = requests.get('{}/login'.format(base_url), auth=auth) if resp.status_code != 200: msg = 'Failed accessing the login URL. Return code: {}'.format( resp.status_code) raise RuntimeError(msg) csrf = resp.cookies['csrftoken'] data = {'email': email, 'password': password} cookies = {'csrftoken': csrf} headers = {'referer': '{}/login'.format(base_url), 'X-CSRFToken': csrf} resp = requests.post( '{}/user_api/v1/account/login_session/'.format(base_url), data=data, cookies=cookies, headers=headers, auth=auth ) if resp.status_code != 200: msg = 'Failed logging in. Return code: {}'.format(resp.status_code) raise RuntimeError(msg) try: session_key = 'prod-edx-sessionid' session_id = resp.cookies[session_key] # production except KeyError: session_key = 'sessionid' session_id = resp.cookies[session_key] # sandbox return {'session_key': session_key, 'session_id': session_id, 'csrf_token': csrf}
def errorlog_missing_page(self, missing_spec, in_spec): """ Errorlog a wiki page that does not exist. """ missing_spec = remove_forward_slash(missing_spec) self.wiki2doc.errorlog.append(\ ("Specified link does not exist. Please check the " +\ "full path and the name of the following link:'{}']".format(\ missing_spec), get_base_url(self.req) + 'wiki/' + in_spec))
def get_wiki_hyperlink(self, spec_name, hyper): """ returns the wiki page hyperlink path for another page that is under same parent path. See regex_id 4. in find_hyperlinks. Example wikipage: http://10.45.43.145:8000/Coconut/ event/wiki/APO/IP006/Dummy-APO-Database/IP006-APO-Spec-Sill Example reference from inside the link above. [[Dummy-APO-Database/GPD/Material_Strength| MS-GPD]] [[Dummy-APO-Database/GPD/Metallic_Joint| MBJ-GPD]] [[/GPD/Material_Strength| MS-GPD]] [[GPD/Material_Strength| MS-GPD]] [[/IP006/Dummy-APO-Database/GPD/Material_Strength| MS-GPD]] [[IP006/Dummy-APO-Database/GPD/Material_Strength| MS-GPD]] [[IP006/Dummy-APO-Database/GPD/Material_Strength]] This works because both "IP006-APO-Spec-Sill" and "GPD/Material_Strength" are under: http://10.45.43.145:8000/Coconut/ event/wiki/APO/IP006/Dummy-APO-Database/ """ given_path = remove_forward_slash(hyper[1]) + hyper[2] given_path_list = given_path.split("/") full_wiki_path = get_base_url(self.req) +\ "wiki/" + spec_name full_path_list = full_wiki_path.split("/") protocol = full_path_list[0] full_path_list = full_path_list[2:] list_index = [] hyperlink = '' for i, item in enumerate(full_path_list): if item in set(given_path_list): list_index.append(i) if len(list_index) > 0: full_path_list = full_path_list[:list_index[0]] elif len(list_index) == 0: full_path_list = full_path_list[:-1] mod_full_path = '' for item in full_path_list: mod_full_path += item + "/" hyperlink = protocol + "//" + mod_full_path + given_path return hyperlink
def get_link_path(self, spec_name, regex_id, hyper): """ for a given hypermatch this function returns the hyperlink path.""" hyperlink = '' if regex_id == 4: another_child_spec_name = get_wiki_specname(spec_name, hyper) page = \ self.wiki2doc.get_wikipage( remove_forward_slash(another_child_spec_name)) if not page: self.errorlog_missing_page(another_child_spec_name, spec_name) hyperlink = self.get_wiki_hyperlink(spec_name, hyper) elif hyper[1] == '/wiki/': page = self.wiki2doc.get_wikipage(hyper[2]) if not page: self.errorlog_missing_page(hyper[2], spec_name) hyperlink = get_base_url(self.req) +\ remove_forward_slash(hyper[1]) +\ hyper[2] elif hyper[1] == 'e:/wiki/': page = self.wiki2doc.get_wikipage(hyper[2]) if not page: self.errorlog_missing_page(hyper[2], spec_name) hyperlink = get_base_url(self.req) +\ 'wiki/' + hyper[2] elif hyper[1] == 'wiki:': page = \ self.wiki2doc.get_wikipage(remove_forward_slash(hyper[2])) if not page: self.errorlog_missing_page(hyper[2], spec_name) hyperlink = get_base_url(self.req) +\ 'wiki/' + hyper[2] else: hyperlink = hyper[1] + hyper[2] return hyperlink
def parse_html(self, args, context, wiki): """ Parse html string to docx args[1] = paragraph, context, wiki, args[3] = spec_name""" try: html_code = HtmlFormatter(self.env, context, wiki).generate() #print('inside parse_html before DocumentHTMLParser') DocumentHTMLParser(self.document, args[1], html_code) #print('inside parse_html after DocumentHTMLParser') return html_code except AttributeError: self.wiki2doc.errorlog.append( ("HtmlFormatter could not parse" +\ " the following wikitext: {}".format(wiki), get_base_url(self.req) + 'wiki/' + args[3]))
def find_merged_cells(self, args): """ for a given table data, analyses the data and finds merged cells. args = [data, table, col_size, spec_name]""" print('inside find_merged_cells args:', args) merge_list = [] table_row_length = set() for idr, row in enumerate(args[0]): row_length = 0 for item in row: row_length += len(item) params = [ idr, row, table_row_length, args[2], row_length, args[1], args[3] ] print('inside find_merged_cells params:', params) args[1], table_row_length, merge_row = self.get_merge_row(params) merge_list.append(merge_row) merge_row = [] row_length = 0 if idr < len(args[0]) - 1: args[1].add_row() if len(list(table_row_length)) > 0: page_path = get_base_url(self.req) +\ 'wiki/' + args[3] self.wiki2doc.errorlog.append(( "There might be an extra pipe || in the wikitext of" +\ " a table that needs to be removed. Number of" +\ " columns in each row must match including merged" +\ " cells! Check the following table with a:" +\ " header: {}".format(args[0][0]), page_path)) return (args[1], merge_list)
def get_template(self, req): """ return path of standard auto report template """ print("get_template:") print(req) page_path = get_base_url(req) + 'wiki/' + TEMPLATE_PAGE # self.envs[TEMPLATE_INSTANCE].project_url +\ # '/wiki/' + TEMPLATE_PAGE print("page_path", page_path) for attachment in Attachment.select(self.env, 'wiki', TEMPLATE_PAGE): if attachment.filename == TEMPLATE_NAME: return attachment.path self.errorlog.append( ("Attachment {} could not be found at {}.".\ format(TEMPLATE_NAME, TEMPLATE_PAGE), page_path))