Exemplo n.º 1
0
def get_metadata(request):
    """
    Get metadata for the given items
    """
    item_list = request.POST.getlist('items')
    metadata_object = request.POST.get('obj', 'original')
    metadata_view = request.POST.get('advanced', False)
    workspace = request.session.get('workspace', None) 
    user = User.objects.get(pk=request.session['_auth_user_id'])
    default_language = get_metadata_default_language(user, workspace)
    if metadata_view == 'true':
        metadata_view = True
    else:
        metadata_view = False

    try:
        item_media_types = set(Item.objects.filter(pk__in=item_list).values_list('type__name', flat=True))
    
        if metadata_view:
            form_list = _advanced_metadata_view(item_list, workspace, default_language, metadata_object, item_media_types)
        else:
            form_list = _simple_metadata_view(item_list, workspace, default_language, metadata_object, item_media_types)
        form_dict = {'rows': form_list}
    except Exception, ex:
        logger.exception(ex)
        form_dict = {'rows': []}
Exemplo n.º 2
0
    def _read_xmp_features(self, features):
        xpath = re.compile(r'(?P<prefix>\w+):(?P<property>\w+)(?P<array_index>\[\d+\]){,1}')
        ctype = ContentType.objects.get_for_model(self.item)
        ctype_component = ContentType.objects.get_for_model(self.component)

        user = self.item.uploaded_by()
        metadata_default_language = get_metadata_default_language(user)

        metadata_dict = {}
        metadata_list = []
        delete_list = []

        log.debug('READ XMP FEATURES')

        if not isinstance(features, dict):
            item.state = 1  
            item.save()
            return [], []

        for feature in features.keys():
            try:
                namespace_obj = XMPNamespace.objects.get(uri=feature)
            except Exception, e:
                log.error('#######  Error: unknown namespace %s: %s' % (feature, str(e)))
                continue

            metadata_dict[namespace_obj] = {}

            namespace_properties = MetadataProperty.objects.filter(namespace=namespace_obj)
            for property_values in features[feature]:
                property_xpath = property_values[0]
                property_value = property_values[1]
                property_options = property_values[2]
                xpath_splitted = xpath.findall(property_xpath)
                metadata_property = xpath_splitted[0][1].strip()
                metadata_index = xpath_splitted[0][2].strip()
                found_property = namespace_properties.filter(field_name__iexact=metadata_property)
                if found_property.count() > 0 and len(property_value.strip()) > 0:
                    if found_property[0].is_array == 'not_array':
                        delete_list.append(found_property[0])
                    if property_options['IS_QUALIFIER'] and xpath_splitted[-1][1] == 'lang':
                        #log.debug('############# setting throw away IS_QUALIFIER option')
                        find_xpath = property_xpath.replace('/?xml:lang', '')
                        if metadata_dict[namespace_obj].has_key(find_xpath):
                            if property_value == 'x-default':
                                property_value = metadata_default_language
                            metadata_dict[namespace_obj][find_xpath].language = property_value
                        else:
                            log.debug('metadata property not found: ' + find_xpath)
                            pass
                        #log.debug('###@@@@ %s: (%s)' % (find_xpath, property_value))
                    else:
                        if found_property[0].is_variant:
                            x = MetadataValue(schema=found_property[0], object_id=self.component.pk, content_type=ctype_component, value=property_value, xpath=property_xpath)
                        else:
                            x = MetadataValue(schema=found_property[0], object_id=self.item.pk, content_type=ctype, value=property_value, xpath=property_xpath)
                        metadata_dict[namespace_obj][property_xpath] = x
                        metadata_list.append(x)
