From 051fc392558857d5d1efa338751a555bd9b98fbc Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Tue, 9 Jul 2024 17:47:13 +0800 Subject: [PATCH] Fixed to using `yield from` Signed-off-by: Toomore Chiang --- models/mailletterdb.py | 3 +-- models/waitlistdb.py | 3 +-- module/dispense.py | 28 +++++++++++++--------------- module/expense.py | 14 ++++++-------- module/form.py | 24 ++++++++---------------- module/gsuite.py | 12 ++++-------- module/sender.py | 6 ++---- module/tasks.py | 6 ++---- module/users.py | 16 ++++++---------- module/usession.py | 15 ++++++--------- module/waitlist.py | 8 +++----- 11 files changed, 52 insertions(+), 83 deletions(-) diff --git a/models/mailletterdb.py b/models/mailletterdb.py index 8a73f382..3e91b200 100644 --- a/models/mailletterdb.py +++ b/models/mailletterdb.py @@ -92,5 +92,4 @@ def need_to_send(self, code: str) -> Generator[dict[str, Any], None, None]: The data haven't been sent yet. ''' - for raw in self.find({f'code.{code}': {'$exists': False}}): - yield raw + yield from self.find({f'code.{code}': {'$exists': False}}) diff --git a/models/waitlistdb.py b/models/waitlistdb.py index 8b2abee1..889048d4 100644 --- a/models/waitlistdb.py +++ b/models/waitlistdb.py @@ -89,8 +89,7 @@ def list_by(self, pid: str, tid: Optional[str] = None, uid: Optional[str] = None if data: yield data else: - for data in self.find(query): - yield data + yield from self.find(query) def make_result(self, _id: str, pid: str, uid: str, result: Literal['approval', 'deny']) -> Optional[dict[str, Any]]: diff --git a/module/dispense.py b/module/dispense.py index 06e2d230..4d6d9bde 100644 --- a/module/dispense.py +++ b/module/dispense.py @@ -4,12 +4,12 @@ from pymongo.collection import ReturnDocument from pymongo.cursor import Cursor -from models.expensedb import ExpenseDB -from models.dispensedb import DispenseDB from models.budgetdb import BudgetDB - +from models.dispensedb import DispenseDB +from models.expensedb import ExpenseDB from module.expense import Expense + class Dispense: ''' Dispense class ''' @@ -28,15 +28,15 @@ def create(pid: str, expense_ids: list[str], dispense_date: str) -> dict[str, An ''' dispense_data = DispenseDB.new(pid, expense_ids) dispense_data['dispense_date'] = dispense_date - dispense = DispenseDB().add(data = dispense_data) + dispense = DispenseDB().add(data=dispense_data) for expense_id in expense_ids: ExpenseDB().find_one_and_update( - { '_id': expense_id }, + {'_id': expense_id}, { '$set': { 'dispense_id': dispense['_id'], - 'status': '3' # 出款中 + 'status': '3' # 出款中 } }, return_document=ReturnDocument.AFTER, @@ -65,8 +65,7 @@ def get_all_by_pid(pid: str) -> Generator[dict[str, Any], None, None]: Return the dispense data in `pid`. ''' - for raw in DispenseDB().find({'pid': pid}): - yield raw + yield from DispenseDB().find({'pid': pid}) @staticmethod def get_order_by_date(pid: str) -> Generator[dict[str, Any], None, None]: @@ -78,10 +77,9 @@ def get_order_by_date(pid: str) -> Generator[dict[str, Any], None, None]: Yields: Return the dispense data in `pid` ''' - for raw in DispenseDB().find({'pid': pid}).sort([ - ('dispense_date', 1), - ('create_at', 1)]): - yield raw + yield from DispenseDB().find({'pid': pid}).sort([ + ('dispense_date', 1), + ('create_at', 1)]) @staticmethod def get_by_ids(ids: list[str]) -> Cursor[dict[str, Any]]: @@ -95,8 +93,8 @@ def get_by_ids(ids: list[str]) -> Cursor[dict[str, Any]]: ''' return DispenseDB().find({ - '_id': { '$in': ids }, - 'enable': { '$eq': True } + '_id': {'$in': ids}, + 'enable': {'$eq': True} }) @staticmethod @@ -131,7 +129,7 @@ def update(dispense_id: str, data: dict[str, Any]) -> dict[str, Any] | int: for exp_id in resp['expense_ids']: ExpenseDB().find_one_and_update( {'_id': exp_id}, - {'$set': {'status': '2'}}, # back to 審核中 + {'$set': {'status': '2'}}, # back to 審核中 return_document=ReturnDocument.AFTER, ) diff --git a/module/expense.py b/module/expense.py index 9a5a2156..97c55f48 100644 --- a/module/expense.py +++ b/module/expense.py @@ -9,6 +9,7 @@ from models.budgetdb import BudgetDB from models.expensedb import ExpenseDB + class Expense: ''' Expense class ''' @@ -110,8 +111,7 @@ def get_all_by_pid(pid: str) -> Generator[dict[str, Any], None, None]: Return the expenses data in `pid`. ''' - for raw in ExpenseDB().find({'pid': pid}): - yield raw + yield from ExpenseDB().find({'pid': pid}) @staticmethod def get_by_eid(expense_id: str) -> Generator[dict[str, Any], None, None]: @@ -124,8 +124,7 @@ def get_by_eid(expense_id: str) -> Generator[dict[str, Any], None, None]: Return the expenses data in `expense_id`. ''' - for raw in ExpenseDB().find({'_id': expense_id}): - yield raw + yield from ExpenseDB().find({'_id': expense_id}) @staticmethod def update_invoices(expense_id: str, invoices: list[dict[str, Any]]) -> dict[str, Any]: @@ -261,7 +260,7 @@ def get_by_create_by(pid: str, create_by: str) -> Cursor[dict[str, Any]]: return ExpenseDB().find({'pid': pid, 'create_by': create_by, 'enable': True}) @staticmethod - def get_by_dispense_id (dispense_ids: list[str], pid: str) -> Cursor[dict[str, Any]]: + def get_by_dispense_id(dispense_ids: list[str], pid: str) -> Cursor[dict[str, Any]]: ''' Retrieve by dispense_id Args: @@ -272,7 +271,7 @@ def get_by_dispense_id (dispense_ids: list[str], pid: str) -> Cursor[dict[str, A ''' return ExpenseDB().find({ 'pid': pid, - 'dispense_id': { '$in': dispense_ids } + 'dispense_id': {'$in': dispense_ids} }) @staticmethod @@ -288,8 +287,7 @@ def get_has_sent(pid: str, budget_id: str) -> Generator[dict[str, Any], None, No ''' query = {'pid': pid, 'request.buid': budget_id, 'enable': True} - for raw in ExpenseDB().find(query, {'invoices': 1, 'code': 1}): - yield raw + yield from ExpenseDB().find(query, {'invoices': 1, 'code': 1}) @staticmethod def dl_format(pid: str) -> list[dict[str, Any]]: diff --git a/module/form.py b/module/form.py index 75fb9288..1860b677 100644 --- a/module/form.py +++ b/module/form.py @@ -60,8 +60,7 @@ def all_appreciation(pid: str) -> Generator[dict[str, Any], None, None]: Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'appreciation', 'pid': pid}): - yield raw + yield from FormDB().find({'case': 'appreciation', 'pid': pid}) @staticmethod def update_volunteer_certificate(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]: @@ -108,8 +107,7 @@ def all_volunteer_certificate(pid: str) -> Generator[dict[str, Any], None, None] Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'volunteer_certificate', 'pid': pid}): - yield raw + yield from FormDB().find({'case': 'volunteer_certificate', 'pid': pid}) @staticmethod def update_traffic_fee(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]: @@ -159,8 +157,7 @@ def all_traffic_fee(pid: str) -> Generator[dict[str, Any], None, None]: Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'traffic_fee', 'pid': pid}): - yield raw + yield from FormDB().find({'case': 'traffic_fee', 'pid': pid}) @staticmethod def update_accommodation(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]: @@ -216,8 +213,7 @@ def all_accommodation(pid: str) -> Generator[dict[str, Any], None, None]: Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'accommodation', 'pid': pid}): - yield raw + yield from FormDB().find({'case': 'accommodation', 'pid': pid}) @staticmethod def update_clothes(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]: @@ -264,8 +260,7 @@ def all_clothes(pid: str) -> Generator[dict[str, Any], None, None]: Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'clothes', 'pid': pid}): - yield raw + yield from FormDB().find({'case': 'clothes', 'pid': pid}) @staticmethod def update_parking_card(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]: @@ -314,8 +309,7 @@ def all_parking_card(pid: str) -> Generator[dict[str, Any], None, None]: Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'parking_card', 'pid': pid}): - yield raw + yield from FormDB().find({'case': 'parking_card', 'pid': pid}) @staticmethod def update_drink(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]: @@ -362,8 +356,7 @@ def all_drink(pid: str) -> Generator[dict[str, Any], None, None]: Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'drink', 'pid': pid}): - yield raw + yield from FormDB().find({'case': 'drink', 'pid': pid}) class FormTrafficFeeMapping: @@ -434,8 +427,7 @@ def get(pid: str) -> Generator[dict[str, Any], None, None]: Return the data in `pid`. ''' - for raw in FormDB().find({'case': 'accommodation', 'pid': pid, 'data.key': {'$ne': 'no'}}): - yield raw + yield from FormDB().find({'case': 'accommodation', 'pid': pid, 'data.key': {'$ne': 'no'}}) @staticmethod def update_room(pid: str, uid: str, room: str, change_key: bool = True) -> dict[str, Any]: diff --git a/module/gsuite.py b/module/gsuite.py index 8d8d13af..bc668d85 100644 --- a/module/gsuite.py +++ b/module/gsuite.py @@ -114,12 +114,10 @@ def groups_list_loop(self, page_token: Union[str, None] = None) ->\ ''' groups = self.groups_list(page_token=page_token) - for group in groups['groups']: - yield group + yield from groups['groups'] if 'nextPageToken' in groups: - for group in self.groups_list_loop(page_token=groups['nextPageToken']): - yield group + yield from self.groups_list_loop(page_token=groups['nextPageToken']) def groups_insert(self, email: str, description: Union[str, None] = None, name: Union[str, None] = None) -> Any: @@ -166,14 +164,12 @@ def members_list_loop(self, group_key: str) -> Generator[dict[str, str], None, N ''' members = self.members_list(group_key) - for member in members.get('members', []): - yield member + yield from members.get('members', []) while 'nextPageToken' in members: members = self.members_list( group_key, page_token=members['nextPageToken']) - for member in members.get('members', []): - yield member + yield from members.get('members', []) def members_insert(self, group_key: str, email: str, role: str = 'MEMBER', delivery_settings: str = 'ALL_MAIL') -> Any: diff --git a/module/sender.py b/module/sender.py index 0b2689f3..74c6e36e 100644 --- a/module/sender.py +++ b/module/sender.py @@ -7,8 +7,7 @@ from pymongo.cursor import Cursor import setting -from models.senderdb import (SenderCampaignDB, SenderLogsDB, SenderReceiverDB, - SenderSESLogsDB) +from models.senderdb import SenderCampaignDB, SenderLogsDB, SenderReceiverDB, SenderSESLogsDB from module.awsses import AWSSES from module.team import Team from module.users import User @@ -233,8 +232,7 @@ def get(cid: str) -> Generator[dict[str, Any], None, None]: :param str cid: cid ''' - for raw in SenderLogsDB().find({'cid': cid}, sort=(('_id', -1), )): - yield raw + yield from SenderLogsDB().find({'cid': cid}, sort=(('_id', -1), )) class SenderSESLogs: diff --git a/module/tasks.py b/module/tasks.py index 25bd8fa5..396c5b61 100644 --- a/module/tasks.py +++ b/module/tasks.py @@ -62,8 +62,7 @@ def get_by_pid(pid: str) -> Generator[dict[str, Any], None, None]: Yield return the data. ''' - for raw in TasksDB().find({'pid': pid}, sort=(('starttime', 1), )): - yield raw + yield from TasksDB().find({'pid': pid}, sort=(('starttime', 1), )) @staticmethod def get_with_pid(pid: str, _id: str) -> Optional[dict[str, Any]]: @@ -251,5 +250,4 @@ def get(pid: str) -> Generator[dict[str, Any], None, None]: Return the datas. ''' - for user in TasksStarDB().find({'pid': pid}, {'uid': 1}): - yield user + yield from TasksStarDB().find({'pid': pid}, {'uid': 1}) diff --git a/module/users.py b/module/users.py index bc8169d2..91667c1b 100644 --- a/module/users.py +++ b/module/users.py @@ -357,11 +357,10 @@ def get_suspend_uids(uids: list[str]) -> Generator[dict[str, Any], None, None]: Return all user's uids ''' - for row in UsersDB().find({ + yield from UsersDB().find({ '_id': {'$in': uids}, 'property.suspend': True, - }, {'_id': 1}): - yield row + }, {'_id': 1}) @staticmethod def get_all_users(include_suspend: bool = False) -> Generator[dict[str, Any], None, None]: @@ -382,8 +381,7 @@ def get_all_users(include_suspend: bool = False) -> Generator[dict[str, Any], No {'property.suspend': False}, ]} - for row in UsersDB().find(query, {'_id': 1}): - yield row + yield from UsersDB().find(query, {'_id': 1}) @staticmethod def count(include_suspend: bool = False) -> int: @@ -487,8 +485,7 @@ def query(query: dict[str, Any]) -> Generator[dict[str, Any], None, None]: if _or: _query['$or'] = _or - for raw in TobeVolunteerDB().find(_query): - yield raw + yield from TobeVolunteerDB().find(_query) class PolicySigned: @@ -517,10 +514,9 @@ def is_recently_signed(uid: str, _type: PolicyType, days: int = 90) -> \ ''' sign_at = arrow.now().shift(days=days*-1).naive - for raw in PolicySignedDB().find({ + yield from PolicySignedDB().find({ 'type': _type.value, 'uid': uid, 'sign_at': {'$gte': sign_at} - }, {'_id': 0}): - yield raw + }, {'_id': 0}) class AccountPass(BaseModel): diff --git a/module/usession.py b/module/usession.py index e5345791..1ace0931 100644 --- a/module/usession.py +++ b/module/usession.py @@ -47,9 +47,8 @@ def get_no_ipinfo() -> Generator[dict[str, Any], None, None]: Return the data, `ipinfo` is not exist. ''' - for raw in USessionDB().find({'ipinfo': {'$exists': False}}, - {'header.X-Real-Ip': 1, 'header.X-Forwarded-For': 1}): - yield raw + yield from USessionDB().find({'ipinfo': {'$exists': False}}, + {'header.X-Real-Ip': 1, 'header.X-Forwarded-For': 1}) @staticmethod def update_ipinfo(sid: str, data: dict[str, Any]) -> None: @@ -75,10 +74,9 @@ def get_recently(uid: str, limit: int = 25) -> Generator[dict[str, Any], None, N Return the recently datas. ''' - for raw in USessionDB(token='').find({'uid': uid}, + yield from USessionDB(token='').find({'uid': uid}, sort=(('created_at', -1), ), - limit=limit): - yield raw + limit=limit) @staticmethod def get_alive(uid: str) -> Generator[dict[str, Any], None, None]: @@ -91,9 +89,8 @@ def get_alive(uid: str) -> Generator[dict[str, Any], None, None]: Return the datas. ''' - for raw in USessionDB(token='').find({'uid': uid, 'alive': True}, - sort=(('created_at', -1), )): - yield raw + yield from USessionDB(token='').find({'uid': uid, 'alive': True}, + sort=(('created_at', -1), )) @staticmethod def make_dead(sid: str, uid: Optional[str] = None) -> None: diff --git a/module/waitlist.py b/module/waitlist.py index 5929f7a8..24a23c33 100644 --- a/module/waitlist.py +++ b/module/waitlist.py @@ -103,8 +103,7 @@ def find_history_in_team(uid: str, pid: str, tid: str) -> Generator[dict[str, An Return the datas. ''' - for raw in WaitListDB().find({'pid': pid, 'tid': tid, 'uid': uid}): - yield raw + yield from WaitListDB().find({'pid': pid, 'tid': tid, 'uid': uid}) @staticmethod def get_note(pid: str, tid: str, uid: str) -> Generator[dict[str, str], None, None]: @@ -119,10 +118,9 @@ def get_note(pid: str, tid: str, uid: str) -> Generator[dict[str, str], None, No Return the datas. ''' - for raw in WaitListDB().find( + yield from WaitListDB().find( filter={'pid': pid, 'tid': tid, 'uid': uid}, projection={'_id': 0}, sort=(('_id', -1), ), limit=1, - ): - yield raw + )