From fce0c3b04536e3ac11786d91eebb86d22596c08e Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Mon, 20 Apr 2020 11:41:13 +0200 Subject: [PATCH] [common][utils] Move annotation method here --- beat/web/common/utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/beat/web/common/utils.py b/beat/web/common/utils.py index ffe47af7a..127378d8e 100644 --- a/beat/web/common/utils.py +++ b/beat/web/common/utils.py @@ -31,6 +31,8 @@ Reusable help functions from django.core.exceptions import ValidationError from django.utils.encoding import force_text from django.utils import six +from django.db.models import CharField, Value as V +from django.db.models.functions import Concat from docutils import utils from docutils.nodes import Element @@ -145,3 +147,21 @@ def py3_cmp(a, b): https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons """ return (a > b) - (a < b) + + +def annotate_full_name(db_class): + """ + Annotate a query with the asset full name so that it can be more easily + filtered. + """ + + return db_class.objects.annotate( + full_name=Concat( + "author__username", + V("/"), + "name", + V("/"), + "version", + output_field=CharField(), + ) + ) -- GitLab