Exemplo n.º 3
0
 def _cb_xmp_ok(self, features):
     try:
         ctype_component = ContentType.objects.get_for_model(self.component)
         ctype = ContentType.objects.get_for_model(self.item)
         xpath = re.compile(r'(?P<prefix>\w+):(?P<property>\w+)(?P<array_index>\[\d+\]){,1}')
         user = self.item.uploaded_by()
         metadata_default_language = get_metadata_default_language(user)
     except Exception, e:
         log.error('Error in %s: %s %s' % (self.__class__.__name__, type(e), str(e)))
         return
Exemplo n.º 4
0
def _save_uploaded_component(request, res_id, file_name, variant, item, user, workspace):
    """
    Create component for the given item and generate mediadart tasks. 
    Used only when user uploaded an item's variant
    """
    try:
        logger.debug('############### _save_uploaded_component: %s' % variant.pk)
        comp = item.create_variant(variant, workspace)
        
        if variant.auto_generated:
            comp.imported = True
    
        logger.debug('comp._id %s'%comp._id)
        logger.debug('res_id %s'%res_id)
        comp.file_name = file_name
        comp._id = res_id
        
        mimetypes.add_type('video/flv','.flv')
        mimetypes.add_type('video/ts','.ts')
        mimetypes.add_type('video/mpeg4','.m4v')
        mimetypes.add_type('video/dv','.dv')
        mimetypes.add_type('doc/pdf','.pdf')
        mimetypes.add_type('image/nikon', '.nef')
        mimetypes.add_type('image/canon', '.cr2')
        mimetypes.add_type('image/digitalnegative', '.dng')
        mime_type = mimetypes.guess_type(file_name)[0]
            
        ext = mime_type.split('/')[1]
        comp.format = ext
        comp.save()
        
        default_language = get_metadata_default_language(user)
        
        for key in request.POST.keys():
            if key.startswith('metadata'):
                value = request.POST[key]
                metadata_id = key.split('_')[-1]
                descriptor = MetadataDescriptor.objects.get(pk = metadata_id)
                MetadataValue.objects.save_descriptor_values(descriptor, item, value, workspace, 'original', default_language)
    
        metadataschema_mimetype = MetadataProperty.objects.get(namespace__prefix='dc',field_name='format')
        orig=MetadataValue.objects.create(schema=metadataschema_mimetype, content_object=comp,  value=mime_type)
        transaction.commit()
       
        generate_tasks(comp, workspace)
        
#-        if not variant.auto_generated:
#-            for ws in item.workspaces.all():
#-                EventRegistration.objects.notify('upload', workspace,  **{'items':[item]})
        
    except Exception, ex:
        print traceback.print_exc(ex)
        transaction.rollback()
        raise    
Exemplo n.º 5
0
def _save_uploaded_component(request, res_id, file_name, variant, item, user, workspace):
    """
    Create component for the given item and generate mediadart tasks. 
    Used only when user uploaded an item's variant
    """
    try:
        logger.debug('############### _save_uploaded_component: %s' % variant.pk)
        comp = item.create_variant(variant, workspace)
        
        if variant.auto_generated:
            comp.imported = True
    
        logger.debug('comp._id %s'%comp._id)
        logger.debug('res_id %s'%res_id)
        comp.file_name = file_name
        comp._id = res_id
        
        mimetypes.add_type('video/flv','.flv')
        mimetypes.add_type('video/ts','.ts')
        mimetypes.add_type('video/mpeg4','.m4v')
        mimetypes.add_type('video/dv','.dv')
        mimetypes.add_type('doc/pdf','.pdf')
        mimetypes.add_type('image/nikon', '.nef')
        mimetypes.add_type('image/canon', '.cr2')
        mimetypes.add_type('image/digitalnegative', '.dng')
        mime_type = mimetypes.guess_type(file_name)[0]
            
        ext = mime_type.split('/')[1]
        comp.format = ext
        comp.save()
        
        default_language = get_metadata_default_language(user)
        
        for key in request.POST.keys():
            if key.startswith('metadata'):
                value = request.POST[key]
                metadata_id = key.split('_')[-1]
                descriptor = MetadataDescriptor.objects.get(pk = metadata_id)
                MetadataValue.objects.save_descriptor_values(descriptor, item, value, workspace, 'original', default_language)
    
        metadataschema_mimetype = MetadataProperty.objects.get(namespace__prefix='dc',field_name='format')
        orig=MetadataValue.objects.create(schema=metadataschema_mimetype, content_object=comp,  value=mime_type)
        transaction.commit()
       
        generate_tasks(comp, workspace)
        
