Python django.db.models.ExpressionWrapper() Examples

The following are 13 code examples for showing how to use django.db.models.ExpressionWrapper(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.db.models , or try the search function .

Example 1
Project: seqr   Author: macarthur-lab   File: awesomebar_api.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def _get_matching_omim(query, projects):
    """Returns OMIM records that match the given query string"""
    records = Omim.objects.filter(
        Q(phenotype_mim_number__icontains=query) | Q(phenotype_description__icontains=query)
    ).filter(phenotype_mim_number__isnull=False).annotate(
        description_start=ExpressionWrapper(Q(phenotype_description__istartswith=query), output_field=BooleanField()),
        mim_number_start=ExpressionWrapper(Q(phenotype_mim_number__istartswith=query), output_field=BooleanField()),
    ).only('phenotype_mim_number', 'phenotype_description').order_by(
        '-description_start', '-mim_number_start', 'phenotype_description').distinct()[:MAX_RESULTS_PER_CATEGORY]
    result = []
    for record in records:
        result.append({
            'key': record.phenotype_mim_number,
            'title': record.phenotype_description,
            'description': '({})'.format(record.phenotype_mim_number) if record.phenotype_mim_number else None,
        })

    return result 
Example 2
Project: seqr   Author: macarthur-lab   File: awesomebar_api.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def _get_matching_hpo_terms(query, projects):
    """Returns OMIM records that match the given query string"""
    records = HumanPhenotypeOntology.objects.filter(
        Q(hpo_id__icontains=query) | Q(name__icontains=query)
    ).annotate(
        name_start=ExpressionWrapper(Q(name__istartswith=query), output_field=BooleanField()),
        hpo_id_start=ExpressionWrapper(Q(hpo_id__istartswith=query), output_field=BooleanField()),
    ).only('hpo_id', 'name', 'category_id').order_by(
        '-name_start', '-hpo_id_start', 'name').distinct()[:MAX_RESULTS_PER_CATEGORY]
    result = []
    for record in records:
        result.append({
            'key': record.hpo_id,
            'title': record.name,
            'description': '({})'.format(record.hpo_id),
            'category': record.category_id,
        })

    return result 
Example 3
Project: hawthorne   Author: indietyp   File: views.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def punishments(request):
  name = request.resolver_match.url_name

  if "ban" in name:
    mode = "ban"
  elif "mute" in name:
    mode = "mute"
  elif "gag" in name:
    mode = "gag"

  Punishment.objects.annotate(completion=ExpressionWrapper(F('created_at') + F('length'),
                                                           output_field=DateTimeField()))\
                    .filter(completion__lte=timezone.now(),
                            resolved=False,
                            length__isnull=False).update(resolved=True)
  servers = Server.objects.all()

  return render(request, 'pages/punishments/general.pug', {'mode': mode,
                                                           'servers': servers}) 
Example 4
Project: django-healthchecks   Author: mvantellingen   File: models.py    License: MIT License 5 votes vote down vote up
def annotate_expires_at(self):
        """Add an ``expires_at`` field to the queryset results."""
        return self.annotate(
            expires_at=ExpressionWrapper(
                (F('last_beat') + F('timeout')),
                output_field=EXPIRES_COLUMN_TYPE
            )
        ) 
Example 5
Project: koku   Author: project-koku   File: helpers.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _populate_storage_daily_summary_table(self):
        """Populate the daily summary table."""
        included_fields = [
            "usage_start",
            "usage_end",
            "namespace",
            "report_period_id",
            "pod",
            "node",
            "persistentvolumeclaim",
            "persistentvolume",
            "storageclass",
            "cluster_id",
            "cluster_alias",
        ]
        annotations = {
            "volume_labels": Coalesce(F("persistentvolume_labels"), F("persistentvolumeclaim_labels")),
            "persistentvolumeclaim_capacity_gigabyte": ExpressionWrapper(
                F("persistentvolumeclaim_capacity_bytes") * math.pow(2, -30), output_field=DecimalField()
            ),
            "persistentvolumeclaim_capacity_gigabyte_months": ExpressionWrapper(
                F("persistentvolumeclaim_capacity_byte_seconds") / 86400 * 30 * math.pow(2, -30),
                output_field=DecimalField(),
            ),
            "volume_request_storage_gigabyte_months": ExpressionWrapper(
                F("volume_request_storage_byte_seconds") / 86400 * 30 * math.pow(2, -30), output_field=DecimalField()
            ),
            "persistentvolumeclaim_usage_gigabyte_months": ExpressionWrapper(
                F("persistentvolumeclaim_usage_byte_seconds") / 86400 * 30 * math.pow(2, -30),
                output_field=DecimalField(),
            ),
            "data_source": Value("Storage", output_field=CharField()),
        }

        entries = OCPStorageLineItemDaily.objects.values(*included_fields).annotate(**annotations)

        for entry in entries:
            summary = OCPUsageLineItemDailySummary(**entry)
            summary.save() 
Example 6
Project: tom_base   Author: TOMToolkit   File: filters.py    License: GNU General Public License v3.0 5 votes vote down vote up
def filter_cone_search(self, queryset, name, value):
        """
        Executes cone search by annotating each target with separation distance from either the specified RA/Dec or
        the RA/Dec of the specified target. Formula is from Wikipedia: https://en.wikipedia.org/wiki/Angular_distance
        The result is converted to radians.

        Cone search is preceded by a square search to reduce the search radius before annotating the queryset, in
        order to make the query faster.
        """
        if name == 'cone_search':
            ra, dec, radius = value.split(',')
        elif name == 'target_cone_search':
            target_name, radius = value.split(',')
            targets = Target.objects.filter(
                Q(name__icontains=target_name) | Q(aliases__name__icontains=target_name)
            ).distinct()
            if len(targets) == 1:
                ra = targets[0].ra
                dec = targets[0].dec
            else:
                return queryset.filter(name=None)

        ra = float(ra)
        dec = float(dec)

        double_radius = float(radius) * 2
        queryset = queryset.filter(ra__gte=ra - double_radius, ra__lte=ra + double_radius,
                                   dec__gte=dec - double_radius, dec__lte=dec + double_radius)

        separation = ExpressionWrapper(
            180 * ACos(
                (Sin(radians(dec)) * Sin(Radians('dec'))) +
                (Cos(radians(dec)) * Cos(Radians('dec')) * Cos(radians(ra) - Radians('ra')))
            ) / Pi(), FloatField()
        )

        return queryset.annotate(separation=separation).filter(separation__lte=radius)

    # hide target grouping list if user not logged in 
Example 7
Project: mangaki   Author: mangaki   File: models.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def pearls(self):
        return (self.exclude(nb_likes=0)
                    .annotate(
                        dislike_rate=ExpressionWrapper(
                            Cast(F('nb_dislikes'), FloatField()) / F('nb_likes'), output_field=FloatField())
                    )
                    .filter(nb_ratings__gte=PEARLS_MIN_RATINGS, nb_ratings__lte=PEARLS_MAX_RATINGS, dislike_rate__lte=PEARLS_MAX_DISLIKE_RATE)
                    .order_by('dislike_rate')) 
Example 8
Project: online-judge   Author: DMOJ   File: problems.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def hot_problems(duration, limit):
    cache_key = 'hot_problems:%d:%d' % (duration.total_seconds(), limit)
    qs = cache.get(cache_key)
    if qs is None:
        qs = Problem.get_public_problems() \
                    .filter(submission__date__gt=timezone.now() - duration, points__gt=3, points__lt=25)
        qs0 = qs.annotate(k=Count('submission__user', distinct=True)).order_by('-k').values_list('k', flat=True)

        if not qs0:
            return []
        # make this an aggregate
        mx = float(qs0[0])

        qs = qs.annotate(unique_user_count=Count('submission__user', distinct=True))
        # fix braindamage in excluding CE
        qs = qs.annotate(submission_volume=Count(Case(
            When(submission__result='AC', then=1),
            When(submission__result='WA', then=1),
            When(submission__result='IR', then=1),
            When(submission__result='RTE', then=1),
            When(submission__result='TLE', then=1),
            When(submission__result='OLE', then=1),
            output_field=FloatField(),
        )))
        qs = qs.annotate(ac_volume=Count(Case(
            When(submission__result='AC', then=1),
            output_field=FloatField(),
        )))
        qs = qs.filter(unique_user_count__gt=max(mx / 3.0, 1))

        qs = qs.annotate(ordering=ExpressionWrapper(
            0.5 * F('points') * (0.4 * F('ac_volume') / F('submission_volume') + 0.6 * F('ac_rate')) +
            100 * e ** (F('unique_user_count') / mx), output_field=FloatField(),
        )).order_by('-ordering').defer('description')[:limit]

        cache.set(cache_key, qs, 900)
    return qs 
Example 9
Project: django-pivot   Author: martsberger   File: test.py    License: MIT License 5 votes vote down vote up
def test_pivot_aggregate(self):
        shirt_sales = ShirtSales.objects.all()

        data = ExpressionWrapper(F('units') * F('price'), output_field=DecimalField())
        pt = pivot(ShirtSales, 'store__region__name', 'shipped', data, Avg, default=0)

        for row in pt:
            region_name = row['store__region__name']
            for dt in (key for key in row.keys() if key != 'store__region__name'):
                spends = [ss.units * ss.price for ss in shirt_sales if force_text(ss.shipped) == force_text(dt) and ss.store.region.name == region_name]
                avg = sum(spends) / len(spends) if spends else 0
                self.assertAlmostEqual(row[dt], float(avg), places=4) 
Example 10
Project: scirius   Author: StamusNetworks   File: rest_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _get_hits_order(self, request, order):
        try:
            result = ESTopRules(request).get(count=Rule.objects.count(), order=order)
        except ESError:
            queryset = Rule.objects.order_by('sid')
            queryset = queryset.annotate(hits=models.Value(0, output_field=models.IntegerField()))
            queryset = queryset.annotate(hits=models.ExpressionWrapper(models.Value(0), output_field=models.IntegerField()))
            return queryset.values_list('sid', 'hits')

        result = map(lambda x: (x['key'], x['doc_count']), result)
        return result 
Example 11
Project: koku   Author: project-koku   File: helpers.py    License: GNU Affero General Public License v3.0 4 votes vote down vote up
def _populate_daily_summary_table(self):
        """Populate the daily summary table."""
        OCPUsageLineItemDailySummary.objects.all().delete()
        included_fields = [
            "usage_start",
            "usage_end",
            "namespace",
            "report_period_id",
            "pod",
            "node",
            "cluster_id",
            "cluster_alias",
            "node_capacity_cpu_cores",
            "pod_labels",
        ]
        annotations = {
            "pod_usage_cpu_core_hours": F("pod_usage_cpu_core_seconds") / 3600,
            "pod_request_cpu_core_hours": Sum(
                ExpressionWrapper(F("pod_request_cpu_core_seconds") / 3600, output_field=DecimalField())
            ),
            "pod_limit_cpu_core_hours": Sum(
                ExpressionWrapper(F("pod_limit_cpu_core_seconds") / 3600, output_field=DecimalField())
            ),
            "pod_usage_memory_gigabyte_hours": Sum(
                ExpressionWrapper(F("pod_usage_memory_byte_seconds") / 3600, output_field=DecimalField())
            )
            * math.pow(2, -30),
            "pod_request_memory_gigabyte_hours": Sum(
                ExpressionWrapper(F("pod_request_memory_byte_seconds") / 3600, output_field=DecimalField())
            )
            * math.pow(2, -30),
            "pod_limit_memory_gigabyte_hours": ExpressionWrapper(
                F("pod_limit_memory_byte_seconds") / 3600, output_field=DecimalField()
            )
            * math.pow(2, -30),
            "node_capacity_cpu_core_hours": F("node_capacity_cpu_core_seconds") / 3600,
            "node_capacity_memory_gigabytes": F("node_capacity_memory_bytes") * math.pow(2, -30),
            "node_capacity_memory_gigabyte_hours": ExpressionWrapper(
                F("node_capacity_memory_byte_seconds") / 3600, output_field=DecimalField()
            )
            * math.pow(2, -30),
            "cluster_capacity_cpu_core_hours": F("cluster_capacity_cpu_core_seconds") / 3600,
            "cluster_capacity_memory_gigabyte_hours": ExpressionWrapper(
                F("cluster_capacity_memory_byte_seconds") / 3600, output_field=DecimalField()
            )
            * math.pow(2, -30),
            "total_capacity_cpu_core_hours": F("cluster_capacity_cpu_core_seconds") / 3600 * 2,
            "total_capacity_memory_gigabyte_hours": ExpressionWrapper(
                F("cluster_capacity_memory_byte_seconds") / 3600 * 2, output_field=DecimalField()
            )
            * math.pow(2, -30),
            "data_source": Value("Pod", output_field=CharField()),
        }

        entries = OCPUsageLineItemDaily.objects.values(*included_fields).annotate(**annotations)

        for entry in entries:
            summary = OCPUsageLineItemDailySummary(**entry)
            summary.save() 
Example 12
Project: pythonic-news   Author: sebst   File: views.py    License: GNU Affero General Public License v3.0 4 votes vote down vote up
def _front_page(paging_size=settings.PAGING_SIZE, page=0, add_filter={}, add_q=[], as_of=None, days_back=50):
    # TODO: weighting https://medium.com/hacking-and-gonzo/how-hacker-news-ranking-algorithm-works-1d9b0cf2c08d
    # (P-1) / (T+2)^G
    if as_of is None:
        now = timezone.now()
    else:
        now = as_of
    if connection.vendor == 'postgresql':
        now_value = Value(now, output_field=fields.DateTimeField())
        submission_age_float = ExpressionWrapper(  ( now_value - F('created_at')), output_field=fields.DurationField())
        submission_age_hours = ExpressionWrapper(Extract(F('tf'), 'epoch') / 60 / 60 + 2.1 , output_field=fields.FloatField())
        real_p = ExpressionWrapper(F('points') - 1, output_field=fields.FloatField())
        formula = ExpressionWrapper(   F('p') / ( Power(F('tfh'), F('g'))  +0.001)   , output_field=fields.FloatField())
        return Story.objects.select_related('user')\
                .filter(duplicate_of__isnull=True)\
                .filter(points__gte=1) \
                .filter(created_at__gte=now - datetime.timedelta(days=days_back)) \
                .filter(created_at__lte=now) \
                .filter(**add_filter) \
                .annotate(tf=submission_age_float) \
                .annotate(tfh=submission_age_hours) \
                .annotate(p=real_p) \
                .annotate(g=Value(1.8, output_field=fields.FloatField())) \
                .annotate(formula=formula) \
                .order_by('-formula')[(page*paging_size):(page+1)*(paging_size)]
    elif connection.vendor == 'sqlite':
        now_value = Value(now, output_field=fields.DateTimeField())
        submission_age_float = ExpressionWrapper(  ( now_value - F('created_at')), output_field=fields.FloatField())
        submission_age_hours = ExpressionWrapper(F('tf') / 60 / 60 / 1000000 + 2.1 , output_field=fields.FloatField())
        real_p = ExpressionWrapper(F('points') - 1, output_field=fields.FloatField())
        formula = ExpressionWrapper(   F('p') / ( Power(F('tfh'), F('g'))  +0.001)   , output_field=fields.FloatField())
        return Story.objects.select_related('user')\
                .filter(duplicate_of__isnull=True)\
                .filter(points__gte=1) \
                .filter(created_at__gte=now - datetime.timedelta(days=days_back)) \
                .filter(created_at__lte=now) \
                .filter(**add_filter) \
                .annotate(tf=submission_age_float) \
                .annotate(tfh=submission_age_hours) \
                .annotate(p=real_p) \
                .annotate(g=Value(1.8, output_field=fields.FloatField())) \
                .annotate(formula=formula) \
                .order_by('-formula')[(page*paging_size):(page+1)*(paging_size)]
    else: 
        raise NotImplementedError("No frontpage magic for database engine %s implemented"%(connection.vendor)) 
Example 13
Project: helfertool   Author: helfertool   File: overview.py    License: GNU Affero General Public License v3.0 4 votes vote down vote up
def overview(request, event_url_name):
    event = get_object_or_404(Event, url_name=event_url_name)

    # permission
    if not event.is_admin(request.user):
        return nopermission(request)

    num_helpers = event.helper_set.count()

    num_coordinators = event.all_coordinators.count()

    num_vegetarians = event.helper_set.filter(vegetarian=True).count()

    num_shift_slots = Shift.objects.filter(job__event=event).aggregate(
        Sum('number'))['number__sum']

    empty_slots_expr = ExpressionWrapper(F('number') - F('num_helpers'),
                                         output_field=fields.IntegerField())
    num_empty_shift_slots = Shift.objects.filter(job__event=event) \
        .annotate(num_helpers=Count('helper')) \
        .annotate(empty_slots=empty_slots_expr) \
        .aggregate(Sum('empty_slots'))['empty_slots__sum']

    total_duration = ExpressionWrapper((F('end') - F('begin')) * F('number'),
                                       output_field=fields.DurationField())
    try:
        hours_total = Shift.objects.filter(job__event=event) \
                           .annotate(duration=total_duration) \
                           .aggregate(Sum('duration'))['duration__sum']
    except (OperationalError, OverflowError):
        hours_total = None
    except Exception as e:
        # handle psycopg2.DataError without importing psycopg2
        # happens on overflow with postgresql
        if 'DataError' in str(e.__class__):
            hours_total = None
        else:
            raise e

    # render
    context = {'event': event,
               'num_helpers': num_helpers,
               'num_coordinators': num_coordinators,
               'num_vegetarians': num_vegetarians,
               'num_shift_slots': num_shift_slots,
               'num_empty_shift_slots': num_empty_shift_slots,
               'hours_total': hours_total}
    return render(request, 'statistic/overview.html', context)