<s> import os <EOL> import sys <EOL> import json <EOL> from github import Github <EOL> from flask import * <EOL> from pymongo import MongoClient <EOL> import utils <EOL> from auth import * <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( auth ) <EOL> app . debug = True <EOL> app . secret_key = os . environ [ '<STR_LIT>' ] <EOL> app . jinja_env . add_extension ( '<STR_LIT>' ) <EOL> db_name = os . environ [ '<STR_LIT>' ] <EOL> mongo = MongoClient ( os . environ [ '<STR_LIT>' ] ) <EOL> users_db = mongo [ db_name ] . users <EOL> teams_db = mongo [ db_name ] . teams <EOL> users_db . ensure_index ( "<STR_LIT>" , unique = True , dropDups = True ) ; <EOL> teams_db . ensure_index ( "<STR_LIT:id>" , unique = True , dropDups = True ) ; <EOL> MAX_TEAM_MEMBERS = <NUM_LIT:3> <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def main ( ) : <EOL> return render_template ( "<STR_LIT>" ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] ) <EOL> @ auth_check <EOL> def user ( github ) : <EOL> user_info = getGHcreds ( github ) <EOL> user = users_db . find_one ( { "<STR_LIT>" : user_info [ "<STR_LIT>" ] } ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> if user : <EOL> user_info [ "<STR_LIT>" ] = user [ "<STR_LIT>" ] <EOL> user_info [ "<STR_LIT>" ] = user [ "<STR_LIT>" ] <EOL> return render_template ( '<STR_LIT>' , ** user_info ) <EOL> else : <EOL> user_info [ "<STR_LIT>" ] = request . json [ "<STR_LIT>" ] <EOL> team_id = user_info [ "<STR_LIT>" ] = request . json [ "<STR_LIT>" ] <EOL> team = teams_db . find_one ( { "<STR_LIT:id>" : team_id } ) <EOL> if ( team_id != "<STR_LIT>" ) and ( not team ) : <EOL> return json . dumps ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> if team and team [ "<STR_LIT:size>" ] >= MAX_TEAM_MEMBERS and user [ "<STR_LIT>" ] != team_id : <EOL> return json . dumps ( { "<STR_LIT:error>" : "<STR_LIT>" + str ( MAX_TEAM_MEMBERS ) } ) <EOL> users_db . update ( { "<STR_LIT>" : user_info [ "<STR_LIT>" ] } , user_info , upsert = True ) ; <EOL> if team and ( not user or user [ "<STR_LIT>" ] != team_id ) : <EOL> team [ "<STR_LIT:size>" ] += <NUM_LIT:1> <EOL> teams_db . update ( { "<STR_LIT:id>" : team_id } , team ) <EOL> return json . dumps ( { "<STR_LIT:success>" : '<STR_LIT>' } ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] ) <EOL> @ auth_check <EOL> def team ( github , id ) : <EOL> user = getGHcreds ( github ) <EOL> team = teams_db . find_one ( { "<STR_LIT:id>" : id } ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> if not team : team = { } <EOL> team [ "<STR_LIT>" ] = [ ] <EOL> seenUser = False <EOL> for member in users_db . find ( { "<STR_LIT>" : id } ) : <EOL> team [ "<STR_LIT>" ] . append ( member ) <EOL> if member [ "<STR_LIT>" ] == user [ "<STR_LIT>" ] : <EOL> seenUser = True <EOL> if not seenUser : <EOL> team = { } <EOL> return render_template ( '<STR_LIT>' , ** team ) <EOL> else : <EOL> team [ "<STR_LIT:name>" ] = request . json [ "<STR_LIT:name>" ] <EOL> teams_db . update ( { "<STR_LIT:id>" : id } , team ) ; <EOL> return json . dumps ( { "<STR_LIT:success>" : '<STR_LIT>' } ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] ) <EOL> @ auth_check <EOL> def quit ( github , id ) : <EOL> user_info = getGHcreds ( github ) <EOL> user = users_db . find_one ( { "<STR_LIT>" : user_info [ "<STR_LIT>" ] } ) <EOL> team = teams_db . find_one ( { "<STR_LIT:id>" : id } ) <EOL> old_team = user [ '<STR_LIT>' ] <EOL> user [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> users_db . update ( { "<STR_LIT>" : user [ "<STR_LIT>" ] } , user ) ; <EOL> if team and old_team == id : <EOL> team [ "<STR_LIT:size>" ] -= <NUM_LIT:1> <EOL> if team [ "<STR_LIT:size>" ] <= <NUM_LIT:0> : <EOL> teams_db . remove ( { "<STR_LIT:id>" : id } ) <EOL> else : <EOL> teams_db . update ( { "<STR_LIT:id>" : id } , team ) <EOL> return redirect ( "<STR_LIT>" ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> @ auth_check <EOL> def key ( github ) : <EOL> user = getGHcreds ( github ) <EOL> key = request . form [ "<STR_LIT:key>" ] ; <EOL> team = { <EOL> "<STR_LIT:id>" : key , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : user [ "<STR_LIT>" ] , <EOL> "<STR_LIT:size>" : <NUM_LIT:0> <EOL> } <EOL> teams_db . insert ( team ) ; <EOL> return "<STR_LIT>" <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ auth_check <EOL> @ admin_check <EOL> def create_proj ( github , id ) : <EOL> return Response ( utils . createProj ( teams_db , users_db , github , id ) , mimetype = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ auth_check <EOL> @ admin_check <EOL> def fix_teams ( github ) : <EOL> return Response ( utils . fixTeamDB ( teams_db , users_db ) , mimetype = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ auth_check <EOL> @ admin_check <EOL> def csv ( github ) : <EOL> csv = "<STR_LIT>" <EOL> for team in teams_db . find ( { "<STR_LIT:size>" : { "<STR_LIT>" : <NUM_LIT:0> } } ) : <EOL> csv += team [ '<STR_LIT:id>' ] + "<STR_LIT:U+002C>" <EOL> i = MAX_TEAM_MEMBERS <EOL> for user in users_db . find ( { "<STR_LIT>" : team [ '<STR_LIT:id>' ] } ) : <EOL> csv += user [ '<STR_LIT>' ] + "<STR_LIT:U+002C>" <EOL> i -= <NUM_LIT:1> <EOL> while i : <EOL> csv += "<STR_LIT:U+002C>" <EOL> i -= <NUM_LIT:1> ; <EOL> csv += str ( team [ '<STR_LIT:size>' ] ) + "<STR_LIT:\n>" <EOL> response = Response ( csv , mimetype = '<STR_LIT>' ) <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return response </s>
