本文整理汇总了Python中nereid.jsonify函数的典型用法代码示例。如果您正苦于以下问题:Python jsonify函数的具体用法?Python jsonify怎么用?Python jsonify使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了jsonify函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add
def add(cls):
"""
Adds a contact mechanism to the party's contact mechanisms
"""
form = cls.get_form()
if form.validate_on_submit():
cls.create(
[
{
"party": request.nereid_user.party.id,
"type": form.type.data,
"value": form.value.data,
"comment": form.comment.data,
}
]
)
if request.is_xhr:
return jsonify({"success": True})
return redirect(request.referrer)
if request.is_xhr:
return jsonify({"success": False})
else:
for field, messages in form.errors:
flash("<br>".join(messages), "Field %s" % field)
return redirect(request.referrer)
示例2: new_post
def new_post(cls):
"""Create a new post
"""
post_form = BlogPostForm(request.form)
if request.method == 'POST' and post_form.validate():
post, = cls.create([{
'title': post_form.title.data,
'uri': post_form.uri.data,
'content': post_form.content.data,
'nereid_user': request.nereid_user.id,
'allow_guest_comments': post_form.allow_guest_comments.data,
}])
if post_form.publish.data:
cls.publish([post])
flash('Your post has been published.')
else:
flash('Your post has been saved.')
if request.is_xhr:
return jsonify(success=True, item=post.serialize())
return redirect(url_for(
'blog.post.render', user_id=post.nereid_user.id,
uri=post.uri
))
if request.is_xhr:
return jsonify(
success=request.method != 'POST', # False for POST, else True
errors=post_form.errors or None,
)
return render_template('blog_post_form.jinja', form=post_form)
示例3: edit_post
def edit_post(self):
"""
Edit an existing post
"""
if self.nereid_user != request.nereid_user:
abort(404)
# Search for a post with same uri
post_form = BlogPostForm(request.form, obj=self)
with Transaction().set_context(blog_id=self.id):
if request.method == 'POST' and post_form.validate():
self.title = post_form.title.data
self.content = post_form.content.data
self.allow_guest_comments = post_form.allow_guest_comments.data
self.save()
flash('Your post has been updated.')
if request.is_xhr:
return jsonify(success=True, item=self.serialize())
return redirect(url_for(
'blog.post.render', user_id=self.nereid_user.id,
uri=self.uri
))
if request.is_xhr:
return jsonify(
success=request.method != 'POST', # False for POST, else True
errors=post_form.errors or None,
)
return render_template(
'blog_post_edit.jinja', form=post_form, post=self
)
示例4: start_session
def start_session(cls):
'''
POST: Start chat session with another user.
:args user: User Id of nereid user.
:return: JSON as
{
thread_id: uuid,
members: Serialized members list.
}
'''
NereidUser = Pool().get('nereid.user')
form = NewChatForm()
if not form.validate_on_submit():
return jsonify(errors=form.errors), 400
chat_with = NereidUser(form.user.data)
if not request.nereid_user.can_chat(chat_with):
abort(403, "You can only talk to friends")
chat = cls.get_or_create_room(
request.nereid_user.id, chat_with.id
)
return jsonify({
'thread_id': chat.thread,
'members': map(
lambda m: m.user.serialize(), chat.members
)
})
示例5: render_comments
def render_comments(self):
"""
Render comments
GET: Return json of all the comments of this post.
POST: Create new comment for this post.
"""
if self.state != 'Published':
abort(404)
# Add re_captcha if the configuration has such an option and user
# is guest
if 're_captcha_public' in CONFIG.options and request.is_guest_user:
comment_form = GuestCommentForm(
request.form, captcha={'ip_address': request.remote_addr}
)
else:
comment_form = PostCommentForm(request.form)
if request.method == 'GET':
if self.nereid_user == request.nereid_user:
return jsonify(comments=[
comment.serialize() for comment in self.comments
])
return jsonify(comments=[
comment.serialize() for comment in self.comments
if not comment.is_spam
])
# If post does not allow guest comments,
# then dont allow guest user to comment
if not self.allow_guest_comments and request.is_guest_user:
flash('Guests are not allowed to write comments')
if request.is_xhr:
return jsonify(
success=False,
errors=['Guests are not allowed to write comments']
)
return redirect(url_for(
'blog.post.render', user_id=self.nereid_user.id, uri=self.uri
))
if request.method == 'POST' and comment_form.validate():
self.write([self], {
'comments': [('create', [{
'nereid_user': current_user.id
if not current_user.is_anonymous() else None,
'name': current_user.display_name
if not current_user.is_anonymous()
else comment_form.name.data,
'content': comment_form.content.data,
}])]
})
if request.is_xhr:
return jsonify(success=True) if comment_form.validate() \
else jsonify(success=False, errors=comment_form.errors)
return redirect(url_for(
'blog.post.render', user_id=self.nereid_user.id, uri=self.uri
))
示例6: add_to_cart
def add_to_cart(cls):
"""
Adds the given item to the cart if it exists or to a new cart
The form is expected to have the following data is post
quantity : decimal
product : integer ID
action : set (default), add
Response:
'OK' if X-HTTPRequest
Redirect to shopping cart if normal request
"""
Product = Pool().get('product.product')
form = AddtoCartForm()
if form.validate_on_submit():
cart = cls.open_cart(create_order=True)
action = request.values.get('action', 'set')
if form.quantity.data <= 0:
message = _(
'Be sensible! You can only add real quantities to cart')
if request.is_xhr:
return jsonify(message=unicode(message)), 400
flash(message)
return redirect(url_for('nereid.cart.view_cart'))
if not Product(form.product.data).template.salable:
message = _("This product is not for sale")
if request.is_xhr:
return jsonify(message=unicode(message)), 400
flash(message)
return redirect(request.referrer)
sale_line = cart.sale._add_or_update(
form.product.data, form.quantity.data, action
)
# Validate that product availability in inventory is not less than
# warehouse quantity
sale_line.validate_for_product_inventory()
sale_line.save()
if action == 'add':
message = _('The product has been added to your cart')
else:
message = _('Your cart has been updated with the product')
if request.is_xhr:
return jsonify(
message=unicode(message),
line=sale_line.serialize(purpose='cart')
), 200
flash(message, 'info')
return redirect(url_for('nereid.cart.view_cart'))
示例7: registration
def registration(cls):
"""
Invokes registration of an user
"""
Party = Pool().get('party.party')
registration_form = cls.get_registration_form()
if registration_form.validate_on_submit():
with Transaction().set_context(active_test=False):
existing = cls.search([
('email', '=', registration_form.email.data),
('company', '=', request.nereid_website.company.id),
])
if existing:
message = _(
'A registration already exists with this email. '
'Please contact customer care'
)
if request.is_xhr or request.is_json:
return jsonify(message=unicode(message)), 400
else:
flash(message)
else:
party = Party(name=registration_form.name.data)
party.addresses = []
party.save()
nereid_user = cls(**{
'party': party.id,
'display_name': registration_form.name.data,
'email': registration_form.email.data,
'password': registration_form.password.data,
'company': request.nereid_website.company.id,
}
)
nereid_user.save()
registration.send(nereid_user)
nereid_user.send_activation_email()
message = _(
'Registration Complete. Check your email for activation'
)
if request.is_xhr or request.is_json:
return jsonify(message=unicode(message)), 201
else:
flash(message)
return redirect(
request.args.get('next', url_for('nereid.website.home'))
)
if registration_form.errors and (request.is_xhr or request.is_json):
return jsonify({
'message': unicode(_('Form has errors')),
'errors': registration_form.errors,
}), 400
return render_template('registration.jinja', form=registration_form)
示例8: login
def login(cls):
"""
Simple login based on the email and password
Required post data see :class:LoginForm
"""
login_form = LoginForm(request.form)
if not current_user.is_anonymous() and request.args.get('next'):
return redirect(request.args['next'])
if request.method == 'POST' and login_form.validate():
NereidUser = Pool().get('nereid.user')
user = NereidUser.authenticate(
login_form.email.data, login_form.password.data
)
# Result can be the following:
# 1 - Browse record of User (successful login)
# 2 - None - Login failure without message
# 3 - Any other false value (no message is shown. useful if you
# want to handle the message shown to user)
if user:
# NOTE: Translators leave %s as such
flash(_("You are now logged in. Welcome %(name)s",
name=user.display_name))
if login_user(user, remember=login_form.remember.data):
if request.is_xhr:
return jsonify({
'success': True,
'user': user.serialize(),
})
else:
return redirect(
request.values.get(
'next', url_for('nereid.website.home')
)
)
else:
flash(_("Your account has not been activated yet!"))
elif user is None:
flash(_("Invalid login credentials"))
failed_login.send(form=login_form)
if request.is_xhr:
rv = jsonify(message="Bad credentials")
rv.status_code = 401
return rv
return render_template('login.jinja', login_form=login_form)
示例9: registration
def registration(cls):
"""
Invokes registration of an user
"""
Party = Pool().get("party.party")
ContactMechanism = Pool().get("party.contact_mechanism")
registration_form = cls.get_registration_form()
if registration_form.validate_on_submit():
with Transaction().set_context(active_test=False):
existing = cls.search(
[("email", "=", registration_form.email.data.lower()), ("company", "=", current_website.company.id)]
)
if existing:
message = _("A registration already exists with this email. " "Please contact customer care")
if request.is_xhr or request.is_json:
return jsonify(message=unicode(message)), 400
else:
flash(message)
else:
party = Party(name=registration_form.name.data)
party.addresses = []
party.contact_mechanisms = [ContactMechanism(type="email", value=registration_form.email.data)]
party.save()
nereid_user = cls(
**{
"party": party.id,
"display_name": registration_form.name.data,
"email": registration_form.email.data,
"password": registration_form.password.data,
"company": current_website.company.id,
}
)
nereid_user.save()
registration.send(nereid_user)
nereid_user.send_activation_email()
message = _("Registration Complete. Check your email for activation")
if request.is_xhr or request.is_json:
return jsonify(message=unicode(message)), 201
else:
flash(message)
return redirect(request.args.get("next", url_for("nereid.website.home")))
if registration_form.errors and (request.is_xhr or request.is_json):
return jsonify({"message": unicode(_("Form has errors")), "errors": registration_form.errors}), 400
return render_template("registration.jinja", form=registration_form)
示例10: edit_task
def edit_task(self):
"""
Edit the task
"""
Activity = Pool().get("nereid.activity")
Work = Pool().get("timesheet.work")
task = self.get_task(self.id)
Work.write([task.work], {"name": request.form.get("name")})
self.write([task], {"comment": request.form.get("comment")})
Activity.create(
[
{
"actor": request.nereid_user.id,
"object_": "project.work, %d" % task.id,
"verb": "edited_task",
"target": "project.work, %d" % task.parent.id,
"project": task.parent.id,
}
]
)
if request.is_xhr:
return jsonify({"success": True, "name": self.rec_name, "comment": self.comment})
return redirect(request.referrer)
示例11: change_password
def change_password(cls):
"""
Changes the password
.. tip::
On changing the password, the user is logged out and the login page
is thrown at the user
"""
form = ChangePasswordForm()
if request.method == "POST" and form.validate():
if current_user.match_password(form.old_password.data):
cls.write([current_user], {"password": form.password.data})
logout_user()
return cls.build_response(
"Your password has been successfully changed! " "Please login again",
redirect(url_for("nereid.website.login")),
200,
)
else:
return cls.build_response(
"The current password you entered is invalid",
render_template("change-password.jinja", change_password_form=form),
400,
)
if form.errors and (request.is_xhr or request.is_json):
return jsonify(errors=form.errors), 400
return render_template("change-password.jinja", change_password_form=form)
示例12: add_to_cart
def add_to_cart(cls):
"""
Adds the given item to the cart if it exists or to a new cart
The form is expected to have the following data is post
quantity : decimal
product : integer ID
action : set (default), add
Response:
'OK' if X-HTTPRequest
Redirect to shopping cart if normal request
"""
form = AddtoCartForm()
if form.validate_on_submit():
cart = cls.open_cart(create_order=True)
action = request.values.get('action', 'set')
if form.quantity.data <= 0:
flash(
_('Be sensible! You can only add real quantities to cart')
)
return redirect(url_for('nereid.cart.view_cart'))
cart._add_or_update(
form.product.data, form.quantity.data, action
)
if action == 'add':
flash(_('The product has been added to your cart'), 'info')
else:
flash(_('Your cart has been updated with the product'), 'info')
if request.is_xhr:
return jsonify(message='OK')
return redirect(url_for('nereid.cart.view_cart'))
示例13: get_all_countries
def get_all_countries(cls):
"""
Returns serialized list of all countries
"""
return jsonify(countries=[
country.serialize() for country in cls.search([])
])
示例14: delete_task
def delete_task(cls, task_id):
"""
Delete the task from project
Tasks can be deleted only if
1. The user is project admin
2. The user is an admin member in the project
:param task_id: Id of the task to be deleted
"""
task = cls.get_task(task_id)
# Check if user is among the project admins
if not request.nereid_user.is_admin_of_project(task.parent):
flash(
"Sorry! You are not allowed to delete tasks. \
Contact your project admin for the same."
)
return redirect(request.referrer)
cls.write([task], {"active": False})
if request.is_xhr:
return jsonify({"success": True})
flash("The task has been deleted")
return redirect(url_for("project.work.render_project", project_id=task.parent.id))
示例15: add_to_cart
def add_to_cart(cls):
"""
Adds the given item to the cart if it exists or to a new cart
The form is expected to have the following data is post
quantity : decimal
product : integer ID
action : set (default), add
Response:
'OK' if X-HTTPRequest
Redirect to shopping cart if normal request
"""
form = AddtoCartForm(request.form)
if request.method == "POST" and form.validate():
cart = cls.open_cart(create_order=True)
action = request.values.get("action", "set")
if form.quantity.data <= 0:
flash(_("Be sensible! You can only add real quantities to cart"))
return redirect(url_for("nereid.cart.view_cart"))
cls._add_or_update(cart.sale.id, form.product.data, form.quantity.data, action)
if action == "add":
flash(_("The product has been added to your cart"), "info")
else:
flash(_("Your cart has been updated with the product"), "info")
if request.is_xhr:
return jsonify(message="OK")
return redirect(url_for("nereid.cart.view_cart"))