2022-08-26 02:02:16 +00:00
|
|
|
from frog_api.exceptions import (
|
|
|
|
InvalidAPIKeyException,
|
|
|
|
NonexistentCategoryException,
|
|
|
|
NonexistentProjectException,
|
|
|
|
NonexistentVersionException,
|
|
|
|
)
|
|
|
|
from frog_api.models import Category, Project, Version
|
2022-08-28 12:38:14 +00:00
|
|
|
from frogress.settings import ULTIMATE_API_KEY
|
2022-08-26 02:02:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_project(slug: str) -> Project:
|
|
|
|
ret = Project.objects.filter(slug=slug).first()
|
|
|
|
if not ret:
|
|
|
|
raise NonexistentProjectException(slug)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def get_version(slug: str, project: Project) -> Version:
|
|
|
|
ret = Version.objects.filter(slug=slug, project=project).first()
|
|
|
|
if not ret:
|
|
|
|
raise NonexistentVersionException(project.slug, slug)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def get_category(slug: str, version: Version) -> Category:
|
|
|
|
ret = Category.objects.filter(slug=slug, version=version).first()
|
|
|
|
if not ret:
|
|
|
|
raise NonexistentCategoryException(version.project.slug, version.slug, slug)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2022-08-28 13:59:52 +00:00
|
|
|
def validate_ultimate_api_key(key: str) -> bool:
|
|
|
|
if key == ULTIMATE_API_KEY:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
raise InvalidAPIKeyException()
|
|
|
|
|
|
|
|
|
2022-08-26 02:02:16 +00:00
|
|
|
def validate_api_key(key: str, project: Project) -> bool:
|
2022-08-28 12:38:14 +00:00
|
|
|
if key == ULTIMATE_API_KEY or key == project.auth_key:
|
|
|
|
return True
|
|
|
|
else:
|
2022-08-26 02:02:16 +00:00
|
|
|
raise InvalidAPIKeyException()
|