diff --git a/tom_common/static/tom_common/css/dark.css b/tom_common/static/tom_common/css/dark.css new file mode 100644 index 000000000..f7a017eb4 --- /dev/null +++ b/tom_common/static/tom_common/css/dark.css @@ -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%) +} diff --git a/tom_common/templates/tom_common/base.html b/tom_common/templates/tom_common/base.html index 4837eedb6..dd4184633 100644 --- a/tom_common/templates/tom_common/base.html +++ b/tom_common/templates/tom_common/base.html @@ -9,6 +9,11 @@ {% bootstrap_css %} + + {% dark_mode as dark_mode %} + {% if dark_mode %} + + {% endif %} {% block additional_css %} {% endblock %} diff --git a/tom_common/templates/tom_common/index.html b/tom_common/templates/tom_common/index.html index 93efe1501..295d5a588 100644 --- a/tom_common/templates/tom_common/index.html +++ b/tom_common/templates/tom_common/index.html @@ -46,3 +46,4 @@

Other Resources

{% endblock %} + \ No newline at end of file diff --git a/tom_common/templatetags/tom_common_extras.py b/tom_common/templatetags/tom_common_extras.py index 4e1aa4ecb..8652670ba 100644 --- a/tom_common/templatetags/tom_common_extras.py +++ b/tom_common/templatetags/tom_common_extras.py @@ -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) diff --git a/tom_targets/models.py b/tom_targets/models.py index 3955cc486..136d7d303 100644 --- a/tom_targets/models.py +++ b/tom_targets/models.py @@ -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( diff --git a/tom_targets/templates/tom_targets/target_list.html b/tom_targets/templates/tom_targets/target_list.html index b336e54c4..6fc90584e 100644 --- a/tom_targets/templates/tom_targets/target_list.html +++ b/tom_targets/templates/tom_targets/target_list.html @@ -23,7 +23,7 @@ {% select_target_js %} - {% target_distribution filter.qs %} + {% target_distribution object_list %} {% bootstrap_pagination page_obj extra=request.GET.urlencode %} diff --git a/tom_targets/templatetags/targets_extras.py b/tom_targets/templatetags/targets_extras.py index d5c8a40b1..69cdd3217 100644 --- a/tom_targets/templatetags/targets_extras.py +++ b/tom_targets/templatetags/targets_extras.py @@ -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() @@ -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) @@ -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], @@ -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} diff --git a/tom_targets/views.py b/tom_targets/views.py index 500c1e472..9bf6e89e4 100644 --- a/tom_targets/views.py +++ b/tom_targets/views.py @@ -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