本文整理汇总了Python中assembl.models.LangString.create_from_json方法的典型用法代码示例。如果您正苦于以下问题:Python LangString.create_from_json方法的具体用法?Python LangString.create_from_json怎么用?Python LangString.create_from_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类assembl.models.LangString
的用法示例。
在下文中一共展示了LangString.create_from_json方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_idea
# 需要导入模块: from assembl.models import LangString [as 别名]
# 或者: from assembl.models.LangString import create_from_json [as 别名]
def create_idea(request):
discussion_id = int(request.matchdict['discussion_id'])
session = Discussion.default_db
discussion = session.query(Discussion).get(int(discussion_id))
user_id = request.authenticated_userid
permissions = get_permissions(user_id, discussion.id)
idea_data = json.loads(request.body)
kwargs = {
"discussion": discussion
}
for key, attr_name in langstring_fields.iteritems():
if key in idea_data:
ls_data = idea_data[key]
if ls_data is None:
continue
assert isinstance(ls_data, dict)
current = LangString.create_from_json(
ls_data, user_id, permissions=permissions)
kwargs[attr_name] = current
new_idea = Idea(**kwargs)
session.add(new_idea)
if idea_data['parentId']:
parent = Idea.get_instance(idea_data['parentId'])
else:
parent = discussion.root_idea
session.add(IdeaLink(source=parent, target=new_idea, order=idea_data.get('order', 0.0)))
session.flush()
return {'ok': True, '@id': new_idea.uri()}
示例2: save_synthesis
# 需要导入模块: from assembl.models import LangString [as 别名]
# 或者: from assembl.models.LangString import create_from_json [as 别名]
def save_synthesis(request):
synthesis_id = request.matchdict['id']
discussion_id = int(request.matchdict['discussion_id'])
if synthesis_id == 'next_synthesis':
discussion = Discussion.get_instance(discussion_id)
synthesis = discussion.get_next_synthesis()
else:
synthesis = Synthesis.get_instance(synthesis_id)
if not synthesis:
raise HTTPBadRequest("Synthesis with id '%s' not found." % synthesis_id)
synthesis_data = json.loads(request.body)
user_id = request.authenticated_userid
permissions = get_permissions(user_id, discussion_id)
for key in ('subject', 'introduction', 'conclusion'):
if key in synthesis_data:
ls_data = synthesis_data[key]
if ls_data is None:
continue
assert isinstance(ls_data, dict)
current = LangString.create_from_json(
ls_data, user_id, permissions=permissions)
setattr(synthesis, key, current)
Synthesis.default_db.add(synthesis)
Synthesis.default_db.flush()
return {'ok': True, 'id': synthesis.uri()}
示例3: create_post
# 需要导入模块: from assembl.models import LangString [as 别名]
# 或者: from assembl.models.LangString import create_from_json [as 别名]
def create_post(request):
"""
We use post, not put, because we don't know the id of the post
"""
localizer = request.localizer
request_body = json.loads(request.body)
user_id = authenticated_userid(request)
if not user_id:
raise HTTPUnauthorized()
user = Post.default_db.query(User).filter_by(id=user_id).one()
body = request_body.get('body', None)
html = request_body.get('html', None) # BG: Is this used now? I cannot see it.
reply_id = request_body.get('reply_id', None)
idea_id = request_body.get('idea_id', None)
subject = request_body.get('subject', None)
publishes_synthesis_id = request_body.get('publishes_synthesis_id', None)
if not body and not publishes_synthesis_id:
# Should we allow empty messages otherwise?
raise HTTPBadRequest(localizer.translate(
_("Your message is empty")))
if reply_id:
in_reply_to_post = Post.get_instance(reply_id)
else:
in_reply_to_post = None
if idea_id:
in_reply_to_idea = Idea.get_instance(idea_id)
else:
in_reply_to_idea = None
discussion_id = int(request.matchdict['discussion_id'])
discussion = Discussion.get_instance(discussion_id)
if not discussion:
raise HTTPNotFound(localizer.translate(_(
"No discussion found with id=%s")) % (discussion_id,)
)
ctx = DummyContext({Discussion: discussion})
if html:
log.warning("Still using html")
# how to guess locale in this case?
body = LangString.create(html)
elif body:
body = LangString.create_from_json(
body, context=ctx, user_id=user_id)
else:
body = LangString.EMPTY(discussion.db)
if subject:
subject = LangString.create_from_json(
subject, context=ctx, user_id=user_id)
else:
# print(in_reply_to_post.subject, discussion.topic)
if in_reply_to_post:
subject = (in_reply_to_post.get_title().first_original().value
if in_reply_to_post.get_title() else '')
elif in_reply_to_idea:
# TODO: THis should use a cascade like the frontend
subject = (in_reply_to_idea.short_title
if in_reply_to_idea.short_title else '')
else:
subject = discussion.topic if discussion.topic else ''
# print subject
if subject is not None and len(subject):
new_subject = "Re: " + restrip_pat.sub('', subject).strip()
if (in_reply_to_post and new_subject == subject and
in_reply_to_post.get_title()):
# reuse subject and translations
subject = in_reply_to_post.get_title()
else:
# how to guess locale in this case?
subject = LangString.create(new_subject)
else:
raven_client = get_raven_client()
if raven_client:
raven_client.captureMessage(
"A message is about to be written to the database with an "
"empty subject. This is not supposed to happen.")
subject = LangString.EMPTY(discussion.db)
post_constructor_args = {
'discussion': discussion,
'creator_id': user_id,
'subject': subject,
'body': body
}
if publishes_synthesis_id:
published_synthesis = Synthesis.get_instance(publishes_synthesis_id)
post_constructor_args['publishes_synthesis'] = published_synthesis
new_post = SynthesisPost(**post_constructor_args)
else:
new_post = AssemblPost(**post_constructor_args)
discussion.db.add(new_post)
#.........这里部分代码省略.........
示例4: save_idea
# 需要导入模块: from assembl.models import LangString [as 别名]
# 或者: from assembl.models.LangString import create_from_json [as 别名]
def save_idea(request):
"""Update this idea.
In case the ``parentId`` is changed, handle all
``IdeaLink`` changes and send relevant ideas on the socket."""
discussion_id = int(request.matchdict['discussion_id'])
user_id = request.authenticated_userid
permissions = get_permissions(user_id, discussion_id)
idea_id = request.matchdict['id']
idea_data = json.loads(request.body)
#Idea.default_db.execute('set transaction isolation level read committed')
# Special items in TOC, like unsorted posts.
if idea_id in ['orphan_posts']:
return {'ok': False, 'id': Idea.uri_generic(idea_id)}
idea = Idea.get_instance(idea_id)
if not idea:
raise HTTPNotFound("No such idea: %s" % (idea_id))
if isinstance(idea, RootIdea):
raise HTTPBadRequest("Cannot edit root idea.")
discussion = Discussion.get(int(discussion_id))
if not discussion:
raise HTTPNotFound("Discussion with id '%s' not found." % discussion_id)
if(idea.discussion_id != discussion.id):
raise HTTPBadRequest(
"Idea from discussion %s cannot saved from different discussion (%s)." % (idea.discussion_id,discussion.id ))
simple_fields = {
'message_view_override': 'message_view_override',
'messages_in_parent': 'messages_in_parent',
}
for key, attr_name in simple_fields.iteritems():
if key in idea_data:
setattr(idea, attr_name, idea_data[key])
for key, attr_name in langstring_fields.iteritems():
if key in idea_data:
current = getattr(idea, attr_name)
ls_data = idea_data[key]
# TODO: handle legacy string instance?
assert isinstance(ls_data, (dict, NoneType))
if current:
if ls_data:
current.update_from_json(
ls_data, user_id, permissions=permissions)
else:
current.delete()
elif ls_data:
current = LangString.create_from_json(
ls_data, user_id, permissions=permissions)
setattr(idea, attr_name, current)
if 'parentId' in idea_data and idea_data['parentId'] is not None:
# TODO: Make sure this is sent as a list!
parent = Idea.get_instance(idea_data['parentId'])
if not parent:
raise HTTPNotFound("Missing parentId %s" % (idea_data['parentId']))
# calculate it early to maximize contention.
prev_ancestors = parent.get_all_ancestors()
new_ancestors = set()
order = idea_data.get('order', 0.0)
for parent_link in idea.source_links:
# still assuming there's only one.
pl_parent = parent_link.source
pl_ancestors = pl_parent.get_all_ancestors()
new_ancestors.update(pl_ancestors)
if parent_link.source != parent:
parent_link.copy(True)
parent_link.source = parent
parent.db.expire(parent, ['target_links'])
parent.db.expire(pl_parent, ['target_links'])
for ancestor in pl_ancestors:
if ancestor in prev_ancestors:
break
ancestor.send_to_changes()
for ancestor in prev_ancestors:
if ancestor in new_ancestors:
break
ancestor.send_to_changes()
parent_link.order = order
parent_link.db.expire(parent_link.source, ['target_links'])
parent_link.source.send_to_changes()
parent_link.db.flush()
idea.send_to_changes()
return {'ok': True, 'id': idea.uri() }
示例5: create_post
# 需要导入模块: from assembl.models import LangString [as 别名]
# 或者: from assembl.models.LangString import create_from_json [as 别名]
def create_post(request):
"""
Create a new post in this discussion.
We use post, not put, because we don't know the id of the post
"""
localizer = request.localizer
request_body = json.loads(request.body)
user_id = request.authenticated_userid
if not user_id:
raise HTTPUnauthorized()
user = Post.default_db.query(User).filter_by(id=user_id).one()
body = request_body.get('body', None)
html = request_body.get('html', None) # BG: Is this used now? I cannot see it.
reply_id = request_body.get('reply_id', None)
idea_id = request_body.get('idea_id', None)
subject = request_body.get('subject', None)
publishes_synthesis_id = request_body.get('publishes_synthesis_id', None)
message_classifier = request_body.get('message_classifier', None)
if not body and not publishes_synthesis_id:
# Should we allow empty messages otherwise?
raise HTTPBadRequest(localizer.translate(
_("Your message is empty")))
if reply_id:
in_reply_to_post = Post.get_instance(reply_id)
else:
in_reply_to_post = None
if idea_id:
in_reply_to_idea = Idea.get_instance(idea_id)
else:
in_reply_to_idea = None
discussion_id = int(request.matchdict['discussion_id'])
discussion = Discussion.get_instance(discussion_id)
if not discussion:
raise HTTPNotFound(localizer.translate(_(
"No discussion found with id=%s")) % (discussion_id,)
)
ctx = DummyContext({Discussion: discussion})
if html:
log.warning("Still using html")
# how to guess locale in this case?
body = LangString.create(sanitize_html(html))
# TODO: AssemblPosts are pure text right now.
# Allowing HTML requires changes to the model.
elif body:
# TODO: Accept HTML body.
for e in body['entries']:
e['value'] = sanitize_text(e['value'])
body = LangString.create_from_json(
body, context=ctx, user_id=user_id)
else:
body = LangString.EMPTY(discussion.db)
if subject:
for e in subject['entries']:
e['value'] = sanitize_text(e['value'])
subject = LangString.create_from_json(
subject, context=ctx, user_id=user_id)
else:
from assembl.models import Locale
locale = Locale.UNDEFINED
# print(in_reply_to_post.subject, discussion.topic)
if in_reply_to_post and in_reply_to_post.get_title():
original_subject = in_reply_to_post.get_title().first_original()
if original_subject:
locale = original_subject.locale_code
subject = (
original_subject.value or ''
if in_reply_to_post.get_title() else '')
elif in_reply_to_idea:
# TODO: THis should use a cascade like the frontend
# also, some ideas have extra langstring titles
subject = (in_reply_to_idea.short_title
if in_reply_to_idea.short_title else '')
locale = discussion.main_locale
else:
subject = discussion.topic if discussion.topic else ''
locale = discussion.main_locale
# print subject
if subject is not None and len(subject):
new_subject = "Re: " + restrip_pat.sub('', subject).strip()
if (in_reply_to_post and new_subject == subject and
in_reply_to_post.get_title()):
# reuse subject and translations
subject = in_reply_to_post.get_title().clone(discussion.db)
else:
# how to guess locale in this case?
subject = LangString.create(new_subject, locale)
else:
capture_message(
"A message is about to be written to the database with an "
"empty subject. This is not supposed to happen.")
#.........这里部分代码省略.........