cmsmanage/paperwork/autocomplete_views.py

43 lines
1.3 KiB
Python

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):
if not self.request.user.has_perm("paperwork.view_certificationversion"):
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)