def add_or_update_profile_callback(room, event): username = event['sender'] args = event['content']['body'].split() profile_name = args[1] profile_phone = args[2] profile_password = args[3] # TODO проверка, есть ли уже профиль с введенными данными. # TODO Если есть - вывести сообщение и return user = User.get_or_create(username=username)[0] profile = Profile.get_or_none(Profile.owner == user, Profile.name == profile_name) if profile: profile.phone_number = profile_phone profile.password = profile_password profile.save() else: profile = Profile.create(owner=user, name=profile_name, phone_number=profile_phone, password=profile_password) current_profile = CurrentProfile.get_or_none(user=user) if current_profile: current_profile.profile = profile current_profile.save() else: current_profile = CurrentProfile.create(user=user, profile=profile) connection = UmsConnection(profile_phone, profile_password) CONNECTIONS.append((profile, connection)) captcha = connection.get_captcha() url = BOT.client.upload(captcha, 'image/png') room.send_text('Профиль добавлен, введите капчу командой captcha <key>') room.send_image(url, 'captcha.png')
def remove_profile_callback(room, event): username = event['sender'] # TODO сделать закрытие соединений при удалении профиля args = event['content']['body'].split() profile_name = args[1] user = User.get_or_create(username=username)[0] profile = Profile.get_or_none(owner=user, name=profile_name) if profile: answer = f"Удалён профиль **{profile.name}:** {profile.phone_number} {profile.password}" profile.delete_instance() room.send_html(markdown(answer)) else: room.send_text(f'У вас отсутствует профиль {profile_name}')
def select_profile_callback(room, event): username = event['sender'] args = event['content']['body'].split() profile_name = args[1] user = User.get_or_create(username=username)[0] profile = Profile.get_or_none(owner=user, name=profile_name) if profile: current_profile = CurrentProfile.get_or_none(user=user) if current_profile: current_profile.profile = profile current_profile.save() else: current_profile = CurrentProfile.create(user=user, profile=profile) answer = f"Выбран профиль **{profile.name}:** {profile.phone_number} {profile.password}" room.send_html(markdown(answer)) else: room.send_text(f'У вас отсутствует профиль {profile_name}')
def _sync_organization(self, organization): print('Starting to sync users for the {} organization.'.format( organization.name)) client = self._get_graph_service_client(organization.tenantId) record, is_new_record = DataSyncRecord.get_or_create( tenantId=organization.tenantId, query=self._users_query) print('\tExecuting Differential Query') if is_new_record: query = {'$select': 'jobTitle,department,mobilePhone'} users, next_link, delta_link = client.get_users_delta(query) print( '\tFirst time executing differential query; all items will return.' ) else: users, next_link, delta_link = client.get_users(record.deltaLink) while True: print('\tGet {} user(s).'.format(len(users))) for user in users: profile = Profile.get_or_none(Profile.o365UserId == user['id']) if profile: if user.get('@removed'): self._delete_profile_and_related(profile) else: self._update_profile(profile, user) else: # Note: for real-world projects, you may need to add users to the database. print( "\tSkipping updating user {} who does not exist in the local database." .format(user['id'])) if next_link: users, next_link, delta_link = client.get_users(next_link) else: break self._update_data_sync_record(record, delta_link)