From 0798f2a8f5af8078b1af186617dbe44e344cd9d9 Mon Sep 17 00:00:00 2001 From: Alex Kulikov <7394728+alexeyqu@users.noreply.github.com> Date: Sun, 10 Sep 2023 14:28:24 +0100 Subject: [PATCH] hr pt acquisition job (#295) * hr pt acquisition job * works --- config.json | 1 + src/bot.py | 6 ++ src/jobs/__init__.py | 1 + src/jobs/hr_acquisition_pt_job.py | 108 ++++++++++++++++++++++++++++++ src/sheets/sheets_client.py | 7 ++ src/sheets/sheets_objects.py | 25 +++++++ 6 files changed, 148 insertions(+) create mode 100644 src/jobs/hr_acquisition_pt_job.py diff --git a/config.json b/config.json index 7598333..a90fa52 100644 --- a/config.json +++ b/config.json @@ -23,6 +23,7 @@ "authors_sheet_key": "do_not_set_here_please_go_to_config_override", "curators_sheet_key": "do_not_set_here_please_go_to_config_override", "hr_sheet_key": "do_not_set_here_please_go_to_config_override", + "hr_pt_sheet_key": "do_not_set_here_please_go_to_config_override", "post_registry_sheet_key": "do_not_set_here_please_go_to_config_override", "postcards_sheet_key": "do_not_set_here_please_go_to_config_override", "rubrics_registry_sheet_key": "do_not_set_here_please_go_to_config_override", diff --git a/src/bot.py b/src/bot.py index dbd1118..66676ab 100644 --- a/src/bot.py +++ b/src/bot.py @@ -130,6 +130,12 @@ def init_handlers(self): self.manager_reply_handler("hr_acquisition_job"), "обработать новые анкеты", ) + self.add_admin_handler( + "hr_acquisition_pt", + CommandCategories.HR, + self.manager_reply_handler("hr_acquisition_pt_job"), + "обработать новые анкеты Пишу Тебе", + ) self.add_manager_handler( "get_hr_status", CommandCategories.HR, diff --git a/src/jobs/__init__.py b/src/jobs/__init__.py index 957f129..1423261 100644 --- a/src/jobs/__init__.py +++ b/src/jobs/__init__.py @@ -18,6 +18,7 @@ from .fb_analytics_report_job import FBAnalyticsReportJob from .fill_posts_list_job import FillPostsListJob from .hr_acquisition_job import HRAcquisitionJob +from .hr_acquisition_pt_job import HRAcquisitionPTJob from .hr_check_chat_consistency_frozen_job import HRCheckChatConsistencyFrozenJob from .hr_check_chat_consistency_job import HRCheckChatConsistencyJob from .hr_check_trello_consistency_frozen_job import HRCheckTrelloConsistencyFrozenJob diff --git a/src/jobs/hr_acquisition_pt_job.py b/src/jobs/hr_acquisition_pt_job.py new file mode 100644 index 0000000..c92a4c4 --- /dev/null +++ b/src/jobs/hr_acquisition_pt_job.py @@ -0,0 +1,108 @@ +import logging +from typing import Callable, List + +from sheetfu import Table + +from ..app_context import AppContext +from ..sheets.sheets_objects import HRPersonPTProcessed, HRPersonPTRaw +from ..strings import load +from ..tg.sender import pretty_send +from .base_job import BaseJob + +logger = logging.getLogger(__name__) + + +class HRAcquisitionPTJob(BaseJob): + @staticmethod + def _execute( + app_context: AppContext, send: Callable[[str], None], called_from_handler=False + ): + paragraphs = [load("hr_acquisition_job__hello")] # list of paragraph strings + + new_people = HRAcquisitionPTJob._process_new_people(app_context) + + if not new_people: + return + + paragraphs += [ + HRAcquisitionPTJob._get_new_person_paragraph(item) for item in new_people + ] + + pretty_send(paragraphs, send) + + @staticmethod + def _process_new_people( + app_context: AppContext, + ) -> List[HRPersonPTProcessed]: + forms_raw = app_context.sheets_client.fetch_hr_pt_forms_raw() + forms_processed = app_context.sheets_client.fetch_hr_pt_forms_processed() + + new_items = HRAcquisitionPTJob._process_raw_forms(forms_raw, forms_processed) + + try: + forms_raw.commit() + forms_processed.commit() + except Exception as e: + logger.error(f"failed to export data: {e}") + + return new_items + + @staticmethod + def _process_raw_forms( + forms_raw: Table, forms_processed: Table + ) -> List[HRPersonPTProcessed]: + people = [HRPersonPTRaw(item) for item in forms_raw] + existing_people = [person for person in people if person.status] + new_people = [person for person in people if not person.status] + new_items = [] + + for person in new_people: + # filter out incomplete responses + if not person.telegram and not person.other_contacts: + person.status = load("sheets__hr__pt__raw__status_rejection") + continue + if person.telegram and ( + person.telegram in {person.telegram for person in existing_people} + or person.telegram in {person.telegram for person in new_items} + ): + person.status = load("sheets__hr__pt__raw__status_double") + continue + + # move good ones to another sheet + person.status = load("sheets__hr__pt__raw__status_processed") + # TODO: PR to sheetfu which will allow better API here + person_dict = { + "id": len(forms_processed) + + 2, # 1 for starting with 1 and 1 for the header + "name": person.name, + "interests": person.interests, + "about": person.about, + "date_submitted": person.ts, + "referral": person.referral, + "telegram": person.telegram, + "status": "TODO", # this is a legitimate value, not an actual TODO + } + new_items.append( + HRPersonPTProcessed.add_one_to_table(forms_processed, person_dict) + ) + + return new_items + + @staticmethod + def _get_new_person_paragraph(item: HRPersonPTProcessed) -> str: + name = load( + "hr_acquisition_job__name", + name=item.name, + telegram=item.telegram, + date=item.date_submitted.split(" ")[0], + ) + interests = load("hr_acquisition_job__interests", interests=item.interests) + about = load("hr_acquisition_job__about", description=item.about) + paragraph = load( + "hr_acquisition_job__person", + name=name, + interests=interests, + about=about, + contacts="", # no other contacts field in PT + ) + return paragraph diff --git a/src/sheets/sheets_client.py b/src/sheets/sheets_client.py index 044352b..4da645a 100644 --- a/src/sheets/sheets_client.py +++ b/src/sheets/sheets_client.py @@ -29,6 +29,7 @@ def _update_from_config(self): self.authors_sheet_key = self._sheets_config["authors_sheet_key"] self.curators_sheet_key = self._sheets_config["curators_sheet_key"] self.hr_sheet_key = self._sheets_config["hr_sheet_key"] + self.hr_pt_sheet_key = self._sheets_config["hr_pt_sheet_key"] self.post_registry_sheet_key = self._sheets_config["post_registry_sheet_key"] self.rubrics_registry_sheet_key = self._sheets_config[ "rubrics_registry_sheet_key" @@ -57,6 +58,12 @@ def fetch_hr_forms_raw(self) -> Table: def fetch_hr_forms_processed(self) -> Table: return self._fetch_table(self.hr_sheet_key, "Анкеты") + def fetch_hr_pt_forms_raw(self) -> Table: + return self._fetch_table(self.hr_pt_sheet_key, "Ответы Главный сайт") + + def fetch_hr_pt_forms_processed(self) -> Table: + return self._fetch_table(self.hr_pt_sheet_key, "Анкеты") + def fetch_hr_team(self) -> Table: return self._fetch_table(self.hr_sheet_key, "Команда (с заморозкой)") diff --git a/src/sheets/sheets_objects.py b/src/sheets/sheets_objects.py index ef97cca..69ff0a4 100644 --- a/src/sheets/sheets_objects.py +++ b/src/sheets/sheets_objects.py @@ -244,6 +244,31 @@ class HRPersonProcessed(SheetsItem): } +class HRPersonPTRaw(SheetsItem): + field_alias = { + "ts": "sheets__hr__pt__raw__timestamp", + "name": "sheets__hr__pt__raw__name", + "interests": "sheets__hr__pt__raw__interests", + "about": "sheets__hr__pt__raw__about", + "telegram": "sheets__hr__pt__raw__telegram", + "referral": "sheets__hr__pt__raw__referral", + "status": "sheets__hr__raw__status", + } + + +class HRPersonPTProcessed(SheetsItem): + field_alias = { + "id": "sheets__hr__pt__processed__id", + "name": "sheets__hr__pt__processed__name", + "interests": "sheets__hr__pt__processed__interests", + "about": "sheets__hr__pt__processed__about", + "referral": "sheets__hr__pt__processed__referral", + "date_submitted": "sheets__hr__pt__processed__date_submitted", + "telegram": "sheets__hr__pt__processed__telegram", + "status": "sheets__hr__pt__processed__status", + } + + class PostRegistryItem(SheetsItem): field_alias = { "name": "sheets__post_registry__column_name",