本文整理汇总了Python中utils.db_utils.db函数的典型用法代码示例。如果您正苦于以下问题:Python db函数的具体用法?Python db怎么用?Python db使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了db函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, name=None,
# portal_plan_id=None,
logo_file_id=None,
company_owner=None,
favicon_file_id=None,
lang='uk',
host=None, divisions=[], portal_layout_id=None):
self.name = name
self.lang = lang
self.logo_file_id = logo_file_id
self.favicon_file_id = favicon_file_id
self.host = host
self.divisions = divisions
self.portal_layout_id = portal_layout_id if portal_layout_id else db(PortalLayout).first().id
self.own_company = company_owner
self.company_members = [
MemberCompanyPortal(portal=self, company=company_owner, status=MemberCompanyPortal.STATUSES['ACTIVE'],
plan=db(MemberCompanyPortalPlan).first())]
# self.own_company.company_portals = db(MemberCompanyPortalPlan).first()
# db(MemberCompanyPortalPlan).first().portal_companies.add(MemberCompanyPortal(company=self.own_company))
# self.company_assoc = [MemberCompanyPortal(portal = self,
# company = self.own_company,
# company_portal_plan_id=db(MemberCompanyPortalPlan).first().id)]
pass
示例2: list_reader_load
def list_reader_load(json):
next_page = json.get('next_page') if json.get('next_page') else 1
search_text = request.args.get('search_text') or ''
article_fields = 'title|id|subtitle|short|image_file_id|subtitle|publishing_tm|read_count,company.name|logo_file_id|id,' \
'division.name,portal.name|host|logo_file_id|id'
favorite = request.args.get('favorite') == 'True'
localtime = time.gmtime(time.time())
filter = and_(Publication.portal_division_id == db(PortalDivision).filter(
PortalDivision.portal_id == db(UserPortalReader, user_id=g.user.id).subquery().c.portal_id).subquery().c.id,
Publication.status == Publication.STATUSES['PUBLISHED'],
Publication.publishing_tm < datetime.datetime(*localtime[:6])) if not favorite\
else (Publication.id == db(ReaderPublication, user_id=g.user.id,
favorite=True).subquery().c.article_portal_division_id)
articles, pages, page = Search().search({'class': Publication,
'filter': filter,
'tags': True, 'return_fields': article_fields}, page=1,
items_per_page=5*next_page,
search_text=search_text)
list_articles = Publication.get_list_reader_articles(articles)
return {
'end': True if pages == 1 or pages == 0 else False,
'articles': list_articles,
'pages': pages,
'current_page': page,
'page_buttons': Config.PAGINATION_BUTTONS,
# 'portals': portals,
'favorite': favorite
}
示例3: crop_image
def crop_image(image_id, coordinates, zoom, params):
from ..models.company import Company
image_query = db(File, id=image_id).one() # get file object
company_owner = db(Company).filter(or_(
Company.system_folder_file_id == image_query.root_folder_id,
Company.journalist_folder_file_id == image_query.root_folder_id)).one() # get company file owner
return File.crop(image_query, coordinates, zoom, company_owner, params)
示例4: subquery_company_articles
def subquery_company_articles(search_text=None, company_id=None, **kwargs):
sub_query = db(ArticleCompany, company_id=company_id)
if 'filter' in kwargs.keys():
if 'material_status' in kwargs['filter'].keys():
sub_query = db(ArticleCompany, company_id=company_id, status=kwargs['filter']['material_status'])
if 'publication_status' in kwargs['filter'].keys() or 'portals' in kwargs['filter'].keys():
sub_query = sub_query.join(ArticlePortalDivision,
ArticlePortalDivision.article_company_id == ArticleCompany.id)
if 'publication_status' in kwargs['filter'].keys():
sub_query = sub_query.filter(ArticlePortalDivision.status == kwargs['filter']['publication_status'])
if 'portals' in kwargs['filter'].keys():
sub_query = sub_query.join(PortalDivision,
PortalDivision.id == ArticlePortalDivision.portal_division_id). \
filter(PortalDivision.portal_id == kwargs['filter']['portals'])
if search_text:
if 'title' in search_text:
sub_query = sub_query.filter(ArticleCompany.title.ilike("%" + search_text['title'] + "%"))
if 'sort' in kwargs.keys():
if 'date' in kwargs['sort'].keys():
sub_query = sub_query.order_by(ArticleCompany.md_tm.asc()) if kwargs[
'sort']['date'] == 'asc' else sub_query.order_by(
ArticleCompany.md_tm.desc())
else:
sub_query = sub_query.order_by(expression.desc(ArticleCompany.md_tm))
return sub_query
示例5: get_allowed_statuses
def get_allowed_statuses(company_id=None, portal_id=None):
if company_id:
sub_query = db(MemberCompanyPortal, company_id=company_id).filter(
MemberCompanyPortal.status != "DELETED").all()
else:
sub_query = db(MemberCompanyPortal, portal_id=portal_id)
return sorted(list({partner.status for partner in sub_query}))
示例6: validate
def validate(self):
ret = {'errors': {}, 'warnings': {}, 'notices': {}}
if db(Portal, company_owner_id=self.company_owner_id).count():
ret['errors']['ok'] = 'portal for company already exists'
if not re.match('[^\s]{3,}', self.name):
ret['errors']['name'] = 'pls enter a bit longer name'
if not re.match(
'^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9]{1,})$',
self.host):
ret['errors']['host'] = 'pls enter valid host name'
grouped = {}
for div in self.divisions:
if div.portal_division_type_id in grouped:
grouped[div.portal_division_type_id] += 1
else:
grouped[div.portal_division_type_id] = 1
for check_division in db(PortalDivisionType).all():
if check_division.id not in grouped:
grouped[check_division.id] = 0
if check_division.min > grouped[check_division.id]:
ret['errors'][
'division_%s' % (check_division.id,)] = 'you need at least %s `%s`' % (
check_division.min, check_division.id)
if grouped[check_division.id] == 0:
ret['errors']['add_division'] = 'add at least one `%s`' % (check_division.id,)
if check_division.max < grouped[check_division.id]:
ret['errors'][
'division_%s' % (check_division.id,)] = 'you you can have only %s `%s`' % (
check_division.max, check_division.id)
return ret
示例7: subquery_user_articles
def subquery_user_articles(search_text=None, user_id=None, **kwargs):
article_filter = db(ArticleCompany, article_id=Article.id, **kwargs)
if search_text:
article_filter = article_filter.filter(ArticleCompany.title.ilike(
"%" + repr(search_text).strip("'") + "%"))
return db(Article, author_user_id=user_id).filter(article_filter.exists())
示例8: create_company
def create_company(self, data, file):
comp_dict = {"author_user_id": g.user_dict["id"]}
status = STATUS()
for x, y in zip(data.keys(), data.values()):
comp_dict[x] = y
company = Company(**comp_dict)
db_session.add(company)
db_session.commit()
user_rbac = UserCompany(user_id=company.author_user_id, company_id=company.id, status=status.ACTIVE())
db(Company, id=company.id).update(
{
"logo_file": File.upload(
file=file,
company_id=company.id,
parent_id=company.corporate_folder_file_id,
author=g.user_dict["name"],
author_user_id=g.user_dict["id"],
)
}
)
db_session.add(user_rbac)
db_session.commit()
r = Right()
r.add_rights(company.author_user_id, company.id, COMPANY_OWNER)
示例9: companies_load
def companies_load(json):
companies, pages, page, count = pagination(query=db(Company)
.filter(
Company.id == db(UserCompany, user_id=g.user.id).subquery().c.company_id), page=1,
items_per_page=6 * json.get('next_page') if json.get('next_page') else 6)
return {'companies': [usr_cmp.get_client_side_dict() for usr_cmp in companies],
'user_id': g.user.id, 'end': True if pages == 1 or pages == 0 else False}
示例10: publications_load
def publications_load(json, company_id):
portal = db(Company, id=company_id).one().own_portal
if not portal:
return dict(portal_not_exist=True)
current_page = json.get('page') or 1
params = {'search_text': json.get('search_text'), 'portal_id': portal.id}
if json.get('status'):
params['status'] = json.get('status')
subquery = ArticlePortalDivision.subquery_portal_articles(**params)
if json.get('company_id'):
subquery = subquery.filter(db(ArticleCompany,
company_id=json.get('company_id'),
id=ArticlePortalDivision.article_company_id).exists())
articles, pages, current_page = pagination(subquery,
page=current_page)
companies = ArticlePortalDivision.get_companies_which_send_article_to_portal(portal.id)
statuses = {status: status for status in ARTICLE_STATUS_IN_PORTAL.all}
publications = []
for a in articles:
a = a.get_client_side_dict()
del a['long']
publications.append(a)
return {'publications': publications,
'companies': companies,
'pages': {'total': pages,
'current_page': current_page,
'page_buttons': Config.PAGINATION_BUTTONS},
'statuses': statuses}
示例11: subquery_search
def subquery_search(filters=None, sorts=None, edit=None):
sub_query = db(TranslateTemplate)
list_filters = []; list_sorts = []
if edit:
exist = db(TranslateTemplate, template=edit['template'], name=edit['name']).first()
i = datetime.datetime.now()
TranslateTemplate.get(exist.id).attr({edit['col']: edit['newValue'], 'md_tm':i}).save().get_client_side_dict()
if 'url' in filters:
list_filters.append({'type': 'select', 'value': filters['url'], 'field': TranslateTemplate.url})
if 'template' in filters:
list_filters.append({'type': 'select', 'value': filters['template'], 'field': TranslateTemplate.template})
if 'name' in filters:
list_filters.append({'type': 'text', 'value': filters['name'], 'field': TranslateTemplate.name})
if 'uk' in filters:
list_filters.append({'type': 'text', 'value': filters['uk'], 'field': TranslateTemplate.uk})
if 'en' in filters:
list_filters.append({'type': 'text', 'value': filters['en'], 'field': TranslateTemplate.en})
if 'portal.name' in filters:
sub_query = sub_query.join(Portal,
Portal.id == TranslateTemplate.portal_id)
list_filters.append({'type': 'text', 'value': filters['portal.name'], 'field': Portal.name})
if 'cr_tm' in sorts:
list_sorts.append({'type': 'date', 'value': sorts['cr_tm'], 'field': TranslateTemplate.cr_tm})
elif 'ac_tm' in sorts:
list_sorts.append({'type': 'date', 'value': sorts['ac_tm'], 'field': TranslateTemplate.ac_tm})
else:
list_sorts.append({'type': 'date', 'value': 'desc', 'field': TranslateTemplate.cr_tm})
sub_query = Grid.subquery_grid(sub_query, list_filters, list_sorts)
return sub_query
示例12: __init__
def __init__(self, name=None,
# portal_plan_id=None,
logo_file_id=None,
company_owner=None,
host=None, divisions=[], portal_layout_id=None):
self.name = name
self.logo_file_id = logo_file_id
# self.company_owner_id = company_owner_id
# self.articles = articles
self.host = host
self.divisions = divisions
# self.portal_plan_id = portal_plan_id if portal_plan_id else db(MemberCompanyPortalPlan).first().id
self.portal_layout_id = portal_layout_id if portal_layout_id else db(PortalLayout).first().id
self.own_company = company_owner
self.company_members = [
MemberCompanyPortal(portal=self, company=company_owner, plan=db(MemberCompanyPortalPlan).first())]
# self.own_company.company_portals = db(MemberCompanyPortalPlan).first()
# db(MemberCompanyPortalPlan).first().portal_companies.add(MemberCompanyPortal(company=self.own_company))
# self.company_assoc = [MemberCompanyPortal(portal = self,
# company = self.own_company,
# company_portal_plan_id=db(MemberCompanyPortalPlan).first().id)]
pass
示例13: list_reader_load
def list_reader_load(json):
favorite = request.args.get("favorite") == "True"
localtime = time.gmtime(time.time())
if favorite:
publication_filter = (
Publication.id == db(ReaderPublication, user_id=g.user.id, favorite=True).subquery().c.publication_id
)
else:
division_filter = and_(
PortalDivision.portal_id == db(UserPortalReader, user_id=g.user.id).subquery().c.portal_id
)
publication_filter = and_(
Publication.portal_division_id == db(PortalDivision).filter(division_filter).subquery().c.id,
Publication.status == Publication.STATUSES["PUBLISHED"],
Publication.publishing_tm < datetime.datetime(*localtime[:6]),
)
publications, next_page = Publication.get_page(
filter=publication_filter, order_by=desc(Publication.publishing_tm), page=json.get("next_page"), per_page=10
)
return {
"next_page": next_page,
"end": next_page < 0,
"articles": [p.create_article() for p in publications],
"favorite": favorite,
}
示例14: crop_image
def crop_image(image_id, coordinates):
image_query = db(File, id=image_id).one()
if db(ImageCroped, original_image_id=image_id).count():
return update_croped_image(image_id, coordinates)
company_owner = db(Company).filter(or_(
Company.system_folder_file_id == image_query.root_folder_id,
Company.journalist_folder_file_id == image_query.root_folder_id)).one()
bytes_file = crop_with_coordinates(image_query, coordinates)
if bytes_file:
croped = File()
croped.md_tm = strftime("%Y-%m-%d %H:%M:%S", gmtime())
croped.size = sys.getsizeof(bytes_file.getvalue())
croped.name = image_query.name + '_cropped'
croped.parent_id = company_owner.system_folder_file_id
croped.root_folder_id = company_owner.system_folder_file_id
croped.mime = image_query.mime
fc = FileContent(content=bytes_file.getvalue(), file=croped)
copy_original_image_to_system_folder = \
File(parent_id=company_owner.system_folder_file_id, name=image_query.name+'_original',
mime=image_query.mime, size=image_query.size, user_id=g.user.id,
root_folder_id=company_owner.system_folder_file_id, author_user_id=g.user.id)
cfc = FileContent(content=image_query.file_content.content,
file=copy_original_image_to_system_folder)
g.db.add_all([croped, fc, copy_original_image_to_system_folder, cfc])
g.db.flush()
ImageCroped(original_image_id=copy_original_image_to_system_folder.id,
croped_image_id=croped.id,
x=float(coordinates['x']), y=float(coordinates['y']),
width=float(coordinates['width']),
height=float(coordinates['height']), rotate=int(coordinates['rotate'])).save()
return croped.id
else:
return image_query.id
示例15: update_article_portal
def update_article_portal(json, article_id):
db(ArticlePortalDivision, id=article_id).update({'status': json.get('new_status')})
article = db(ArticlePortalDivision, id=article_id).one().get_client_side_dict()
allowed_statuses = ArticlePortalDivision.STATUSES.keys()
json['allowed_statuses'] = allowed_statuses
json['article']['status'] = json.get('new_status')
return json