#-        if not variant.auto_generated:
#-            for ws in item.workspaces.all():
#-                EventRegistration.objects.notify('upload', workspace,  **{'items':[item]})
        
    except Exception, ex:
        print traceback.print_exc(ex)
        transaction.rollback()
        raise    
Exemplo n.º 6
0
def save_descriptors(request):

    """
    Save descriptors values (method called by DAM GUI)
    """

    metadata = simplejson.loads(request.POST.get('metadata'))

    item_list = request.POST.getlist('items')
    variant_name = request.POST.get('obj', 'original')

    ctype_item = ContentType.objects.get_for_model(Item)
    ctype_obj = ContentType.objects.get_for_model(Component)

    workspace = request.session.get('workspace', None) 
    user = User.objects.get(pk=request.session['_auth_user_id'])

    default_language = get_metadata_default_language(user, workspace)

    items = Item.objects.filter(pk__in=item_list)            
    Latitude = None
    Longitude = None
    logger.debug('metadata %s'%metadata)
    for m in metadata:
        logger.debug('m %s'%m)
        ids = m.split('_')
        desc_id = ids[1]
        if desc_id == 'license':
            logger.debug('license')
            license_id = metadata[m]
            license = RightsValue.objects.get(pk=int(license_id))
            comp = Component.objects.get(item=item, variant__name=variant_name, workspace=workspace)
            comp.save_rights_value(license, workspace)
        else:
            descriptor = MetadataDescriptor.objects.get(pk=int(desc_id))
            #logger.debug('descriptor %s'%descriptor)
            if descriptor.name == 'Latitude':
                Latitude = metadata[m]
            elif descriptor.name == 'Longitude':
                Longitude = metadata[m]
            
            logger.debug('ids %s'%ids)        
            if len(ids) == 2:
                try:
                    MetadataValue.objects.save_descriptor_values(descriptor, items, metadata[m], workspace, variant_name, default_language)
                except Exception, ex:
                    logger.debug('Error while saving descriptor %s' % ex )
                    raise ex

                    
            else:
                
                MetadataValue.objects.save_descriptor_structure_values(descriptor, ids[2], items, metadata[m], workspace, variant_name)
Exemplo n.º 7
0
 def _cb_xmp_ok(self, features):
     try:
         ctype_component = ContentType.objects.get_for_model(self.component)
         ctype = ContentType.objects.get_for_model(self.item)
         xpath = re.compile(
             r'(?P<prefix>\w+):(?P<property>\w+)(?P<array_index>\[\d+\]){,1}'
         )
         user = self.item.uploaded_by()
         metadata_default_language = get_metadata_default_language(user)
     except Exception, e:
         log.error('Error in %s: %s %s' %
                   (self.__class__.__name__, type(e), str(e)))
         self.deferred.errback(e)
         return
Exemplo n.º 8
0
def _run_sync_pipes(items, original_name, variants, workspace, user):
    pipes = Pipeline.objects.filter(triggers__name='sync_xmp')
    if not pipes:
        raise Exception('No pipelines registered for trigger event "sync_xmp"')

    # update xmp:MetadataDate
    try:
        default_language= get_metadata_default_language(user, workspace)
        metadata_schema = MetadataProperty.objects.get(field_name = 'MetadataDate')
        metadataschema_id = str(metadata_schema.pk)
        metadatavalue = time.strftime("%d/%m/%yT%H:%M%S",time.gmtime()) + time.strftime('%z')
        my_metadata = {metadataschema_id.decode('utf-8'):[metadatavalue.decode('utf-8')]}
        items_objs = Item.objects.filter(pk__in=items)
        MetadataValue.objects.save_metadata_value(items_objs, my_metadata, original_name, workspace, default_language)
    except Exception, err:
        logger.debug('Error while changing xmp MetadataDate: ')
        logger.exception(err)
