def create_new_comp_for_user(user): key = None while True: #find a unique url for the competition key = rand_key(6) try: Competition.objects.get(hosted_url=key) except: break competition = Competition(owner=user, hosted_url=key) competition.save() current_phase = Phase(competition=competition) current_phase.save() competition.current_phase = current_phase competition.save() if not user.get_profile(): #ensure old accounts have a profile profile = UserProfile(user=request.user) profile.selected_competition = competition profile.save() return competition