def __init__(self, args): self.args = args self.name = self.get_name() if not self.name: raise CustomError400("<name> parameter is required") self.freq = self.get_freq() if not self.freq: raise CustomError400("<freq> parameter is required")
def remove_one(abbr): description = DescriptionOperations.get_one(abbr) if not description: raise CustomError400("Error: Can not delete description. " f"No record for variable {repr(abbr)} found.") try: db.session.delete(description) db.session.commit() except Exception: db.session.rollback() raise CustomError400("Error on deleting.")
def __init__(self, inner_path: str): """Extract parameters from *inner_path* string. Args: inner_path is a string like 'eop/2015/2017/csv' Methods: get_dict() returns inner path tokens as dictionary """ # make list of non-empty strings tokens = [token.strip() for token in inner_path.split('/') if token] helper = TokenHelper(tokens) self.dates = helper.get_dates_dict() # finaliser self.fin = helper.fin() # transforms self.rate = helper.rate() self.agg = helper.agg() if self.rate and self.agg: raise CustomError400("Cannot combine rate and aggregation.") # find unit name, if present if tokens: self.unit = tokens[0] else: self.unit = self.rate or None
def validate_start_is_not_in_future(start_date): current_date = datetime.date(datetime.utcnow()) #TODO: test on date = today must pass if start_date > current_date: raise CustomError400('Start date cannot be in future') else: return True
def validate_freq_exist(freq): allowed = list(queries.select_unique_frequencies()) if freq in allowed: return True else: raise CustomError400(message=f'Invalid frequency <{freq}>', payload={'allowed': allowed})
def to_date(date_str: str): """Convert YYYY-MM-DD *date_str* to datetime.date object. Raises error if *date_str* not in YYYY-MM-DD format. """ try: return datetime.strptime(date_str, "%Y-%m-%d").date() except ValueError: raise CustomError400(f'Invalid date parameter {date_str}')
def validate_name_exist_for_given_freq(freq, name): possible_names = queries.possible_names_values(freq) if name in possible_names: return True else: msg = f'No such name <{name}> for <{freq}> frequency.' raise CustomError400(message=msg, payload={"allowed": possible_names})
def get_datapoints_response(data, output_format: str): if output_format == 'csv' or not output_format: csv_str = utils.to_csv([row.serialized for row in data]) return Response(response=csv_str, mimetype='text/plain') elif output_format == 'json': return jsonify([row.serialized for row in data]) else: msg = (f"Wrong value for parameter 'format': <{output_format}>." "\n'csv' (default) or 'json' expected") raise CustomError400(msg)
def add_descriptions(descriptions): for decs in descriptions: abbr = decs['abbr'] description_already_exists = DescriptionOperations.get_one(abbr) if description_already_exists: raise CustomError400( f"Error: Description for variable {repr(abbr)} " "already exists.") ru = decs.get('ru') en = decs.get('en') description = Description(abbr, ru, en) db.session.add(description) db.session.commit()
def _find_one(self, allowed_values): """Find entries of *allowed_values* into *tokens*. Pops values found away from *tokens*. """ values_found = [p for p in allowed_values if p in self.tokens] if not values_found: return None elif len(values_found) == 1: x = values_found[0] self._pop(x) return x else: raise CustomError400(values_found)
def test_custom_error_is_initable(self): assert CustomError400(message='text').dict == dict(message='text')
def test_custom_error_is_initable(): assert CustomError400(message='text').to_dict() == \ dict(message='text')
def validate_end_date_after_start_date(start_date, end_date): if end_date < start_date: raise CustomError400('End date must be after start date') else: return True
def make_freq(freq: str): if freq not in ALLOWED_FREQUENCIES: raise CustomError400(f'Frequency <{freq}> is not valid') return freq
def validate_rate_and_agg(rate, agg): if rate and agg: raise CustomError400("Cannot combine rate and aggregation.")
def validate_frequency(freq): if freq not in ALLOWED_FREQUENCIES: raise CustomError400(f'Frequency <{freq}> is not valid')