Exemplo n.º 9
0
def save_metadata(request):

    """
    Save XMP Values for the items in item_list
    """

    metadata = simplejson.loads(request.POST.get('metadata'))

    item_list = request.POST.getlist('items')
    variant_name = request.POST.get('obj', 'original')

    workspace = request.session.get('workspace', None) 

    user = User.objects.get(pk=request.session['_auth_user_id'])
    default_language = get_metadata_default_language(user, workspace)

    items = Item.objects.filter(pk__in=item_list)
    MetadataValue.objects.save_metadata_value(items, metadata, variant_name, workspace, default_language)
    _save_coords(items, metadata)

    return HttpResponse('OK')   
Exemplo n.º 10
0
    def get_formatted_descriptors(self, group, user, workspace):
        """
        Retrieves all the values for the descriptors for the current component
        and returns them in the format required by the Metadata GUI
        @param group an instance of MetadataDescriptorGroup
        @param user an instance of auth.User
        @param workspace an instance of workspace.DAMWorkspace
        """
        from dam.metadata.models import MetadataDescriptor, MetadataProperty
        from dam.preferences.views import get_metadata_default_language

        descriptors = self.get_descriptors(group)
        default_language = get_metadata_default_language(user, workspace)
        values = []
        for d, v in descriptors.iteritems():
            desc = MetadataDescriptor.objects.get(pk=d)
            desc_dict = {'caption': '%s' % desc.name}
            desc_value = v
            logger.info("desc_value %s " % desc_value)
            if isinstance(v, dict):
                if v.has_key(default_language):
                    desc_value = v.get(default_language)
                else:
                    continue
            elif isinstance(v, list):
                for value in v:
                    if isinstance(value, dict):
                        if not isinstance(desc_value, dict):
                            desc_value = {'properties': []}
                        for key, v_value in value.iteritems():
                            p = MetadataProperty.objects.get(pk=key)
                            desc_value['properties'].append({
                                'caption': p.caption,
                                'value': v_value
                            })
            desc_dict['value'] = desc_value
            values.append(desc_dict)

        return values
Exemplo n.º 11
0
    def get_formatted_descriptors(self, group, user, workspace):
        """
        Retrieves all the values for the descriptors for the current component
        and returns them in the format required by the Metadata GUI
        @param group an instance of MetadataDescriptorGroup
        @param user an instance of auth.User
        @param workspace an instance of workspace.DAMWorkspace
        """
        from dam.metadata.models import MetadataDescriptor, MetadataProperty
        from dam.preferences.views import get_metadata_default_language
    
        descriptors = self.get_descriptors(group)
        default_language = get_metadata_default_language(user, workspace)
        values = []
        for d, v in descriptors.iteritems():
            desc = MetadataDescriptor.objects.get(pk=d)
            desc_dict = {'caption': '%s' % desc.name}
            desc_value = v
            logger.info("desc_value %s " %desc_value)
            if isinstance(v, dict):
                if v.has_key(default_language):
                    desc_value = v.get(default_language)
                else:
                    continue
            elif isinstance(v, list):
                for value in v:
                    if isinstance(value, dict):
                        if not isinstance(desc_value, dict):
                            desc_value = {'properties': []}
                        for key, v_value  in value.iteritems():
                            p = MetadataProperty.objects.get(pk=key)
                            desc_value['properties'].append({'caption': p.caption, 'value': v_value})
            desc_dict['value'] = desc_value
            values.append(desc_dict)

        return values
