Skip to content

Instantly share code, notes, and snippets.

@santiagomalter
Created February 28, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santiagomalter/3f0fc83a3b6fe3ad948e27776033fceb to your computer and use it in GitHub Desktop.
Save santiagomalter/3f0fc83a3b6fe3ad948e27776033fceb to your computer and use it in GitHub Desktop.
Matching score calculation
# Same role family
queryset = queryset.annotate(
same_role_family=Case(
When(target_role_families__in=[
user.role_family], then=Value(30)),
default=Value(0),
output_field=IntegerField()
)
)
# Same role category
queryset = queryset.annotate(
same_role_category=Case(
When(target_role_categories__in=[
user.role_category], then=Value(30)),
default=Value(0),
output_field=IntegerField()
)
)
# Same branch
queryset = queryset.annotate(
same_branch=Case(
When(
created_by__department__entity__branch=user.department.entity.branch, then=Value(10)),
default=Value(0),
output_field=IntegerField()
)
)
# Same entity
queryset = queryset.annotate(
same_entity=Case(
When(created_by__department__entity=user.department.entity,
then=Value(10)),
default=Value(0),
output_field=IntegerField()
)
)
# Same department
queryset = queryset.annotate(
same_department=Case(
When(created_by__department=user.department, then=Value(10)),
default=Value(0),
output_field=IntegerField()
)
)
# Match category
queryset = queryset.annotate(
match_category=Case(
When(category__in=user.interests.all(), then=Value(50)),
default=Value(0),
output_field=IntegerField()
)
)
# Sum
queryset = queryset.annotate(
relevancy_score=F('same_role_family') + F('same_role_category') +
F('same_branch') + F('same_entity') +
F('same_department') + F('match_category')
)
queryset = queryset.order_by('-relevancy_score')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment