我创建了一个简单的eve-sqlalchemy
应用,其中包含User
和Role
资源,以模仿an example on their webpage。 User
类可以有多个角色。完整的示例代码如下所示。
我有兴趣能够POST
有角色的用户,或PATCH
用户的角色。我没有能够找到一种方法来做到这一点,而不会遇到错误。
似乎eve-sqlalchemy
强制User.roles
的类型为整数,即Role
类的主键id
。如果我改为设置POST
或PATCH
请求以使roles
字段为整数,eve-sqlalchemy
会抱怨userroles
关联表没有存在于self.driver.app.config['SOURCES']
中。虽然我可以进行这些更改(即使userroles
表成为声明性ORM并将其注册到装饰器)但我不确定这是否是正确的方法。
总之,eve-sqlalchemy
如何POST
User
roles
PATCH
列表from eve import Eve
from eve.utils import config
from sqlalchemy import create_engine, Table, Column, String, Integer, ForeignKey, func, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from eve_sqlalchemy.decorators import registerSchema
from eve_sqlalchemy import SQL
from eve_sqlalchemy.validation import ValidatorSQL
ID_FIELD = 'id'
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/eve-sqla-test.db'
config.ID_FIELD = ID_FIELD
config.ITEM_LOOKUP_FIELD = ID_FIELD
Base = declarative_base()
userroles_table = Table('userroles', Base.metadata,
Column('user_id', Integer, ForeignKey("users.id"), primary_key=True),
Column('role_id', Integer, ForeignKey("roles.id"), primary_key=True)
)
class CommonColumns(Base):
__abstract__ = True
_created = Column(DateTime, default=func.now())
_updated = Column(DateTime, default=func.now(), onupdate=func.now())
_etag = Column(String(40))
class Role(CommonColumns):
__tablename__ = 'roles'
id = Column(Integer, primary_key=True, autoincrement=True)
role = Column(String, unique=True, nullable=False)
class User(CommonColumns):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, autoincrement=True)
login = Column(String, unique=True, nullable=False)
roles = relationship("Role", backref="users", secondary=userroles_table)
def create_entries():
'''Creates test entries for querying with eve'''
engine = create_engine(SQLALCHEMY_DATABASE_URI)
Base.metadata.bind = engine
Base.metadata.create_all()
SessionMaker = sessionmaker(bind=engine)
s = SessionMaker()
u1 = User(login='user1')
u1.roles.append(Role(role='admin'))
u2 = User(login='user2')
u2.roles.append(Role(role='user'))
s.add_all((u1, u2))
s.commit()
def run():
'''Runs the eve server'''
for table in (User, Role):
registerSchema(table.__tablename__)(table)
users = User._eve_schema[User.__tablename__]
users.update({
'item_methods': ['GET', 'PATCH', 'DELETE'],
'resource_methods': ['GET', 'POST'],
})
roles = Role._eve_schema[Role.__tablename__]
roles.update({
'resource_methods': ['GET', 'POST'],
})
DOMAIN = {
'users': users,
'roles': roles,
}
SETTINGS = {
'SQLALCHEMY_DATABASE_URI': SQLALCHEMY_DATABASE_URI,
'SQLALCHEMY_TRACK_MODIFICATIONS': False,
'ID_FIELD': ID_FIELD,
'ITEM_LOOKUP_FIELD': ID_FIELD,
'DOMAIN': DOMAIN,
}
app = Eve(validator=ValidatorSQL, data=SQL, settings=SETTINGS)
db = app.data.driver
Base.metadata.bind = db.engine
db.Model = Base
app.run(debug=True)
if __name__ == '__main__':
'''Test area'''
#create_entries()
run()
,或import json
import requests
u4 = {
'login': 'user4',
}
# users get works
r = requests.get('http://localhost:5000/users')
print(r.text)
# user creation works
r = requests.post('http://localhost:5000/users', json=u4)
print(r.text)
# roles get works
r = requests.get('http://localhost:5000/roles')
print(r.text)
# user creation with roles fail
u5 = {
'login': 'user5',
'roles': [1,]
}
r = requests.post('http://localhost:5000/users', json=u5)
print(r.text) # {"_issues": {"roles": ["field 'roles' could not be coerced", "must be of integer type"]}, "_status": "ERR"}
# user patch with role fails
r = requests.get('http://localhost:5000/users/1')
patch_headers = {"If-Match": r.json()['_etag'], 'Content-type': 'application/json; charset=utf-8'}
r = requests.patch('http://localhost:5000/users/1', headers=patch_headers, json={'roles': [1,]})
print(r.text) # {"_issues": {"roles": ["field 'roles' could not be coerced", "must be of integer type"]}, "_status": "ERR"}
用户现有角色?
Public Sub TEST()
POPage = "http://www.sample.com"
CurRow = Sheet1.UsedRange.Rows.Count
For i = 1 To CurRow
Dim IE As New InternetExplorer
Dim Doc As New HTMLDocument
IE.Visible = True
IE.navigate POPage
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("0:00:04"))
Set Doc = IE.document
Application.Wait (Now + TimeValue("0:00:4"))
Doc.getElementsByTagName("input")(0).Value = Sheet1.Range("A" & i).Value
Doc.getElementsByTagName("button")(1).Click
Application.Wait (Now + TimeValue("0:00:03"))
IE.Quit
Set Doc = Nothing
Set IE = Nothing
Application.Wait (Now + TimeValue("0:00:03"))
Next i
End Sub
IBM WebSphere MQ
答案 0 :(得分:1)
我最终做的是:
SELECT
X
,Y
,Z
CASE Y
WHEN 10
THEN X
WHEN 11
THEN X * 333.3
WHEN 12
THEN X * 222.2
WHEN 13
THEN (X * 999) * 222.2
WHEN 14
THEN X * Z
WHEN 15
THEN (X * 222.2) * Z
END AS W
FROM myTable
;
从表转换为声明性类userroles_table
。UserRole
UserRole
eve_sqlalchemy
添加到UserRole
。然后可以通过新的资源端点DOMAIN
直接查询或修改关联表。 user_roles
似乎在生成模式时隐藏了外键,因此包含与外键的关系非常重要。