我正在努力扩展我的烧瓶技能,并参与了树屋烧瓶社交网络项目并让它发挥作用。我现在正在更改设置并允许社交登录我已经从普通的 bcrypt 转移到烧瓶安全,因为它是烧瓶社交的要求。我有单独的应用程序和模型文件,RuntimeError: working outside of application context
当我尝试使用utils.encrypt_password()
它抛出错误的函数时遇到了问题。我可以通过这样做来消除错误,from app import app
然后它表明我的数据库不是模块的属性。
我很困惑我已经用谷歌搜索了这个问题,但人们的建议不断出现错误,想知道是否有人可以解释我做错了什么。这是一个我认为可能类似但我不理解我的代码的问题的链接。我在下面发布了我的代码,以向您展示我的用户模型和我的 app.py 的上半部分,因为它们是堆栈跟踪中的两个部分。
模型.py
import datetime
#from app import app
from flask.ext.login import UserMixin
from flask.ext.security import Security, RoleMixin, utils
from peewee import *
DATABASE = SqliteDatabase('social.db')
#DATABASE = SqliteDatabase(None)
class User(UserMixin, Model):
username = CharField(unique=True)
email = CharField(unique=True)
password = CharField(max_length=100)
joined_at = DateTimeField(default=datetime.datetime.now)
is_admin = BooleanField(default=False)
class Meta:
database = DATABASE
order_by = ('-joined_at',)
def get_posts(self):
return Post.select().where(Post.user == self)
def get_stream(self):
return Post.select().where(
(Post.user << self.following()) |
(Post.user == self))
def following(self):
return (
User.select().join(
Relationship, on=Relationship.to_user
).where(
Relationship.from_user == self
)
)
def followers(self):
return (
User.select().join(
Relationship, on=Relationship.from_user
).where(
Relationship.to_user == self
)
)
@classmethod
def create_user(cls, username, email, password, admin=False):
try:
with DATABASE.transaction():
cls.create(
username=username,
email=email,
password=utils.encrypt_password(password),
is_admin=admin)
except IntegrityError:
raise ValueError("User already exists")
def initialise():
#DATABASE.init_app(app)
DATABASE.connect()
DATABASE.create_tables([User, Post, Relationship, Role, UserRoles, Connection], safe=True)
DATABASE.close()
应用程序.py
from flask import (Flask, g, render_template, flash, redirect, url_for, abort, session, request )
from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user
from flask.ext.social import Social, PeeweeConnectionDatastore
from flask.ext.security import PeeweeUserDatastore, Security
from flask.ext.social.utils import get_provider_or_404
from flask.ext.social.views import connect_handler
from peewee import *
import forms
import models
DEBUG = True
PORT = 8000
HOST = '0.0.0.0'
app = Flask(__name__)
app.secret_key = 'tfdghsf3wquhivfcdsz5.5432jkicdsahuihuj7564jinjnf'
app.config['SECURITY_PASSWORD_HASH'] = 'pbkdf2_sha512'
app.config['SECURITY_PASSWORD_SALT'] = '1b3kxc8s9fdsa9431vbgvhui43212ijkdrdwui'
app.config['SOCIAL_GOOGLE'] = {
'consumer_key': '1048991051512-6k0vianfkdece33tool0el7pg72h5lku.apps.googleusercontent.com',
'consumer_secret': 'kDyeeetXqIEq7G9LfGfozLH9'
}
DATABASE = SqliteDatabase('social.db')
app.config['SECURITY_POST_LOGIN'] = '/profile'
user_datastore = PeeweeUserDatastore(models.DATABASE, models.User, models.Role, models.UserRoles)
social_datastore = PeeweeConnectionDatastore(models.DATABASE, models.Connection)
security = Security(app, user_datastore)
social = Social(app, social_datastore)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
编辑: 在进一步研究了这个问题之后,似乎它可能是烧瓶安全的问题,因为我在我的主 app.py 中实现了一个测试函数,并且这两个函数给出了相同的运行时错误。