def get_talent(contribution_id, tid): headers = requestutil.get_header_using_session(session) try: if id: result = requestutil.request_talent(headers, contribution_id, tid) if result.status_code != 200: err_json = parse_response_error(result) logging.error("Talent GET " + json.dumps(err_json)) return {} else: print("GET ok.".format(id)) except Exception: # traceback.print_exc() return False return result.json()
def talent_details(contribution_id, id): # check if the user is logged in is_logged_in = False try: # create error to see if the use is logged in or now # TODO this should be changed to better way if (session["name"] == ""): is_logged_in = True else: is_logged_in = True except: is_logged_in = False is_reviewer = False name = "" if (is_logged_in): is_contribution_admin = False the_json_res = get_talent(contribution_id, id) username = session["username"] name = session["name"] contribution_admins = the_json_res["contributionAdmins"] if username in contribution_admins: is_contribution_admin = True if is_contribution_admin: is_reviewer = True else: # check if the user is reviewer by requesting to endpoint username = session["username"] headers = requestutil.get_header_using_session(session) is_reviewer = adminutil.check_if_reviewer(username, headers) return render_template("contribute/talent_details.html", reviewer=is_reviewer, post=the_json_res, user=name) else: the_json_res = get_talent_with_api_key(contribution_id, id) return render_template("contribute/talent_details.html", reviewer=is_reviewer, post=the_json_res)
def get_contribution(contribution_id): headers = requestutil.get_header_using_session(session) try: if contribution_id: result = requests.get(cfg.CONTRIBUTION_BUILDING_BLOCK_URL + "/" + str(contribution_id), headers=headers) if result.status_code != 200: err_json = parse_response_error(result) logging.error("Contribution GET " + json.dumps(err_json)) return {} else: print("GET ok.".format(contribution_id)) except Exception: # traceback.print_exc() return False return result.json()
def create(): json_contribute = None if request.method == 'POST': result = request.form.to_dict(flat=False) # result = dict((key, request.form.getlist(key) if len(request.form.getlist(key)) > 1 else request.form.getlist(key)[0]) for key in request.form.keys()) contribution = to_contribution(result) # add contributionAdmins to the json_contribution contribution = jsonutil.add_contribution_admins(contribution) contribution["status"] = "Submitted" json_contribution = json.dumps(contribution, indent=4) response, s, post_json = post_contribution(json_contribution) if response: return redirect( url_for('contribute.contribution_details', contribution_id=s)) elif not response: logging.error(s) msg = "Contribution submission failed. Please try again after some time!" if "name" in session: return render_template( 'contribute/error.html', user=session["name"], token=session['oauth_token']['access_token'], error_msg=msg) else: return render_template('contribute/error.html', error_msg=msg) # get capability list to create required capability list header = requestutil.get_header_using_session(session) required_capability_list = requestutil.request_required_capability_list( header) return render_template('contribute/contribute.html', required_capabilities=required_capability_list, post=json_contribute, user=session["name"], token=session['oauth_token']['access_token'])
def put_contribution(json_data, contribution_id): headers = requestutil.get_header_using_session(session) try: # set PUT url put_url = cfg.CONTRIBUTION_BUILDING_BLOCK_URL + "/" + contribution_id # Setting up post request result = requests.put(put_url, headers=headers, data=json_data) if result.status_code != 200: logging.error("PUT method failed. " + str(result.text)) return False, str( "Error Code: " + str(result.status_code) + ". There was an error when editing your Contribution. Please try again later!" ) else: logging.info("PUT OK.".format(contribution_id)) return True, str( "Your Contribution has been successfully updated.") except Exception: traceback.print_exc() var = "There was an error when updating your Contribution. Please try again later!" return False, var
def home(): show_sel = request.args.get('show') user = None token = None is_logged_in = False cap_json = [] tal_json = [] if 'GIT_TAG' in os.environ: git_tag = os.environ['GIT_TAG'] else: git_tag = '' if 'GIT_SHA' in os.environ: git_sha = os.environ['GIT_SHA'] else: git_sha = '' try: # create error to see if the user is logged in or now # TODO this should be changed to better way if (session["name"] == ""): is_logged_in = True else: is_logged_in = True user = session["name"] token = session['oauth_token']['access_token'] except: is_logged_in = False if (is_logged_in): # query with auth headers = requestutil.get_header_using_session(session) result = requestutil.request_contributions(headers) if show_sel == "capability": # create the json for only capability cap_json = jsonutil.create_capability_json_from_contribution_json( result.json()) elif show_sel == "talent": # create the json for only talent tal_json = jsonutil.create_talent_json_from_contribution_json( result.json()) else: # create the json for only capability and talent cap_json = jsonutil.create_capability_json_from_contribution_json( result.json()) tal_json = jsonutil.create_talent_json_from_contribution_json( result.json()) return render_template('contribute/home.html', git_tag=git_tag, git_sha=git_sha, cap_json=cap_json, tal_json=tal_json, show_sel=show_sel, user=user) else: # query only published ones headers = requestutil.get_header_using_api_key() result = requestutil.request_contributions(headers) if show_sel == "capability": # create the json for only capability cap_json = jsonutil.create_capability_json_from_contribution_json( result.json()) elif show_sel == "talent": # create the json for only talent tal_json = jsonutil.create_talent_json_from_contribution_json( result.json()) else: # create the json for only capability and talent cap_json = jsonutil.create_capability_json_from_contribution_json( result.json()) tal_json = jsonutil.create_talent_json_from_contribution_json( result.json()) return render_template('contribute/home.html', git_tag=git_tag, git_sha=git_sha, cap_json=cap_json, tal_json=tal_json, show_sel=show_sel)
def contribution_edit(contribution_id): if request.method == 'POST': is_put = False contribution_id = None result = request.form.to_dict(flat=False) # check if it is PUT try: contribution_id = result["contribution_id"][0] is_put = True except: s = "There is a error in edit. The method is not an edit." return render_template('contribute/error.html', error_msg=s) if is_put: contribution = to_contribution(result) contribution = jsonutil.add_contribution_admins(contribution, is_edit=True) # remove id from json_data del contribution["id"] json_contribution = json.dumps(contribution, indent=4) response, s = put_contribution(json_contribution, contribution_id) if response: if "name" in session: return render_template( 'contribute/submitted.html', user=session["name"], token=session['oauth_token']['access_token']) else: return render_template('contribute/submitted.html') elif not response: if "name" in session: return render_template( 'contribute/error.html', user=session["name"], token=session['oauth_token']['access_token'], error_msg=s) else: return render_template('contribute/error.html', error_msg=s) else: the_json_res = get_contribution(contribution_id) # check if the user is editable then set the is_editable is_editable = False username = session["username"] headers = requestutil.get_header_using_session(session) is_editable = adminutil.check_if_reviewer(username, headers) # get capability list to create required capability list required_capability_list = requestutil.request_required_capability_list( headers) if is_editable: return render_template( 'contribute/contribute.html', required_capabilities=required_capability_list, is_editable=is_editable, user=session["name"], token=session['oauth_token']['access_token'], post=the_json_res) else: s = "You don't have a permission to edit the contribution." return render_template('contribute/error.html', error_msg=s)