MCPcopy Index your code
hub / github.com/ronreiter/interactive-tutorials / index

Function index

main.py:493–564  ·  view source on GitHub ↗
(title, language="en")

Source from the content-addressed store, hash-verified

491@app.route("/<title>", methods=["GET", "POST"])
492@app.route("/<language>/<title>", methods=["GET", "POST"])
493def index(title, language="en"):
494 tutorial = title.replace("_", " ")
495 try:
496 current_tutorial_data = get_tutorial(tutorial, language)
497 except KeyError:
498 return error404()
499 domain_data = get_domain_data()
500 domain_data["language_code"] = language
501
502 if request.method == "GET":
503 title_suffix = "Learn %s - Free Interactive %s Tutorial" % (domain_data["language_uppercase"], domain_data["language_uppercase"])
504 html_title = "%s - %s" % (title.replace("_", " "), title_suffix) if title != "Welcome" else title_suffix
505
506 if not "uid" in session:
507 session["uid"] = binascii.b2a_hex(os.urandom(16))
508
509 uid = session["uid"]
510
511 try:
512 site_links = tutorial_data[get_host()][language]["Welcome"]["links"]
513 except Exception as e:
514 site_links = []
515 logging.error("cant get site links for %s %s" % (get_host(), language))
516
517 print(request.host)
518
519 # https://ipinfo.io/1.2.3.4/json - country = DE
520 # check if user is German
521 # xff = request.headers.get("X-Forwarded-For")
522 # if xff and "," in xff:
523 # ip = xff.split(",")[0]
524 # else:
525 # ip = request.remote_addr
526
527 # try:
528 # with geoip2.database.Reader('GeoLite2-Country.mmdb') as reader:
529 # response = reader.country(ip)
530 # except Exception as e:
531 # logging.exception('error looking up IP %s' % ip)
532 # response = None
533 # is_german_user = response and response.country.iso_code == 'DE'
534
535 return make_response(render_template(
536 "index-python.html" if (domain_data["language"] == "python") else "index.html",
537 tutorial_page=tutorial != "Welcome",
538 domain_data=domain_data,
539 all_data=constants.DOMAIN_DATA,
540 site_tutorial_links=site_links,
541 tutorial_data=current_tutorial_data,
542 tutorial_data_json=json.dumps(current_tutorial_data),
543 domain_data_json=json.dumps(domain_data),
544 html_title=html_title,
545 language_code=language,
546 languages=get_languages(),
547 language_names=get_language_names(),
548 uid=uid,
549 env="dev" if request.host == "localhost:5000" else "prod",
550 is_german_user=False,

Callers 1

default_indexFunction · 0.85

Calls 8

get_tutorialFunction · 0.85
error404Function · 0.85
get_domain_dataFunction · 0.85
get_hostFunction · 0.85
printFunction · 0.85
get_languagesFunction · 0.85
get_language_namesFunction · 0.85
run_codeFunction · 0.85

Tested by

no test coverage detected