Click here to Skip to main content
15,353,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
monthly.py
Python
from config import DB_ARGS

def main():

    mongo = DB_ARGS()


    regs = dict([
        [reg.get('identId'),reg]
        async for reg in mongo.registrationKYC.find(
            {"deletedAt": {'$eq':None}},
            {"_id": False}
        )
    ])

    fas = [
        fa
        async for fa in mongo.financeAsset.find(
            {"deletedAt": {'$eq':None}},
            {"_id": False, 'contractorAccountId':True}
        )
    ]
    faCount = lambda accountId:len([fa for fa in fas if fa.get('contractorAccountId','')==accountId])

    mas = [
        ma
        async for ma in mongo.memoryAsset.find(
            {"deletedAt": {'$eq':None}},
            {"_id": False, 'contractorAccountId':True}
        )
    ]
    maCount = lambda accountId: len([ma for ma in mas if ma.get('contractorAccountId','')==accountId])

    def extractReg( key ):
        if key not in regs:
            return ['','']

        else:
            createdAt = regs[key].get('createdAt')
            completedAt = regs[key].get('completedAt')
            if createdAt is None:
                createdAt = ''
            else:
                createdAt = createdAt.strftime("%Y-%m-%d %H:%M:%S")
            if completedAt is None:
                completedAt = ''
            else:
                completedAt = completedAt.strftime("%Y-%m-%d %H:%M:%S")
            return [createdAt, completedAt]

    logs = [
        log
        async for log in mongo.log.find(
            {"logType":"DOWNLOAD_PROPERTY_INVENTORY_PDF"},
            {"_id":0,"accountId":1}
        )
    ]
    logCount = lambda accountId: len([log for log in logs if log.get('accountId','')==accountId])

    accs = [
        acc
        async for acc in mongo.account.find(
            {"deletedAt": {'$eq':None}},
            {"_id": False}
        )
    ]
    accs = [
        [
            acc['accountId'],
            '' if ('createdAt' not in acc or acc['createdAt'] is None) else acc['createdAt'].strftime("%Y-%m-%d %H:%M:%S"),
            '' if ('authorizedAt' not in acc or acc['authorizedAt'] is None) else acc['authorizedAt'].strftime("%Y-%m-%d %H:%M:%S"),
            *extractReg( acc.get('identId') ),
            str(faCount(acc['accountId'])),
            str(maCount(acc['accountId'])),
            str(len(acc.get('loginTrial',[]))),
            str(len(acc.get('activationTrial',[]))),
            str(logCount(acc['accountId']))
        ]
        async for acc in accs
        if acc['userLoginKey'][0:3]!='777' and acc['userLoginKey'] not in DB_ARGS
    ]
    accs = [[
        'accountId',
        'createdAt',
        'authorizedAt',
        'registrationKYC_CreatedAt',
        'registrationKYC_CompletedAt',
        'financeAssetCount',
        'memoryAssetCount',
        'loginTrial',
        'activationTrial',
        "download trial"
    ]] + accs
    accs = '\n'.join([','.join(acc) for acc in accs])
    open('./registered-list.csv','w').write(accs)

main()


then, I want to get return of accs as csv file.

routers.py
Python
@router.post(f"/admin/test")
async def get_id_photo_image_of_user(db=Depends(get_database)):
    mongo = Mongo(db)

    regs = dict([
        [reg.get('identId'),reg]
        async for reg in mongo.registrationKYC.find(
            {"deletedAt": {'$eq':None}},
            {"_id": False}
        )
    ])

    return regs

    ident_id = await validationAccountId(acc.get(PropAccount.IDENT_ID))
    reg: Dict = await getRegistrationKYC(db=db, identId=ident_id)

    file_name = f"{ident_id}_IDPhoto.jpg"
    return StreamingResponse(
        content=mongo.download(reg.get(PropRegistrationKYC.ID_PHOTO_URL)),
        media_type="",
        headers={
            "Content-Type": "",
            "Content-Disposition": f"attachment; filename={file_name}",
            HTTPHeader.X_FILENAME: file_name,
        },
    )


I have no idea what shall I do this.....

What I have tried:

Quote:
accs = '\n'.join([','.join(acc) for acc in accs])
open('./registered-list.csv','w').write(accs)


i tried openning but it doesnt work too..
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900