我正在金字塔中开发一个相当大的项目.我之前用过django.我非常喜欢它构建项目的方式,并将功能封装到应用程序中.我想用金字塔实现相同的结构.我知道金字塔非常灵活,但是我需要一些帮助来实现松散耦合的相同结构.项目结构应该类似于:
Project/
app1/
models.py
routes.py
views.py
app2/
models.py
routes.py
views.py
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我在尝试捕获错误时遇到问题.我正在使用Pyramid/SQLAlchemy并使用电子邮件作为主键创建了一个注册表单.问题是当输入重复的电子邮件时它会引发IntegrityError,所以我试图捕获该错误并提供一条消息,但无论我做什么我都抓不到它,错误不断出现.
try:
new_user = Users(email, firstname, lastname, password)
DBSession.add(new_user)
return HTTPFound(location = request.route_url('new'))
except IntegrityError:
message1 = "Yikes! Your email already exists in our system. Did you forget your password?"
Run Code Online (Sandbox Code Playgroud)
当我尝试时,我得到了同样的信息except exc.SQLAlchemyError(虽然我想捕捉到特定的错误而不是全部捕获).我也试过exc.IntegrityError但没有运气(虽然它存在于API中).
我的Python语法有什么问题,或者我需要在SQLAlchemy中做些什么才能捕获它?
我不知道如何解决这个问题,但我对可能导致问题的原因有一些想法.也许try语句没有失败但是成功,因为SQLAlchemy本身就引发了异常而Pyramid正在生成视图,因此except IntegrityError:永远不会被激活.或者,更有可能的是,我完全错误地抓住了这个错误.
我正在使用Pyramid和Cornice为Web服务开发REST API ; 服务器端的数据使用SQLAlchemy和MySQL处理.Web服务器是使用uwsgi的nginx,它被配置为运行多个Python进程:
[uwsgi]
socket = localhost:6542
plugins = python34
...
processes = 2 # spawn the specified number of workers/processes
threads = 2 # run each worker in prethreaded mode with the specified number of threads
Run Code Online (Sandbox Code Playgroud)
问题
假设customers服务器端有一个表.使用API可以读取客户数据,修改或删除客户数据.除此之外,还有其他API函数可以读取客户数据.
我可以同时发出多个API调用,然后竞争相同的客户资源:
# Write/modify the customer {id} data
curl --request POST ... https://some.host/api/customer/{id}
# Delete customer {id} and all of its associated data
curl --request DELETE https://some.host/api/customer/{id}
# …Run Code Online (Sandbox Code Playgroud) 对于使用Pyramid Web框架的新(Python)Web应用程序,我想使用表单绑定和验证库,到目前为止找到simpleform和deform.有没有人有这些经验,可以告诉我为什么我应该选择其中一个?我没有使用ORM,只是说POPO.
我想我现在更喜欢最简单的.
每个Pyramid应用程序都有一个包含其设置的关联.ini文件.例如,默认值可能如下所示:
[app:main]
use = egg:MyProject
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
...
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以在那里添加自己的配置值,并在运行时读取它们(主要来自可调用的视图).例如,我可能想要
[app:main]
blog.title = "Custom blog name"
blog.comments_enabled = true
...
Run Code Online (Sandbox Code Playgroud)
或者更好的是有一个单独的.ini文件并在启动时解析它?
我正在使用Pyramid框架,我想访问发出请求的IP地址.我假设它在某个地方的请求对象(传递给每个视图函数)中,但我找不到告诉我它在哪里的文档.
金字塔项目中有development.ini或production.ini.我在ini文件中添加自己的配置数据,如:
[thrift]
host = 0.0.0.0
port = 8080
Run Code Online (Sandbox Code Playgroud)
我想在项目中的一个py文件中使用配置数据.如何在没有请求对象的情况下获取数据?(我见过一个使用请求的解决方案.)
我有一个表格,我想要获取所有列名称但是在浏览网站后我找不到可行的方法.这就是我的表格:
class myTable(Base):
__tablename__ = 'myTable'
col1 = Column(Integer, primary_key=True)
col2 = Column(Unicode(10))
col3 = Column(Integer)
col4 = Column(Numeric(10, 6))
col5 = Column(Numeric(6,3))
col6 = Column(Numeric(6,3))
child = relationship('tChild',
backref=backref('children'))
Run Code Online (Sandbox Code Playgroud)
我希望能够从for循环中打印所有列名.例如:
"col1", "col2", "col3".... etc
Run Code Online (Sandbox Code Playgroud)
使用常规sql这很容易,但我似乎无法弄清楚如何使用sqlAlchemy表模型
目前,我使用以下方法在Pyramid中上传文件(通过HTML表单).
if request.params.get('form.submitted'):
upload_directory = os.getcwd() + '/myapp/static/uploads/'
my_file = request.POST.get('thumbnail')
saved_file = str(upload_directory) + str(my_file.filename)
perm_file = open(saved_file, 'w')
shutil.copyfileobj(my_file.file, perm_file)
my_file.file.close()
perm_file.close()
Run Code Online (Sandbox Code Playgroud)
我只是想知道,这是保存文件上传的好方法,我的方法有任何安全问题吗?我怎么能改进我的方法.谢谢.
我想保留development.ini并production.ini受版本控制,但出于安全原因,不希望sqlalchemy.url存储连接字符串,因为这将包含用于数据库连接的用户名和密码.
在金字塔中,从一个额外的外部文件中获取此设置的规范方法是什么?
编辑 除了使用环境变量的解决方案之外,我在询问#pyramid之后想出了这个解决方案:
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
# Read db password from config file outside of version control
secret_cfg = ConfigParser()
secret_cfg.read(settings['secrets'])
dbpass = secret_cfg.get("secrets", "dbpass")
settings['sqlalchemy.url'] = settings['connstr'] % (dbpass,)
Run Code Online (Sandbox Code Playgroud)