forked from RedHatProductSecurity/osidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
52 lines (36 loc) · 1.19 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
various helper functions
"""
import re
from django.http import Http404
from osidb.validators import CVE_RE_STR
from .exceptions import APIError
def get_flaw_or_404(pk):
"""get flaw instance or raise HTTP 404 error"""
# import here to prevent cycle
from osidb.models import Flaw
try:
if re.match(CVE_RE_STR, pk):
return Flaw.objects.get(cve_id=pk)
return Flaw.objects.get(pk=pk)
except Flaw.DoesNotExist as e:
raise Http404 from e
# singleton wrapper based on the following docs
# https://pypi.org/project/singleton-decorator/
# simple but has its disadvantage described
# there and if it was a problem replace it
def singleton(cls):
"""singleton class decorator"""
instances = {}
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper
def str2bool(val, param):
"""process textual form of boolean to native boolean"""
if val in ["true", "True", "1"]:
return True
if val in ["false", "False", "0"]:
return False
raise APIError(f'Unexpected boolean value "{val}" for query parameter "{param}"')