本文整理汇总了Python中inbox.models.Tag类的典型用法代码示例。如果您正苦于以下问题:Python Tag类的具体用法?Python Tag怎么用?Python Tag使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tag类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_sync
def prepare_sync(self):
"""Ensures that canonical tags are created for the account, and gets
and save Folder objects for folders on the IMAP backend. Returns a list
of tuples (folder_name, folder_id) for each folder we want to sync (in
order)."""
with mailsync_session_scope() as db_session:
account = db_session.query(ImapAccount).get(self.account_id)
Tag.create_canonical_tags(account.namespace, db_session)
with _pool(self.account_id).get() as crispin_client:
sync_folders = crispin_client.sync_folders()
save_folder_names(log, self.account_id,
crispin_client.folder_names(), db_session)
sync_folder_names_ids = []
for folder_name in sync_folders:
try:
id_, = db_session.query(Folder.id). \
filter(Folder.name == folder_name,
Folder.account_id == self.account_id).one()
sync_folder_names_ids.append((folder_name, id_))
except NoResultFound:
log.error("Missing Folder object when starting sync",
folder_name=folder_name)
raise MailsyncError("Missing Folder '{}' on account {}"
.format(folder_name, self.account_id))
return sync_folder_names_ids
示例2: create_canonical_tags
def create_canonical_tags(db):
"""Ensure that all canonical tags exist for the namespace we're testing
against. This is normally done when an account sync starts."""
from inbox.models import Namespace, Tag
namespace = db.session.query(Namespace).first()
Tag.create_canonical_tags(namespace, db.session)
db.session.commit()
示例3: sync
def sync(self):
""" Start per-folder syncs. Only have one per-folder sync in the
'initial' state at a time.
"""
with mailsync_session_scope() as db_session:
with _pool(self.account_id).get() as crispin_client:
sync_folders = crispin_client.sync_folders()
account = db_session.query(ImapAccount)\
.get(self.account_id)
save_folder_names(log, account,
crispin_client.folder_names(), db_session)
Tag.create_canonical_tags(account.namespace, db_session)
folder_id_for = {name: id_ for id_, name in db_session.query(
Folder.id, Folder.name).filter_by(account_id=self.account_id)}
saved_states = {name: state for name, state in
db_session.query(Folder.name,
ImapFolderSyncStatus.state)
.join(ImapFolderSyncStatus.folder)
.filter(ImapFolderSyncStatus.account_id ==
self.account_id)}
for folder_name in sync_folders:
if folder_name not in folder_id_for:
log.error("Missing Folder object when starting sync",
folder_name=folder_name, folder_id_for=folder_id_for)
raise MailsyncError("Missing Folder '{}' on account {}"
.format(folder_name, self.account_id))
if saved_states.get(folder_name) != 'finish':
log.info('initializing folder sync')
# STOPSHIP(emfree): replace by appropriate base class.
thread = self.sync_engine_class(self.account_id, folder_name,
folder_id_for[folder_name],
self.email_address,
self.provider_name,
self.poll_frequency,
self.syncmanager_lock,
self.refresh_flags_max,
self.retry_fail_classes)
thread.start()
self.folder_monitors.add(thread)
while not self._thread_polling(thread) and \
not self._thread_finished(thread) and \
not thread.ready():
sleep(self.heartbeat)
# Allow individual folder sync monitors to shut themselves down
# after completing the initial sync.
if self._thread_finished(thread) or thread.ready():
log.info('folder sync finished/killed',
folder_name=thread.folder_name)
# NOTE: Greenlet is automatically removed from the group.
self.folder_monitors.join()
示例4: sync
def sync(self):
""" Start per-folder syncs. Only have one per-folder sync in the
'initial' state at a time.
"""
with session_scope(ignore_soft_deletes=False) as db_session:
with _pool(self.account_id).get() as crispin_client:
sync_folders = crispin_client.sync_folders()
account = db_session.query(ImapAccount)\
.get(self.account_id)
save_folder_names(self.log, account,
crispin_client.folder_names(), db_session)
Tag.create_canonical_tags(account.namespace, db_session)
folder_id_for = {name: id_ for id_, name in db_session.query(
Folder.id, Folder.name).filter_by(account_id=self.account_id)}
saved_states = {name: state for name, state in
db_session.query(Folder.name,
ImapFolderSyncStatus.state)
.join(ImapFolderSyncStatus.folder)
.filter(ImapFolderSyncStatus.account_id ==
self.account_id)}
for folder_name in sync_folders:
if folder_name not in folder_id_for:
self.log.error("Missing Folder object when starting sync",
folder_name=folder_name,
folder_id_for=folder_id_for)
raise MailsyncError("Missing Folder '{}' on account {}"
.format(folder_name, self.account_id))
if saved_states.get(folder_name) != 'finish':
self.log.info('initializing folder sync')
thread = ImapFolderSyncMonitor(self.account_id, folder_name,
folder_id_for[folder_name],
self.email_address,
self.provider_name,
self.shared_state,
self.folder_state_handlers,
self.retry_fail_classes)
thread.start()
self.folder_monitors.add(thread)
while not self._thread_polling(thread) and \
not self._thread_finished(thread):
sleep(self.heartbeat)
# Allow individual folder sync monitors to shut themselves down
# after completing the initial sync.
if self._thread_finished(thread):
self.log.info('folder sync finished')
# NOTE: Greenlet is automatically removed from the group
# after finishing.
self.folder_monitors.join()
示例5: sync
def sync(self):
""" Start per-folder syncs. Only have one per-folder sync in the
'initial' state at a time.
"""
with session_scope() as db_session:
saved_states = dict()
folder_id_for = dict()
for saved_state in db_session.query(ImapFolderSyncStatus)\
.filter_by(account_id=self.account_id):
saved_states[saved_state.folder.name] = saved_state.state
folder_id_for[saved_state.folder.name] = saved_state.folder.id
# it's possible we've never started syncs for these folders before
for folder_id, folder_name, in \
db_session.query(Folder.id, Folder.name).filter_by(
account_id=self.account_id):
folder_id_for[folder_name] = folder_id
with connection_pool(self.account_id).get() as crispin_client:
sync_folders = crispin_client.sync_folders()
account = db_session.query(ImapAccount)\
.get(self.account_id)
save_folder_names(self.log, account,
crispin_client.folder_names(), db_session)
Tag.create_canonical_tags(account.namespace, db_session)
for folder_name in sync_folders:
if saved_states.get(folder_name) != 'finish':
self.log.info("Initializing folder sync for {0}"
.format(folder_name))
thread = ImapFolderSyncMonitor(self.account_id, folder_name,
folder_id_for[folder_name],
self.email_address,
self.provider,
self.shared_state,
self.folder_state_handlers,
self.retry_fail_classes)
thread.start()
self.folder_monitors.add(thread)
while not self._thread_polling(thread) and \
not self._thread_finished(thread):
sleep(self.heartbeat)
# Allow individual folder sync monitors to shut themselves down
# after completing the initial sync.
if self._thread_finished(thread):
self.log.info("Folder sync for {} is done."
.format(folder_name))
# NOTE: Greenlet is automatically removed from the group
# after finishing.
self.folder_monitors.join()
示例6: tag_update_api
def tag_update_api(public_id):
try:
valid_public_id(public_id)
tag = g.db_session.query(Tag).filter(
Tag.public_id == public_id,
Tag.namespace_id == g.namespace.id).one()
except NoResultFound:
raise NotFoundError('No tag found')
data = request.get_json(force=True)
if not ('name' in data.keys() and isinstance(data['name'], basestring)):
raise InputError('Malformed tag update request')
if 'namespace_id' in data.keys():
ns_id = data['namespace_id']
valid_public_id(ns_id)
if ns_id != g.namespace.public_id:
raise InputError('Cannot change the namespace on a tag.')
if not tag.user_created:
raise InputError('Cannot modify tag {}'.format(public_id))
# Lowercase tag name, regardless of input casing.
new_name = data['name'].lower()
if new_name != tag.name: # short-circuit rename to same value
if not Tag.name_available(new_name, g.namespace.id, g.db_session):
return err(409, 'Tag name already used')
tag.name = new_name
g.db_session.commit()
return g.encoder.jsonify(tag)
示例7: tag_update_api
def tag_update_api(public_id):
try:
valid_public_id(public_id)
tag = g.db_session.query(Tag).filter(
Tag.public_id == public_id,
Tag.namespace_id == g.namespace.id).one()
except InputError:
return err(400, '{} is not a valid id'.format(public_id))
except NoResultFound:
return err(404, 'No tag found')
data = request.get_json(force=True)
if 'name' not in data.keys():
return err(400, 'Malformed tag update request')
if 'namespace_id' in data.keys():
ns_id = data['namespace_id']
valid_public_id(ns_id)
if ns_id != g.namespace.id:
return err(400, 'Cannot change the namespace on a tag.')
if not tag.user_created:
return err(403, 'Cannot modify tag {}'.format(public_id))
new_name = data['name']
if new_name != tag.name: # short-circuit rename to same value
if not Tag.name_available(new_name, g.namespace.id, g.db_session):
return err(409, 'Tag name already used')
tag.name = new_name
g.db_session.commit()
# TODO(emfree) also support deleting user-created tags.
return g.encoder.jsonify(tag)
示例8: upgrade
def upgrade():
from inbox.models.session import session_scope
from inbox.models import Namespace, Tag, Thread
from inbox.sqlalchemy_ext.util import safer_yield_per
from sqlalchemy import func
from sqlalchemy.orm import joinedload
with session_scope() as db_session:
# Create the attachment tag
for ns in db_session.query(Namespace):
Tag.create_canonical_tags(ns, db_session)
thread_count, = db_session.query(func.count(Thread.id)).one()
q = db_session.query(Thread).options(joinedload(Thread.messages))
processed_count = 0
for thr in safer_yield_per(q, Thread.id, 1, thread_count):
if any(m.attachments for m in thr.messages):
attachment_tag = thr.namespace.tags['attachment']
thr.apply_tag(attachment_tag)
processed_count += 1
print processed_count
示例9: folder_sync_engine
def folder_sync_engine(db, monkeypatch):
# super ugly, but I don't want to have to mock tons of stuff
import inbox.mailsync.backends.imap.generic
from inbox.mailsync.backends.imap.generic import FolderSyncEngine
from inbox.models import Tag
monkeypatch.setattr(inbox.mailsync.backends.imap.generic,
"_pool", lambda(account): True)
# setup a dummy FolderSyncEngine - we only need to call a couple
# methods.
email = "[email protected]"
account = GenericAuthHandler('fastmail').create_account(
db.session, email, {"email": email, "password": "BLAH"})
Tag.create_canonical_tags(account.namespace, db.session)
db.session.add(account)
db.session.commit()
engine = None
engine = FolderSyncEngine(account.id, "Inbox", 0,
email, "fastmail",
3200, None, 20, None)
return engine
示例10: tag_create_api
def tag_create_api():
data = request.get_json(force=True)
if data.keys() != ['name']:
return err(400, 'Malformed tag request')
tag_name = data['name']
if not Tag.name_available(tag_name, g.namespace.id, g.db_session):
return err(409, 'Tag name not available')
if len(tag_name) > MAX_INDEXABLE_LENGTH:
return err(400, 'Tag name is too long.')
tag = Tag(name=tag_name, namespace=g.namespace, user_created=True)
g.db_session.commit()
return g.encoder.jsonify(tag)
示例11: tag_create_api
def tag_create_api():
data = request.get_json(force=True)
if 'name' not in data.keys():
return err(400, 'Malformed tag request')
if 'namespace_id' in data.keys():
ns_id = data['namespace_id']
valid_public_id(ns_id)
if ns_id != g.namespace.id:
return err(400, 'Cannot change the namespace on a tag.')
tag_name = data['name']
if not Tag.name_available(tag_name, g.namespace.id, g.db_session):
return err(409, 'Tag name not available')
if len(tag_name) > MAX_INDEXABLE_LENGTH:
return err(400, 'Tag name is too long.')
tag = Tag(name=tag_name, namespace=g.namespace, user_created=True)
g.db_session.commit()
return g.encoder.jsonify(tag)
示例12: tag_create_api
def tag_create_api():
data = request.get_json(force=True)
if not ('name' in data.keys() and isinstance(data['name'], basestring)):
raise InputError('Malformed tag request')
if 'namespace_id' in data.keys():
ns_id = data['namespace_id']
valid_public_id(ns_id)
if ns_id != g.namespace.id:
raise InputError('Cannot change the namespace on a tag.')
# Lowercase tag name, regardless of input casing.
tag_name = data['name'].lower()
if not Tag.name_available(tag_name, g.namespace.id, g.db_session):
return err(409, 'Tag name not available')
if len(tag_name) > MAX_INDEXABLE_LENGTH:
raise InputError('Tag name is too long.')
tag = Tag(name=tag_name, namespace=g.namespace, user_created=True)
g.db_session.commit()
return g.encoder.jsonify(tag)
示例13: tag_read_update_api
def tag_read_update_api(public_id):
try:
valid_public_id(public_id)
tag = g.db_session.query(Tag).filter(Tag.public_id == public_id, Tag.namespace_id == g.namespace.id).one()
except InputError:
return err(400, "{} is not a valid id".format(public_id))
except NoResultFound:
return err(404, "No tag found")
if request.method == "GET":
return g.encoder.jsonify(tag)
elif request.method == "PUT":
data = request.get_json(force=True)
if data.keys() != ["name"]:
return err(400, "Malformed tag update request")
if not tag.user_created:
return err(403, "Cannot modify tag {}".format(public_id))
new_name = data["name"]
if not Tag.name_available(new_name, g.namespace.id, g.db_session):
return err(409, "Tag name already used")
tag.name = new_name
g.db_session.commit()
return g.encoder.jsonify(tag)
示例14: tag_read_update_api
def tag_read_update_api(public_id):
try:
valid_public_id(public_id)
tag = g.db_session.query(Tag).filter(
Tag.public_id == public_id,
Tag.namespace_id == g.namespace.id).one()
except ValueError:
return err(400, '{} is not a valid id'.format(public_id))
except NoResultFound:
return err(404, 'No tag found')
if request.method == 'GET':
return g.encoder.jsonify(tag)
elif request.method == 'PUT':
data = request.get_json(force=True)
if data.keys() != ['name']:
return err(400, 'Malformed tag update request')
if not tag.user_created:
return err(403, 'Cannot modify tag {}'.format(public_id))
new_name = data['name']
if not Tag.name_available(new_name, g.namespace.id, g.db_session):
return err(409, 'Tag name already used')
tag.name = new_name
g.db_session.commit()
return g.encoder.jsonify(tag)
示例15: create_canonical_tags
def create_canonical_tags(db, default_namespace):
"""Ensure that all canonical tags exist for the namespace we're testing
against. This is normally done when an account sync starts."""
Tag.create_canonical_tags(default_namespace, db.session)
db.session.commit()