Skip to content

Commit

Permalink
Fixed to using yield from
Browse files Browse the repository at this point in the history
Signed-off-by: Toomore Chiang <[email protected]>
  • Loading branch information
toomore committed Jul 9, 2024
1 parent f25d624 commit 051fc39
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 83 deletions.
3 changes: 1 addition & 2 deletions models/mailletterdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
3 changes: 1 addition & 2 deletions models/waitlistdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
28 changes: 13 additions & 15 deletions module/dispense.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '''

Expand All @@ -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,
Expand Down Expand Up @@ -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]:
Expand All @@ -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]]:
Expand All @@ -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
Expand Down Expand Up @@ -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,
)

Expand Down
14 changes: 6 additions & 8 deletions module/expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from models.budgetdb import BudgetDB
from models.expensedb import ExpenseDB


class Expense:
''' Expense class '''

Expand Down Expand Up @@ -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]:
Expand All @@ -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]:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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]]:
Expand Down
24 changes: 8 additions & 16 deletions module/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand Down
12 changes: 4 additions & 8 deletions module/gsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions module/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions module/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down Expand Up @@ -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})
16 changes: 6 additions & 10 deletions module/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 6 additions & 9 deletions module/usession.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]:
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 051fc39

Please sign in to comment.