def get(self, username):
     user = self.get_user()
     if user:
         searched_user_list = User.query(User.username == username).fetch(1)
         if searched_user_list:
             searched_user = searched_user_list[0]
             self.redirect('/profile/' + str(searched_user.key.id()))
         else:
             self.render("frontpage.html", user=user, no_user=True, username=username)
         
     else:
         self.redirect("/login")
 def get(self):
     if not self.get_user():
         self.redirect("/login")
     else:
         user = self.get_user()
             
         user_db_qry = User.query(User.oauth2_id == user['oauth2_id'])
         user_db_list = user_db_qry.fetch(1)
         currentregistereduser = user_db_list[0]
         has_username = False
         
         name = ""
         if currentregistereduser.name:
             name = currentregistereduser.name
         username = ""
         if currentregistereduser.username:
             username = currentregistereduser.username
             has_username = True
         bio = ""
         if currentregistereduser.bio:
             bio = currentregistereduser.bio
         quote = ""
         if  currentregistereduser.quote:
              quote = currentregistereduser.quote
         email = ""
         if currentregistereduser.email:
             email = currentregistereduser.email
         geo_center = ""
         if currentregistereduser.geo_center:
             geo_center = currentregistereduser.geo_center
         high_school = ""
         if currentregistereduser.high_school:
             high_school = currentregistereduser.high_school
         university = ""
         if currentregistereduser.university:
             university = currentregistereduser.university
         sports_played = ""
         if currentregistereduser.sports_played:
             sports_played = currentregistereduser.sports_played
             
         self.render('editprofile.html', bio=bio, name=name, username=username, quote=quote, GPS=geo_center, user = user, 
                     email=email, has_username=has_username, high_school=high_school, university=university, sports_played=sports_played)
    def get(self):
        user = self.get_user()
        if user["model_id"] == 5187942536445952:
            user_model = User.get_by_id(5187942536445952)
            user_model.admin = True
            user_model.put

            all_videos = Video.query()
            video_count = 0
            for v in all_videos:
                video_count = video_count + 1

            all_users = User.query()
            user_count = 0
            for u in all_users:
                user_count = user_count + 1

            all_comments = VideoComment.query()
            comment_count = 0
            for c in all_comments:
                comment_count = comment_count + 1

            all_votes = Vote.query()
            vote_count = 0
            for vs in all_votes:
                vote_count = vote_count + 1

            self.response.write("There are " + str(video_count) + " videos.")
            self.response.write("<br>")
            self.response.write("There are " + str(user_count) + " users.")
            self.response.write("<br>")
            self.response.write("There are " + str(comment_count) + " comments.")
            self.response.write("<br>")
            self.response.write("There are " + str(vote_count) + " votes.")
        else:
            self.redirect("/noaccess")
 def post(self):
     name = self.request.get('name')
     username = self.request.get('username')
     bio = self.request.get('bio')
     quote = self.request.get('quote')
     geo_center = self.request.get('geo_center')
     email = self.request.get('email')
     sports_played = self.request.get_all('sports_played')
     high_school = self.request.get('high_school')
     university = self.request.get('university')
             
     user = self.get_user()
     
     username_is_valid = re.match('^[\w-]+$', username) is not None
     
     if not username_is_valid and not user['username']:
         error = "Sorry, the username can only be letters and numbers."
         self.render("editprofile.html", name=name, username=username, bio=bio, quote=quote, email=email, error=error, user=user, 
                     geo_center=geo_center, high_school=high_school, university=university, sports_played=sports_played) 
     
     elif username or user['username']:
         if not username:
             username = user['username']
         keyname =  "username-" + str(username)
         previous_username = UniqueUsername.get_by_id(keyname)
         if previous_username != None and previous_username.user_id_record != str(user['model_id']):
             error = "Sorry, that username is already in use."
             self.render("editprofile.html", name=name, username=username, bio=bio, quote=quote, email=email, error=error,
                          user=user, geo_center=geo_center, high_school=high_school, university=university, sports_played=sports_played)
         else:
             username_record = UniqueUsername.get_or_insert(keyname, user_id_record=str(user['model_id']))
         
             user_db_qry = User.query(User.oauth2_id == user['oauth2_id'])
             user_db_list = user_db_qry.fetch(1)
             currentregistereduser = user_db_list[0]
             
             profile_id = currentregistereduser.key.id()
             currentregistereduser.name = name
             currentregistereduser.username = username
             currentregistereduser.bio = bio
             currentregistereduser.quote = quote
             currentregistereduser.email = email
             currentregistereduser.high_school = high_school
             currentregistereduser.university = university
             if sports_played:
                 currentregistereduser.sports_played = sports_played
             
             error = None
             GPSlocation = None
             if geo_center:
                 g = geocoders.GoogleV3()
                 #to catch an error where there is no corresponding location
                 try:
                     #to catch an error where there are multiple returned locations
                     try:
                         place, (lat, lng) = g.geocode(geo_center)
                         place, searched_location = g.geocode(geo_center)
                     except ValueError:
                         geocodespot = g.geocode(geo_center, exactly_one=False)
                         place, (lat, lng) = geocodespot[0]
                         place, searched_location = geocodespot[0]
                     GPSlocation = "("+str(lat)+", "+str(lng)+")"                 
 
                 #this is straight from the docs:  http://code.google.com/p/geopy/wiki/Exceptions and happens when there is no location that matches
                 except geocoders.googlev3.GQueryError:
                     logging.error('could not find the spot')
                     error = "We cannot find " + geo_center +"... try to be a little more specific or search a nearby location."
                         
                 except geocoders.googlev3.GTooManyQueriesError:
                     logging.error('the request did not go through because there are too many queries')
                     #TODO
                     something = 'nada'
                     
             if error:
                 self.render('editprofile.html', bio=bio, name=name, username=username, email=email, error=error, quote=quote, 
                             user = user, geo_center=geo_center, high_school=high_school, university=university, sports_played=sports_played)
             
             else:            
                 if GPSlocation:
                     currentregistereduser.geo_center = GPSlocation
                 if not currentregistereduser.geo_center:
                     currentregistereduser.geo_center = None
                 
                 currentregistereduser.put()
                 currentregistereduser.created = None
                 currentregistereduser.last_modified = None
                 currentregistereduser.avatar = None
                 self.session['_current_user'] = currentregistereduser.to_dict()
                 
                 self.redirect('/profile/%s' % (profile_id))
                 
     elif not username:
                 
         error = "Come on you can put a little something.  At least a username is required."
         self.render("editprofile.html", name=name, username=username, bio=bio, quote=quote, email=email, error=error, user=user, 
                     geo_center=geo_center, high_school=high_school, university=university, sports_played=sports_played)   
    def _on_signin(self, data, auth_info, provider):
        """Callback whenever a new or existing user is logging in.
        data is a user info dictionary.
        auth_info contains access token or oauth token and secret.

        See what's in it with logging.info(data, auth_info)
        """
            
        auth_id = '%s:%s' % (provider, data['id'])
        
        # 1. check whether user exist, e.g.
        #    User.get_by_auth_id(auth_id)
        #
        # 2. create a new user if it doesn't
        #    User(**data).put()
        #
        # 3. sign in the user
        #    self.session['_user_id'] = auth_id
        #
        # 4. redirect somewhere, e.g. self.redirect('/profile')
        #
        # See more on how to work the above steps here:
        # http://webapp-improved.appspot.com/api/webapp2_extras/auth.html
        # http://code.google.com/p/webapp-improved/issues/detail?id=20
    
        currentregistereduser = None
        #check for user existance upon login/registration
        try:
            user_db_qry = User.query(User.oauth2_id == auth_id)
            user_db_list = user_db_qry.fetch(1)
            currentregistereduser = user_db_list[0]
            
        #if the user does not exist yet
        except IndexError:
            name = None
            #
            try:
                name = data['name']
            except KeyError:
                name = None
                
            user_db = User(
                name=name,
                oauth2_id=auth_id,
                prestige = 100,
                )
            user_db.put()
            user_db.model_id = user_db.key.id()
            user_db.put()
            
            currentregistereduser = user_db
                        
        self.session['_user_id'] = auth_id
        self.session['data'] = data
        currentregistereduser.created = None
        currentregistereduser.last_modified = None
        currentregistereduser.avatar = None
        self.session['_current_user'] = currentregistereduser.to_dict()
        
        if currentregistereduser.username:
            self.redirect("/")
        else:
            self.redirect("/profile")