def signin(request_url): title_dict = { "surveygroup": "Survey Group", "demographics": "Demographics", "profile": "My Profile", "predictions": "Predictions" } if request.method == "GET": return render_template('auth.html', title=title_dict[request_url], page=pageData["auth"][0], site=site, CLIENT_ID=constants.get_google_client_id(), request_url=request_url) else: try: email_cookie_key = None id_cookie_key = None token = request.data userEmail, userId = auth.authenticate_google_signin(token) email_dict = None if request_url in ["surveygroup", "demographics"]: email_cookie_key = get_email_cookie_key("demographics") id_cookie_key = get_id_cookie_key("demographics") db = auth.get_survey_firestore_client() auth.create_respondent(userEmail, userId, db) elif request_url in ["profile"]: email_cookie_key = get_email_cookie_key("profile") id_cookie_key = get_id_cookie_key("profile") db = auth.get_website_firestore_client() Member.get_member(userEmail, userId, db) elif request_url in ["predictions"]: email_cookie_key = get_email_cookie_key("predictions") id_cookie_key = get_id_cookie_key("predictions") db = auth.get_survey_firestore_client() auth.create_prediction_user(userEmail, userId, db) # set the values of cookies to persist sign in response = make_response("SUCCESS", 201) response.set_cookie(email_cookie_key, userEmail) response.set_cookie(id_cookie_key, userId) return response except Exception as e: print(e) # if there is an error, delete their cookies and indicate failure response = make_response("FAILURE", 406) response.set_cookie(email_cookie_key, expires=0) response.set_cookie(id_cookie_key, expires=0) return response
def demographics(): userEmail = None userId = None db = auth.get_survey_firestore_client() emails_ref = db.collection("emails") email_cookie_key = get_email_cookie_key("demographics") id_cookie_key = get_id_cookie_key("demographics") if request.method == 'GET': if email_cookie_key in request.cookies: userEmail = request.cookies[email_cookie_key] if id_cookie_key in request.cookies: userId = request.cookies[id_cookie_key] if not auth.is_authenticated(userEmail, userId, emails_ref): return redirect("/auth/surveygroup/") responsesDict = auth.get_responses_dict(userEmail, db) return render_template("demographics.html", page=pageData["demographics"][0], site=site, demographics=responsesDict["demographics"], questions=demographicQuestions(), CLIENT_ID=constants.get_google_client_id(), responded=False) else: userEmail = request.cookies[email_cookie_key] userId = request.cookies[id_cookie_key] if auth.is_authenticated(userEmail, userId, emails_ref): server.demographics.update_demographics(userEmail, request.form, demographicQuestions(), db) responsesDict = auth.get_responses_dict(userEmail, db) return render_template("demographics.html", page=pageData["demographics"][0], site=site, demographics=responsesDict["demographics"], questions=demographicQuestions(), CLIENT_ID=constants.get_google_client_id(), responded=True) else: # this happens if for some reason they've tried to f**k with their email or something gets corrupted abort( "User credentials improper. Please sign out and sign back in")
def update_prediction_user_scores(): db = auth.get_survey_firestore_client() server.predictions.update_all_scores(db)
def finals_app(): finals_semester = "2019_FALL" final_data = [ row for row in csv.reader(open( 'static/assets/webapp-data/finalsf19_nov14.csv', 'r'), delimiter=",", quotechar='|') ] courses = [] for row in final_data: courses.append(row[0]) if request.method == "GET": return render_template("webapps/finals.html", page=pageData["finals_app"][0], site=site, courses=courses, questions=finalsQuestions()) elif request.method == "POST": form_data = request.form form_classes = request.form.getlist('classes') ga_id: str = request.cookies.get("_ga", None) if ga_id is not None: db = auth.get_survey_firestore_client() doc = db.collection("finals_classes").document(ga_id).get() if not doc.exists: db.collection("finals_classes").document(ga_id).set({ "year": form_data.get("year", None), "house": form_data.get("house", None), "concentration": form_data.get("concentration", None), finals_semester: form_classes }) else: db.collection("finals_classes").document(ga_id).update({ "year": form_data.get("year", None), "house": form_data.get("house", None), "concentration": form_data.get("concentration", None), finals_semester: form_classes }) examInfo = [] gLinks = [] lday = 0 i = 0 for i in range(0, len(form_classes)): j = 0 while j < len(courses): if form_classes[i] == courses[j]: date = final_data[j][3] #find last exam day if int(date[3:5]) > lday: lday = int(date[3:5]) #calc gcal links, adjust codes depending on fall/spring if final_data[j][4] == "09:00 AM": sCode = "201912" + date[3:5] + "T140000Z" eCode = "201912" + date[3:5] + "T170000Z" elif final_data[j][4] == "02:00 PM": sCode = "201912" + date[3:5] + "T190000Z" eCode = "201912" + date[3:5] + "T220000Z" calLink = "http://www.google.com/calendar/event?action=TEMPLATE&dates=" + sCode + "%2F" + eCode + "&text=" + courses[ j] + "%20Final&location=" + final_data[j][6] #gLinks.append(calLink.replace(" ","%20")) examInfo.append("Your " + courses[j] + " Final is " + date + " at " + final_data[j][4] + " in " + final_data[j][6] + " <a href=\"" + calLink.replace(" ", "%20") + "\" target=\"_blank\">Add to GCal</a>") j = len(courses) else: j += 1 i += 1 fLink = "https://www.google.com/flights?lite=0#flt=BOS.2019-12-" + str( lday) + ";c:USD;e:1;sd:1;t:f;tt:o" return render_template("webapps/finalsresult.html", page=pageData["finals_app"][0], site=site, examInfo=examInfo, gLinks=gLinks, fLink=fLink)
def predictions(): userEmail = None userId = None db = auth.get_survey_firestore_client() prediction_users_ref = db.collection("prediction_users") email_cookie_key = get_email_cookie_key("predictions") id_cookie_key = get_id_cookie_key("predictions") current_time = datetime.datetime.now(tz=easternTime()) questions = sorted(getYml("./data/predictions.yml"), key=lambda x: x["deadline"], reverse=False) if request.method == 'GET': if email_cookie_key in request.cookies: userEmail = request.cookies[email_cookie_key] if id_cookie_key in request.cookies: userId = request.cookies[id_cookie_key] if not auth.is_authenticated(userEmail, userId, prediction_users_ref): return render_template( "webapps/predictions.html", site=site, page=pageData["predictions"][0], questions=questions, predictions={}, CLIENT_ID=constants.get_google_client_id(), responded=False, signed_in=False, current_time=current_time, to_datetime=yml_str_to_datetime, to_display_str=datetime_to_display_str, get_points=server.predictions.calculate_points, user_score=None, leaders=[], consent_checked=False, username=None) predictionsDict = auth.get_predictions_dict(userEmail, db) user_score = server.predictions.get_user_score(userEmail, db) consent_checked = server.predictions.can_be_displayed(userEmail, db) leaders = server.predictions.get_leaderboard(db) username = userEmail.split("@")[0] return render_template("webapps/predictions.html", site=site, page=pageData["predictions"][0], questions=questions, predictions=predictionsDict, CLIENT_ID=constants.get_google_client_id(), responded=False, signed_in=True, current_time=current_time, to_datetime=yml_str_to_datetime, to_display_str=datetime_to_display_str, get_points=server.predictions.calculate_points, user_score=user_score, leaders=leaders, consent_checked=consent_checked, username=username) else: userEmail = request.cookies[email_cookie_key] userId = request.cookies[id_cookie_key] if auth.is_authenticated(userEmail, userId, prediction_users_ref): server.predictions.update_predictions( userEmail, request.form, getYml("./data/predictions.yml"), db) predictionsDict = auth.get_predictions_dict(userEmail, db) user_score = server.predictions.get_user_score(userEmail, db) consent_checked = server.predictions.can_be_displayed( userEmail, db) leaders = server.predictions.get_leaderboard(db) username = userEmail.split("@")[0] return render_template( "webapps/predictions.html", site=site, page=pageData["predictions"][0], questions=questions, predictions=predictionsDict, CLIENT_ID=constants.get_google_client_id(), responded=True, signed_in=True, current_time=current_time, to_datetime=yml_str_to_datetime, to_display_str=datetime_to_display_str, get_points=server.predictions.calculate_points, user_score=user_score, leaders=leaders, consent_checked=consent_checked, username=username) else: # this happens if for some reason they've tried to f**k with their email or something gets corrupted abort( "User credentials improper. Please sign out and sign back in")