Exemplo n.º 1
0
    def get_assets(self, obj):
        asset = D2dDict([
            ('MediaType', 'Audio'),
            ('Resources', get_instance_resources(obj)),
        ])

        return [asset]
Exemplo n.º 2
0
def get_instance_d2d_resources(instance):
    '''
    Returns a list of Data2Dome resources
    '''
    result = []
    for x in dir(instance.Archive):
        if not isinstance(getattr(instance.Archive, x), ResourceManager):
            continue

        resource = getattr(instance, 'resource_%s' % x)

        # Skip non-file resources (e.g. zoomable):
        if not resource or not isfile(resource.path):
            continue

        result.append(
            D2dDict([
                ('ResourceType', x),
                ('URL', resource.absolute_url),
                ('FileSize', resource.size),
            ]))

        # Checking the size apparently opens the file, we close it to prevent
        # IOError: [Errno 24] Too many open files:
        resource.close()

    return result
Exemplo n.º 3
0
 def get_contact(self, obj):
     return D2dDict([
         ('Address', obj.contact_address),
         ('City', obj.contact_city),
         ('Country', obj.contact_country),
         ('PostalCode', obj.contact_postal_code),
         ('StateProvince', obj.contact_state_province),
     ])
Exemplo n.º 4
0
 def get_paginated_response(self, data):
     '''
     Override to add custom feed values
     '''
     return Response(
         D2dDict([('Count', self.page.paginator.count),
                  ('Next', self.get_next_link()),
                  ('Previous', self.get_previous_link()),
                  ('Collections', data)]))
Exemplo n.º 5
0
 def get_paginated_response(self, data):
     '''
     Override to add "type" to feed
     '''
     return Response(D2dDict([
         ('Type', self.feed_type),
         ('Count', self.page.paginator.count),
         ('Next', self.get_next_link()),
         ('Previous', self.get_previous_link()),
         ('Collections', data)
     ]))
Exemplo n.º 6
0
def get_instance_d2d_resource(instance, resource_name, name, media_type):
    resource = getattr(instance, 'resource_%s' % resource_name)
    if hasattr(instance, 'web_category'):
        web_categories = [c.name for c in instance.web_category.all()]
    else:
        web_categories = []

    # Skip non-file resources (e.g. zoomable):
    if not resource or not isfile(resource.path):
        return {}

    if instance.__class__.__name__ == 'Image':
        # Figure out ProjectionType
        if hasattr(instance, 'type'):
            if instance.type == 'Observation':
                projection_type = 'Observation'
            elif instance.fov_x == 360:
                projection_type = 'Equirectangular'
            elif 'Fulldome' in web_categories:
                projection_type = 'Fulldome'
            else:
                projection_type = 'Tan'
        else:
            projection_type = 'Tan'

        if hasattr(instance, 'fov_x_l'):
            if instance.fov_x_l and instance.fov_x_r:
                fov_x = [instance.fov_x_l, instance.fov_x_r]
            else:
                fov_x = None
            if instance.fov_y_d and instance.fov_y_u:
                fov_y = [instance.fov_y_d, instance.fov_y_u]
            else:
                fov_y = None
        else:
            fov_x = None
            fov_y = None

    elif instance.__class__.__name__ == 'Video':
        fov_x = None
        fov_y = None
        if hasattr(instance, 'web_category'):
            if 'Fulldome' in web_categories:
                projection_type = 'Fulldome'
            else:
                projection_type = 'Tan'
        else:
            projection_type = 'Tan'
    else:
        fov_x = None
        fov_y = None
        projection_type = None

    return D2dDict([
        ('ResourceType', name),
        ('MediaType', media_type),
        ('URL', resource.absolute_url),
        ('FileSize', resource.size),
        ('Dimensions', get_resource_dimension(instance, resource_name)),
        ('HorizontalFOV', fov_x),
        ('VerticalFOV', fov_y),
        ('ProjectionType', projection_type),
        ('Checksum', get_instance_checksum(instance, resource_name)),
    ])
