def create_table(self, table): ''' create table in current database :param table: :return: ''' with Lock(self.lock): # test if the table has loaded for t in self.tables: if t.table == table: Logger.info("create table %s...exists.", table.name) return #create new table table = FSTable().create(self.path, table) for i in range(0, len(self.tables)): t = self.tables[i] if t.table.name == table.name: self.tables.pop(i) break self.tables.append(table) self._rebuild_tindex()
def select(self): ''' select all data from table :return: ''' try: with Lock(self.lock): with open(self.data_file, "r") as fdata: models = [] #read field names nfields = strips(fdata.readline().strip().split(",")) #read data records data = fdata.readline() while data: data = data.strip() vfields = strips(data.split(",")) model = {} for idx in range(0, len(nfields)): model[nfields[idx]] = str2obj(vfields[idx], ',') models.append(model) data = fdata.readline() return models except Exception as e: Logger.info("select data from table %s...failed. error: %s", self.name, str(e)) raise e
def close(self): ''' close store :return: ''' with Lock(self.lock): pass
def open(self, path): ''' open store or create it if not exist :return: ''' try: with Lock(self.lock): #init store path self.path = path if not path_exists(self.path): #create database self._create() else: # load database self._load() self._rebuild_tindex() return self Logger.info("open store %s...success. %d tables.", self.path, len(self.tables)) except Exception as e: Logger.error("open store %s...failed. error: %s", self.path, str(e)) raise e
def describe_tables(self): ''' describe all table structures :return: ''' with Lock(self.lock): tables = [] for table in self.tables: tables.append(table.desc()) return tables
def drop_tables(self): ''' clear all tables in store :return: ''' with Lock(self.lock): for table in self.tables: table.drop() self.tables = [] self._rebuild_tindex()
def truncate(self): ''' truncate table :return: ''' try: with Lock(self.lock): remove_files(self.data_file) self._create_data_file() except Exception as e: Logger.error("truncate table %s...failed. error %s", self.name, str(e)) raise e
def drop_table(self, name): ''' drop table in current database :param table: :return: ''' with Lock(self.lock): for table in self.tables: if table.name == name: table.drop() self.tables.remove(table) break self._rebuild_tindex()
def insert(self, models): ''' insert data to table :param models: :return: ''' try: with Lock(self.lock): with open(self.data_file, "a") as fdata: lines = [] for model in models: vfields = [] for nfield in self.table.nfields(): vfields.append(objtostr(model.get(nfield), ',')) lines.append("%s\n" % ",".join(vfields)) fdata.writelines(lines) except Exception as e: Logger.info("insert data to table %s...failed. error: %s", self.name, str(e)) raise e
class IndexHandler(RequestHandler): locker = Lock() field_to_error_name = { "first_name": "Имя", "last_name": "Фамилия", "phone": "Телефон", "email": "Электронный адрес", } def compute_etag(self): return @staticmethod def add_api_headers(headers: dict = None) -> dict: if headers is None: headers = {} headers.update( {"Authorization": "Bearer {}".format(options.api_key),} ) return headers @staticmethod def compose_api_url(path: str) -> str: return urljoin(options.api_endpoint, "account", options.api_account_id, path) def finish_with_error( self, error_text: str = "Не удалось отправить отклик, попробуйте еще раз" ): self.set_status(400) self.finish({"errors": {"common": [error_text]}}) def get(self): self.render( "templates/index.html", vacancy_name=options.vacancy_name, vacancy_url=options.vacancy_url, privacy_url=options.privacy_url, vacancy_og=options.vacancy_og, ) async def post(self): try: data = { "first_name": self.get_argument("first_name"), "last_name": self.get_argument("last_name"), "phone": self.get_argument("phone"), "email": self.get_argument("email"), } except MissingArgumentError as e: self.finish_with_error( 'Не заполнено поле "{}"'.format(self.field_to_error_name[e.arg_name]) ) return lock_key = real_ip(self.request) try: self.locker.gain_lock(lock_key) except LockException: self.finish_with_error("Вы отправляете запросы слишком часто. Попробуйте позднее.") return comment = self.get_argument("comment", None) cv_link = self.get_argument("cv", None) cv_files = self.request.files.get("cv_file") cv_file = None if cv_files: cv_file = self.request.files["cv_file"][0] if cv_file: content_type, body = encode_multipart_formdata( [], [("file", cv_file["filename"], cv_file["body"])] ) response, parsed_file_data = await self.fetch_request( self.make_request( "upload", method="POST", full_url=self.compose_api_url("upload"), headers=self.add_api_headers( { "Content-Type": content_type, "Content-Length": str(len(body)), "X-File-Parse": "true", } ), data=body, allow_ipv6=False, validate_cert=False, request_timeout=5.0, ) ) if not response.error: if parsed_file_data.get("photo"): data["photo"] = parsed_file_data["photo"]["id"] fields = parsed_file_data.get("fields") if fields: if fields.get("birthdate") and fields["birthdate"].get("precision") == "day": data["birthday_day"] = fields["birthdate"]["day"] data["birthday_month"] = fields["birthdate"]["month"] data["birthday_year"] = fields["birthdate"]["year"] if fields.get("experience"): data["position"] = fields["experience"][0]["position"] data["company"] = fields["experience"][0]["company"] if fields.get("salary"): data["money"] = fields["salary"] data["externals"] = [ { "data": { "body": "\n\n".join( filter( None, [ "Ссылка на резюме: {}".format(cv_link) if cv_link else None, parsed_file_data.get("text"), ], ) ) }, "files": [{"id": parsed_file_data["id"]}], "auth_type": "NATIVE", "account_source": options.source_id, } ] if not data.get("externals"): data["externals"] = [ { "data": {"body": cv_link or ""}, "auth_type": "NATIVE", "account_source": options.source_id, } ] response, applicant_data = await self.fetch_request( self.make_request( "applicant", method="POST", full_url=self.compose_api_url("applicants"), headers=self.add_api_headers(), data=json.dumps(data), allow_ipv6=False, validate_cert=False, request_timeout=5.0, ) ) if response.error: self.finish_with_error() return response, vacancy_applicant_data = await self.fetch_request( self.make_request( "applicant-vacancy", method="POST", full_url=self.compose_api_url("applicants/{}/vacancy".format(applicant_data["id"])), headers=self.add_api_headers(), validate_cert=False, allow_ipv6=False, data=json.dumps( { "vacancy": options.vacancy_id, "status": options.status_id, "comment": "Сопроводительное письмо: {}".format(comment) if comment else None, } ), ) ) if response.error: self.finish_with_error() return self.finish({"status": "Отлик успешно добавлен"})