本文整理汇总了Python中services.util.safe_execute函数的典型用法代码示例。如果您正苦于以下问题:Python safe_execute函数的具体用法?Python safe_execute怎么用?Python safe_execute使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_execute函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: delete_storage
def delete_storage(self, user_id):
"""Removes all user data"""
for query in ('DELETE_USER_COLLECTIONS', 'DELETE_USER_WBOS'):
query = self._get_query(query, user_id)
safe_execute(self._engine, query, user_id=user_id)
# XXX see if we want to check the rowcount
return True
示例2: set_collection
def set_collection(self, user_id, collection_name, **values):
"""Creates a collection"""
# XXX values is not used for now because there are no values besides
# the name
if self.collection_exists(user_id, collection_name):
return
values['userid'] = user_id
values['name'] = collection_name
if self.standard_collections:
ids = _STANDARD_COLLECTIONS.keys()
min_id = max(ids) + 1
else:
min_id = 0
# getting the max collection_id
# XXX why don't we have an autoinc here ?
# see https://bugzilla.mozilla.org/show_bug.cgi?id=579096
next_id = -1
while next_id < min_id:
query = self._get_query('COLLECTION_NEXTID', user_id)
max_ = safe_execute(self._engine, query, user_id=user_id).first()
if max_[0] is None:
next_id = min_id
else:
next_id = max_[0] + 1
# insertion
values['collectionid'] = next_id
query = insert(collections).values(**values)
safe_execute(self._engine, query, **values)
return next_id
示例3: set_user
def set_user(self, user_id, **values):
"""set information for a user. values contains the fields to set.
If the user doesn't exists, it will be created.
"""
values['id'] = user_id
if not self.user_exists(user_id):
query = insert(users).values(**values)
else:
query = update(users).where(users.c.id == user_id)
query = query.values(**values)
safe_execute(self._engine, query)
示例4: update_password
def update_password(self, user_id, password, old_password=None, key=None):
"""Change the user password
Args:
user_id: user id
password: new password
Returns:
True if the change was successful, False otherwise
"""
if old_password is None:
if key:
#using a key, therefore we should check it
if self._get_reset_code(user_id) == key:
self.clear_reset_code(user_id)
else:
logger.error("bad key used for update password")
return False
else:
return False
password_hash = ssha256(password)
query = update(users).where(users.c.id == user_id)
res = safe_execute(self._engine,
query.values(password_hash=password_hash))
return res.rowcount == 1
示例5: clear_reset_code
def clear_reset_code(self, user_id):
if self._engine is None:
raise NotImplementedError()
query = delete(reset_codes).where(reset_codes.c.username == user_id)
res = safe_execute(self._engine, query)
return res.rowcount > 0
示例6: create_user
def create_user(self, username, password, email, **extra_fields):
"""Creates a user. Returns True on success."""
if not self.allow_new_users:
raise BackendError("Creation of new users is disabled")
password_hash = sscrypt(password)
values = {
'username': username,
'password': password_hash,
'mail': email,
}
for field in ('userid', 'accountStatus', 'mailVerified', 'syncNode'):
if field in extra_fields:
values[field] = extra_fields[field]
query = insert(users).values(**values)
try:
res = safe_execute(self._engine, query)
except IntegrityError:
#Name already exists
return False
if res.rowcount != 1:
return False
#need a copy with some of the info for the return value
userobj = User()
userobj['username'] = username
userobj['userid'] = res.lastrowid
userobj['mail'] = email
return userobj
示例7: get_user_info
def get_user_info(self, user, attrs):
"""Returns user info
Args:
user: the user object
attrs: the pieces of data requested
Returns:
user object populated with attrs
"""
user_id = self.get_user_id(user)
if user_id is None:
return user
attrs = [attr for attr in attrs if not user.get(attr)]
if attrs == []:
return user
fields = []
for attr in attrs:
fields.append(getattr(users.c, attr))
_USER_INFO = select(fields, users.c.userid == bindparam('user_id'))
res = safe_execute(self._engine, _USER_INFO,
user_id=user_id).fetchone()
if res is None:
return user
for attr in attrs:
try:
user[attr] = getattr(res, attr)
except AttributeError:
user[attr] = None
return user
示例8: get_collection
def get_collection(self, user_id, collection_name, fields=None,
create=True):
"""Return information about a collection."""
if fields is None:
fields = [collections]
field_names = collections.columns.keys()
else:
field_names = fields
fields = [getattr(collections.c, field) for field in fields]
query = select(fields, and_(collections.c.userid == user_id,
collections.c.name == collection_name))
res = safe_execute(self._engine, query).first()
# the collection is created
if res is None and create:
collid = self.set_collection(user_id, collection_name)
res = {'userid': user_id, 'collectionid': collid,
'name': collection_name}
if fields is not None:
for key in res.keys():
if key not in field_names:
del res[key]
else:
# make this a single step
res = dict([(key, value) for key, value in res.items()
if value is not None])
return res
示例9: collection_exists
def collection_exists(self, user_id, collection_name):
"""Returns True if the collection exists"""
query = self._get_query('COLLECTION_EXISTS', user_id)
res = safe_execute(self._engine, query, user_id=user_id,
collection_name=collection_name)
res = res.fetchone()
return res is not None
示例10: get_user_id
def get_user_id(self, user_name):
"""Returns the id for a user name"""
user = safe_execute(self._engine, _USER_ID,
user_name=user_name).fetchone()
if user is None:
return None
return user.id
示例11: _set_reset_code
def _set_reset_code(self, user_id):
code, expiration = generate_reset_code()
query = update(users).values(reset=code, reset_expiration=expiration)
res = safe_execute(self._engine, query.where(users.c.id == user_id))
if res.rowcount != 1:
logger.debug('Unable to add a new reset code')
return None # XXX see if appropriate
return code
示例12: delete_item
def delete_item(self, user_id, collection_name, item_id,
storage_time=None):
"""Deletes an item"""
collection_id = self._get_collection_id(user_id, collection_name)
query = self._get_query('DELETE_SOME_USER_WBO', user_id)
res = safe_execute(self._engine, query, user_id=user_id,
collection_id=collection_id,
item_id=item_id)
return res.rowcount == 1
示例13: get_collections
def get_collections(self, user_id, fields=None):
"""returns the collections information """
if fields is None:
fields = [collections]
else:
fields = [getattr(collections.c, field) for field in fields]
query = select(fields, collections.c.userid == user_id)
return safe_execute(self._engine, query).fetchall()
示例14: get_collection_counts
def get_collection_counts(self, user_id):
"""Return the collection counts for a given user"""
query = self._get_query('COLLECTION_COUNTS', user_id)
res = safe_execute(self._engine, query, user_id=user_id,
ttl=_int_now())
try:
return dict([(self._collid2name(user_id, collid), count)
for collid, count in res])
finally:
self._purge_cache(user_id)
示例15: get_collection_timestamps
def get_collection_timestamps(self, user_id):
"""return the collection names for a given user"""
query = 'COLLECTION_STAMPS'
query = self._get_query(query, user_id)
res = safe_execute(self._engine, query, user_id=user_id)
try:
return dict([(self._collid2name(user_id, coll_id),
bigint2time(stamp)) for coll_id, stamp in res])
finally:
self._purge_cache(user_id)