本文整理汇总了Python中c2corg_api.models.DBSession.flush方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.flush方法的具体用法?Python DBSession.flush怎么用?Python DBSession.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类c2corg_api.models.DBSession
的用法示例。
在下文中一共展示了DBSession.flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def post(self):
user = schema_create_user.objectify(self.request.validated)
user.password = self.request.validated['password']
user.update_validation_nonce(
Purpose.registration,
VALIDATION_EXPIRE_DAYS)
# directly create the user profile, the document id of the profile
# is the user id
lang = user.lang
user.profile = UserProfile(
categories=['amateur'],
locales=[DocumentLocale(lang=lang, title='')]
)
DBSession.add(user)
try:
DBSession.flush()
except:
log.warning('Error persisting user', exc_info=True)
raise HTTPInternalServerError('Error persisting user')
# also create a version for the profile
DocumentRest.create_new_version(user.profile, user.id)
# The user needs validation
email_service = get_email_service(self.request)
nonce = user.validation_nonce
settings = self.request.registry.settings
link = settings['mail.validate_register_url_template'] % nonce
email_service.send_registration_confirmation(user, link)
return to_json_dict(user, schema_user)
示例2: _put
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def _put(self, clazz, schema):
id = self.request.validated['id']
document_in = \
schema.objectify(self.request.validated['document'])
self._check_document_id(id, document_in.document_id)
# get the current version of the document
document = self._get_document(clazz, id)
self._check_versions(document, document_in)
# remember the current version numbers of the document
old_versions = document.get_versions()
# update the document with the input document
document.update(document_in)
try:
DBSession.flush()
except StaleDataError:
raise HTTPConflict('concurrent modification')
# when flushing the session, SQLAlchemy automatically updates the
# version numbers in case attributes have changed. by comparing with
# the old version numbers, we can check if only figures or only locales
# have changed.
(update_type, changed_langs) = \
self._check_update_type(document, old_versions)
self._update_version(
document, self.request.validated['message'], update_type,
changed_langs)
return to_json_dict(document, schema)
示例3: collection_post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def collection_post(self):
settings = self.request.registry.settings
locale = self.request.validated['locale']
title = "{}_{}".format(locale.document_id, locale.lang)
content = '<a href="{}">{}</a>'.format(
self.request.referer,
locale.title)
category = settings['discourse.category']
# category could be id or name
try:
category = int(category)
except:
pass
client = get_discourse_client(settings)
try:
response = client.client.create_post(content,
title=title,
category=category)
except:
raise HTTPInternalServerError('Error with Discourse')
if "topic_id" in response:
document_topic = DocumentTopic(topic_id=response['topic_id'])
locale.document_topic = document_topic
update_cache_version_direct(locale.document_id)
DBSession.flush()
return response
示例4: remove_token
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def remove_token(token):
now = datetime.datetime.utcnow()
condition = Token.value == token and Token.expire > now
result = DBSession.execute(Token.__table__.delete().where(condition))
if result.rowcount == 0:
log.debug("Failed to remove token %s" % token)
DBSession.flush()
示例5: update_feed_document_update
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def update_feed_document_update(document, user_id, update_types):
"""Update the feed entry for a document:
- update `area_ids` if the geometry has changed.
- update `activities` if figures have changed.
- update `user_ids` if the document is an outing and the participants
have changed.
Only when updating `user_ids`, the "actor" of the feed entry is changed.
And only then the time is updated and the `change_type` set to `updated`
to push the entry to the top of the feed.
"""
if document.redirects_to:
# TODO delete existing feed entry?
# see https://github.com/c2corg/v6_api/issues/386
return
if document.type in [IMAGE_TYPE, USERPROFILE_TYPE, AREA_TYPE]:
return
DBSession.flush()
# update areas
if UpdateType.GEOM in update_types:
update_areas_of_changes(document)
# updates activities
if document.type in [ARTICLE_TYPE, OUTING_TYPE, ROUTE_TYPE] and \
UpdateType.FIGURES in update_types:
update_activities_of_changes(document)
# update users_ids/participants (only for outings)
if document.type != OUTING_TYPE:
return
update_participants_of_outing(document.document_id, user_id)
示例6: update_feed_document_create
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def update_feed_document_create(document, user_id):
"""Creates a new entry in the feed table when creating a new document.
"""
if document.redirects_to or \
document.type in NO_FEED_DOCUMENT_TYPES:
return
# make sure all updates are written to the database, so that areas and
# users can be queried
DBSession.flush()
activities = []
if document.type in [ARTICLE_TYPE, OUTING_TYPE, ROUTE_TYPE]:
activities = document.activities
user_ids = [user_id]
if document.type == OUTING_TYPE:
participant_ids = _get_participants_of_outing(document.document_id)
user_ids = list(set(user_ids).union(participant_ids))
area_ids = []
if document.type != ARTICLE_TYPE:
area_ids = _get_area_ids(document)
change = DocumentChange(
user_id=user_id,
change_type='created',
document_id=document.document_id,
document_type=document.type,
activities=activities,
area_ids=area_ids,
user_ids=user_ids
)
DBSession.add(change)
DBSession.flush()
示例7: _collection_post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def _collection_post(
self, schema, before_add=None, after_add=None):
user_id = self.request.authenticated_userid
document_in = self.request.validated
document = schema.objectify(document_in)
document.document_id = None
if before_add:
before_add(document, user_id=user_id)
DBSession.add(document)
DBSession.flush()
DocumentRest.create_new_version(document, user_id)
if document.type != AREA_TYPE:
update_areas_for_document(document, reset=False)
if after_add:
after_add(document, user_id=user_id)
if document_in.get('associations', None):
create_associations(document, document_in['associations'], user_id)
notify_es_syncer(self.request.registry.queue_config)
return {'document_id': document.document_id}
示例8: _update_version
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def _update_version(self, document, user_id, comment, update_types,
changed_langs):
assert user_id
assert update_types
meta_data = HistoryMetaData(comment=comment, user_id=user_id)
archive = self._get_document_archive(document, update_types)
geometry_archive = \
self._get_geometry_archive(document, update_types)
cultures = \
self._get_cultures_to_update(document, update_types, changed_langs)
locale_versions = []
for culture in cultures:
locale = document.get_locale(culture)
locale_archive = self._get_locale_archive(locale, changed_langs)
version = DocumentVersion(
document_id=document.document_id,
culture=locale.culture,
document_archive=archive,
document_geometry_archive=geometry_archive,
document_locales_archive=locale_archive,
history_metadata=meta_data
)
locale_versions.append(version)
DBSession.add(archive)
DBSession.add(meta_data)
DBSession.add_all(locale_versions)
DBSession.flush()
示例9: _put
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def _put(
self, clazz, schema, clazz_locale=None, before_update=None,
after_update=None):
user_id = self.request.authenticated_userid
id = self.request.validated['id']
document_in = \
schema.objectify(self.request.validated['document'])
self._check_document_id(id, document_in.document_id)
# get the current version of the document
document = self._get_document(clazz, id, clazz_locale=clazz_locale)
if document.redirects_to:
raise HTTPBadRequest('can not update merged document')
if document.protected and not self.request.has_permission('moderator'):
raise HTTPForbidden('No permission to change a protected document')
self._check_versions(document, document_in)
# remember the current version numbers of the document
old_versions = document.get_versions()
# update the document with the input document
document.update(document_in)
if before_update:
before_update(document, document_in, user_id=user_id)
try:
DBSession.flush()
except StaleDataError:
raise HTTPConflict('concurrent modification')
# when flushing the session, SQLAlchemy automatically updates the
# version numbers in case attributes have changed. by comparing with
# the old version numbers, we can check if only figures or only locales
# have changed.
(update_types, changed_langs) = document.get_update_type(old_versions)
if update_types:
# A new version needs to be created and persisted
self._update_version(
document, user_id, self.request.validated['message'],
update_types, changed_langs)
if document.type != AREA_TYPE and UpdateType.GEOM in update_types:
update_areas_for_document(document, reset=True)
if after_update:
after_update(document, update_types, user_id=user_id)
# And the search updated
notify_es_syncer(self.request.registry.queue_config)
associations = self.request.validated.get('associations', None)
if associations:
synchronize_associations(document, associations, user_id)
return {}
示例10: add_or_retrieve_token
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def add_or_retrieve_token(value, expire, userid):
token = DBSession.query(Token).filter(Token.value == value and User.id == userid).first()
if not token:
token = Token(value=value, expire=expire, userid=userid)
DBSession.add(token)
DBSession.flush()
return token
示例11: post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def post(self):
user = schema_create_user.objectify(self.request.validated)
user.password = self.request.validated['password']
DBSession.add(user)
try:
DBSession.flush()
except:
# TODO: log the error for debugging
raise HTTPInternalServerError('Error persisting user')
return to_json_dict(user, schema_user)
示例12: _collection_post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def _collection_post(self, clazz, schema):
document = schema.objectify(self.request.validated)
document.document_id = None
DBSession.add(document)
DBSession.flush()
user_id = self.request.authenticated_userid
self._create_new_version(document, user_id)
sync_search_index(document)
return to_json_dict(document, schema)
示例13: _collection_post
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def _collection_post(self, clazz, schema):
document = schema.objectify(self.request.validated)
document.document_id = None
# TODO additional validation: at least one culture, only one instance
# for each culture, geometry
DBSession.add(document)
DBSession.flush()
self._create_new_version(document)
return to_json_dict(document, schema)
示例14: _put
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def _put(self, clazz, schema):
user_id = self.request.authenticated_userid
id = self.request.validated['id']
document_in = \
schema.objectify(self.request.validated['document'])
self._check_document_id(id, document_in.document_id)
# get the current version of the document
document = self._get_document(clazz, id)
self._check_versions(document, document_in)
# remember the current version numbers of the document
old_versions = document.get_versions()
# find out whether the update of the geometry should be skipped.
skip_geometry_update = document_in.geometry is None
# update the document with the input document
document.update(document_in)
try:
DBSession.flush()
except StaleDataError:
raise HTTPConflict('concurrent modification')
# when flushing the session, SQLAlchemy automatically updates the
# version numbers in case attributes have changed. by comparing with
# the old version numbers, we can check if only figures or only locales
# have changed.
(update_type, changed_langs) = document.get_update_type(old_versions)
if update_type:
# A new version needs to be created and persisted
self._update_version(
document, user_id, self.request.validated['message'],
update_type, changed_langs)
# And the search updated
sync_search_index(document)
json_dict = to_json_dict(document, schema)
if skip_geometry_update:
# Optimization: the geometry is not sent back if the client
# requested to skip the geometry update. Geometries may be very
# huge; this optimization should speed the data transfer.
json_dict['geometry'] = None
return json_dict
示例15: update_feed_images_upload
# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import flush [as 别名]
def update_feed_images_upload(images, images_in, user_id):
"""When uploading a set of images, create a feed entry for the document
the images are linked to.
"""
if not images or not images_in:
return
assert len(images) == len(images_in)
# get the document that the images were uploaded to
document_id, document_type = get_linked_document(images_in)
if not document_id or not document_type:
return
image1_id, image2_id, image3_id, more_images = get_images(
images, images_in, document_id, document_type)
if not image1_id:
return
# load the feed entry for the images
change = get_existing_change(document_id)
if not change:
log.warn('no feed change for document {}'.format(document_id))
return
if change.user_id == user_id:
# if the same user, only update time and change_type.
# this avoids that multiple entries are shown in the feed for the same
# document.
change.change_type = 'updated'
change.time = func.now()
else:
# if different user: copy the feed entry
change = change.copy()
change.change_type = 'added_photos'
change.user_id = user_id
change.user_ids = list(set(change.user_ids).union([user_id]))
change.image1_id = image1_id
change.image2_id = image2_id
change.image3_id = image3_id
change.more_images = more_images
DBSession.add(change)
DBSession.flush()