From c36f21b8198f290d2bb5cb78982e88e3fd2e318e Mon Sep 17 00:00:00 2001 From: Kseninia Mikhaylova Date: Fri, 19 Jul 2024 10:55:32 +0300 Subject: [PATCH] add user id --- back/tgbot/tgbot.py | 7 +++++++ back/tgbot/views.py | 14 ++++++++++++++ front/components/getAuthor.vue | 21 +++++++++++++++++++++ front/pages/table/index.vue | 3 +++ front/store/authors.ts | 21 ++++++++++----------- 5 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 front/components/getAuthor.vue diff --git a/back/tgbot/tgbot.py b/back/tgbot/tgbot.py index 57dbfc2..4609427 100644 --- a/back/tgbot/tgbot.py +++ b/back/tgbot/tgbot.py @@ -70,6 +70,13 @@ class TgBot: TgBotUpdater.return_values[item.text] = text.file_path except Exception as e: TgBotUpdater.return_values[item.text] = None + if name == 'admin_get_name': + item = queryset[0] + try: + text = await TgBot.app.bot.get_chat(item) + TgBotUpdater.return_values[item] = text.effective_name + except Exception as e: + TgBotUpdater.return_values[item] = None async def set_handlers(self): TgBot.app.add_handler( diff --git a/back/tgbot/views.py b/back/tgbot/views.py index b120822..7688ea5 100644 --- a/back/tgbot/views.py +++ b/back/tgbot/views.py @@ -37,6 +37,20 @@ class TmcFieldViewset(viewsets.ModelViewSet): serializer_class = TmcFieldSerializer http_method_names = ["get"] + @action(detail=False, methods=["get"], url_path=r"get_name/(?P[^/.]+)") + def get_name(self, request, chat_id): + TgBotUpdater.my_queue.put({"name": "admin_get_name", "queryset": [chat_id]}) + response = [] + timer = 30 + while timer > 0: + sleeping = 1 + timer -= sleeping + time.sleep(sleeping) + if chat_id in TgBotUpdater.return_values: + response.append(TgBotUpdater.return_values[chat_id]) + del TgBotUpdater.return_values[chat_id] + break + return Response(response) @action(detail=False, methods=["get"], url_path=r"get_image/(?P[^/.]+)") def get_image(self, request, field_id): if TmcField.objects.filter(id=field_id).exists(): diff --git a/front/components/getAuthor.vue b/front/components/getAuthor.vue new file mode 100644 index 0000000..d726dbd --- /dev/null +++ b/front/components/getAuthor.vue @@ -0,0 +1,21 @@ + + \ No newline at end of file diff --git a/front/pages/table/index.vue b/front/pages/table/index.vue index 5ec5f31..23bbb9a 100644 --- a/front/pages/table/index.vue +++ b/front/pages/table/index.vue @@ -53,6 +53,9 @@ const columns = [ + diff --git a/front/store/authors.ts b/front/store/authors.ts index a4f9cc7..9c8da72 100644 --- a/front/store/authors.ts +++ b/front/store/authors.ts @@ -4,40 +4,39 @@ interface imagesList { result?: string } } -export const useImagesStore = defineStore('images', { +export const useAuthorsStore = defineStore('authors', { state: () => ({ list: {} as imagesList }), actions: { - getImage(name: string) { + getItem(name: string) { if (!this.list[name]) { this.list[name] = { status: 'idle', - // result: 'https://media.istockphoto.com/id/1490517357/photo/scientists-are-researching-and-analyzing-harmful-contaminants-in-the-laboratory.webp?s=170667a&w=0&k=20&c=Fh4t-P_b-a1QxwyBUzUa0AuLp8FLNyLy4hl4HUm82Ao=' } } if ( !Object.entries(this.list).filter(el => el[1].status == 'pending').length && Object.entries(this.list).filter(el => el[1].status == 'idle').length) { - this.loadImages() + this.loadItems() } return this.list[name] }, - async loadOneImage(name: string) { - const file_url_data = await apiCall(`tgbot/items/get_image/${name}/`) - if (file_url_data.length > 0 && file_url_data[0] !== null) { + async loadOneItem(name: string) { + const result_data = await apiCall(`tgbot/items/get_name/${name}/`) + if (result_data.length > 0 && result_data[0] !== null) { this.list[name].status = 'success' - this.list[name].result = file_url_data[0] + this.list[name].result = result_data[0] } else { this.list[name].status = 'error' } if (Object.entries(this.list).filter(el => el[1].status == 'idle').length) { - this.loadImages() + this.loadItems() } }, - loadImages() { + loadItems() { const elements = Object.entries(this.list).filter(el => el[1].status == 'idle') elements.slice(0, 2).map(el => { this.list[el[0]].status = 'pending' - this.loadOneImage(el[0]) + this.loadOneItem(el[0]) }) } },