async def delete(request, post_id): if not post_id: abort(404) post = await Post.get(id=post_id) if not post: return response.json({'r': 0, 'msg': 'Post not exist'}) await post.delete() await PostTag.filter(Q(post_id=post_id)).delete() return response.json({'r': 1})
async def archives(request): rv = { year: list(items) for year, items in groupby( await Post.filter(Q( status=Post.STATUS_ONLINE)).order_by('-id'), grouper) } archives = sorted(rv.items(), key=lambda x: x[0], reverse=True) return {'archives': archives}
async def get_derivation_outputs(drv: str) -> typing.List[DerivationOutputResult]: async def filter(q_filter): qs = ( Derivation.filter(q_filter) .prefetch_related("derivationoutputs") .prefetch_related("derivationoutputs__derivationoutputresults") ) return (await qs) coros: typing.List[typing.Coroutine] = [ filter(q_filter) for q_filter in (Q(from_ref_recursive__referrer=drv), Q(drv=drv)) ] items: typing.List[DerivationOutputResult] = [] for items_ in await asyncio.gather(*coros): items.extend(items_) return items
def find_contacts( cls, user_id: int, enabled_only: bool = False) -> queryset.QuerySet[Contact]: query_set = cls.filter( Q(user1_id=user_id, user2_id=user_id, join_type='OR')) if enabled_only: query_set = query_set.filter(contact_enabled=True) return query_set.all()
def _filter_or_exclude(self, *args: Q, negate: bool, **kwargs: Any) -> "QuerySet[MODEL]": queryset = self._clone() for arg in args: if not isinstance(arg, Q): raise TypeError("expected Q objects as args") if negate: queryset._q_objects.append(~arg) else: queryset._q_objects.append(arg) for key, value in kwargs.items(): if negate: queryset._q_objects.append(~Q(**{key: value})) else: queryset._q_objects.append(Q(**{key: value})) return queryset
async def find_notification_events_by_device_id( device: Device, start: int = 0, size: int = 10, events: List[Event] = (), order_bys: List[str] = (), ): event_filter = [Q(device=device)] if events: event_filter.append(Q(event__in=events)) query_set = DeviceNotificationEvent.filter( Q(*event_filter)).prefetch_related('device', 'notification') for order_by in order_bys: if order_by.isascii(): query_set = query_set.order_by(order_by) return (await query_set.count(), await query_set.offset(start).limit(size).all())
async def get_related(self, limit=4): tag_ids = [tag.id for tag in await self.tags] if not tag_ids: return [] post_ids = set(await PostTag.filter(Q(post_id__not=self.id), Q(tag_id__in=tag_ids)).values_list( 'post_id', flat=True)) excluded_ids = await self.filter( Q(created_at__lt=(datetime.now() - timedelta(days=180))) | Q(status__not=self.STATUS_ONLINE)).values_list('id', flat=True) post_ids -= set(excluded_ids) try: post_ids = random.sample(post_ids, limit) except ValueError: ... return await self.get_multi(post_ids)
async def find_notifications_by_status( status: NotificationStatus = None, start: int = 0, size: int = 10, order_bys: List[str] = () ) -> Tuple[int, List[Notification]]: notification_filter = Q() if status is not None: notification_filter = Q(status=status) query_set = Notification.filter(notification_filter) for order_by in order_bys: if order_by.isascii(): query_set = query_set.order_by(order_by) return ( await query_set.count(), await query_set.offset(start).limit(size).all() )
async def test_q_object_backward_related_query(self): await Tournament.create(name="0") tournament = await Tournament.create(name="Tournament") event = await Event.create(name="1", tournament=tournament) fetched_tournament = await Tournament.filter(events=event.event_id).first() self.assertEqual(fetched_tournament.id, tournament.id) fetched_tournament = await Tournament.filter(Q(events=event.event_id)).first() self.assertEqual(fetched_tournament.id, tournament.id)
async def test_filtering(self): tournament = Tournament(name='Tournament') await tournament.save() second_tournament = Tournament(name='Tournament 2') await second_tournament.save() event_first = Event(name='1', tournament=tournament) await event_first.save() event_second = Event(name='2', tournament=second_tournament) await event_second.save() event_third = Event(name='3', tournament=tournament) await event_third.save() event_forth = Event(name='4', tournament=second_tournament) await event_forth.save() team_first = Team(name='First') await team_first.save() team_second = Team(name='Second') await team_second.save() await team_first.events.add(event_first) await event_second.participants.add(team_second) found_events = await Event.filter( Q(id__in=[event_first.id, event_second.id]) | Q(name='3')).filter(participants__not=team_second.id ).order_by('name', 'tournament_id').distinct() self.assertEqual(len(found_events), 2) self.assertEqual(found_events[0].id, event_first.id) self.assertEqual(found_events[1].id, event_third.id) await Team.filter(events__tournament_id=tournament.id ).order_by('-events__name') await Tournament.filter(events__name__in=['1', '3'], ).distinct() teams = await Team.filter(name__icontains='CON') self.assertEqual(len(teams), 1) self.assertEqual(teams[0].name, 'Second') tournaments = await Tournament.filter( events__participants__name__startswith='Fir') self.assertEqual(len(tournaments), 1) self.assertEqual(tournaments[0], tournament)
async def run(): await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]}) await Tortoise.generate_schemas() tournament = Tournament(name="Tournament") await tournament.save() second_tournament = Tournament(name="Tournament 2") await second_tournament.save() event_first = Event(name="1", tournament=tournament) await event_first.save() event_second = await Event.create(name="2", tournament=second_tournament) await Event.create(name="3", tournament=tournament) await Event.create(name="4", tournament=second_tournament) await Event.filter(tournament=tournament) team_first = Team(name="First") await team_first.save() team_second = Team(name="Second") await team_second.save() await team_first.events.add(event_first) await event_second.participants.add(team_second) print(await Event.filter( Q(id__in=[event_first.id, event_second.id]) | Q(name="3")).filter(participants__not=team_second.id ).order_by("tournament__id").distinct()) print(await Team.filter(events__tournament_id=tournament.id ).order_by("-events__name")) print( await Tournament.filter(events__name__in=["1", "3"] ).order_by("-events__participants__name").distinct()) print(await Team.filter(name__icontains="CON")) print(await Tournament.filter(events__participants__name__startswith="Fir")) print(await Tournament.filter(id__icontains=1).count())
async def test_filtering(self): tournament = Tournament(name="Tournament") await tournament.save() second_tournament = Tournament(name="Tournament 2") await second_tournament.save() event_first = Event(name="1", tournament=tournament) await event_first.save() event_second = Event(name="2", tournament=second_tournament) await event_second.save() event_third = Event(name="3", tournament=tournament) await event_third.save() event_forth = Event(name="4", tournament=second_tournament) await event_forth.save() team_first = Team(name="First") await team_first.save() team_second = Team(name="Second") await team_second.save() await team_first.events.add(event_first) await event_second.participants.add(team_second) found_events = (await Event.filter( Q(id__in=[event_first.id, event_second.id]) | Q(name="3")).filter(participants__not=team_second.id ).order_by("name", "tournament_id").distinct()) self.assertEqual(len(found_events), 2) self.assertEqual(found_events[0].id, event_first.id) self.assertEqual(found_events[1].id, event_third.id) await Team.filter(events__tournament_id=tournament.id ).order_by("-events__name") await Tournament.filter(events__name__in=["1", "3"]).distinct() teams = await Team.filter(name__icontains="CON") self.assertEqual(len(teams), 1) self.assertEqual(teams[0].name, "Second") tournaments = await Tournament.filter( events__participants__name__startswith="Fir") self.assertEqual(len(tournaments), 1) self.assertEqual(tournaments[0], tournament)
async def register_user(user_in: UserCreate, response: Response): """ Create new user. """ # 验证验证码 # await verify_code(user_in.vcode_id, user_in.vcode) await verify_auth_code(user_in.phone, user_in.email, user_in.sms_code) get_filter = Q(name=user_in.name) if not (user_in.phone or user_in.email): raise HTTPException(status_code=400, detail="Must specify phone/email") if user_in.phone: get_filter = Q(phone=user_in.phone) if user_in.email: get_filter |= Q(email=user_in.email) user = await MarketUser.get_or_none(get_filter) if user: raise HTTPException( status_code=404, detail="The user with this phone already exists in the system.", ) try: market = await StrategyMarket.get(id=config.MARKET_ID) except DoesNotExist: raise HTTPException(status_code=500, detail="配置错误,请联系管理员!") user_data = user_in.dict() user_data["uuid"] = uuid.uuid1().hex user_data["password"] = get_password_hash(user_in.password, user_data["uuid"]) user_data["market"] = market user_data["status"] = UserStatus.normal # user_data["market"] = get_password_hash(user_in.password, user_data["uuid"]) user = MarketUser(**user_data) await user.save() access_token_expires = timedelta( minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES) access_token = create_access_token(data={"uuid": user.uuid.hex}, expires_delta=access_token_expires) # response.headers[APIKEY_HEADER_NAME] = access_token response.set_cookie(key=APIKEY_HEADER_NAME, value=access_token) return UserToken(**user.__dict__, token=access_token)
async def test_filter_by_aggregation_field_with_not(self): tournament = await Tournament.create(name="0") tournament_second = await Tournament.create(name="1") await Event.create(name="1", tournament=tournament) await Event.create(name="2", tournament=tournament_second) tournaments = await Tournament.annotate( events_count=Count("events")).filter(~Q(events_count=1, name="0")) self.assertEqual(len(tournaments), 1) self.assertEqual(tournaments[0].id, tournament_second.id)
async def search_strategy_package(schema_in: QStrategySearch): """搜索策略""" if schema_in.package_id: if schema_in.status: strategies = await QStrategy.filter( package_id=schema_in.package_id, status=schema_in.status) else: strategies = await QStrategy.filter( package_id=schema_in.package_id, status=ListStatus.online) else: if schema_in.status: strategies = await QStrategy.filter(status=schema_in.status) else: strategies = await QStrategy.filter(status=ListStatus.online) if schema_in.product_id: strategies = await QStrategy.filter(product_id=schema_in.product_id) if schema_in.market_id: strategies = await QStrategy.filter(market_id=schema_in.market_id) if schema_in.package_id: strategies = await QStrategy.filter(package_id=schema_in.package_id, status=ListStatus.online) if schema_in.task_id: strategies = await QStrategy.filter(task_id__contains=schema_in.task_id ) if schema_in.style: strategies = await QStrategy.filter(style__contains=schema_in.style) if schema_in.category: strategies = await QStrategy.filter( category__contains=schema_in.category) if schema_in.name: strategies = await QStrategy.filter(name__contains=schema_in.name) if schema_in.fuzzy: strategies = await QStrategy.filter( Q(name__contains=schema_in.fuzzy, status=ListStatus.online) | Q(author_name__contains=schema_in.fuzzy, status=ListStatus.online)) if schema_in.package_id and schema_in.style: strategies = await QStrategy.filter(package_id=schema_in.package_id, style__contains=schema_in.style) if schema_in.package_id and schema_in.style and schema_in.fuzzy: strategies = await QStrategy.filter(package_id=schema_in.package_id, style__contains=schema_in.style, name__contains=schema_in.fuzzy) return strategies
async def run(): await Tortoise.init(config_file='config.json') await Tortoise.generate_schemas() tournament = Tournament(name='Tournament') await tournament.save() second_tournament = Tournament(name='Tournament 2') await second_tournament.save() event_first = Event(name='1', tournament=tournament) await event_first.save() event_second = await Event.create(name='2', tournament=second_tournament) await Event.create(name='3', tournament=tournament) await Event.create(name='4', tournament=second_tournament) await Event.filter(tournament=tournament) team_first = Team(name='First') await team_first.save() team_second = Team(name='Second') await team_second.save() await team_first.events.add(event_first) await event_second.participants.add(team_second) print(await Event.filter( Q(id__in=[event_first.id, event_second.id]) | Q(name='3')).filter(participants__not=team_second.id ).order_by('tournament__id').distinct()) print(await Team.filter(events__tournament_id=tournament.id ).order_by('-events__name')) print(await Tournament.filter(events__name__in=[ '1', '3' ], ).order_by('-events__participants__name').distinct()) print(await Team.filter(name__icontains='CON')) print(await Tournament.filter(events__participants__name__startswith='Fir')) print(await Tournament.filter(id__icontains=1).count())
async def search_devices( device_ids: List[str], conditions: ConditionClause, start: int = 0, size: int = 10, order_bys: List[str] = (), ) -> Tuple[int, List[Device]]: filter_ = Q() if conditions.key is not None: filter_ = _resolve_condition_clause_to_q(conditions) device_id_filter = Q() if device_ids: device_id_filter = Q(device_id__in=device_ids) query_set = _device_relational_query_set( Device.filter(filter_, device_id_filter)) for order_by in order_bys: if order_by.isascii(): query_set = query_set.order_by(order_by) return (await query_set.count(), await query_set.offset(start).limit(size).all())
async def test_filter_by_aggregation_field_with_or_as_one_node(self): tournament = await Tournament.create(name="0") await Tournament.create(name="1") await Tournament.create(name="2") await Event.create(name="1", tournament=tournament) tournaments = await Tournament.annotate(events_count=Count("events")).filter( Q(events_count=1, name="2", join_type=Q.OR) ) self.assertEqual(len(tournaments), 2) self.assertSetEqual({t.name for t in tournaments}, {"0", "2"})
async def set_indexes(self, indexes): origin_map = {i.post_id: i for i in await self.get_items()} pids = [pid for pid, index in indexes] need_del_pids = set(origin_map) - set(pids) if need_del_pids: await SpecialItem.filter(Q(special_id=self.id), Q(post_id__in=need_del_pids)).delete() for pid, index in indexes: if pid in origin_map: special = origin_map[pid] if index != special.index: special.index = index await special.save() else: await SpecialItem.get_or_create( post_id=pid, special_id=self.id, index=index) await clear_mc(MC_KEY_SPECIAL_ITEMS % self.id, MC_KEY_SPECIAL_POST_ITEMS % self.id)
async def test_filter_by_aggregation_field_with_and_as_one_node(self): tournament = await Tournament.create(name='0') tournament_second = await Tournament.create(name='1') await Event.create(name='1', tournament=tournament) await Event.create(name='2', tournament=tournament_second) tournaments = await Tournament.annotate(events_count=Count('events')).filter( Q(events_count=1, name='0'), ) self.assertEqual(len(tournaments), 1) self.assertEqual(tournaments[0].id, tournament.id)
async def test_filter_by_aggregation_field_with_or_as_one_node(self): tournament = await Tournament.create(name='0') await Tournament.create(name='1') await Tournament.create(name='2') await Event.create(name='1', tournament=tournament) tournaments = await Tournament.annotate(events_count=Count('events')).filter( Q(events_count=1, name='2', join_type=Q.OR), ) self.assertEqual(len(tournaments), 2) self.assertSetEqual({t.name for t in tournaments}, {'0', '2'})
async def update_emoji_stats(self, guild, author, message): custom_emojis = functions.custom_emoji_counter(guild, message) if not len(custom_emojis): return data = await models.EmojiUsageStat.filter(guild_id=guild.id).values() # TODO: Design optimized and combine with 2nd for loop for d in data: for emoji, amount in custom_emojis.items(): try: if d["emoji_id"] == emoji.id: await models.EmojiUsageStat.filter( Q(guild_id=guild.id) & Q(user_id=author.id) & Q(emoji_id=emoji.id)).update( amount=d["amount"] + amount, last_usage=datetime.utcnow(), ) except KeyError: await models.EmojiUsageStat.create( guild_id=guild.id, user_id=author.id, emoji_id=emoji.id, amount=0, last_usage=datetime.utcnow(), ) for emoji, amount in custom_emojis.items(): try: if not emoji.id in [d["emoji_id"] for d in data]: await models.EmojiUsageStat.create( guild_id=guild.id, user_id=author.id, emoji_id=emoji.id, amount=amount, last_usage=datetime.utcnow(), ) except AttributeError: continue
async def fetch_infraction_pages(guild_id, query, amount, fields, requested): key = get_key(guild_id, query, fields, amount) if query == "": infs = await Infraction.filter(guild_id = guild_id).order_by("-id").limit(50) else: subfilters = [] if "[user]" in fields and isinstance(query, int): subfilters.append(Q(user_id=query)) if "[mod]" in fields and isinstance(query, int): subfilters.append(Q(mod_id=query)) if "[reason]" in fields: subfilters.append(Q(reason__icontains=str(query))) infs = await Infraction.filter(Q(Q(*subfilters, join_type="OR"), guild_id=guild_id, join_type="AND")).order_by("-id").limit(int(amount)) longest_type = 4 longest_id = len(str(infs[0].id)) if len(infs) > 0 else len(Translator.translate('id', guild_id)) longest_timestamp = max(len(Translator.translate('timestamp', guild_id)), 19) types = dict() for inf in infs: t = inf.type.lower() longest_type = max(longest_type, len(Translator.translate(t, guild_id))) if t not in types: types[t] = 1 else: types[t] += 1 header = ", ".join(Translator.translate(f"{k}s", guild_id, count=v) for k, v in types.items()) name = await Utils.username(query) if isinstance(query, int) else await Utils.clean(bot.get_guild(guild_id).name) title = f"{Emoji.get_chat_emoji('SEARCH')} {Translator.translate('inf_search_header', guild_id, name=name, page_num=100, pages=100)}\n```md\n\n```" page_header = get_header(longest_id, 37, longest_type, longest_timestamp, guild_id) mcount = 2000 - len(header) - len(page_header) - len(title) out = "\n".join(f"{Utils.pad(str(inf.id), longest_id)} | <@{Utils.pad(str(inf.user_id), 37)}> | <@{Utils.pad(str(inf.mod_id), 37)}> | {datetime.datetime.fromtimestamp(inf.start)} | {Utils.pad(Translator.translate(inf.type.lower(), guild_id), longest_type)} | {Utils.trim_message(inf.reason, 1000)}" for inf in infs) pages = Pages.paginate(out, max_chars=mcount) if bot.redis_pool is not None: GearbotLogging.debug(f"Pushing placeholders for {key}") pipe = bot.redis_pool.pipeline() pipe.unlink(key) for page in pages: pipe.lpush(key, "---NO PAGE YET---") await pipe.execute() bot.loop.create_task(update_pages(guild_id, query, fields, amount, pages, requested, longest_id, longest_type, longest_timestamp, header)) return len(pages)
async def set_permission(self, group_id, switch) -> bool: """设置指定位置权限""" query = Group.filter(Q(id=group_id)) group = await query.first() if not group: await Group.create(id=group_id, admin=switch) return True if group.admin == switch: return False await query.update(admin=switch) return True
async def del_strategy( schema_in: QStrategyStatusOp, current_user: MarketAdminUser = Depends(require_super_scope_admin), ): """删除策略,在上架状态不能直接删除""" id_list = schema_in.product_id if isinstance(id_list, str): id_list = [id_list] await QStrategy.filter( ~Q(status=ListStatus.online), product_id__in=id_list).update(status=ListStatus.deleted) return CommonOut()
async def flush_to_db(ctx): redis = await create_pool(RedisSettings.from_url(REDIS_URL)) while 1: post_id = await redis.spop(RK_VISITED_POST_IDS) if post_id is None: break post = await Post.get(Q(id=post_id)) if post: post._pageview = int(await redis.get(RK_PAGEVIEW.format(post_id)) or 0) await post.save()
async def update_multi(cls, post_id: int, tags: List[str]) -> None: origin_tags = set([t.name for t in ( await Post.sync_get(id=post_id)).tags]) need_add = set(tags) - origin_tags need_del = origin_tags - set(tags) need_add_tag_ids = [] need_del_tag_ids = set() for tag_name in need_add: tag, _ = await Tag.get_or_create(name=tag_name) need_add_tag_ids.append([tag.id, tag_name]) for tag_name in need_del: tag, _ = await Tag.get_or_create(name=tag_name) need_del_tag_ids.add(tag.id) if need_del_tag_ids: await cls.filter(Q(post_id=post_id), Q(tag_id__in=need_del_tag_ids)).delete() for tag_id, _ in sorted(need_add_tag_ids, key=lambda x: tags.index(x[1])): await cls.get_or_create(post_id=post_id, tag_id=tag_id) await clear_mc(MC_KEY_TAGS_BY_POST_ID % post_id)
async def search(filters: dict) -> List[Script]: """Search scripts Arguments: filters {dict} -- Filters for script search Returns: List[Script] -- a list of matched scripts """ queries = [] snapshot_id = filters.get("snapshot_id") if snapshot_id is not None: queries.append(Q(snapshot_id=snapshot_id)) sha256 = filters.get("sha256") if sha256 is not None: queries.append(Q(sha256=sha256)) query = Q(*queries) return await Script.filter(query).order_by("-id")
async def update_multi(cls, post_id, tags): tags = set(tags) origin_tags = set( [t.name for t in (await Post.sync_get(id=post_id)).tags]) need_add = tags - origin_tags need_del = origin_tags - tags need_add_tag_ids = set() need_del_tag_ids = set() for tag_name in need_add: tag, _ = await Tag.get_or_create(name=tag_name) need_add_tag_ids.add(tag.id) for tag_name in need_del: tag, _ = await Tag.get_or_create(name=tag_name) need_del_tag_ids.add(tag.id) if need_del_tag_ids: await cls.filter(Q(post_id=post_id), Q(tag_id__in=need_del_tag_ids)).delete() for tag_id in need_add_tag_ids: await cls.get_or_create(post_id=post_id, tag_id=tag_id) await clear_mc(MC_KEY_TAGS_BY_POST_ID % post_id)
async def backend_api_messages(resource_id): resource = await Resource.filter(remote_id=resource_id).first() if not resource: return jsonify_error() items_per_page = request.args.get('items_per_page', 25, type=int) page = request.args.get('page', 1, type=int) messages = Message.filter(client_id=resource.client_id) if request.args.get('begin'): messages = messages.filter( Q(created__gt=date.fromisoformat(request.args.get('begin')))) if request.args.get('end'): messages = messages.filter( Q(created__lt=date.fromisoformat(request.args.get('end')) + timedelta(days=1))) count = await messages.count() messages = messages.order_by('-id')\ .limit(items_per_page)\ .offset((page - 1) * items_per_page)\ .all() return jsonify_success([message.to_dict() for message in await messages], count=count)