本文整理匯總了Python中models.Project.to_dict方法的典型用法代碼示例。如果您正苦於以下問題:Python Project.to_dict方法的具體用法?Python Project.to_dict怎麽用?Python Project.to_dict使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類models.Project
的用法示例。
在下文中一共展示了Project.to_dict方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: update_project
# 需要導入模塊: from models import Project [as 別名]
# 或者: from models.Project import to_dict [as 別名]
def update_project(project_id):
"""Updates the project matching the id project_id.
Only the parameters to update or to add should be passed in the request body.
"""
project = Project.objects(id=project_id).get()
patched = Project(**dict(chain(project.to_dict().items(), request.get_json().items())))
patched.save()
return jsonify(patched.to_dict())
示例2: create_project
# 需要導入模塊: from models import Project [as 別名]
# 或者: from models.Project import to_dict [as 別名]
def create_project(self):
if 'project_name' in self.request.POST:
project_name = self.request.get('project_name').strip()
hourly_rate = self.request.get('hourly_rate')
if not project_name:
self.response.set_status(400)
self.response.write(json.dumps({'code': 12, 'error': 'Project name can\'t be only spaces'}))
return
if not hourly_rate:
hourly_rate = '0'
try:
if not re.match("^[a-zA-Z0-9_\-\s]+$", project_name):
self.response.set_status(400)
self.response.write(json.dumps({'code': 13, 'error': 'Project name contains invalid characters'}))
return
if not re.match(r"(\d+(\.\d*)?|\.\d+)", hourly_rate):
self.response.set_status(400)
self.response.write(json.dumps({'code': 14, 'error': 'Hourly rate is not a positive number or 0'}))
return
except Exception as e:
self.response.set_status(500)
self.response.write(json.dumps({'code': 15, 'error': e.message}))
return
if len(project_name) > 25:
self.response.set_status(400)
self.response.write(json.dumps({'code': 16, 'error': 'Project name must be under 25 chars'}))
return
try:
project = Project(parent=self.user.key)
project = ProjectService.save_project(project, project_name, hourly_rate, self.user)
proj = project.to_dict()
proj['id'] = project.key.urlsafe()
self.response.write(json.dumps({'project': proj}))
except Exception as e:
self.response.set_status(500)
self.response.write(json.dumps({'code': 17, 'error': e.message}))
else:
self.response.set_status(400)
self.response.write(json.dumps({'code': 18, 'error': 'Request doesn\'t contain a project_name'}))
return
示例3: projects
# 需要導入模塊: from models import Project [as 別名]
# 或者: from models.Project import to_dict [as 別名]
def projects():
"""Retrieves a list of projects from the database or create a new project.
When retrieving a list of projects, the URL can contain pagination parameters
page and per_page which default respectively to 1 and 10 if omitted.
"""
if request.method == 'GET':
return paginate(resource_name='projects', endpoint='projects', objects=Project.objects)
json = request.get_json()
author_id = json.get('author_id')
if author_id is not None:
assert_author_exists(json.get('author_id'))
project = Project(**json)
project.save()
# Add the project in the list of the author projects.
Author.objects(id=project.author_id).update_one(push__projects=project.id)
return jsonify(project.to_dict()), 201