本文整理汇总了Python中newslynx.models.util.fetch_by_id_or_field函数的典型用法代码示例。如果您正苦于以下问题:Python fetch_by_id_or_field函数的具体用法?Python fetch_by_id_or_field怎么用?Python fetch_by_id_or_field使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fetch_by_id_or_field函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: org_user
def org_user(user, org_id_slug, user_email):
# fetch org
org = fetch_by_id_or_field(Org, 'slug', org_id_slug)
if not org:
raise NotFoundError(
'This Org does not exist.')
# ensure the active user can edit this Org
if user.id not in org.user_ids:
raise ForbiddenError(
'You are not allowed to access this Org')
# localize
localize(org)
# get this new user by id / email
org_user = fetch_by_id_or_field(User, 'email', user_email)
if not org_user:
raise RequestError(
'This user does not yet exist')
# check whether this user can view this other user:
if not len(list(set(org_user.org_ids).intersection(set(user.org_ids)))):
raise ForbiddenError(
'You are not allowed to view this user.'
.format(user.email))
return jsonify(org_user)
示例2: merge_authors
def merge_authors(user, org, from_author_id, to_author_id):
"""
Merge two authors.
"""
from_a = fetch_by_id_or_field(Author, 'name', from_author_id,
org_id=org.id, transform='upper')
if not from_a:
raise NotFoundError(
'Author with ID/Name "{}" does not exist."'
.format(from_author_id))
to_a = fetch_by_id_or_field(Author, 'name', to_author_id,
org_id=org.id, transform='upper')
if not to_a:
raise NotFoundError(
'Author with ID/Name "{}" does not exist."'
.format(to_author_id))
# re associate content
stmt = update(content_items_authors)\
.where(and_(
content_items_authors.c.author_id == from_author_id,
~content_items_authors.c.content_item_id.in_(to_a.content_item_ids)))\
.values(author_id=to_author_id)
db.session.execute(stmt)
# remove from author id
db.session.delete(from_a)
db.session.commit()
return jsonify(to_a.to_dict(incl_content=True))
示例3: org_remove_user
def org_remove_user(user, org_id_slug, user_email):
if not user.admin:
raise AuthError(
'You must be an admin to remove a user from an Org.')
# fetch org
org = fetch_by_id_or_field(Org, 'slug', org_id_slug)
# if it still doesn't exist, raise an error.
if not org:
raise NotFoundError('This Org does not exist.')
# localize
localize(org)
# ensure the active user can edit this Org
if user.id not in org.user_ids:
raise ForbiddenError(
"You are not allowed to access this Org.")
# get this existing user by id / email
existing_user = fetch_by_id_or_field(User, 'email', user_email)
if not existing_user:
raise RequestError(
'User "{}" does not yet exist'
.format(user_email))
# ensure that user is not already a part of this Org.
if existing_user.id not in org.user_ids:
raise RequestError(
'User "{}" is not a part of Org "{}"'
.format(existing_user.email, org.name))
# remove the user from the org
org.users.remove(existing_user)
# if we're force-deleting the user, do so
# but make sure their recipes are re-assigned
# to the super-user
if arg_bool('force', False):
cmd = "UPDATE recipes set user_id={} WHERE user_id={}"\
.format(org.super_user.id, existing_user.id)
db.session.execute(cmd)
db.session.delete(user)
db.session.commit()
return delete_response()
示例4: update_author
def update_author(user, org, author_id):
"""
Update an author.
"""
a = fetch_by_id_or_field(Author, 'name', author_id,
org_id=org.id, transform='upper')
if not a:
raise NotFoundError(
'Author with ID/Name "{}" does not exist."'
.format(author_id))
req_data = request_data()
cols = get_table_columns(Author)
for k in req_data.keys():
if k not in cols or k in ['id', 'org_id']:
req_data.pop(k, None)
for k, v in req_data.items():
if k == 'name':
if v:
v = v.upper()
setattr(a, k, v)
db.session.add(a)
db.session.commit()
return jsonify(a)
示例5: delete_recipe
def delete_recipe(user, org, recipe_id):
r = fetch_by_id_or_field(Recipe, 'slug', recipe_id, org_id=org.id)
if not r:
raise NotFoundError('Recipe with id/slug {} does not exist.'
.format(recipe_id))
force = arg_bool('force', default=False)
if force:
db.session.delete(r)
else:
# just delete recipes with no approved events.
event_cnt = r.events.filter_by(status='approved').count()
if event_cnt > 0:
r.status = 'inactive'
db.session.add(r)
else:
db.session.delete(r)
# set the status of associated pending events to 'deleted'
cmd = """
UPDATE events SET status='deleted' WHERE recipe_id = {} AND status='pending';
""".format(r.id)
db.session.execute(cmd)
db.session.commit()
return delete_response()
示例6: author_add_content
def author_add_content(user, org, author_id, content_item_id):
"""
Add an author to a content item.
"""
a = fetch_by_id_or_field(Author, 'name', author_id,
org_id=org.id, transform='upper')
if not a:
raise NotFoundError(
'Author with ID/Name "{}" does not exist."'
.format(author_id))
c = ContentItem.query\
.filter_by(id=content_item_id, org_id=org.id)\
.first()
if not c:
raise RequestError(
'ContentItem with ID {} does not exist.'
.format(content_item_id))
if a.id not in c.author_ids:
c.authors.append(a)
db.session.add(c)
db.session.commit()
# return modified author
return jsonify(a.to_dict(incl_content=True))
示例7: bulk_create_org_timeseries
def bulk_create_org_timeseries(user, org_id_slug):
# fetch org
org = fetch_by_id_or_field(Org, 'slug', org_id_slug)
# if it still doesn't exist, raise an error.
if not org:
raise NotFoundError(
'This Org does not exist.')
# ensure the active user can edit this Org
if user.id not in org.user_ids:
raise ForbiddenError(
'You are not allowed to access this Org')
req_data = request_data()
job_id = ingest_bulk.org_timeseries(
req_data,
org_id=org.id,
metrics_lookup=org.timeseries_metrics,
commit=False
)
ret = url_for_job_status(apikey=user.apikey, job_id=job_id, queue='bulk')
return jsonify(ret)
示例8: decorated_function
def decorated_function(*args, **kw):
# get the org
org_id = arg_str('org', default=None)
if not org_id:
raise AuthError(
'An org is required for this request.')
# get the user object.
user = kw.get('user')
org = fetch_by_id_or_field(Org, 'slug', org_id)
# if it still doesn't exist, raise an error.
if not org:
raise NotFoundError(
'An Org with ID/Slug {} does exist.'
.format(org_id))
# otherwise ensure the active user can edit this Org
if user.id not in org.user_ids:
raise ForbiddenError(
'User "{}" is not allowed to access Org "{}".'
.format(user.name, org.name))
# check if we should localize this request
localize(org)
kw['org'] = org
return f(*args, **kw)
示例9: author_remove_content
def author_remove_content(user, org, author_id, content_item_id):
"""
Remove an author from a content item.
"""
a = fetch_by_id_or_field(Author, 'name', author_id,
org_id=org.id, transform='upper')
if not a:
raise NotFoundError(
'Author with ID/Name "{}" does not exist."'
.format(author_id))
c = ContentItem.query\
.filter_by(id=content_item_id, org_id=org.id)\
.first()
if not c:
raise RequestError(
'ContentItem with ID {} does not exist.'
.format(content_item_id))
if a.id in c.author_ids:
a.content_items.remove(c)
db.session.add(a)
db.session.commit()
return delete_response()
示例10: update_sous_chef
def update_sous_chef(user, sous_chef):
sc = fetch_by_id_or_field(SousChef, 'slug', sous_chef)
if not sc:
raise NotFoundError(
'A SousChef does not exist with ID/slug {}'
.format(sous_chef))
req_data = request_data()
# validate the schema
new_sous_chef = sous_chef_schema.update(sc, req_data)
# udpate
for name, value in new_sous_chef.items():
setattr(sc, name, value)
db.session.add(sc)
try:
db.session.commit()
except Exception as e:
raise RequestError(
"An error occurred while updating SousChef '{}'. "
"Here's the error message: {}"
.format(sc.slug, e.message))
return jsonify(sc)
示例11: org_metrics_summary
def org_metrics_summary(user, org_id_slug):
# fetch org
org = fetch_by_id_or_field(Org, 'slug', org_id_slug)
# if it still doesn't exist, raise an error.
if not org:
raise NotFoundError(
'This Org does not exist.')
# ensure the active user can edit this Org
if user.id not in org.user_ids:
raise ForbiddenError(
'You are not allowed to access this Org')
# localize
localize(org)
req_data = request_data()
ret = ingest_metric.org_summary(
req_data,
org_id=org.id,
valid_metrics=org.org_summary_metric_names,
commit=True
)
return jsonify(ret)
示例12: get_sous_chef
def get_sous_chef(user, sous_chef):
sc = fetch_by_id_or_field(SousChef, "slug", sous_chef)
if not sc:
raise NotFoundError("A SousChef does not exist with ID/slug {}".format(sous_chef))
return jsonify(sc)
示例13: update_metric
def update_metric(user, org, name_id):
m = fetch_by_id_or_field(Metric, 'name', name_id, org_id=org.id)
if not m:
raise NotFoundError(
'Metric "{}" does not yet exist for Org "{}"'
.format(name_id, org.name))
# get the request data
req_data = request_data()
# filter out any non-columns
columns = get_table_columns(Metric)
for k in req_data.keys():
if k not in columns:
req_data.pop(k)
# don't ever overwrite these:
for k in ['id', 'recipe_id', 'name', 'org_id', 'created', 'updated']:
if k in req_data:
req_data.pop(k, None)
# update fields
for k, v in req_data.items():
setattr(m, k, v)
try:
db.session.add(m)
db.session.commit()
except Exception as e:
raise RequestError("Error updating Metric: {}".format(e.message))
return jsonify(m)
示例14: delete_metric
def delete_metric(user, org, name_id):
m = fetch_by_id_or_field(Metric, 'name', name_id, org_id=org.id)
if not m:
raise NotFoundError(
'Metric "{}" does not yet exist.'
.format(name_id))
# format for deleting metrics from metric store.
cmd_fmt = "UPDATE {table} " + \
"SET metrics=json_del_keys(metrics, '{name}'::text);"\
.format(name=m.name)
# delete metric from metric stores.
if 'timeseries' in m.content_levels:
db.session.execute(cmd_fmt.format(table="content_metric_timeseries"))
if 'summary' in m.content_levels:
db.session.execute(cmd_fmt.format(table="content_metric_summary"))
if 'timeseries' in m.org_levels:
db.session.execute(cmd_fmt.format(table="org_metric_timeseries"))
if 'summary' in m.org_levels:
db.session.execute(cmd_fmt.format(table="org_metric_summary"))
db.session.delete(m)
db.session.commit()
return delete_response()
示例15: org_delete
def org_delete(user, org_id_slug):
if not user.admin:
raise AuthError(
'You must be an admin to delete an Org')
# fetch org
org = fetch_by_id_or_field(Org, 'slug', org_id_slug)
# if it still doesn't exist, raise an error.
if not org:
raise NotFoundError(
'This Org does not exist.')
# localize
localize(org)
# ensure the active user can edit this Org
if user.id not in org.user_ids:
raise ForbiddenError(
'User "{}" is not allowed to access Org "{}".'
.format(user.name, org.name))
db.session.delete(org)
db.session.commit()
return delete_response()