Skip to content

Commit

Permalink
Merge branch 'release/0.19.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
JackMorganNZ committed May 13, 2022
2 parents 29aba14 + 9d2edce commit edea3d0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 39 deletions.
2 changes: 1 addition & 1 deletion dthm4kaiako/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Configuration for Django system."""

__version__ = "0.19.1"
__version__ = "0.19.2"
__version_info__ = tuple(
[
int(num) if num.isdigit() else num
Expand Down
17 changes: 17 additions & 0 deletions dthm4kaiako/general/management/commands/rebuild_search_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Module for the custom Django rebuild_search_indexes command."""

from django.core import management
from django.db import transaction
from resources.models import Resource
from utils.search_utils import get_search_index_updater


class Command(management.base.BaseCommand):
"""Required command class for the custom Django rebuild_search_indexes command."""

help = "Rebuild search indexes in database."

def handle(self, *args, **options):
"""Automatically called when the command is given."""
for resource in Resource.objects.all():
transaction.on_commit(get_search_index_updater(resource))
4 changes: 4 additions & 0 deletions dthm4kaiako/general/management/commands/update_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ def handle(self, *args, **options):

management.call_command('load_poet_data')
print('POET data loaded.\n')

# Rebuild search indexes as deployment could result in changed indexes.
management.call_command('rebuild_search_indexes')
print('Search indexes rebuilt.')
44 changes: 8 additions & 36 deletions dthm4kaiako/resources/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,18 @@
post_save,
m2m_changed,
)
from django.db.models import Value
from django.contrib.postgres.search import SearchVector

from django.db import transaction
from resources.models import (
Resource,
)
from utils.search_utils import get_search_index_updater


SEARCH_INDEX_UPDATE_MODELS = (
Resource,
)


@receiver(post_save)
@receiver(post_save, sender=Resource)
def on_save(sender, **kwargs):
"""Trigger functions after any model save."""
if issubclass(sender, SEARCH_INDEX_UPDATE_MODELS):
transaction.on_commit(make_updater(kwargs['instance']))
transaction.on_commit(get_search_index_updater(kwargs['instance']))


def on_m2m_changed(sender, **kwargs):
Expand All @@ -43,33 +37,11 @@ def on_m2m_changed(sender, **kwargs):
"""
instance = kwargs['instance']
model = kwargs['model']
if isinstance(instance, SEARCH_INDEX_UPDATE_MODELS):
transaction.on_commit(make_updater(instance))
elif issubclass(model, SEARCH_INDEX_UPDATE_MODELS):
if isinstance(instance, Resource):
transaction.on_commit(get_search_index_updater(instance))
elif issubclass(model, Resource):
for obj in model.objects.filter(pk__in=kwargs['pk_set']):
transaction.on_commit(make_updater(obj))


def make_updater(instance):
"""Return function for updating search index of resource."""
components = instance.index_contents()
pk = instance.pk

def on_commit():
search_vector_list = []
for weight, text in components.items():
search_vector_list.append(
SearchVector(Value(text), weight=weight)
)
search_vectors = search_vector_list[0]
for search_vector in search_vector_list[1:]:
search_vectors += search_vector

instance.__class__.objects.filter(pk=pk).update(
search_vector=search_vectors
)

return on_commit
transaction.on_commit(get_search_index_updater(obj))


# Register specific fields as a lot of m2m relationships exist in this website.
Expand Down
18 changes: 18 additions & 0 deletions dthm4kaiako/users/migrations/0008_alter_user_username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2022-05-10 04:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0007_auto_20220406_1005'),
]

operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(default='user', max_length=50),
),
]
4 changes: 2 additions & 2 deletions dthm4kaiako/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class User(AbstractUser):
"""User of website."""

username = models.CharField(max_length=12, default='user')
username = models.CharField(max_length=50, default='user')
first_name = models.CharField(max_length=50, verbose_name='first name')
last_name = models.CharField(max_length=150, verbose_name='last name')

Expand All @@ -22,7 +22,7 @@ def get_absolute_url(self):

def __str__(self):
"""Name of the user."""
return self.first_name
return f'{self.first_name} {self.last_name}'


class Entity(models.Model):
Expand Down
25 changes: 25 additions & 0 deletions dthm4kaiako/utils/search_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Search utility functions."""

from django.db.models import Value
from django.contrib.postgres.search import SearchVector


def concat_field_values(*args):
"""Return string of field values for search indexing.
Expand All @@ -16,3 +19,25 @@ def concat_field_values(*args):
for field in instance:
field_names.append(str(field))
return ' '.join(field_names)


def get_search_index_updater(instance):
"""Return function for updating search index of instance."""
components = instance.index_contents()
pk = instance.pk

def on_commit():
search_vector_list = []
for weight, text in components.items():
search_vector_list.append(
SearchVector(Value(text), weight=weight)
)
search_vectors = search_vector_list[0]
for search_vector in search_vector_list[1:]:
search_vectors += search_vector

instance.__class__.objects.filter(pk=pk).update(
search_vector=search_vectors
)
print(f'Rebuilt index for {instance}')
return on_commit

0 comments on commit edea3d0

Please sign in to comment.