Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/slow target list #619

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tom_common/static/tom_common/css/dark.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
body {
background-color: black;
color: white;
}

.table {
color: #f2f7fc;
}

.card {
background-color: #ffffff3d;
}

.card-header {
background-color: rgb(0 0 0 / 45%)
}
5 changes: 5 additions & 0 deletions tom_common/templates/tom_common/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<!-- Bootstrap CSS -->
{% bootstrap_css %}
<link rel="stylesheet" href="{% static 'tom_common/css/main.css' %}">
<!-- Implement Dark Mode CSS if indicated in Settings -->
{% dark_mode as dark_mode %}
{% if dark_mode %}
<link rel="stylesheet" href="{% static 'tom_common/css/dark.css' %}">
{% endif %}
{% block additional_css %}
{% endblock %}
<link rel="icon" type="image/x-icon" href="{% static 'tom_common/img/favicon-32.ico' %}" sizes="32x32" />
Expand Down
1 change: 1 addition & 0 deletions tom_common/templates/tom_common/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ <h3>Other Resources</h3>
</div>
</div>
{% endblock %}
</div>
6 changes: 6 additions & 0 deletions tom_common/templatetags/tom_common_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ def truncate_number(value):
@register.simple_tag
def tom_name():
return getattr(settings, 'TOM_NAME', 'TOM Toolkit')


@register.simple_tag
def dark_mode():
"""Check for Dark Mode in Settings"""
return getattr(settings, 'DARK_MODE', False)
2 changes: 1 addition & 1 deletion tom_targets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Target(models.Model):
max_length=100, choices=TARGET_TYPES, verbose_name='Target Type', help_text='The type of this target.'
)
created = models.DateTimeField(
auto_now_add=True, verbose_name='Time Created',
auto_now_add=True, verbose_name='Time Created', db_index=True,
help_text='The time which this target was created in the TOM database.'
)
modified = models.DateTimeField(
Expand Down
2 changes: 1 addition & 1 deletion tom_targets/templates/tom_targets/target_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
</div>
{% select_target_js %}
{% target_distribution filter.qs %}
{% target_distribution object_list %}
{% bootstrap_pagination page_obj extra=request.GET.urlencode %}
<label id="displaySelected"></label>
<button id="optionSelectAll" type="button" class="btn btn-link" onClick="select_all({{ target_count }})"></button>
Expand Down
33 changes: 30 additions & 3 deletions tom_targets/templatetags/targets_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
from django.conf import settings
from django.db.models import Q
from guardian.shortcuts import get_objects_for_user
from guardian.models import GroupObjectPermission
from guardian.core import ObjectPermissionChecker
import numpy as np
from plotly import offline
from plotly import graph_objs as go

from tom_observations.utils import get_sidereal_visibility
from tom_targets.models import Target, TargetExtra, TargetList
from tom_targets.models import TargetExtra, TargetList, Target
from tom_targets.forms import TargetVisibilityForm

register = template.Library()
Expand All @@ -24,8 +26,28 @@ def recent_targets(context, limit=10):
"""
Displays a list of the most recently created targets in the TOM up to the given limit, or 10 if not specified.
"""
# Get User and group permissions for user
user = context['request'].user
return {'targets': get_objects_for_user(user, 'tom_targets.view_target').order_by('-created')[:limit]}
groups = user.groups.all()
group_permissions = GroupObjectPermission.objects.filter(group__in=groups, permission__codename='view_target')

# Build Query for the most recently created objects
target_query = Target.objects.order_by('-created').prefetch_related()[:limit]

# Build permission checker and check if user has permission to view each target
checker = ObjectPermissionChecker(user)
checker.prefetch_perms(target_query)
targets = [target for target in target_query if checker.has_perm('view_target', target)]

if targets:
# If any of these targets are viewable, display them
return {'targets': targets}
elif group_permissions.count():
# Otherwise, if user has permission to view ANY target, find them. (EXPENSIVE)
return {'targets': get_objects_for_user(user, 'tom_targets.view_target').order_by('-created')[:limit]}
else:
# Return empty list if user has no permissions.
return {'targets': []}


@register.inclusion_tag('tom_targets/partials/recently_updated_targets.html', takes_context=True)
Expand Down Expand Up @@ -225,7 +247,8 @@ def target_distribution(targets):
"""
Displays a plot showing on a map the locations of all sidereal targets in the TOM.
"""
locations = targets.filter(type=Target.SIDEREAL).values_list('ra', 'dec', 'name')
locations = targets.values_list('ra', 'dec', 'name')

data = [
dict(
lon=[location[0] for location in locations],
Expand Down Expand Up @@ -315,4 +338,8 @@ def target_table(targets):
Returns a partial for a table of targets, used in the target_list.html template
by default
"""
# Prefetch related tables to speed up target List Load
related_tables = ['aliases', 'dataproduct_set', 'observationrecord_set']
for table in related_tables:
targets = targets.prefetch_related(table)
return {'targets': targets}
2 changes: 1 addition & 1 deletion tom_targets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TargetListView(PermissionListMixin, FilterView):
View for listing targets in the TOM. Only shows targets that the user is authorized to view. Requires authorization.
"""
template_name = 'tom_targets/target_list.html'
paginate_by = 25
paginate_by = 10
strict = False
model = Target
filterset_class = TargetFilter
Expand Down