Repository: TOMToolkit/tom_base

Issue:
ObservationRecordDetailView crashes when trying to load latest image
**Describe the bug**
After saving a FITS file data product from an observation, the detail page for that observation will no longer load.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to an observation record (URL ending in /observations/55/, for example)
2. Check the box next to a FITS file, and click on "save selected"
3. When the /observations/55/ page reloads, it will display an `AttributeError: 'DataProduct' object has no attribute 'get_image_data'`

**Expected behavior**
The observation record detail page should load as usual (maybe with an image thumbnail? I'm not sure what is supposed to happen.)

**Desktop (please complete the following information):**
- OS: macOS 11.2.3
- Browser: Safari
- Version: 14.0.3
- Python Version: 3.7.7

**Additional context**
Here is the full traceback:
```
ERROR:django.request:Internal Server Error: /observations/55/
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 183, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 339, in process_exception_by_middleware
    response = middleware_method(request, exception)
  File "/usr/local/lib/python3.7/site-packages/tom_common/middleware.py", line 29, in process_exception
    raise exception
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/generic/detail.py", line 107, in get
    context = self.get_context_data(object=self.object)
  File "/usr/local/lib/python3.7/site-packages/tom_observations/views.py", line 465, in get_context_data
    context['image'] = newest_image.get_image_data()
AttributeError: 'DataProduct' object has no attribute 'get_image_data'
```

**Note**
I think this is just because `get_image_data` is not defined. However I think there's another problem, in that any file ending in `.fits` is assumed to be an image file. In my case, I was saving a FITS spectrum, so the thumbnail would have been useless anyway.


Discussion:
Ran into this bug again today!

Resolving patch:
diff --git a/tom_observations/views.py b/tom_observations/views.py
--- a/tom_observations/views.py
+++ b/tom_observations/views.py
@@ -24,6 +24,7 @@
 from tom_common.hints import add_hint
 from tom_common.mixins import Raise403PermissionRequiredMixin
 from tom_dataproducts.forms import AddProductToGroupForm, DataProductUploadForm
+from tom_dataproducts.models import is_fits_image_file
 from tom_observations.cadence import CadenceForm, get_cadence_strategy
 from tom_observations.facility import get_service_class, get_service_classes
 from tom_observations.facility import BaseManualObservationFacility
@@ -460,9 +461,9 @@ def get_context_data(self, *args, **kwargs):
         newest_image = None
         for data_product in context['data_products']['saved']:
             newest_image = data_product if (not newest_image or data_product.modified > newest_image.modified) and \
-                data_product.get_file_extension() == '.fits' else newest_image
+                is_fits_image_file(data_product.data.file) else newest_image
         if newest_image:
-            context['image'] = newest_image.get_image_data()
+            context['image'] = newest_image.get_preview()
         data_product_upload_form = DataProductUploadForm(
             initial={
                 'observation_record': self.get_object(),


Test patch: