def result(): if not request.args: return redirect(url_for('hello')) all_languages = any_(['ru', 'sr']) all_semantics = any_([x.semantics for x in session.query(Semantics).all()]) all_speech_acts = any_( [x.speech_act for x in session.query(SpeechAct).all()]) word = request.args.get('word') language = request.args.get('language') if not language: language = all_languages pr_sem = request.args.get('pr_sem') if not pr_sem: pr_sem = all_semantics speech_act = request.args.get('speech_act') if not speech_act: speech_act = all_speech_acts add_sem = request.args.getlist('add_sem') if not add_sem: add_sem = all_semantics else: add_sem = any_(add_sem) new_query = session.query(DF) records = new_query.filter( DF.language == language, DF.primary_semantics.has(Semantics.semantics == pr_sem), DF.speech_act.has(SpeechAct.speech_act == speech_act), DF.additional_semantics.has(Semantics.semantics == add_sem)).all() records = merge_dfs(records) return render_template('result.html', records=records)
def _update_feed_entries(source_user_id, target_user_id): # Transfer feed entries to the target user only if no similar entry # already exists in the target user feed items. shared_doc_ids = DBSession.query(DocumentChange.document_id). \ filter(DocumentChange.user_id == target_user_id). \ intersect( DBSession.query(DocumentChange.document_id). filter(DocumentChange.user_id == source_user_id) ).subquery() DBSession.execute( DocumentChange.__table__.update().where(and_( DocumentChange.user_id == source_user_id, ~DocumentChange.document_id.in_(shared_doc_ids) )).values({ DocumentChange.user_id: target_user_id }) ) # Remove remaining feed items since they already exist for the target user DBSession.query(DocumentChange). \ filter(DocumentChange.user_id == source_user_id).delete() # If target user_id is already in the list of users associated # to feed items (user_ids), simply remove source user_id from the list: DBSession.execute( DocumentChange.__table__.update().where(and_( any_(DocumentChange.user_ids) == source_user_id, any_(DocumentChange.user_ids) == target_user_id )).values({ DocumentChange.user_ids: func.array_remove( DocumentChange.user_ids, source_user_id) }) ) # Else replace source user_id by target user_id in user_ids DBSession.execute( DocumentChange.__table__.update().where( any_(DocumentChange.user_ids) == source_user_id ).values({ DocumentChange.user_ids: func.array_replace( DocumentChange.user_ids, source_user_id, target_user_id) }) ) # Remove subscriptions to/of the source user DBSession.query(FollowedUser). \ filter(or_( FollowedUser.followed_user_id == source_user_id, FollowedUser.follower_user_id == source_user_id )).delete() # Remove feed filter prefs DBSession.query(FilterArea). \ filter(FilterArea.user_id == source_user_id).delete()
def find_by_year_and_genere(cls, _year, _genre): return cls.query.filter( and_( _genre == any_(cls.genres), cls.startYear == datetime.strptime(_year, '%Y') ) ).order_by(cls.primaryTitle)
def tag_array(tagids): if not tagids: return None st = d.meta.tables['searchtag'] return sa.func.array( sa.select([st.c.title]) .where(st.c.tagid == any_(list(tagids))) .as_scalar())
def list(pred_id: int, task_type: str): filters = [] if pred_id is not None: filters.append(Task.pred_id == pred_id) if task_type is not None: filters.append(Task.type.like(any_(task_type))) return Task.query.filter(*filters)
def bucket_links(cls, identities): if not identities: return [] q = (cls.get_media_query().filter( getattr(cls, cls._identity) == any_(list(identities)))) buckets = {identity: {} for identity in identities} for link in q: media_data = link.media_item.serialize(link=link) buckets[getattr(link, cls._identity)].setdefault(link.link_type, []).append(media_data) return list(buckets.values())
def select_list(map_table, targetids): if not targetids: return {} mt = map_table q = ( d.sa .select([mt.c.targetid, d.sa.func.array_agg(mt.c.tagid)]) .select_from(mt) .where(mt.c.targetid == any_(targetids)) .group_by(mt.c.targetid)) db = d.connect() return dict(list(db.execute(q)))
def find_by_movie(cls, _knownForTitles: str): return cls.query.filter( _knownForTitles == any_(cls.knownForTitles)).all()
def get_group_topics( # noqa request: Request, after: Optional[str], before: Optional[str], order: Optional[TopicSortOption], per_page: int, rank_start: Optional[int], tag: Optional[Ltree], unfiltered: bool, **kwargs: Any) -> dict: """Get a listing of topics in the group.""" # period needs special treatment so we can distinguish between missing and None period = kwargs.get("period", missing) is_home_page = request.matched_route.name == "home" if is_home_page: # on the home page, include topics from the user's subscribed groups # (or all groups, if logged-out) if request.user: groups = [sub.group for sub in request.user.subscriptions] else: groups = [ group for group in request.query(Group).all() if group.path != "test" ] subgroups = None else: # otherwise, just topics from the single group that we're looking at groups = [request.context] subgroups = (request.query(Group).filter( Group.path.descendant_of(request.context.path), Group.path != request.context.path, ).all()) default_settings = _get_default_settings(request, order) if not order: order = default_settings.order if period is missing: period = default_settings.period # set up the basic query for topics query = (request.query(Topic).join_all_relationships().inside_groups( groups, include_subgroups=not is_home_page).exclude_ignored(). apply_sort_option(order)) # restrict the time period, if not set to "all time" if period: query = query.inside_time_period(period) # restrict to a specific tag, if we're viewing a single one if tag: query = query.has_tag(str(tag)) # apply before/after pagination restrictions if relevant if before: query = query.before_id36(before) if after: query = query.after_id36(after) # apply topic tag filters unless they're disabled or viewing a single tag if request.user and request.user.filtered_topic_tags and not (tag or unfiltered): query = query.filter(~Topic.tags.descendant_of( # type: ignore any_(cast(request.user.filtered_topic_tags, TagList)))) topics = query.get_page(per_page) # don't show pinned topics on home page if request.matched_route.name == "home": pinned_topics = [] else: # get pinned topics pinned_query = (request.query(Topic).join_all_relationships( ).inside_groups(groups).is_pinned(True).apply_sort_option(order)) pinned_topics = pinned_query.all() period_options = [ SimpleHoursPeriod(hours) for hours in (1, 12, 24, 72, 168) ] # add the current period to the bottom of the dropdown if it's not one of the # "standard" ones if period and period not in period_options: period_options.append(period) if isinstance(request.context, Group): wiki_pages = (request.query(GroupWikiPage).filter( GroupWikiPage.group == request.context).order_by( GroupWikiPage.path).all()) # remove the index from the page list, we'll output it separately if any(page.path == "index" for page in wiki_pages): wiki_has_index = True wiki_pages = [page for page in wiki_pages if page.path != "index"] else: wiki_has_index = False else: wiki_pages = None wiki_has_index = False if isinstance(request.context, Group): # Get the most recent topic from each scheduled topic in this group # I'm not even going to attempt to write this query in pure SQLAlchemy topic_id_subquery = """ SELECT topic_id FROM (SELECT topic_id, schedule_id, row_number() OVER (PARTITION BY schedule_id ORDER BY created_time DESC) AS rownum FROM topics) AS t WHERE schedule_id IS NOT NULL AND rownum = 1 """ most_recent_scheduled_topics = ( request.query(Topic).join(TopicSchedule).filter( Topic.topic_id.in_(text(topic_id_subquery)), # type: ignore TopicSchedule.group == request.context, TopicSchedule.next_post_time != None, # noqa ).order_by(TopicSchedule.next_post_time).all()) else: most_recent_scheduled_topics = None if is_home_page: financial_data = get_financial_data(request.db_session) else: financial_data = None return { "group": request.context, "groups": groups, "topics": topics, "pinned_topics": pinned_topics, "order": order, "order_options": TopicSortOption, "period": period, "period_options": period_options, "is_default_period": period == default_settings.period, "is_default_view": (period == default_settings.period and order == default_settings.order), "rank_start": rank_start, "tag": tag, "unfiltered": unfiltered, "wiki_pages": wiki_pages, "wiki_has_index": wiki_has_index, "subgroups": subgroups, "most_recent_scheduled_topics": most_recent_scheduled_topics, "financial_data": financial_data, "current_time": utc_now(), }
def get_group_topics( request: Request, order: Any, # more specific would be better, but missing isn't typed period: Any, # more specific would be better, but missing isn't typed after: str, before: str, per_page: int, rank_start: Optional[int], tag: Optional[Ltree], unfiltered: bool, ) -> dict: """Get a listing of topics in the group.""" # pylint: disable=too-many-arguments, too-many-branches, too-many-locals if request.matched_route.name == "home": # on the home page, include topics from the user's subscribed groups # (or all groups, if logged-out) if request.user: groups = [sub.group for sub in request.user.subscriptions] else: groups = [ group for group in request.query(Group).all() if group.path != "test" ] else: # otherwise, just topics from the single group that we're looking at groups = [request.context] default_settings = _get_default_settings(request, order) if order is missing: order = default_settings.order if period is missing: period = default_settings.period # set up the basic query for topics query = (request.query(Topic).join_all_relationships().inside_groups( groups).apply_sort_option(order)) # restrict the time period, if not set to "all time" if period: query = query.inside_time_period(period) # restrict to a specific tag, if we're viewing a single one if tag: query = query.has_tag(tag) # apply before/after pagination restrictions if relevant if before: query = query.before_id36(before) if after: query = query.after_id36(after) # apply topic tag filters unless they're disabled or viewing a single tag if request.user and not (tag or unfiltered): # pylint: disable=protected-access query = query.filter(~Topic._tags.descendant_of( # type: ignore any_(cast(request.user._filtered_topic_tags, ArrayOfLtree)))) topics = query.get_page(per_page) period_options = [ SimpleHoursPeriod(hours) for hours in (1, 12, 24, 72, 168) ] # add the current period to the bottom of the dropdown if it's not one of the # "standard" ones if period and period not in period_options: period_options.append(period) if isinstance(request.context, Group): wiki_pages = (request.query(GroupWikiPage).filter( GroupWikiPage.group == request.context).order_by( GroupWikiPage.slug).all()) # remove the index from the page list, we'll output it separately if any(page.slug == "index" for page in wiki_pages): wiki_has_index = True wiki_pages = [page for page in wiki_pages if page.slug != "index"] else: wiki_has_index = False else: wiki_pages = None wiki_has_index = False return { "group": request.context, "groups": groups, "topics": topics, "order": order, "order_options": TopicSortOption, "period": period, "period_options": period_options, "is_default_period": period == default_settings.period, "is_default_view": (period == default_settings.period and order == default_settings.order), "rank_start": rank_start, "tag": tag, "unfiltered": unfiltered, "wiki_pages": wiki_pages, "wiki_has_index": wiki_has_index, }
def _count_associated_feed_entries(self, user_id): return self.session.query(DocumentChange). \ filter(any_(DocumentChange.user_ids) == user_id).count()
intonations = {line['intonation_id'], line['source_construction_intonation_id']} pr_ints = set([x.intonation for x in session.query(Intonation).all()]) lack_ints = intonations - pr_ints for intonation in lack_ints: nums = session.query(Intonation).all() if nums: num = nums[-1].id else: num = 0 element = Intonation(id=num+1, intonation=intonation) session.add(element) ps = session.query(Semantics).filter_by(semantics=prim_sem[0]).one() line['primary_semantics_id'] = ps.id ads = session.query(Semantics).filter_by(semantics=any_(add_sem)).all() line['additional_semantics_id'] = '|'.join([str(x.id) for x in ads]) sas = session.query(SpeechAct).filter_by(speech_act=line['speech_act_1_id']).one() line['speech_act_1_id'] = sas.id sa_1s = session.query(SpeechAct).filter_by(speech_act=line['speech_act_id']).one() line['speech_act_id'] = sa_1s.id intonations = session.query(Intonation).filter_by(intonation=str(line['intonation_id'])).one() line['intonation_id'] = intonations.id intonantions_source = session.query(Intonation).filter_by( intonation=str(line['source_construction_intonation_id']) ).all()
def get_group_topics( # noqa request: Request, after: Optional[str], before: Optional[str], order: Optional[TopicSortOption], per_page: int, rank_start: Optional[int], tag: Optional[Ltree], unfiltered: bool, **kwargs: Any) -> dict: """Get a listing of topics in the group.""" # period needs special treatment so we can distinguish between missing and None period = kwargs.get("period", missing) is_home_page = request.matched_route.name in [ "home", "home_atom", "home_rss" ] is_atom = request.matched_route.name in ["home_atom", "group_topics_atom"] is_rss = request.matched_route.name in ["home_rss", "group_topics_rss"] if is_home_page: # on the home page, include topics from the user's subscribed groups # (or all groups, if logged-out) if request.user: groups = [sub.group for sub in request.user.subscriptions] else: groups = [ group for group in request.query(Group).all() if group.path != "test" ] subgroups = None else: # otherwise, just topics from the single group that we're looking at groups = [request.context] subgroups = (request.query(Group).filter( Group.path.descendant_of(request.context.path), Group.path != request.context.path, ).all()) default_settings = _get_default_settings(request, order) if not order: order = default_settings.order if period is missing: period = default_settings.period # force Newest sort order, and All Time period, for RSS feeds if is_atom or is_rss: order = TopicSortOption.NEW period = None # set up the basic query for topics query = (request.query(Topic).join_all_relationships().inside_groups( groups, include_subgroups=not is_home_page).exclude_ignored(). apply_sort_option(order)) # restrict the time period, if not set to "all time" if period: query = query.inside_time_period(period) # restrict to a specific tag, if we're viewing a single one if tag: query = query.has_tag(str(tag)) # apply before/after pagination restrictions if relevant if before: query = query.before_id36(before) if after: query = query.after_id36(after) # apply topic tag filters unless they're disabled if request.user and request.user.filtered_topic_tags and not unfiltered: filtered_topic_tags = request.user.filtered_topic_tags # if viewing single tag, don't filter that tag and its ancestors # for example, if viewing "ask.survey", don't filter "ask.survey" or "ask" if tag: filtered_topic_tags = [ ft for ft in filtered_topic_tags if not tag.descendant_of(ft.replace(" ", "_")) ] query = query.filter(~Topic.tags.descendant_of( # type: ignore any_(cast(filtered_topic_tags, TagList)))) topics = query.get_page(per_page) period_options = [ SimpleHoursPeriod(hours) for hours in (1, 12, 24, 72, 168) ] # add the current period to the bottom of the dropdown if it's not one of the # "standard" ones if period and period not in period_options: period_options.append(period) if isinstance(request.context, Group): wiki_pages = (request.query(GroupWikiPage).filter( GroupWikiPage.group == request.context).order_by( GroupWikiPage.path).all()) # remove the index from the page list, we'll output it separately if any(page.path == "index" for page in wiki_pages): wiki_has_index = True wiki_pages = [page for page in wiki_pages if page.path != "index"] else: wiki_has_index = False else: wiki_pages = None wiki_has_index = False if isinstance(request.context, Group): # Get the most recent topic from each scheduled topic in this group group_schedules = ( request.query(TopicSchedule).options( joinedload(TopicSchedule.latest_topic)).filter( TopicSchedule.group == request.context, TopicSchedule.next_post_time != None, # noqa ).order_by(TopicSchedule.next_post_time).all()) most_recent_scheduled_topics = [ schedule.latest_topic for schedule in group_schedules ] else: most_recent_scheduled_topics = [] if is_home_page: financial_data = get_financial_data(request.db_session) else: financial_data = None if is_atom: request.response.content_type = "application/atom+xml" if is_rss: request.response.content_type = "application/rss+xml" return { "group": request.context, "groups": groups, "topics": topics, "order": order, "order_options": TopicSortOption, "period": period, "period_options": period_options, "is_default_period": period == default_settings.period, "is_default_view": (period == default_settings.period and order == default_settings.order), "rank_start": rank_start, "tag": tag, "unfiltered": unfiltered, "wiki_pages": wiki_pages, "wiki_has_index": wiki_has_index, "subgroups": subgroups, "most_recent_scheduled_topics": most_recent_scheduled_topics, "financial_data": financial_data, "current_time": utc_now(), }