def register_new_user(user): """ Tries to register a new user using the DEFAULT_USER dictionary in settings. If the user already exists, it just returns the new user. """ new_user = User(**user) try: session.flush() except DuplicateKeyError: # User with email address already exists, so flush and retrieve user session.clear() return retrieve_default_user(user)
def write_activity_segments(user, activities, account_id): """ This function writes new activity segments to MongoDB. If the activity already exists, it's not rewritten back to MongoDB. """ source_mapping = { '0': 'mapmyrun', } activity_source = source_mapping[account_id] or 'mapmyrun' external_ids = [a['external_id'] for a in user.activity_segments if a.source == activity_source] for activity in activities: if not activity['external_id'] in external_ids: # Create new activity activity['user'] = user ActivitySegment(**activity) session.flush()
def link_external_account(user, external_account, token_credentials): """ Creates an external account for the current user identified in the session, and then dispatches a task to retrieve all the activities for the user. """ assert user, "User should already exist in MongoDB" create = True # Check if the external account we are going to create is already in the database for account in user.external_accounts: if account.external_id == external_account['external_id']: # Exists, update the object with new token_credentials, don't create a new one account.credentials = token_credentials create = False if create: external_account['user'] = user external_account['credentials'] = token_credentials ExternalAccount(**external_account) session.flush() tasks.retrieve_activities_for_user.delay(str(user._id))