Exemplo n.º 12
0
    def _save_metadata(self, features, ctype):
        c = self.source
        #log.debug('######## _save_metadata %s %s' % (c, features))

        xmp_metadata_commons = {'size':[('notreDAM','FileSize')]}
        xmp_metadata_audio = {'channels':[('xmpDM', 'audioChannelType')], 'sample_rate':[('xmpDM', 'audioSampleRate')], 'duration':[('notreDAM', 'Duration')]}
        xmp_metadata_video = {'height':[('xmpDM', 'videoFrameSize','stDim','h')] , 'width':[('xmpDM', 'videoFrameSize','stDim','w')], 'r_frame_rate':[('xmpDM','videoFrameRate')], 'bit_rate':[('xmpDM','fileDataRate')], 'duration':[('notreDAM', 'Duration')]}
        xmp_metadata_image = {'height':[('tiff', 'ImageLength')] , 'width':[('tiff', 'ImageWidth')]}
        xmp_metadata_doc = {'pages': [('notreDAM', 'NPages')], 'Copyright': [('dc', 'rights')]}
        xmp_metadata_image.update(xmp_metadata_commons)
        xmp_metadata_audio.update(xmp_metadata_commons)
        xmp_metadata_doc.update(xmp_metadata_commons)

        xmp_metadata_video.update(xmp_metadata_audio)
        xmp_metadata = {'image': xmp_metadata_image, 'video': xmp_metadata_video, 'audio': xmp_metadata_audio, 'doc': xmp_metadata_doc}

        metadata_list = []
        delete_list = []

        media_type = c.media_type.name
        user = self.item.uploaded_by()
        metadata_default_language = get_metadata_default_language(user)

        for feature in features.keys():
            if features[feature]=='' or features[feature] == '0':
                continue 
            if feature == 'file_size':
                c.size = features[feature]
            if feature == 'height':
                c.height = features[feature]
            elif feature == 'width':
                c.width = features[feature]

            try:
                xmp_names = xmp_metadata[media_type][feature]
            except KeyError:
                continue

            for m in xmp_names:
                try:
                    ms = MetadataProperty.objects.get(namespace__prefix=m[0], field_name= m[1])
                except:
                    log.debug( 'inside readfeatures, unknown metadata %s:%s ' %  (m[0],m[1]))
                    continue
                if ms.is_variant or c.variant.name == 'original':
                    if len(m) == 4:
                        property_xpath = "%s:%s[1]/%s:%s" % (m[0], m[1], m[2], m[3])
                    else:
                        property_xpath = ''
                    try:
                        if ms.type == 'lang':
                            x = MetadataValue(schema=ms, object_id=c.pk, content_type=ctype, value=features[feature], language=metadata_default_language, xpath=property_xpath)
                        else:                            
                            x = MetadataValue(schema=ms, object_id=c.pk, content_type=ctype, value=features[feature], xpath=property_xpath)
                        metadata_list.append(x) 
                        delete_list.append(ms) 
                    except:
                        log.debug('inside readfeatures, could not get %s' %  ms)
                        continue
        c.save()
        return (metadata_list, delete_list)
Exemplo n.º 13
0
def get_status(request):
    """
    Returns information for the given items, including name, size, url of thumbnail and preview
    Called every 10 seconds by the GUI for refreshing information on pending items
    """
    try:
        workspace = request.session.get('workspace')
        user = request.user
        items_in_progress = request.POST.getlist('items')
        logger.debug('items_in_progress %s'%items_in_progress)
        resp = {'items':[]}
#### Added by orlando
        #process_id = request.POST.get('process_id')
# get completed targets
        #completed_targets = ProcessTarget.objects.filter(process__workspace = workspace, target_id__in=items_in_progress, actions_todo=0).values_list('target_id', flat = True)
