def delete_user(user_id): try: UserTermsLink.delete_user_by_user_id(user_id) Activity.delete_user_by_user_id(user_id) Contact.delete_contact_preferences_for_user(user_id) user = UserDetails.get_user_details_by_id(user_id) if user: db.session.delete(user) else: app.logger.error("No user: {} found for deletion".format(user_id)) db.session.rollback() db.session.close() raise ApplicationError(*errors.get('ulapd_api', 'USER_NOT_FOUND', filler=user_id), http_code=404) db.session.commit() return {'user_id': user_id, 'message': 'user deleted'} except Exception as e: db.session.rollback() db.session.close() raise ApplicationError(*errors.get('ulapd_api', 'DELETE_USER_ERROR', filler=e), http_code=400)
def manage_licence_agreement(data): try: user = UserDetails.get_user_details_by_id(data['user_details_id']) if not user: app.logger.error("No user found for id {}".format( data['user_details_id'])) raise ApplicationError(*errors.get('ulapd_api', 'USER_NOT_FOUND', filler=data['user_details_id']), http_code=404) # check to see if user already has this type of licence ldap_update_needed = True licence_data = _extract_rows( Licence.get_licences_by_dataset_name(data['licence_id'])) for rows in licence_data: user_licence = UserTermsLink.get_user_terms_by_licence_name( user.user_details_id, rows['licence_id']) if user_licence: app.logger.info( 'User {} already has the role {} so not updating ldap...'. format(user.user_details_id, data['licence_id'])) ldap_update_needed = False stored_licence_id = data['licence_id'] if _check_freemium(data['licence_id']): data['licence_id'] += '_direct' licence = UserTermsLink(data) db.session.add(licence) if ldap_update_needed: app.logger.info( 'User {} does not have the role {} so update ldap...'.format( user.user_details_id, stored_licence_id)) _handle_ldap_group(stored_licence_id, user.ldap_id, True) db.session.commit() data['link_id'] = licence.user_terms_link_id return data except Exception as e: app.logger.error( 'Failed to manage licence for user {} with error - {}'.format( data['user_details_id'], str(e))) db.session.rollback() db.session.close() raise ApplicationError(*errors.get('ulapd_api', 'LICENCE_AGREE_ERROR', filler=e), http_code=400)
def _build_users_datasets(user_id): user_licences = _extract_rows( UserTermsLink.get_user_terms_by_user_id(user_id)) dataset_access = {} for rows in user_licences: licence = get_licence_agreement(user_id, rows['licence_id']) if licence['valid_licence']: licence_data = Licence.get_licence_by_licence_name( rows['licence_id']) dataset_data = Dataset.get_dataset_by_name( licence_data.dataset_name) if dataset_data.name in dataset_access: dataset_access[dataset_data.name]['licences'].append( licence_data.title) dataset_access[dataset_data.name]['date_agreed'] = None else: dataset_access[dataset_data.name] = { 'date_agreed': str(licence['date_agreed']), 'private': dataset_data.private, 'valid_licence': licence['valid_licence'], 'licence_type': dataset_data.type, 'licences': [licence_data.title] } # Need to sort freemium licences into their correct order for key, value in dataset_access.items(): if value['licence_type'] == 'freemium': value['licences'] = sorted(value['licences'], key=_order_freemium_licences) return dataset_access
def insert_licence_agreement(user_details_id): with app.app_context(): terms_data = { 'user_details_id': user_details_id, 'licence_id': 'ccod' } user_terms = UserTermsLink(terms_data) db.session.add(user_terms) db.session.commit() agreement_id = user_terms.user_terms_link_id db.session.close() return agreement_id
def get_licence_agreement(user_id, licence_name): result = { 'user_id': user_id, 'licence': licence_name, 'valid_licence': False, 'date_agreed': None } licence = UserTermsLink.get_user_terms_by_licence_name( user_id, licence_name) if licence: result['valid_licence'] = _check_agreement(licence_name, licence.date_agreed) result['date_agreed'] = licence.date_agreed return result
def get_user_licences(user_id): return _extract_rows(UserTermsLink.get_user_terms_by_user_id(user_id))
def manage_multi_licence_agreement(data): try: user = UserDetails.get_user_details_by_id(data['user_details_id']) if not user: app.logger.error("No user found for id {}".format( data['user_details_id'])) raise ApplicationError(*errors.get('ulapd_api', 'USER_NOT_FOUND', filler=data['user_details_id']), http_code=404) current_licences = _extract_rows( UserTermsLink.get_user_terms_by_user_id(data['user_details_id'])) current_list = [d['licence_id'] for d in current_licences] all_licences = Licence.get_all_licences() licence_dict = {} for rows in all_licences: licence_dict[rows.licence_name] = rows.dataset_name groups = {} for row in data['licences']: if row['licence_id'] not in current_list and row['agreed']: app.logger.info('Adding licence {} to db and ldap'.format( row['licence_id'])) role = licence_dict[row['licence_id']] groups[role] = row['agreed'] new_user_licence = UserTermsLink({ 'user_details_id': user.user_details_id, 'licence_id': row['licence_id'] }) db.session.add(new_user_licence) elif row['licence_id'] in current_list and not row['agreed']: app.logger.info('Removing licence {} from db and ldap'.format( row['licence_id'])) role = licence_dict[row['licence_id']] groups[role] = row['agreed'] UserTermsLink.delete_user_licence_agreement( user.user_details_id, row['licence_id']) else: app.logger.info('There is no change for licence {}'.format( row['licence_id'])) # sort out freemium res_covs = [ d for d in data['licences'] if 'res_cov' in d['licence_id'] ] groups = _handle_ldap_freemium_updates('res_cov', res_covs, groups, current_list) if bool(groups): app.logger.info( 'Sending these groups - {}, for ldap update for user {}'. format(groups, user.user_details_id)) update_groups_for_ldap(user.ldap_id, groups) else: app.logger.info('No groups to update in ldap for user {}'.format( user.user_details_id)) db.session.commit() return groups except Exception as e: app.logger.error( 'Failed to manage multi licences for user {} with error - {}'. format(data['user_details_id'], str(e))) db.session.rollback() db.session.close() raise ApplicationError(*errors.get('ulapd_api', 'LICENCE_AGREE_ERROR', filler=e), http_code=400)
def delete_user_agreement(user_details_id, lincence_id): with app.app_context(): UserTermsLink.delete_user_licence_agreement(user_details_id, lincence_id)