pythoneve:在pythoneve中阻塞POST方法并为一些端点启用PUT方法

2024-09-27 21:28:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我最初编写了webservice,以允许在所有端点上同时使用'GET'和POST方法。现在我们的webservices工作流中有一些更改,我们希望阻止某些端点的POST端点,并为它们启用PUT某些端点仍将启用POST。在

我将通过添加settings.py的代码片段来解释更多

__author__ = 'sappal'

# pulling DBSchema from DBTableSchema
from DBSchema.DBTableSchema import DBTableSchema
from Configs import Configs

dbtableSchema = DBTableSchema()


# Let's just use the local mongod instance. Edit as needed.
# Please note that MONGO_HOST and MONGO_PORT could very well be left
# out as they already default to a bare bones local 'mongod' instance.

## LOCALHOST ENTRIES
MONGO_HOST = Configs.MONGO_DB_HOST
MONGO_PORT = Configs.MONGO_DB_PORT
MONGO_USERNAME = Configs.MONGO_DB_USER_NAME
MONGO_PASSWORD = Configs.MONGO_DB_PASSWORD
MONGO_DBNAME = Configs.MONGO_DB

# Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
RESOURCE_METHODS = ['GET', 'PATCH', 'POST', 'DELETE']

# Enable reads (GET), edits (PATCH), replacements (PUT) and deletes of
# individual items  (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE', 'POST']

# Used for implementing user-resource restricted access.
# Returns the documents which are associated with particular user

AUTH_FIELD = 'userid'

people = {
    'item_title': 'person',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'POST'],
    'schema': dbtableSchema.schema_people,
    'public_methods': ['POST']
}

org = {
    'item_title': 'org',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'PATCH'],
    'schema': dbtableSchema.schema_people_org
}

puburl = {
    'item_title': 'puburl',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'PATCH'],
    'schema': dbtableSchema.schema_people_pub_url
}

address = {
    'item_title': 'address',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'PATCH'],
    'schema': dbtableSchema.schema_people_address
}

contactnumber = {
    'item_title': 'contactnumber',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'PATCH'],
    'schema': dbtableSchema.schema_people_contact_number
}

template = {
    'item_title': 'template',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'POST'],
    'schema': dbtableSchema.schema_template
}

usersharedcontacts = {
    'item_title': 'usersharedcontacts',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'PATCH'],
    'schema': dbtableSchema.schema_people_with_user_shared_contacts
}

cardholder = {
    'item_title': 'cardholder',
    'cache_control': 'max-age=10,must-revalidate',
    'cache_expires': 10,
    'resource_methods': ['GET', 'PATCH'],
    'schema': dbtableSchema.schema_people_card_holder
}

DOMAIN = {
   'people': people,
   'org': org,
   'puburl': puburl,
   'address': address,
   'contactnumber': contactnumber,
   'template': template,
   'usersharedcontacts': usersharedcontacts,
   'cardholder': cardholder
}

我想让POST方法为peopletemplate端点启用,正如您所看到的,我已经为上述端点进行了'resource_methods': ['GET', 'POST']配置。在

我还想对其余端点禁用POST方法,因此我为其余端点'resource_methods': ['GET', 'PATCH'],配置了以下内容。在

我还配置了RESOURCE_METHODS = ['GET', 'PATCH', 'POST', 'DELETE']

但是当我试图运行应用程序时,我在控制台上得到了以下类型的错误

^{pr2}$

Tags: cachegettitleschemamongoitem端点people
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:19

PATCH是一个文档(项)方法,而不是资源方法,这就是为什么会出现不允许的异常。尝试使用:

'resource_methods': ['GET'],  # read-only resource endpoint
'item_methods': ['PATCH']     # still allow edits at the document endpoint

有关详细信息,请参阅文档中的CRUD Operations表。在

相关问题 更多 >

    热门问题