#        info = {}

        process_in_progress = Process.objects.filter(end_date__isnull = True)
        if process_in_progress:
            pending_items = ProcessTarget.objects.filter(process__in = process_in_progress, actions_todo__gt = 0)
        else:
            pending_items = ProcessTarget.objects.none()
        
        resp['status_bar'] = {
            'process_in_progress':  process_in_progress.count(),
            'pending_items': pending_items.count()
        }
####
        if request.POST.get('update_script_monitor'):
            from dam.scripts.views import _script_monitor
            processes_info = _script_monitor(workspace)
            resp['scripts'] = processes_info 

        thumb_caption_setting = DAMComponentSetting.objects.get(name='thumbnail_caption')
        thumb_caption = thumb_caption_setting.get_user_setting(user, workspace)
        fullscreen_caption_setting = DAMComponentSetting.objects.get(name='fullscreen_caption')
        fullscreen_caption = fullscreen_caption_setting.get_user_setting(user, workspace)
        default_language = get_metadata_default_language(user, workspace)    
        check_deleted = request.POST.has_key('show_deleted')
            
        for item_id in items_in_progress:
            try:

        
                item = Item.objects.get(pk = int(item_id)) 
                #tmp = item.get_info(workspace, user)
                tmp = item.get_info(workspace, thumb_caption, default_language, check_deleted = check_deleted, fullscreen_caption = fullscreen_caption)
#                if item_id not in completed_targets:
#                    tmp['status'] = 'in_progress'
#                else:
#                    tmp['status'] = 'completed'
                
                resp['items'].append(tmp)
                
            except Item.DoesNotExist:
                logger.debug('process target not found for item %s'%item_id)
                continue


        logger.info('resp: %s' % resp)       
        resp = simplejson.dumps(resp)
        return HttpResponse(resp)
    except Exception,ex:
        logger.exception(ex)
        raise ex
Exemplo n.º 14
0
def load_items(request, view_type=None, unlimited=False):
    logger.debug('******************** load_items')
    logger.debug('request POST inside* load_items %s' % request.POST )
    from datetime import datetime
    try:
        user = request.user
        
        workspace = request.session['workspace']        

        media_type = request.POST.getlist('media_type')
        
#        save media type on session

        if request.session.__contains__('media_type'):
            request.session['media_type'][workspace.pk] = list(media_type)
        else:
            request.session['media_type']= {workspace.pk: list(media_type)}
            
        request.session.modified = True
        
        start = int(request.POST.get('start', 0))
        limit = int(request.POST.get('limit', 30))
		
        if limit == 0:
            start = None
            limit = None
            
        
        items, total_count = _search_items(request, workspace, media_type, start, limit)                    
        item_dict = []
        now = time.time()

        items_pks = [item.pk for item in items]
        
        tasks_pending_obj = []
        tasks_pending = []
                

        default_language = get_metadata_default_language(user, workspace)

        user_basket = Basket.get_basket(user, workspace)
        basket_items = user_basket.items.all().values_list('pk', flat=True)
        
        items_info = []
        thumb_caption_setting = DAMComponentSetting.objects.get(name='thumbnail_caption')
        thumb_caption = thumb_caption_setting.get_user_setting(user, workspace)
        fullscreen_caption_setting = DAMComponentSetting.objects.get(name='fullscreen_caption')
        fullscreen_caption = fullscreen_caption_setting.get_user_setting(user, workspace)
        default_language = get_metadata_default_language(user, workspace)    
        check_deleted = request.POST.has_key('show_deleted')
        for item in items:
            tmp = item.get_info(workspace, thumb_caption, default_language, check_deleted = check_deleted, fullscreen_caption = fullscreen_caption)
            if item.pk in basket_items:
                tmp['item_in_basket'] = 1
            else:
                tmp['item_in_basket'] = 0
                
            items_info.append(tmp)
        
