本文整理汇总了Python中c2corg_api.models.DBSession.query方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.query方法的具体用法?Python DBSession.query怎么用?Python DBSession.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类c2corg_api.models.DBSession
的用法示例。
在下文中一共展示了DBSession.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_association
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def validate_association(request):
"""Check if the given documents exist and if an association between the
two document types is valid.
"""
parent_document_id = request.validated.get('parent_document_id')
child_document_id = request.validated.get('child_document_id')
parent_document_type = None
if parent_document_id:
parent_document_type = DBSession.query(Document.type). \
filter(Document.document_id == parent_document_id).scalar()
if not parent_document_type:
request.errors.add(
'body', 'parent_document_id', 'parent document does not exist')
child_document_type = None
if child_document_id:
child_document_type = DBSession.query(Document.type). \
filter(Document.document_id == child_document_id).scalar()
if not child_document_type:
request.errors.add(
'body', 'child_document_id', 'child document does not exist')
if parent_document_type and child_document_type:
association_type = (parent_document_type, child_document_type)
if association_type not in valid_associations:
request.errors.add(
'body', 'association', 'invalid association type')
示例2: _get_documents
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def _get_documents(
self, clazz, schema, clazz_locale, adapt_schema, custom_filter,
include_areas, set_custom_fields, meta_params, load_documents):
base_query = DBSession.query(clazz).\
filter(getattr(clazz, 'redirects_to').is_(None))
base_total_query = DBSession.query(getattr(clazz, 'document_id')).\
filter(getattr(clazz, 'redirects_to').is_(None))
if custom_filter:
base_query = custom_filter(base_query)
base_total_query = custom_filter(base_total_query)
base_query = add_load_for_locales(base_query, clazz, clazz_locale)
base_query = base_query.options(joinedload(getattr(clazz, 'geometry')))
if clazz == Outing:
base_query = base_query. \
order_by(clazz.date_end.desc()). \
order_by(clazz.document_id.desc())
else:
base_query = base_query.order_by(clazz.document_id.desc())
base_query = add_load_for_profiles(base_query, clazz)
base_total_query = add_profile_filter(base_total_query, clazz)
if include_areas:
base_query = base_query. \
options(
joinedload(getattr(clazz, '_areas')).
load_only(
'document_id', 'area_type', 'version', 'protected',
'type').
joinedload('locales').
load_only(
'lang', 'title',
'version')
)
documents, total = load_documents(base_query, base_total_query)
set_available_langs(documents, loaded=True)
lang = meta_params['lang']
if lang is not None:
set_best_locale(documents, lang)
if include_areas:
self._set_areas_for_documents(documents, lang)
if set_custom_fields:
set_custom_fields(documents, lang)
return {
'documents': [
to_json_dict(
doc,
schema if not adapt_schema else adapt_schema(schema, doc)
) for doc in documents
],
'total': total
}
示例3: get_creators
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def get_creators(document_ids):
""" Get the creator for the list of given document ids.
"""
t = DBSession.query(
ArchiveDocument.document_id.label('document_id'),
User.id.label('user_id'),
User.name.label('name'),
over(
func.rank(), partition_by=ArchiveDocument.document_id,
order_by=HistoryMetaData.id).label('rank')). \
select_from(ArchiveDocument). \
join(
DocumentVersion,
and_(
ArchiveDocument.document_id == DocumentVersion.document_id,
ArchiveDocument.version == 1)). \
join(HistoryMetaData,
DocumentVersion.history_metadata_id == HistoryMetaData.id). \
join(User,
HistoryMetaData.user_id == User.id). \
filter(ArchiveDocument.document_id.in_(document_ids)). \
subquery('t')
query = DBSession.query(
t.c.document_id, t.c.user_id, t.c.name). \
filter(t.c.rank == 1)
return {
document_id: {
'name': name,
'user_id': user_id
} for document_id, user_id, name in query
}
示例4: get
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def get(self):
id = self.request.validated['id']
lang = self.request.validated['lang']
# FIXME conditional permission check (when outings implemented)
# is_outing = DBSession.query(Outing) \
# .filter(Outing.document_id == id).count()
# if is_outing > 0:
# # validate permission (authenticated + associated)
# # return 403 if not correct
title = DBSession.query(DocumentLocale.title) \
.filter(DocumentLocale.document_id == id) \
.filter(DocumentLocale.culture == lang) \
.first()
if not title:
raise HTTPNotFound('no locale document for ' + lang)
versions = DBSession.query(DocumentVersion) \
.options(joinedload('history_metadata').joinedload('user')) \
.filter(DocumentVersion.document_id == id) \
.filter(DocumentVersion.culture == lang) \
.order_by(DocumentVersion.id) \
.all()
return {
'title': title.title,
'versions': [self._serialize_version(v) for v in versions]
}
示例5: set_linked_routes
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def set_linked_routes(waypoint, lang):
"""
Set associated routes for the given waypoint including associated routes
of child and grandchild waypoints.
Note that this function returns a dict and not a list!
"""
with_query_waypoints = _get_select_children(waypoint)
route_ids = get_first_column(
DBSession.query(Route.document_id)
.select_from(with_query_waypoints)
.join(Association, with_query_waypoints.c.document_id == Association.parent_document_id)
.join(Route, Association.child_document_id == Route.document_id)
.filter(Route.redirects_to.is_(None))
.order_by(with_query_waypoints.c.priority.desc(), Route.document_id.desc())
.limit(NUM_ROUTES)
.all()
)
total = (
DBSession.query(Route.document_id)
.select_from(with_query_waypoints)
.join(Association, with_query_waypoints.c.document_id == Association.parent_document_id)
.join(Route, Association.child_document_id == Route.document_id)
.filter(Route.redirects_to.is_(None))
.count()
)
waypoint.associations["all_routes"] = get_documents_for_ids(route_ids, lang, route_documents_config, total)
示例6: _get_history
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def _get_history(self, document_id, lang):
# FIXME conditional permission check (when outings implemented)
# is_outing = DBSession.query(Outing) \
# .filter(Outing.document_id == document_id).count()
# if is_outing > 0:
# # validate permission (authenticated + associated)
# # return 403 if not correct
title = DBSession.query(DocumentLocale.title) \
.filter(DocumentLocale.document_id == document_id) \
.filter(DocumentLocale.lang == lang) \
.first()
if not title:
raise HTTPNotFound('no locale document for "{0}"'.format(lang))
versions = DBSession.query(DocumentVersion) \
.options(joinedload('history_metadata').joinedload('user')) \
.filter(DocumentVersion.document_id == document_id) \
.filter(DocumentVersion.lang == lang) \
.order_by(DocumentVersion.id) \
.all()
return {
'title': title.title,
'versions': [serialize_version(v) for v in versions]
}
示例7: get_changes_of_profile_feed
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def get_changes_of_profile_feed(user_id, token_id, token_time, limit):
user_exists_query = DBSession.query(User).filter(User.id == user_id).exists()
user_exists = DBSession.query(user_exists_query).scalar()
if not user_exists:
raise HTTPNotFound("user not found")
user_filter = DocumentChange.user_ids.op("&&")([user_id])
return get_changes_of_feed(token_id, token_time, limit, user_filter)
示例8: set_linked_routes
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def set_linked_routes(waypoint, lang):
"""
Set associated routes for the given waypoint including associated routes
of child and grandchild waypoints.
Note that this function returns a dict and not a list!
"""
with_query_waypoints = _get_select_children(waypoint)
total = DBSession.query(Route.document_id). \
select_from(with_query_waypoints). \
join(
Association,
with_query_waypoints.c.document_id ==
Association.parent_document_id). \
join(
Route,
Association.child_document_id == Route.document_id). \
filter(Route.redirects_to.is_(None)). \
count()
routes = limit_route_fields(
DBSession.query(Route).
select_from(with_query_waypoints).
join(
Association,
with_query_waypoints.c.document_id ==
Association.parent_document_id).
join(
Route,
Association.child_document_id == Route.document_id).
filter(Route.redirects_to.is_(None)).
order_by(
with_query_waypoints.c.priority.desc(),
Route.document_id.desc()).
limit(NUM_ROUTES)
). \
all()
if lang is not None:
set_best_locale(routes, lang)
waypoint.associations['all_routes'] = {
'total': total,
'routes': [
to_json_dict(route, schema_association_route)
for route in routes
]
}
示例9: post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def post(self):
request = self.request
username = request.validated["username"]
password = request.validated["password"]
user = DBSession.query(User).filter(User.username == username).first()
token = try_login(user, password, request) if user else None
if token:
response = token_to_response(user, token, request)
if "discourse" in request.json:
settings = request.registry.settings
client = get_discourse_client(settings)
try:
if "sso" in request.json and "sig" in request.json:
sso = request.json["sso"]
sig = request.json["sig"]
redirect = client.redirect(user, sso, sig)
response["redirect"] = redirect
else:
r = client.redirect_without_nonce(user)
response["redirect_internal"] = r
except:
# Any error with discourse should not prevent login
log.warning("Error logging into discourse for %d", user.id, exc_info=True)
return response
else:
request.errors.status = 403
request.errors.add("body", "user", "Login failed")
return None
示例10: get
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def get(self):
"""Get the user profile feed for a user.
Request:
`GET` `/profile-feed?u={user_id}[&pl=...][&limit=...][&token=...]`
Parameters:
`u={user_id}` (required)
The id of the user whose profile feed is requested.
For the other parameters, see above for '/feed'.
"""
lang, token_id, token_time, limit = get_params(self.request)
# load the requested user
requested_user_id = self.request.validated["u"]
requested_user = (
DBSession.query(User)
.filter(User.id == requested_user_id)
.filter(User.email_validated)
.options(load_only(User.id, User.is_profile_public))
.first()
)
if not requested_user:
raise HTTPNotFound("user not found")
elif requested_user.is_profile_public or self.request.has_permission("authenticated"):
# only return the feed if authenticated or if the user marked
# the profile as public
changes = get_changes_of_profile_feed(requested_user_id, token_id, token_time, limit)
return load_feed(changes, lang)
else:
raise HTTPForbidden("no permission to see the feed")
示例11: get_linked_xreports
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def get_linked_xreports(document, lang):
condition_as_child = and_(
Association.child_document_id == Xreport.document_id,
Association.parent_document_id == document.document_id
)
condition_as_parent = and_(
Association.child_document_id == document.document_id,
Association.parent_document_id == Xreport.document_id
)
if document.type in [WAYPOINT_TYPE, USERPROFILE_TYPE,
ARTICLE_TYPE, IMAGE_TYPE]:
condition = condition_as_parent
elif document.type in [ROUTE_TYPE, OUTING_TYPE]:
condition = condition_as_child
xreport_ids = get_first_column(
DBSession.query(Xreport.document_id).
filter(Xreport.redirects_to.is_(None)).
join(
Association, condition).
group_by(Xreport.document_id).
all())
return get_documents_for_ids(
xreport_ids, lang, xreport_documents_config).get('documents')
示例12: get_linked_articles
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def get_linked_articles(document, lang):
condition_as_child = and_(
Association.child_document_id == Article.document_id,
Association.parent_document_id == document.document_id
)
condition_as_parent = and_(
Association.child_document_id == document.document_id,
Association.parent_document_id == Article.document_id
)
if document.type == IMAGE_TYPE:
condition = condition_as_parent
elif document.type in [WAYPOINT_TYPE,
OUTING_TYPE,
ROUTE_TYPE,
BOOK_TYPE,
XREPORT_TYPE,
USERPROFILE_TYPE]:
condition = condition_as_child
elif document.type == ARTICLE_TYPE:
condition = or_(condition_as_child, condition_as_parent)
article_ids = get_first_column(
DBSession.query(Article.document_id).
filter(Article.redirects_to.is_(None)).
join(
Association, condition).
group_by(Article.document_id).
all())
return get_documents_for_ids(
article_ids, lang, article_documents_config).get('documents')
示例13: get_linked_routes
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def get_linked_routes(document, lang):
condition_as_child = and_(
Association.child_document_id == Route.document_id,
Association.parent_document_id == document.document_id)
condition_as_parent = and_(
Association.child_document_id == document.document_id,
Association.parent_document_id == Route.document_id)
if document.type == WAYPOINT_TYPE:
condition = condition_as_child
elif document.type in [OUTING_TYPE, IMAGE_TYPE, ARTICLE_TYPE,
XREPORT_TYPE]:
condition = condition_as_parent
else:
condition = or_(condition_as_child, condition_as_parent)
route_ids = get_first_column(
DBSession.query(Route.document_id).
filter(Route.redirects_to.is_(None)).
join(Association, condition).
group_by(Route.document_id).
all())
return get_documents_for_ids(
route_ids, lang, route_documents_config).get('documents')
示例14: update_linked_route_titles
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def update_linked_route_titles(waypoint, update_types, user_id):
"""When a waypoint is the main waypoint of a route, the field
`title_prefix`, which caches the waypoint name, has to be updated.
This method takes care of updating all routes, that the waypoint is
"main waypoint" of.
"""
if UpdateType.LANG not in update_types:
# if the locales did not change, no need to continue
return
linked_routes = DBSession.query(Route). \
filter(Route.main_waypoint_id == waypoint.document_id). \
options(joinedload(Route.locales).load_only(
RouteLocale.lang, RouteLocale.id)). \
options(load_only(Route.document_id)). \
all()
if linked_routes:
waypoint_locales = waypoint.locales
waypoint_locales_index = {
locale.lang: locale for locale in waypoint_locales}
for route in linked_routes:
set_route_title_prefix(
route, waypoint_locales, waypoint_locales_index)
示例15: post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import query [as 别名]
def post(self):
request = self.request
username = request.validated['username']
password = request.validated['password']
user = DBSession.query(User). \
filter(User.username == username).first()
token = try_login(user, password, request) if user else None
if token:
response = token_to_response(user, token, request)
if 'discourse' in request.json:
settings = request.registry.settings
client = get_discourse_client(settings)
try:
if 'sso' in request.json and 'sig' in request.json:
sso = request.json['sso']
sig = request.json['sig']
redirect = client.redirect(user, sso, sig)
response['redirect'] = redirect
else:
r = client.redirect_without_nonce(user)
response['redirect_internal'] = r
except:
# Any error with discourse should not prevent login
log.warning(
'Error logging into discourse for %d', user.id,
exc_info=True)
return response
else:
request.errors.status = 403
request.errors.add('body', 'user', 'Login failed')
return None