2023-04-01 11:37:49 -04:00
|
|
|
from dal import autocomplete
|
|
|
|
from django.db.models.functions import Concat
|
|
|
|
from django.db.models import Q, Value, CharField
|
|
|
|
|
|
|
|
from .models import CertificationVersion
|
|
|
|
|
|
|
|
|
|
|
|
class CertificationVersionAutocomplete(autocomplete.Select2QuerySetView):
|
|
|
|
def get_queryset(self):
|
2023-04-10 18:06:08 -04:00
|
|
|
if not self.request.user.has_perm("paperwork.view_certificationversion"):
|
2023-04-01 11:37:49 -04:00
|
|
|
return CertificationVersion.objects.none()
|
|
|
|
|
|
|
|
qs = CertificationVersion.objects.alias(
|
|
|
|
version_str=Concat(
|
|
|
|
"major",
|
|
|
|
Value("."),
|
|
|
|
"minor",
|
|
|
|
Value("."),
|
|
|
|
"patch",
|
|
|
|
output_field=CharField(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
certification_definition = self.forwarded.get("certification_definition", None)
|
|
|
|
|
|
|
|
if certification_definition:
|
|
|
|
qs = qs.filter(definition=certification_definition)
|
|
|
|
|
|
|
|
if self.q:
|
|
|
|
qs = qs.filter(
|
|
|
|
Q(version_str__istartswith=self.q)
|
|
|
|
| Q(prerelease__istartswith=self.q)
|
|
|
|
| Q(approval_date__istartswith=self.q)
|
|
|
|
)
|
|
|
|
|
|
|
|
return qs
|
|
|
|
|
|
|
|
def get_result_label(self, result: CertificationVersion) -> str:
|
|
|
|
return str(result.semantic_version())
|
|
|
|
|
|
|
|
def get_selected_result_label(self, result: CertificationVersion) -> str:
|
|
|
|
return str(result)
|