#        for item in items:
#            thumb_url,thumb_ready = _get_thumb_url(item, workspace)
#            logger.debug('thumb_url,thumb_ready %s, %s'%(thumb_url,thumb_ready))
#            
#            geotagged = 0
#            inprogress =  int( (not thumb_ready) or (item.pk in tasks_pending)) 
#            
#            
#            if GeoInfo.objects.filter(item=item).count() > 0:
#                geotagged = 1
#            
#            item_in_basket = 0
#
#            if item.pk in basket_items:
#                item_in_basket = 1
#             
#            states = item.stateitemassociation_set.all()
#            try:
#                my_caption = _get_thumb_caption(item, thumb_caption, default_language)
#            except:
#                # problems retrieving thumb, skip this items
#                continue
##                my_caption = ''
#            if inprogress:
#                preview_available = tasks_pending_obj.filter(component__variant__name = 'preview', component__item = item, params__contains = 'adapt_resource').count()
#            else:
#                preview_available = 0
#            item_info = {
#                "name":my_caption,
#                "pk": smart_str(item.pk),
#                "geotagged": geotagged, 
#                "inprogress": inprogress, 
#                'thumb': thumb_ready, 
#                'type': smart_str(item.type.name),
#                'inbasket': item_in_basket,
#                'preview_available': preview_available,
#                "url":smart_str(thumb_url), "url_preview":smart_str("/redirect_to_component/%s/preview/?t=%s" % (item.pk,  now))
#            }
#            
#            if states.count():
#                state_association = states[0]
#                
#                item_info['state'] = state_association.state.pk
#                
#            item_dict.append(item_info)
        
        logger.info('items_info: %s' % items_info) 
        res_dict = {"items": items_info, "totalCount": str(total_count)}       
        resp = simplejson.dumps(res_dict)

        return HttpResponse(resp)
    except Exception,  ex:
        logger.exception(ex)
        raise ex
Exemplo n.º 15
0
    def _read_xmp_features(self, features):
        xpath = re.compile(
            r'(?P<prefix>\w+):(?P<property>\w+)(?P<array_index>\[\d+\]){,1}')
        ctype = ContentType.objects.get_for_model(self.item)
        ctype_component = ContentType.objects.get_for_model(self.component)

        user = self.item.uploaded_by()
        metadata_default_language = get_metadata_default_language(user)

        metadata_dict = {}
        metadata_list = []
        delete_list = []

        log.debug('READ XMP FEATURES')

        if not isinstance(features, dict):
            item.state = 1
            item.save()
            return [], []

        for feature in features.keys():
            try:
                namespace_obj = XMPNamespace.objects.get(uri=feature)
            except Exception, e:
                log.error('#######  Error: unknown namespace %s: %s' %
                          (feature, str(e)))
                continue

            metadata_dict[namespace_obj] = {}

            namespace_properties = MetadataProperty.objects.filter(
                namespace=namespace_obj)
            for property_values in features[feature]:
                property_xpath = property_values[0]
                property_value = property_values[1]
                property_options = property_values[2]
                xpath_splitted = xpath.findall(property_xpath)
                metadata_property = xpath_splitted[0][1].strip()
                metadata_index = xpath_splitted[0][2].strip()
                found_property = namespace_properties.filter(
                    field_name__iexact=metadata_property)
                if found_property.count() > 0 and len(
                        property_value.strip()) > 0:
                    if found_property[0].is_array == 'not_array':
                        delete_list.append(found_property[0])
                    if property_options['IS_QUALIFIER'] and xpath_splitted[-1][
                            1] == 'lang':
                        #log.debug('############# setting throw away IS_QUALIFIER option')
                        find_xpath = property_xpath.replace('/?xml:lang', '')
                        if metadata_dict[namespace_obj].has_key(find_xpath):
                            if property_value == 'x-default':
                                property_value = metadata_default_language
                            metadata_dict[namespace_obj][
                                find_xpath].language = property_value
                        else:
                            log.debug('metadata property not found: ' +
                                      find_xpath)
                            pass
                        #log.debug('###@@@@ %s: (%s)' % (find_xpath, property_value))
                    else:
                        if found_property[0].is_variant:
                            x = MetadataValue(schema=found_property[0],
                                              object_id=self.component.pk,
                                              content_type=ctype_component,
                                              value=property_value,
                                              xpath=property_xpath)
                        else:
                            x = MetadataValue(schema=found_property[0],
                                              object_id=self.item.pk,
                                              content_type=ctype,
                                              value=property_value,
                                              xpath=property_xpath)
                        metadata_dict[namespace_obj][property_xpath] = x
                        metadata_list.append(x)
