<commit_before># coding: utf-8

import logging, json, time, re, math, appengine_config
from time import gmtime, strftime
from datetime import *
from decimal import *

#App Engine platform
from google.appengine.api import taskqueue, mail, memcache, images
from google.appengine.ext.webapp import template
from google.appengine.ext import ndb
from google.appengine.datastore.datastore_query import Cursor

#Mailchimp API
from mailsnake import MailSnake

#Sessions
from gaesessions import get_current_session, Session

#Application files
import DataModels as models

#Search
from google.appengine.api import search

_CONTACT_SEARCH_INDEX = "contact"
_DEPOSIT_SEARCH_INDEX = "deposit"
_DONATION_SEARCH_INDEX = "donation"
_INDIVIDUAL_SEARCH_INDEX = "individual"
_TEAM_SEARCH_INDEX = "team"
_NUM_RESULTS = 15

###### ------ Authentication ------ ######
def checkCredentials(self, email, password):
    try:
        users = models.Individual.gql("WHERE email = :e", e=email)
        target_user = users.fetch(1)[0]

        if target_user.password == password:
            return True, target_user
        else:
            return False, None
    except:
        return False, None

def checkAuthentication(self, admin_required):
    try:
        #If the cookie doesn't exist, send them back to login
        s_key = getSettingsKey(self)
        u_key = getUserKey(self)

        u = u_key.get()

        #If the user tries to enter an admin page with standard credentials,
        #kick them out.
        if admin_required == True and u.admin == False:
            logging.info("Not authorized - kicking out")
            self.redirect("/ajax/notauthorized")

        #Otherwise, good to go
        return u.admin, s_key.get()

    except Exception as e:
        logging.info("Error in checkAuthentication - kicking out to login page. " + str(e))

        self.redirect("/login")
        return None, None

def getUsername(self):
    try:
        user_key = getUserKey(self)
        user = user_key.get()

        return user.name
    except:
        self.redirect("/login")

def getUserKey(self):
    self.session = get_current_session()
    user_key = self.session["key"]

    if user_key == None or user_key == "":
        raise Exception("User key is none")

    return getKey(user_key)

def getSettingsKey(self, endpoints=False):
    try:
        user_key = getUserKey(self)
        user = user_key.get()
        
        return user.settings

    except Exception as e:
        logging.error(str(e))

        if endpoints:
            raise Exception("Error in getSettingsKey")
        else:
            self.redirect("/login")

def RPCcheckAuthentication(self, admin_required):
    try:
        self.session = get_current_session()

        #If the cookie doesn't exist, send them back to login
        if not self.session["key"]:
            return False
        else:
            #The cookie does exist, so store the key
            user_key = getUserKey(self)
            user = user_key.get()

            #Get admin privledges associated with this user
            user_privledges = user.admin

            #If the user tries to enter an admin page with standard credentials,
            #kick them out.
            if admin_required == True and user_privledges == False:
                return "semi"
            
            #Otherwise, good to go
            return True
    except:
        return False

###### ------ Managing entity access w/memcache ------ ######<commit_msg>Removed error logging string<commit_after># coding: utf-8

import logging, json, time, re, math, appengine_config
from time import gmtime, strftime
from datetime import *
from decimal import *

#App Engine platform
from google.appengine.api import taskqueue, mail, memcache, images
from google.appengine.ext.webapp import template
from google.appengine.ext import ndb
from google.appengine.datastore.datastore_query import Cursor

#Mailchimp API
from mailsnake import MailSnake

#Sessions
from gaesessions import get_current_session, Session

#Application files
import DataModels as models

#Search
from google.appengine.api import search

_CONTACT_SEARCH_INDEX = "contact"
_DEPOSIT_SEARCH_INDEX = "deposit"
_DONATION_SEARCH_INDEX = "donation"
_INDIVIDUAL_SEARCH_INDEX = "individual"
_TEAM_SEARCH_INDEX = "team"
_NUM_RESULTS = 15

###### ------ Authentication ------ ######
def checkCredentials(self, email, password):
    try:
        users = models.Individual.gql("WHERE email = :e", e=email)
        target_user = users.fetch(1)[0]

        if target_user.password == password:
            return True, target_user
        else:
            return False, None
    except:
        return False, None

def checkAuthentication(self, admin_required):
    try:
        #If the cookie doesn't exist, send them back to login
        s_key = getSettingsKey(self)
        u_key = getUserKey(self)

        u = u_key.get()

        #If the user tries to enter an admin page with standard credentials,
        #kick them out.
        if admin_required == True and u.admin == False:
            logging.info("Not authorized - kicking out")
            self.redirect("/ajax/notauthorized")

        #Otherwise, good to go
        return u.admin, s_key.get()

    except Exception as e:
        logging.info("Error in checkAuthentication - kicking out to login page. " + str(e))

        self.redirect("/login")
        return None, None

def getUsername(self):
    try:
        user_key = getUserKey(self)
        user = user_key.get()

        return user.name
    except:
        self.redirect("/login")

def getUserKey(self):
    self.session = get_current_session()
    user_key = self.session["key"]

    if user_key == None or user_key == "":
        raise Exception("User key is none")

    return getKey(user_key)

def getSettingsKey(self, endpoints=False):
    try:
        user_key = getUserKey(self)
        user = user_key.get()
        
        return user.settings

    except Exception as e:
        if endpoints:
            raise Exception("Error in getSettingsKey")
        else:
            self.redirect("/login")

def RPCcheckAuthentication(self, admin_required):
    try:
        self.session = get_current_session()

        #If the cookie doesn't exist, send them back to login
        if not self.session["key"]:
            return False
        else:
            #The cookie does exist, so store the key
            user_key = getUserKey(self)
            user = user_key.get()

            #Get admin privledges associated with this user
            user_privledges = user.admin

            #If the user tries to enter an admin page with standard credentials,
            #kick them out.
            if admin_required == True and user_privledges == False:
                return "semi"
            
            #Otherwise, good to go
            return True
    except:
        return False

###### ------ Managing entity access w/memcache ------ ######
{'fraction_non_alphanumeric': 0.09126185685541822, 'fraction_numerical': 0.0014371945961483186, 'mean_word_length': 2.450173525037184, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5151732', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1920, 'n_words': 614}