def dump_collection(collection_name, field_names, output_folder_path): """Dump the given collection.""" collection = _get_mongo_database()[collection_name] count = collection.count() cursor = collection.find( filter={}, projection={ '_id': 0, **{name: 1 for name in field_names} }, ) fpath = os.path.join(output_folder_path, 'dh_{}.csv'.format(collection_name)) with tqdm(total=count) as pbar: with open(fpath, 'w+') as outfile: writer = csv.DictWriter(outfile, fieldnames=field_names, dialect=csv.unix_dialect) writer.writeheader() for x in cursor: try: writer.writerow(x) pbar.update(1) except UnicodeEncodeError: print("problem encoding the following row:") print(x)
def _set_field_true_by_emails(emails, field_name): if field_name: users = _get_mongo_database()['users'] users.update_many( filter={'email': {'$in': emails}}, update={'$set': {field_name: True}}, )
def send_confirmation_emails(): _print_email_stats() print("Sending confirmation emails to all non-confirmed users.") users = _get_mongo_database()['users'] users_to_send = list(users.find( filter={CONFIRM_FIELD_NAME: {'$ne': True}}, projection={'email': 1} )) emails = [doc['email'] for doc in users_to_send] send_batch_emails( emails, CONFIRM_SUBJECT, CONFIRM_BODY, CONFIRM_FIELD_NAME)
def print_user_stats(): db = _get_mongo_database() users = db['users'] print('```\n\n') print("{} total users in the system.".format(users.count_documents({}))) print("{} users got a confirmation email.".format( users.count_documents({CONFIRM_FIELD_NAME: True}))) pprint_two_ordered_dicts( 'Gender', key_value_counts('gender', users), 'Food', key_value_counts('food', users), ) pprint_two_ordered_dicts( 'DataLearn', key_value_counts('workshop', users), 'Student', key_value_counts('student', users), ) pprint_two_ordered_dicts( 'Sleep', key_value_counts('sleep', users), 'Hacker', key_value_counts('hacker', users), ) pprint_two_ordered_dicts( 'Transport', key_value_counts('transport', users), 'TLV Bus', key_value_counts('bus', users), ) pprint_two_ordered_dicts( 'Shirt Type', key_value_counts('shirttype', users), 'Shirt size', key_value_counts('shirtsize', users), ) pprint_two_ordered_dicts( 'Team Status', key_value_counts('teamstatus', users), 'Newsletter', key_value_counts('newsletter', users), ) print('```\n')
def send_rejection_upgrade_emails(sandbox=False): if not sandbox: sandbox = FALSE print("Sending acceptance DATALEARN emails to accepted users who didn't get one.") users = _get_mongo_database()['users'] users_to_send = list(users.find( filter={ 'sandbox': sandbox, IS_ACCEPTED_FIELD_NAME: FALSE, IS_ACCEPTED_DATALEARN_FIELD_NAME: FALSE, IS_REJECTED_FIELD_NAME: True, ACCEPT_EMAIL_FIELD_NAME: FALSE, }, projection={'email': True} )) emails = [doc['email'] for doc in users_to_send] print("Found {} relevant users".format(len(emails))) subject = f"Sorry! You have not been accepted DataHack {YEAR}!" body = _get_body('rejected.txt') send_batch_emails(emails, subject, body, ACCEPT_EMAIL_FIELD_NAME)
def _print_email_stats(): users = _get_mongo_database()['users'] print(f"Emails stats on DataHack {YEAR} registration:") print("{} total users in the system.".format(users.count_documents({}))) print("{} users got a confirmation email.".format(users.count({CONFIRM_FIELD_NAME: True}))) print("{} users got an acceptance email.".format(users.count({ACCEPT_EMAIL_FIELD_NAME: True})))