Exemplo n.º 16
0
    def _save_metadata(self, features, ctype):
        c = self.source
        #log.debug('######## _save_metadata %s %s' % (c, features))

        xmp_metadata_commons = {'size': [('notreDAM', 'FileSize')]}
        xmp_metadata_audio = {
            'channels': [('xmpDM', 'audioChannelType')],
            'sample_rate': [('xmpDM', 'audioSampleRate')],
            'duration': [('notreDAM', 'Duration')]
        }
        xmp_metadata_video = {
            'height': [('xmpDM', 'videoFrameSize', 'stDim', 'h')],
            'width': [('xmpDM', 'videoFrameSize', 'stDim', 'w')],
            'r_frame_rate': [('xmpDM', 'videoFrameRate')],
            'bit_rate': [('xmpDM', 'fileDataRate')],
            'duration': [('notreDAM', 'Duration')]
        }
        xmp_metadata_image = {
            'height': [('tiff', 'ImageLength')],
            'width': [('tiff', 'ImageWidth')]
        }
        xmp_metadata_doc = {
            'pages': [('notreDAM', 'NPages')],
            'Copyright': [('dc', 'rights')]
        }
        xmp_metadata_image.update(xmp_metadata_commons)
        xmp_metadata_audio.update(xmp_metadata_commons)
        xmp_metadata_doc.update(xmp_metadata_commons)

        xmp_metadata_video.update(xmp_metadata_audio)
        xmp_metadata = {
            'image': xmp_metadata_image,
            'video': xmp_metadata_video,
            'audio': xmp_metadata_audio,
            'doc': xmp_metadata_doc
        }

        metadata_list = []
        delete_list = []

        media_type = c.media_type.name
        user = self.item.uploaded_by()
        metadata_default_language = get_metadata_default_language(user)

        for feature in features.keys():
            if features[feature] == '' or features[feature] == '0':
                continue
            if feature == 'file_size':
                c.size = features[feature]
            if feature == 'height':
                c.height = features[feature]
            elif feature == 'width':
                c.width = features[feature]

            try:
                xmp_names = xmp_metadata[media_type][feature]
            except KeyError:
                continue

            for m in xmp_names:
                try:
                    ms = MetadataProperty.objects.get(namespace__prefix=m[0],
                                                      field_name=m[1])
                except:
                    log.debug('inside readfeatures, unknown metadata %s:%s ' %
                              (m[0], m[1]))
                    continue
                if ms.is_variant or c.variant.name == 'original':
                    if len(m) == 4:
                        property_xpath = "%s:%s[1]/%s:%s" % (m[0], m[1], m[2],
                                                             m[3])
                    else:
                        property_xpath = ''
                    try:
                        if ms.type == 'lang':
                            x = MetadataValue(
                                schema=ms,
                                object_id=c.pk,
                                content_type=ctype,
                                value=features[feature],
                                language=metadata_default_language,
                                xpath=property_xpath)
                        else:
                            x = MetadataValue(schema=ms,
                                              object_id=c.pk,
                                              content_type=ctype,
                                              value=features[feature],
                                              xpath=property_xpath)
                        metadata_list.append(x)
                        delete_list.append(ms)
                    except:
                        log.debug('inside readfeatures, could not get %s' % ms)
                        continue
        c.save()
        return (metadata_list, delete_list)