Untitled conversation
- User
[ { "case": { "ref_id": "string", "case_status": "PRC", "referral_submitted_date": "2023-04-24", "modified_by": 0, "lead_id": 1, "loan_amount": 123456, "deal_lost_date": "2023-04-24", "modified_at": "2023-04-21", "property_type": "property type", "hkmc_needed": 0, "customer_name": "vinci", "completion_date": "2023-04-24", "remarks": "money, i need money", "customer_phone_number": 999, "drawdown_date": "2023-04-24", "accepted_offer": "refused", "id": 2, "customer_contact_number": 1122334455, "bank_received_application": "{HSBC}", "predicted_offer": "infinity", "address": "wan chai", "referral_code": "referral_c0de", "created_by": 0, "actual_drawdown_amt": 123456, "created_at": "2023-04-21" }, "status": { "remark": "", "seq": 1, "desc": "Pending Referral Code", "code": "PRC" }, "Leads": { "id": 1, "customer_name": "also vinci in lead", "customer_contact_phone": 1234567890, "remarks": "Additional remarks", "created_by": "John", "customer_phone": 1234567890, "lead_type_id": "L1001", "submitted_referal_form": true, "lead_source": "Source A", "created_at": "2023-04-24" }, "LeadType": { "id": "L1001", "name": "Referral" }, "RebateRecord": { "received_date": "2023-04-24", "loan_amount": 0, "rebate_to_referrer": "string", "received_year": 0, "referral_code": "referral_c0de", "commission_to_sales": "string", "handler": "vinci", "date_sent_to_HKEAA": "2023-04-24", "net_income": 0, "created_at": "2023-04-24", "source": "vinci", "commission_received_date": "2023-04-24", "percentage_fee_rebate_borrower_sales_referrer": 0, "created_by": "0", "id": 1, "borrower_name": "vinci", "total_commission": 0, "percentage_rebate_to_borrower": 0, "mortgage_address": "address", "commission_to_association": 0, "percentage_to_imort": 0, "ref_no": "string", "status": "string", "commission_to_imort": 0, "net_percentage_to_imort": 0, "drawdown_date": "2023-04-24", "received_month": 0 } } ] base on this list of dict of dict make a python program to find the dict contain the value 'vinci' and its upper key. the example output is: [{'case': 'vinci'}, {'Leads': 'vinci'}, {'RebateRecord': 'vinci'}]
- Assistant
result = [] for data in my_list: for key, value in data.items(): if isinstance(value, dict): for sub_key, sub_value in value.items(): if sub_value == 'vinci': result.append({key: 'vinci'}) elif value == 'vinci': result.append({key: 'vinci'})
print(result)
- User
def tabel_keyword_filter(query: str, db: Session = Depends(get_db)): q = ( db.query(Case, CaseStatus, Leads, LeadType, RebateRecord)# as para .select_from(Case, CaseStatus, Leads, LeadType, RebateRecord) .join(CaseStatus, Case.case_status == CaseStatus.code) .join(Leads, Case.lead_id == Leads.id) .join(LeadType, Leads.lead_type_id == LeadType.id) .join(RebateRecord, RebateRecord.referral_code == Case.referral_code)
.filter( or_( Case.customer_name.like(f"%{query}%"), Case.address.like(f"%{query}%"), CaseStatus.code.like(f"%{query}%"), Leads.customer_name.like(f"%{query}%"), LeadType.name.like(f"%{query}%"), RebateRecord.handler.like(f"%{query}%"), RebateRecord.borrower_name.like(f"%{query}%"), RebateRecord.mortgage_address.like(f"%{query}%"), #tel no func.cast(Case.customer_contact_number, String).like(f"%{query}%"), func.cast(Case.customer_phone_number, String).like(f"%{query}%"), func.cast(Leads.customer_phone, String).like(f"%{query}%"), func.cast(Leads.customer_contact_phone, String).like(f"%{query}%"), ) ) .all() )in this code, the inside of db.query, i want to change it as a enum of input parameter
- Assistant
You can define an Enum to represent the different tables to query, and then use that enum in your function as a parameter. Here's an example:
from enum import Enum class QueryTable(str, Enum): CASE = 'Case' CASE_STATUS = 'CaseStatus' LEADS = 'Leads' LEAD_TYPE = 'LeadType' REBATE_RECORD = 'RebateRecord' def tabel_keyword_filter(table: QueryTable, query: str, db: Session = Depends(get_db)): table_class = globals()[table] q = ( db.query(Case, CaseStatus, Leads, LeadType, RebateRecord)# as para .select_from(Case, CaseStatus, Leads, LeadType, RebateRecord) .join(CaseStatus, Case.case_status == CaseStatus.code) .join(Leads, Case.lead_id == Leads.id) .join(LeadType, Leads.lead_type_id == LeadType.id) .join(RebateRecord, RebateRecord.referral_code == Case.referral_code) .filter( or_( getattr(table_class, 'customer_name', None).like(f"%{query}%"), getattr(table_class, 'address', None).like(f"%{query}%"), getattr(table_class, 'code', None).like(f"%{query}%"), getattr(table_class, 'name', None).like(f"%{query}%"), getattr(table_class, 'handler', None).like(f"%{query}%"), getattr(table_class, 'borrower_name', None).like(f"%{query}%"), getattr(table_class, 'mortgage_address', None).like(f"%{query}%"), #tel no func.cast(getattr(table_class, 'customer_contact_number', None), String).like(f"%{query}%"), func.cast(getattr(table_class, 'customer_phone_number', None), String).like(f"%{query}%"), func.cast(getattr(table_class, 'customer_phone', None), String).like(f"%{query}%"), func.cast(getattr(table_class, 'customer_contact_phone', None), String).like(f"%{query}%"), ) ) .all() ) - User
def tabel_keyword_filter(table: QueryTable, query: str, db: Session = Depends(get_db)): table_class = globals()[table] q = ( db.query(table_class) this part is right, but the below part of filter needs to be case senestive
- Assistant
To make the filter case-sensitive, you can replace the
likemethod with theilikemethod, which is case-insensitive. For example:def tabel_keyword_filter(table: QueryTable, query: str, db: Session = Depends(get_db)): table_class = globals()[table] q = ( db.query(table_class) .filter( or_( getattr(table_class, 'customer_name', None).ilike(f"%{query}%"), getattr(table_class, 'address', None).ilike(f"%{query}%"), getattr(table_class, 'code', None).ilike(f"%{query}%"), getattr(table_class, 'name', None).ilike(f"%{query}%"), getattr(table_class, 'handler', None).ilike(f"%{query}%"), getattr(table_class, 'borrower_name', None).ilike(f"%{query}%"), getattr(table_class, 'mortgage_address', None).ilike(f"%{query}%"), #tel no func.cast(getattr(table_class, 'customer_contact_number', None), String).ilike(f"%{query}%"), func.cast(getattr(table_class, 'customer_phone_number', None), String).ilike(f"%{query}%"), func.cast(getattr(table_class, 'customer_phone', None), String).ilike(f"%{query}%"), func.cast(getattr(table_class, 'customer_contact_phone', None), String).ilike(f"%{query}%"), ) ) .all() ) - User
give me shorthand if
- Assistant
To make the filter case-sensitive, you can simply replace all instances of
likewithilike. Here's the updated code:def tabel_keyword_filter(table: QueryTable, query: str, db: Session = Depends(get_db)): table_class = globals()[table] q = ( db.query(table_class) .filter( or_( getattr(table_class, 'customer_name', None).ilike(f"%{query}%"), getattr(table_class, 'address', None).ilike(f"%{query}%"), getattr(table_class, 'code', None).ilike(f"%{query}%"), getattr(table_class, 'name', None).ilike(f"%{query}%"), getattr(table_class, 'handler', None).ilike(f"%{query}%"), getattr(table_class, 'borrower_name', None).ilike(f"%{query}%"), getattr(table_class, 'mortgage_address', None).ilike(f"%{query}%"), #tel no func.cast(getattr(table_class, 'customer_contact_number', None), String).ilike(f"%{query}%"), func.cast(getattr(table_class, 'customer_phone_number', None), String).ilike(f"%{query}%"), func.cast(getattr(table_class, 'customer_phone', None), String).ilike(f"%{query}%"), func.cast(getattr(table_class, 'customer_contact_phone', None), String).ilike(f"%{query}%"), ) ) .all() )