def find_short_name(new_name): # Create a shortened version of this name to appear in URLs # Get ride of spaces and the word "challenge" first short_name = new_name.replace(" ", "") short_name = short_name.replace("Challenge", "") short_name = short_name.replace("challenge", "") if len(short_name) < 25: short_name = short_name[0:15] # For quite long names we'll start short version on second word else: start_short = new_name.find(' ') short_name = short_name[start_short:start_short + 15] # Confirm this particular short name is not already present in the db already_exists = True appended_number = 1 while already_exists: already_exists = Contract.query( Contract.short_name == short_name).get() if already_exists: appendage = str(appended_number) if short_name[-len(appendage):] == appendage: short_name = short_name[-len(appendage):] + str( appended_number + 1) appended_number += 1 # Get the next contract_id in order for the new entry curr_top = Contract.query().order(-Contract.contract_id).get() if curr_top: new_id = curr_top.contract_id + 1 else: new_id = 1 return new_id, short_name
def check_user_auth(short_name): user = users.get_current_user() # Can't see it if they're not even logged in! if not user: return False # Find all the users associated with this challenge contract = Contract.query(Contract.short_name == short_name).get() if not contract: logging.info("Couldnt find that one") return False contract_id = contract.contract_id combatants_info = fetch_combatants_info(contract_id) users_array = [] # Get all users associated with each distinct combatant for combatant in combatants_info: users_info = fetch_users_info(combatant.combatant_id) # Fill the array of users associated with combatant.name with their google emails for user_info in users_info: users_array.append(str(user_info.google_username)) # If the current user's id is in users_array then they're good to go return str(user) in users_array
def get(self, short_name): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) # Check that the user that logged in is someone who was invited to the challenge, or already in invited = check_user_desired(short_name, user.email()) # Add the user to the competition if they were invited if invited: # Grab the username from their email username = grab_username(user.email()) # Create a new user model if this username is not in the db already user_id = create_or_fetch_user(username) # Combatant details combatant_id = create_combatant(username) # Link combatant and user link_combatant_user(combatant_id, user_id) # Get the contract id con_id = Contract.query( Contract.short_name == short_name).get().contract_id # Link contract and combatant link_contract_combatant(con_id, combatant_id) # Tell them to get an invite if they weren't invited context = {"invited": invited} not_invited_page = jinja_environment.get_template("pages/joining.html") self.response.out.write(not_invited_page.render(context))
def get(self, short_name): should_be_here = check_user_auth(short_name) if not should_be_here: self.redirect('/') # Build up context that only contains an array of combatant_name : [user1, user2] objects context = {'joined': []} # Show a list of already-joined combatants with their usernames contract_id = Contract.query( Contract.short_name == short_name).get().contract_id combatants_info = fetch_combatants_info(contract_id) # Get all users associated with each distinct combatant for combatant in combatants_info: users_info = fetch_users_info(combatant.combatant_id) users_array = [] # Fill the array of users associated with combatant.name with their google emails for user_info in users_info: users_array.append(user_info.google_username + "@gmail.com") # Put the combatant-users object into context's array of these objects context['joined'].append({ 'combatant': combatant.name, 'users': users_array }) # Render the page in context and display it invite_page = jinja_environment.get_template("pages/invite.html") self.response.out.write(invite_page.render(context))
def get(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) # Create a dictionary of the number of users in each challenge contract_users = {} all_contract_combatants = ContractCombatant.query().fetch(100) for con_com in all_contract_combatants: users_num = CombatantUser.query( CombatantUser.combatant_id == con_com.combatant_id).count() if con_com.contract_id in contract_users: contract_users[con_com.contract_id] += users_num else: contract_users[con_com.contract_id] = users_num # Find the 3 contract ids with the most users involved desc = sorted(contract_users.iteritems(), key=lambda x: x[1], reverse=True) challenge1_name = challenge2_name = challenge3_name = "" if len(desc) > 0: challenge1_name = Contract.query( Contract.contract_id == desc[0][0]).get().challenge_name if len(desc) > 1: challenge2_name = Contract.query( Contract.contract_id == desc[1][0]).get().challenge_name if len(desc) > 2: challenge3_name = Contract.query( Contract.contract_id == desc[2][0]).get().challenge_name context = { 'exampleText': "Who runs the most miles in a month?", 'topChallenge1': challenge1_name, 'topChallenge2': challenge2_name, 'topChallenge3': challenge3_name } home = jinja_environment.get_template("pages/frontpage.html") self.response.out.write(home.render(context))
def check_user_desired(short_name, email): contract = Contract.query(Contract.short_name == short_name).get() des_users = DesiredUsers.query( DesiredUsers.contract_id == contract.contract_id).get() if not des_users: return False user_list = des_users.users if email not in user_list: return False return True
def update_challenge(short_name, args_obj): # Get the contract to be updateds contract = Contract.query(Contract.short_name == short_name).get() # Update all fields represented in args_obj #contract.challenge_name = args_obj["description"] contract.objective_type = str(args_obj["objective"]) contract.time_unit = str(args_obj["unit"]) contract.time_period = int(args_obj["length"]) contract.start_date = datetime.date(int(args_obj["year"]), int(args_obj["month"]), int(args_obj["day"])) if not contract.stakes_id: curr_stakes = [0, 0, 0, 0] else: curr_stakes = contract.stakes_id stakes_ids = enter_stakes(args_obj["stakes"], curr_stakes) contract.stakes_id = stakes_ids contract.put() # Enter in Objective models if contract.objective_type == "highest-occurrence": # Get current highest GeneralObjective id top_objective = GeneralObjective.query().order( -GeneralObjective.gen_objective_id).get() if top_objective: new_id = top_objective.gen_objective_id + 1 else: new_id = 1 # Make new GeneralObjective model GeneralObjective(objective_name=str(args_obj["objective-name"]), contract_id=contract.contract_id, gen_objective_id=new_id).put() elif contract.objective_type == "location": # Get current highest GeolocationObjective id top_objective = GeolocationObjective.query().order( -GeolocationObjective.geo_objective_id).get() if top_objective: new_id = top_objective.geo_objective_id + 1 else: new_id = 1 # Make new GeolocationObjective model GeolocationObjective(checkin_loc=ndb.GeoPt(args_obj["checkin-loc"]), checkin_radius=int(args_obj["radius"]), loc_name=args_obj["checkin-loc-name"], geo_objective_id=new_id, contract_id=contract.contract_id).put()
def add_desired_user(short_name, email): # Find appropriate contract_id, then get desired users list contract = Contract.query(Contract.short_name == short_name).get() players = DesiredUsers.query( DesiredUsers.contract_id == contract.contract_id).get() # Create new DesiredUsers object if none if not players: DesiredUsers(contract_id=contract.contract_id, users=[email]).put() # Otherwise update list of users that have been invited, then put it back in db else: players.users.append(email) players.put()
def fetch_contract_info(short_name): # Get relevant contract model contract = Contract.query(Contract.short_name == short_name).get() # Will send blanks for the elements of retrieved Contract that are not yet filled in name = contract.challenge_name obj_type = contract.objective_type length = contract.time_period unit = contract.time_unit start = contract.start_date # Get more aspects of challenge using ids to search other Models con_id = contract.contract_id stakes_ids = contract.stakes_id return name, obj_type, length, unit, start, con_id, stakes_ids
def fetch_current_challenges_list(): user = users.get_current_user() if not user: self.redirect('/') username = grab_username(user.email()) user_data = User.query(User.google_username == username).get() if not user_data: return [] # Get all CombatantUser objects for this user com_users = CombatantUser.query( CombatantUser.user_id == user_data.user_id).fetch(20) # Get all ContractCombatant objects for this user con_coms = [] for com_user in com_users: con_coms += ContractCombatant.query( ContractCombatant.combatant_id == com_user.combatant_id).fetch(20) # Create array containing challenge names, links, combatant name in that challenge, and position challenges = [] for con_com in con_coms: con = Contract.query(Contract.contract_id == con_com.contract_id).get() challenge_name = con.challenge_name # Link to details page if competition is in future if not con.start_date or con.start_date > datetime.date.today(): link = '/' + con.short_name + '/details' # Otherwise link to status page else: link = '/' + con.short_name + '/status' com_name = Combatant.query( Combatant.combatant_id == con_com.combatant_id).get().name position = con_com.position challenges.append({ 'challenge_name': challenge_name, 'link': link, 'com_name': com_name, 'position': position }) return challenges, username
def fetch_current_combatant(short_name): # Find what user is logged in user = users.get_current_user() if not user: self.redirect('/') # Find all the users associated with this challenge contract_id = Contract.query( Contract.short_name == short_name).get().contract_id combatants_info = fetch_combatants_info(contract_id) # Get all users associated with each distinct combatant for combatant in combatants_info: users_info = fetch_users_info(combatant.combatant_id) # Return current combatant if user is found for user_info in users_info: if user_info.google_username == str(user): return combatant
def get(self, short_name): should_be_here = check_user_auth(short_name) if not should_be_here: self.redirect('/') # Get this challenge's contract for use in finding objective info contract = Contract.query(Contract.short_name == short_name).get() # And the combatant info for use in finding progress combatant = fetch_current_combatant(short_name) # Switch on objective type if contract.objective_type == 'location': geo_obj = GeolocationObjective.query( GeolocationObjective.contract_id == contract.contract_id).get() # Find last checkin date for context last_checkin = last_checkin_date(geo_obj.geo_objective_id, combatant.combatant_id) if last_checkin: last_checkin = time.mktime(last_checkin.timetuple()) else: last_checkin = "" # Create context and render page context = { 'id': geo_obj.geo_objective_id, 'location': str(geo_obj.checkin_loc), 'radius': geo_obj.checkin_radius, 'placename': geo_obj.loc_name, 'last_checkin': last_checkin } geo_checkin_page = jinja_environment.get_template( "pages/geo-checkin.html") self.response.out.write(geo_checkin_page.render(context)) elif contract.objective_type == 'highest-occurrence': gen_obj = GeneralObjective.query( GeneralObjective.contract_id == contract.contract_id).get() # Find last checkin date for context last_checkin = last_checkin_date(gen_obj.gen_objective_id, combatant.combatant_id) if last_checkin: last_checkin = time.mktime(last_checkin.timetuple()) else: last_checkin = "" # Create context and render page context = { 'id': gen_obj.gen_objective_id, 'objective': gen_obj.objective_name, 'last_checkin': last_checkin } gen_checkin_page = jinja_environment.get_template( "pages/gen-checkin.html") self.response.out.write(gen_checkin_page.render(context)) elif contract.objective_type == 'reddit': return 1 elif contract.objective_type == None: self.redirect('/' + short_name + '/details')