def validate(cls, email_address: str): if not isinstance(email_address, str): raise InvalidValueException(cls, 'email address must be a string') pattern = re.compile(cls.VALIDATION_REGEX) if not pattern.match(email_address): raise InvalidValueException(cls, f'invalid email {email_address}')
def create_user(user_id: UUID, name: str, first_name: str, email: str, is_confirmed: bool, date_last_login: datetime, liked_categories: List[CategoryLike], matches: List[Match], seen_recipes: List[Recipe], languages: List[Language]) -> User: if not isinstance(liked_categories, list): raise InvalidValueException( User, 'liked_categories must be a list of Category instances') if not isinstance(matches, list): raise InvalidValueException( User, 'matches must be a list of Recipe instances') if not isinstance(seen_recipes, list): raise InvalidValueException( User, 'seen_recipes must be a list of Recipe instances') if not isinstance(languages, list): raise InvalidValueException( User, 'languages must be a list of Language instances') user_email_object = EMail(email) return User( user_id=user_id, name=name, first_name=first_name, email=user_email_object, is_confirmed=is_confirmed, date_last_login=date_last_login, liked_categories=liked_categories, matches=matches, seen_recipes=seen_recipes, languages=languages, )
def create_vendor(vendor_id: UUID, name: str, description: str, url: str, is_active: bool, recipe_pattern: str, categories_link: str, date_last_crawled: datetime, language: Language, categories: List[Category]) -> Vendor: if not isinstance(name, str): raise InvalidValueException(Vendor, 'name must be a string') if not isinstance(description, str): raise InvalidValueException(Vendor, 'description must be a string') if not isinstance(url, str): raise InvalidValueException(Vendor, 'url must be a string') if not isinstance(language, Language): raise InvalidValueException(Vendor, 'language must be a Language instance') if not isinstance(categories, list): raise InvalidValueException( Vendor, 'categories must be a list of Language instances') vendor_url_object = URL(url=url) return Vendor( vendor_id=vendor_id, name=name, description=description, url=vendor_url_object, is_active=is_active, recipe_pattern=recipe_pattern, categories_link=categories_link, date_last_crawled=date_last_crawled, language=language, categories=categories, )
def validate(cls, url: str): if not isinstance(url, str): raise InvalidValueException(cls, 'url must be a string') parsed_url = urlparse(url) if parsed_url.scheme not in cls.VALID_PROTOCOLS or parsed_url.netloc == '': raise InvalidValueException( cls, f'Invalid {cls.__class__.__name__} {url}')
def create_match_service(match_repo: AbstractMatchRepository, user_repo: AbstractUserRepository, recipe_repo: AbstractRecipeRepository) -> MatchService: if not isinstance(match_repo, AbstractMatchRepository): raise InvalidValueException(MatchService, 'match_repo must be a AbstractMatchRepository') if not isinstance(user_repo, AbstractUserRepository): raise InvalidValueException(MatchService, 'user_repo must be a AbstractUserRepository') if not isinstance(recipe_repo, AbstractRecipeRepository): raise InvalidValueException(MatchService, 'recipe_repo must be a AbstractRecipeRepository') return MatchService(match_repo=match_repo, user_repo=user_repo, recipe_repo=recipe_repo)
def create_language(language_id: UUID, name: str, code: str) -> Language: if not isinstance(name, str): raise InvalidValueException(Language, 'language name must be a string') if not isinstance(code, str): raise InvalidValueException(Language, 'language code must be a string') if len(code) != 2: raise InvalidValueException( Language, 'Language Acronym must have a length of 2') return Language(language_id=language_id, name=name, code=code)
def add_ingredient(self, ingredient: Ingredient): self._check_not_discarded() if not isinstance(ingredient, Ingredient): raise InvalidValueException( self, 'ingredient must be a Ingredient instance') self._ingredients.append(ingredient) self._increment_version()
def create_category_service( category_repo: AbstractCategoryRepository) -> CategoryService: if not isinstance(category_repo, AbstractCategoryRepository): raise InvalidValueException( CategoryService, 'category_repo must be a AbstractCategoryRepository') return CategoryService(repo=category_repo)
def remove_category(self, category: Category): self._check_not_discarded() if not isinstance(category, Category): raise InvalidValueException( self, 'category must be a Category instance') self._categories.remove(category) self._increment_version()
def date_last_crawled(self, value: datetime): self._check_not_discarded() if not isinstance(value, datetime): raise InvalidValueException( self, 'date_last_crawled must be a datetime') self._date_last_crawled = value self._increment_version()
def recipe_pattern(self, pattern: str): self._check_not_discarded() if not isinstance(pattern, str): raise InvalidValueException(self, 'recipe pattern must be a string') self._recipe_pattern = pattern self._increment_version()
def _parse_env_field(cls, config: dict, field: str, field_type: Any) -> Any: """ Parses a single environmental field into it's target type. The environmental field will be read directly from os or via the passed config dictionary. This function uses the following precedence order: 1. env field 2. config dict Parameters: config (dict): dictionary of config fields. can be default values or content of a local.env file field (str): config field to parse field_type (Any): type of field. Can be any type. Returns: parsed_value (Any): parsed value of field """ value = os.getenv(field, config.get(field)) try: if issubclass(field_type, bool): return value == 'True' if value is not None else None if value is None: return None return field_type(value) except ValueError: raise InvalidValueException(cls, f"Config field '{field}' has an invalid value '{value}' for type '{field_type}'")
def remove_category_like(self, user_id: uuid.UUID, category_like_id: uuid.UUID): user = self.get_by_id(user_id) category_like = self._category_like_repo.get_by_id(category_like_id) if category_like.id not in [user_liked.id for user_liked in user.liked_categories]: raise InvalidValueException(raiser=self, message="Category Like does not belong to the specified User") # TODO other exception self._category_like_repo.delete(category_like)
def create_language_service( language_repo: AbstractLanguageRepository) -> LanguageService: if not isinstance(language_repo, AbstractLanguageRepository): raise InvalidValueException( LanguageService, 'language_repo must be a AbstractLanguageRepository') return LanguageService(repo=language_repo)
def validate_vendor_pattern(cls, url: str, vendor_pattern: str): cls.validate(url) regex = re.compile(vendor_pattern) if not regex.search(url): raise InvalidValueException( cls, f"invalid url '{url}' for Vendor pattern {vendor_pattern}")
def categories_link(self, link: str): self._check_not_discarded() if not isinstance(link, str): raise InvalidValueException(self, 'categories_link must be a string') self._categories_link = link self._increment_version()
def create_user_service(user_repo: AbstractUserRepository, recipe_repo: AbstractRecipeRepository, language_repo: AbstractLanguageRepository, category_like_repo: AbstractCategoryLikeRepository, category_repo: AbstractCategoryRepository) -> UserService: if not isinstance(user_repo, AbstractUserRepository): raise InvalidValueException(UserService, 'user_repo must be a AbstractUserRepository') if not isinstance(recipe_repo, AbstractRecipeRepository): raise InvalidValueException(UserService, 'recipe_repo must be a AbstractRecipeRepository') if not isinstance(language_repo, AbstractLanguageRepository): raise InvalidValueException(UserService, 'language_repo must be a AbstractLanguageRepository') if not isinstance(category_like_repo, AbstractCategoryLikeRepository): raise InvalidValueException(UserService, 'category_like_repo must be a AbstractCategoryLikeRepository') if not isinstance(category_repo, AbstractCategoryRepository): raise InvalidValueException(UserService, 'category_repo must be a AbstractCategoryRepository') return UserService(user_repo=user_repo, recipe_repo=recipe_repo, language_repo=language_repo, category_like_repo=category_like_repo, category_repo=category_repo)
def __init__(self, rating_count: int, rating_value: float): if not isinstance(rating_count, int): raise InvalidValueException(self, 'rating count must be an int') if not isinstance(rating_value, float): raise InvalidValueException(self, 'rating value must be a float') if rating_value < 0: raise InvalidValueException(self, 'rating count cannot be less than 0') if not 0 <= rating_value <= 5: raise InvalidValueException( self, 'rating value has to be between 0 and 5') self._rating_count = rating_count self._rating_value = rating_value
def create_category_like_repository( database: PostgresDatabase, create_logger: Callable) -> CategoryLikeRepository: if not isinstance(database, PostgresDatabase): raise InvalidValueException(CategoryLikeRepository, 'database must be a PostgresDatabase') return CategoryLikeRepository(database=database, create_logger=create_logger)
def create_match(match_id: UUID, user, recipe: Recipe, timestamp: datetime, is_seen_by_user: bool, is_active: bool) -> Match: if not user.__class__.__name__ == 'User': # can't import User because of circular imports raise InvalidValueException(Match, 'user must be an User instance') if not isinstance(recipe, Recipe): raise InvalidValueException(Match, 'recipe must be a Recipe instance') if not isinstance(timestamp, datetime): raise InvalidValueException(Match, 'timestamp must be a datetime') return Match( match_id=match_id, user=user, recipe=recipe, timestamp=timestamp, is_seen_by_user=is_seen_by_user, is_active=is_active, )
def validate(self, env_field_name: str): """validates if the value is a valid python logging level""" super().validate(env_field_name) if self._value not in self.VALID_VALUES: raise InvalidValueException( self, "Config field '{field}' has an invalid value '{value}' for type '{field_type}'. Valid values are: {values}" .format(field=env_field_name, value=self._value, field_type=self.__class__.__name__, values=self.VALID_VALUES))
def create_category_like(category_like_id: UUID, user, category: Category, views: int, matches: int) -> CategoryLike: if not user.__class__.__name__ == 'User': # can't import User because of circular imports raise InvalidValueException(CategoryLike, 'user must be a User instance') if not isinstance(category, Category): raise InvalidValueException(CategoryLike, 'category must be a Category instance') if not isinstance(views, int): raise InvalidValueException(CategoryLike, 'views must be an int') if not isinstance(matches, int): raise InvalidValueException(CategoryLike, 'matches must be an int') return CategoryLike( category_like_id=category_like_id, user=user, category=category, views=views, matches=matches, )
def url(self, category_url: URL): self._check_not_discarded() if not isinstance(category_url, URL): raise InvalidValueException(self, 'url must be a URL instance') self._url = category_url self._increment_version()
def text(self, text: str): self._check_not_discarded() if not isinstance(text, str): raise InvalidValueException(self, 'text must be a string') self._text = text self._increment_version()
def create_recipe_service( recipe_repo: AbstractRecipeRepository) -> RecipeService: if not isinstance(recipe_repo, AbstractRecipeRepository): raise InvalidValueException( RecipeService, 'recipe_repo must be a AbstractRecipeRepository') return RecipeService(recipe_repo=recipe_repo)
def name(self, value: str): self._check_not_discarded() if not isinstance(value, str): raise InvalidValueException(self, 'name must be a string') self._name = value self._increment_version()
def create_vendor_service( vendor_repo: AbstractVendorRepository) -> VendorService: if not isinstance(vendor_repo, AbstractVendorRepository): raise InvalidValueException( VendorService, 'vendor_repo must be a AbstractVendorRepository') return VendorService(repo=vendor_repo)
def __init__(self, name: str): if not isinstance(name, str): raise InvalidValueException(self, 'name must be a string') self._name = name
def create_language_repository(database: PostgresDatabase, create_logger: Callable) -> LanguageRepository: if not isinstance(database, PostgresDatabase): raise InvalidValueException(LanguageRepository, 'database must be a PostgresDatabase') return LanguageRepository(database=database, create_logger=create_logger)
def create_category(category_id: UUID, name: str, url: str, vendor) -> Category: if not vendor.__class__.__name__ == 'Vendor': raise InvalidValueException(Category, 'vendor must be a Vendor instance') url_object = URL(url=url) return Category(category_id=category_id, name=name, url=url_object, vendor=vendor)