def __init__(self, email, password):
self.email = email
self.password = bcrypt.generate_password_hash(password, app.config.get('BCRYPT_LOG_ROUNDS')) \
.decode('utf-8')
self.registered_on = datetime.datetime.now()
python类config()的实例源码
def create_app(self):
"""
Create an app with the development configuration
:return:
"""
app.config.from_object('app.config.DevelopmentConfig')
return app
def create_app(self):
"""
Create an instance of the app with the testing configuration
:return:
"""
app.config.from_object('app.config.TestingConfig')
return app
def search_page():
query = request.args.get('s')
page = request.args.get('p')
if not query:
return render_template('search.html', allow_upload=app.config['ALLOW_UPLOAD'], count_pdf=count_pdf())
try:
page = abs(int(page))
except:
page = 0
query = query.lower()
query = unicodedata.normalize('NFKD', query).encode('ASCII', 'ignore')
words = query.split()[:5] #max 5 words for querying...
words = map(secure_filename, words)
query = " ".join(words)
words = map(lemmatize, words)
if not words:
return render_template('search.html')
rows, speed, next_button = get_results(words, page)
if next_button:
next_button = page + 1
return render_template('results.html', user_request=query, rows=rows, speed=speed, next_button=next_button)
def upload_page():
if not app.config['ALLOW_UPLOAD']:
return render_template('search.html')
return render_template('upload.html')
def return_pdf(pdf_name):
try:
return redirect(url_for('static', filename=app.config['PDF_DIR'] + secure_filename(pdf_name)))
except:
abort(404)
def conn_to_db(db_name):
conn = sqlite3.connect(app.config['DB_PATH'] + db_name)
conn.create_function('LOG', 1, math.log)
return conn
def insert_pdf_to_db(pdf_name):
# insert a pdf into the database and return his id
path = app.config['PDF_DIR_LOC'] + app.config['PDF_DIR'] + pdf_name
conn = conn_to_db('pdf.db')
cursor = conn.execute("INSERT INTO PDF (NAME, HASH, DATE) VALUES ('{}', '{}', {})".format(
pdf_name, hash_file(path), int(time())))
conn.commit()
pdf_id = cursor.lastrowid
conn.close()
return pdf_id
def generate_auth_token(self, expiration=36000):
s = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
self.token = s.dumps({'id': self.id})
return self.token
def verify_auth_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except SignatureExpired:
return None # valid token, but expired
except BadSignature:
return None # invalid token
user = User.query.get(data['id'])
return user
def setUp(self):
self.db_fd, self.db_filename = tempfile.mkstemp()
# FIXME: this isn't actually working.
app.config['DATABASE_URI'] = 'sqlite:///' + self.db_filename
self.app = app.test_client()
db.create_all()
def build_link(document):
doc_id = document['id_arquivo']
link_file = app.config['SALIC_BASE_URL']+'verprojetos/abrir?id=%d'%(doc_id)
return link_file
def build_link(document):
doc_id = document['idDocumentosAgentes']
if document['Anexado'] == '2':
idPronac = document['idPronac']
link_file = app.config['SALIC_BASE_URL']+'verprojetos/abrir-documentos-anexados?id=%d&tipo=2&idPronac=%d'%(doc_id, idPronac)
elif document['Anexado'] == '5':
link_file = app.config['SALIC_BASE_URL']+'verprojetos/abrir?id=%d'%(doc_id)
else:
link_file = ''
return link_file
def instantiate( cls, streamType = "SCREEN", logLevel = "INFO" ):
try:
logging.VERBOSE = 5
logging.addLevelName(logging.VERBOSE, "VERBOSE")
logging.Logger.verbose = lambda inst, msg, *args, **kwargs: inst.log(logging.VERBOSE, msg, *args, **kwargs)
logging.verbose = lambda msg, *args, **kwargs: logging.log(logging.VERBOSE, msg, *args, **kwargs)
cls.logger = logging.getLogger()
if logLevel not in logging._levelNames:
raise Exception( 'Invalid file level' )
cls.logger.setLevel( logging._levelNames[logLevel] )
streamType = app.config['STREAMTYPE']
if streamType == "SCREEN":
stream = logging.StreamHandler()
else:
stream = logging.FileHandler( app.config['LOGFILE'] )
formatter = logging.Formatter( '[%(levelname)-7s - %(asctime)s] %(message)s' )
stream.setFormatter( formatter )
cls.logger.addHandler( stream )
except Exception, e:
print( 'Unable to get/set log configurations. Error: %s'%( e ) )
cls.logger = None
##
# Records a message in a file and/or displays it in the screen.
# @param level - String containing the name of the log message.
# @param message - String containing the message to be recorded.
#