Exemplo n.º 7
0
def get_instance_resources(instance):
    '''
    Return D2D compatible resources list
    '''
    key = 'instance-resource-cache-{}-{}-{}'.format(instance._meta.app_label,
                                                    instance._meta.model_name,
                                                    instance.pk)

    result = cache.get(key)
    if result:
        return result

    if instance.__class__.__name__ == 'Image':
        formats = D2dDict([
            ('Original', ('original', 'Image')),
            ('Large', ('large', 'Image')),
            ('Small', ('screen', 'Image')),
            ('Thumbnail', ('potwmedium', 'Image')),
            ('Icon', ('newsmini', 'Image')),
        ])
    elif instance.__class__.__name__ == 'Video':
        formats = D2dDict()

        # Fulldome videos:
        for fmt in [
                'dome_8kmaster', 'dome_4kmaster', 'dome_2kmaster', 'dome_mov'
        ]:
            if getattr(instance, 'resource_%s' % fmt, None):
                if 'Original' not in formats:
                    formats['Original'] = (fmt, 'Video')
                else:
                    formats['Large'] = (fmt, 'Video')
            if 'Original' in formats and 'Large' in formats:
                break

        if getattr(instance, 'resource_dome_preview', None):
            formats['Preview'] = ('dome_preview', 'Video')

        # Normal videos
        for fmt in ['ultra_hd', 'hd_1080p25_screen', 'ext_highres']:
            if getattr(instance, 'resource_%s' % fmt, None):
                formats['Original'] = (fmt, 'Video')
                break

        for fmt in [
                'hd_and_apple', 'ext_playback', 'medium_podcast', 'old_video'
        ]:
            if getattr(instance, 'resource_%s' % fmt, None):
                formats['Preview'] = (fmt, 'Video')
                break

        formats['Thumbnail'] = ('potwmedium', 'Image')
        formats['Icon'] = ('newsmini', 'Image')
    elif instance.__class__.__name__ == 'Music':
        formats = D2dDict([
            ('Original', ('wav', 'Audio')),
            ('Preview', ('aac', 'Audio')),
        ])
    elif instance.__class__.__name__ == 'Model3d':
        formats = D2dDict([
            ('Original', ('model_3d_c4d', 'Model')),
            ('Obj', ('model_3d_obj', 'Model')),
            ('Thumbnail', ('thumb', 'Image')),
        ])
    else:
        formats = D2dDict()

    resources = [
        get_instance_d2d_resource(instance, fmt, name, media_type)
        for (name, (fmt, media_type)) in formats.items()
    ]

    # Remove empty resources if any:
    result = [r for r in resources if r]

    cache.set(key, result, 60 * 10)

    return result
Exemplo n.º 8
0
 def get_subject(self, obj):
     return D2dDict([
         ('Category', [c.name for c in obj.subject_category.all()]),
         ('Name', [s.name for s in obj.subject_name.all()]),
     ])
Exemplo n.º 9
0
    def get_assets(self, obj):
        '''
        Images return a single Image asset
        '''
        exposures = obj.imageexposure_set.all()

        def get_float(value):
            try:
                return float(value)
            except:  # pylint: disable=bare-except
                return None

        def get_int(value):
            try:
                return int(value)
            except:  # pylint: disable=bare-except
                return None

        def s_to_f(array):
            '''
            Convert an array of strings to an array of float
            Invalid values are returned as None
            '''
            return [get_float(x) for x in array]

        def s_to_i(array):
            '''
            Convert an array of strings to an array of int
            Invalid values are returned as None
            '''
            return [get_int(x) for x in array]

        observation = None
        if obj.type == 'Observation':
            observation = D2dDict([
                ('Facility',
                 [e.facility.name for e in exposures if e.facility]),
                ('Instrument',
                 [e.instrument.name for e in exposures if e.instrument]),
                ('Distance', [obj.distance_ly, obj.distance_z]),
                ('DistanceNotes', obj.distance_notes),
                ('Spectral',
                 D2dDict([
                     ('ColorAssignment',
                      [e.spectral_color_assignment for e in exposures]),
                     ('Band', [e.spectral_bandpass for e in exposures]),
                     ('Bandpass', [e.spectral_bandpass for e in exposures]),
                     ('CentralWavelength',
                      s_to_i(
                          [e.spectral_central_wavelength for e in exposures])),
                 ])),
                ('Temporal',
                 D2dDict([
                     ('StartTime', [e.temporal_start_time for e in exposures]),
                     ('IntegrationTime',
                      s_to_i([e.temporal_integration_time
                              for e in exposures])),
                 ])),
                ('Spatial',
                 D2dDict([
                     ('CoordinateFrame', obj.spatial_coordinate_frame),
                     ('ReferenceValue',
                      s_to_f(obj.get_spatial_reference_value())),
                     ('ReferenceDimension',
                      s_to_f(obj.get_spatial_reference_dimension())),
                     ('ReferencePixel',
                      s_to_f(obj.get_spatial_reference_pixel())),
                     ('Scale', s_to_f(obj.get_spatial_scale())),
                     ('Rotation', get_float(obj.spatial_rotation)),
                     ('CoordsystemProjection',
                      obj.spatial_coordsystem_projection),
                     ('Equinox', obj.spatial_equinox),
                 ])),
            ])

        asset = D2dDict([
            ('MediaType', 'Image'),
            ('Resources', get_instance_resources(obj)),
            ('ObservationData', observation),
        ])

        return [asset]