def load_all_users(): all_users = dict() for user in api.get_users(): user = User(user["name"], user["id"]) all_users[user.id] = user return all_users
def user(slug): authors = api.get_users(slugs=[slug]) if not authors: flask.abort(404) author = authors[0] recent_posts, total_posts, total_pages = helpers.get_formatted_posts( author_ids=[author['id']], per_page=5) return flask.render_template('author.html', author=author, recent_posts=recent_posts)
def fill_table(self): row = 0 r = api.get_users() if api.check_status('GET', r): users = r.json() self.table_widget.setRowCount(len(users)) for user in users: self.table_widget.setItem(row, 0, QTableWidgetItem(f'{user.get("username")}')) self.table_widget.setItem(row, 1, QTableWidgetItem(f'{user.get("password")}')) row += 1 else: self.error_message.setText(f'{r.status_code} Error') self.layout.addWidget(self.error_message)
def load_users(self, table_widget): header = table_widget.horizontalHeader() header.setSectionResizeMode(1, QHeaderView.ResizeToContents) row = 0 r = api.get_users() if api.check_status('GET', r): users = r.json() table_widget.setRowCount(len(users)) for user in users: btn = QPushButton('Edit') table_widget.setItem(row, 0, QTableWidgetItem(f'{user.get("username")}')) table_widget.setItem(row, 1, QTableWidgetItem(f'{user.get("password")}')) table_widget.setCellWidget(row, 2, btn) row += 1 else: # self.error_message.setText(f'{r.status_code} Error') # self.layout.addWidget(self.error_message) print(r.status_code)
def make_broadcast(is_test=True, receivers='users', cont_type='text', msg_text='', file_id='', user_id=303986717, message_id=0): chats = [] if receivers in ['users', 'all']: chats.extend(api.get_users()) if receivers in ['chats', 'all']: chats.extend(api.get_chats()) broadcast_stats = { 'start_time': int(time.time()), 'users': { 'count': api.get_users_count(), 'succ': 0, 'errors': 0 }, 'chats': { 'count': api.get_chats_count(), 'succ': 0, 'errors': 0 } } counter = 0 max_counter = len(chats) max_part = max_counter // 20 kb = types.InlineKeyboardMarkup() kb.add( types.InlineKeyboardButton(text='В главное меню', callback_data='to_main_menu')) for i in chats: counter += 1 try: chat_id = i['chat_id'] chat_type = 'chats' except Exception: chat_id = i['user_id'] chat_type = 'users' if is_test: try: bot.send_chat_action(chat_id, 'typing') broadcast_stats[chat_type][ 'succ'] = broadcast_stats[chat_type]['succ'] + 1 except Exception as e: broadcast_stats[chat_type][ 'errors'] = broadcast_stats[chat_type]['errors'] + 1 else: if cont_type == 'text': try: bot.send_message(chat_id, text=msg_text, parse_mode='HTML') broadcast_stats[chat_type][ 'succ'] = broadcast_stats[chat_type]['succ'] + 1 except Exception as e: broadcast_stats[chat_type][ 'errors'] = broadcast_stats[chat_type]['errors'] + 1 else: try: send_file(chat_id, cont_type, file_id, msg_text) broadcast_stats[chat_type][ 'succ'] = broadcast_stats[chat_type]['succ'] + 1 except Exception as e: broadcast_stats[chat_type][ 'errors'] = broadcast_stats[chat_type]['errors'] + 1 if counter % 50 == 0 or counter == max_counter: used_time = round(time.time() - broadcast_stats['start_time'], 2) speed = round(counter / used_time, 2) curr_perc_all = counter / max_counter * 100 used_dots_all = int(curr_perc_all // (100 / 20)) last_dots_all = int(20 - used_dots_all) curr_perc_users = broadcast_stats['users'][ 'succ'] / broadcast_stats['users']['count'] * 100 used_dots_users = int(curr_perc_users // (100 / 20)) last_dots_users = int(20 - used_dots_users) curr_perc_chats = broadcast_stats['chats'][ 'succ'] / broadcast_stats['chats']['count'] * 100 used_dots_chats = int(curr_perc_chats // (100 / 20)) last_dots_chats = int(20 - used_dots_chats) try: print(chat_id, message_id) bot.edit_message_text( chat_id=user_id, message_id=message_id, text=text.service_messages['broadcast'][receivers].format( all_chats=max_counter, chats_sent=counter, all_perc=round(counter / max_counter * 100, 2), successfully_sent_users=broadcast_stats['users'] ['succ'], errors_sent_users=broadcast_stats['users']['errors'], successfully_sent_chats=broadcast_stats['chats'] ['succ'], errors_sent_chats=broadcast_stats['chats']['errors'], perc_suc_users=round( broadcast_stats['users']['succ'] / broadcast_stats['users']['count'] * 100, 2), perc_err_users=round( broadcast_stats['users']['errors'] / broadcast_stats['users']['count'] * 100, 2), perc_suc_chats=round( broadcast_stats['chats']['succ'] / broadcast_stats['chats']['count'] * 100, 2), perc_err_chats=round( broadcast_stats['chats']['errors'] / broadcast_stats['chats']['count'] * 100, 2), spent_time=datetime.timedelta(seconds=int(used_time)), speed=speed, pbar_users=('█' * used_dots_users) + ('▒' * last_dots_users), pbar_chats=('█' * used_dots_chats) + ('▒' * last_dots_chats), pbar=('█' * used_dots_all) + ('▒' * last_dots_all), eta=datetime.timedelta( seconds=int((max_counter - counter) / speed))), parse_mode='HTML', reply_markup=kb) except Exception as e: print(e)
def test_get_users(self): """confirms that it returns array of all users information""" self.assertEqual(api.get_users().status_code, 200)