def on_delete_sound(sender,instance, **kwargs): if instance.moderation_state == "OK" and instance.processing_state == "OK": try: DeletedSound.objects.get_or_create(sound_id=instance.id, user=instance.user) except User.DoesNotExist: deleted_user = User.objects.get(id=settings.DELETED_USER_ID) DeletedSound.objects.get_or_create(sound_id=instance.id, user=deleted_user) try: if instance.geotag: instance.geotag.delete() except: pass if instance.pack: instance.pack.process() delete_sound_from_solr(instance) delete_object_files(instance, web_logger) if instance.similarity_state=='OK': try: if Similarity.contains(instance.id): Similarity.delete(instance.id) except: web_logger.warn("ommitting similarity deletion for deleted sound %d"%instance.id) web_logger.debug("Deleted sound with id %i"%instance.id)
def on_delete_sound(sender, instance, **kwargs): if instance.moderation_state == "OK" and instance.processing_state == "OK": try: DeletedSound.objects.get_or_create(sound_id=instance.id, user=instance.user) except User.DoesNotExist: deleted_user = User.objects.get(id=settings.DELETED_USER_ID) DeletedSound.objects.get_or_create(sound_id=instance.id, user=deleted_user) try: if instance.geotag: instance.geotag.delete() except: pass if instance.pack: instance.pack.process() delete_sound_from_solr(instance) delete_object_files(instance, web_logger) if instance.similarity_state == 'OK': try: if Similarity.contains(instance.id): Similarity.delete(instance.id) except: web_logger.warn( "ommitting similarity deletion for deleted sound %d" % instance.id) web_logger.debug("Deleted sound with id %i" % instance.id)
def read(self, request, sound_id): try: sound = Sound.objects.get(id=sound_id, moderation_state="OK", processing_state="OK", similarity_state="OK") #TODO: similarity_state="OK" #TODO: this filter has to be added again, but first the db has to be updated except Sound.DoesNotExist: #@UndefinedVariable raise ReturnError(404, "NotFound", {"explanation": "Sound with id %s does not exist or similarity data is not ready." % sound_id}) similar_sounds = get_similar_sounds(sound,request.GET.get('preset', None), int(request.GET.get('num_results', settings.SOUNDS_PER_PAGE)) ) sounds = [] for similar_sound in similar_sounds : try: sound = prepare_collection_sound(Sound.objects.select_related('user').get(id=similar_sound[0]), custom_fields = request.GET.get('fields', False)) sound['distance'] = similar_sound[1] sounds.append(sound) except Exception, e: # Delete sound from gaia index so it does not appear again in similarity searches if Similarity.contains(similar_sound[0]): Similarity.delete(similar_sound[0]) # Invalidate similarity search cache cache_key = "similar-for-sound-%s-%s" % (similar_sound[0], request.GET.get('preset', None)) cache.delete(cache_key)
def on_delete_sound(sender, instance, **kwargs): if instance.moderation_state == "OK" and instance.processing_state == "OK": try: DeletedSound.objects.get_or_create(sound_id=instance.id, user=instance.user) except User.DoesNotExist: deleted_user = User.objects.get(id=settings.DELETED_USER_ID) DeletedSound.objects.get_or_create(sound_id=instance.id, user=deleted_user) try: if instance.geotag: instance.geotag.delete() except: pass try: if instance.pack: instance.pack.process() except Pack.DoesNotExist: ''' It might happen when we do user.delete() to a user that has several sounds in packs that when post_delete signals for sounds are called, the packs have already been deleted. This is because the way in which django deletes all the related objects with foreign keys. When a user is deleted, its packs and sounds must be deleted too. Django first runs pre_delete on all objects to be deleted, then delete and then post_delete. Therefore it can happen that when the post_delete signal for a sound is called, the pack has already been deleted but the instance passed to the post_delete function still points to that pack. We can therefore safely use try/except here and we'll still be doing the job correctly. ''' pass delete_sound_from_solr(instance) delete_object_files(instance, web_logger) if instance.similarity_state == 'OK': try: if Similarity.contains(instance.id): Similarity.delete(instance.id) except: web_logger.warn( "ommitting similarity deletion for deleted sound %d" % instance.id) web_logger.debug("Deleted sound with id %i" % instance.id)
def on_delete_sound(sender, instance, **kwargs): if instance.moderation_state == "OK" and instance.processing_state == "OK": try: DeletedSound.objects.get_or_create(sound_id=instance.id, user=instance.user) except User.DoesNotExist: deleted_user = User.objects.get(id=settings.DELETED_USER_ID) DeletedSound.objects.get_or_create(sound_id=instance.id, user=deleted_user) try: if instance.geotag: instance.geotag.delete() except: pass try: if instance.pack: instance.pack.process() except Pack.DoesNotExist: ''' It might happen when we do user.delete() to a user that has several sounds in packs that when post_delete signals for sounds are called, the packs have already been deleted. This is because the way in which django deletes all the related objects with foreign keys. When a user is deleted, its packs and sounds must be deleted too. Django first runs pre_delete on all objects to be deleted, then delete and then post_delete. Therefore it can happen that when the post_delete signal for a sound is called, the pack has already been deleted but the instance passed to the post_delete function still points to that pack. We can therefore safely use try/except here and we'll still be doing the job correctly. ''' pass delete_sound_from_solr(instance) delete_object_files(instance, web_logger) if instance.similarity_state == 'OK': try: if Similarity.contains(instance.id): Similarity.delete(instance.id) except: web_logger.warn("ommitting similarity deletion for deleted sound %d" % instance.id) web_logger.debug("Deleted sound with id %i" % instance.id)
def on_delete_sound(sender,instance, **kwargs): if instance.moderation_state == "OK" and instance.processing_state == "OK": try: DeletedSound.objects.get_or_create(sound_id=instance.id, user=instance.user) except User.DoesNotExist: deleted_user = User.objects.get(id=settings.DELETED_USER_ID) DeletedSound.objects.get_or_create(sound_id=instance.id, user=deleted_user) try: if instance.geotag: instance.geotag.delete() except: pass if instance.pack: instance.pack.process() delete_sound_from_solr(instance) delete_object_files(instance, web_logger) # N.B. be watchful of errors that might be thrown if the sound is not in the similarity index if Similarity.contains(instance.id): Similarity.delete(instance.id) web_logger.debug("Deleted sound with id %i"%instance.id)
def handle(self, **options): # Update sounds max_sound_id = Sound.objects.all().aggregate(Max('id'))['id__max'] counter = 0 for sound_id in xrange(max_sound_id + 1): changed = False try: sound = Sound.objects.get(id=sound_id) except Sound.DoesNotExist: continue # check some random paths if sound.processing_state != 'OK' and \ os.path.exists(sound.locations('path')) and \ os.path.exists(sound.locations('preview.HQ.mp3.path')) and \ os.path.exists(sound.locations('display.spectral.L.path')): sound.processing_state = 'OK' changed = True if sound.analysis_state != 'OK' and \ os.path.exists(sound.locations('analysis.statistics.path')) and \ os.path.exists(sound.locations('analysis.frames.path')): sound.analysis_state = 'OK' changed = True if sound.analysis_state == 'OK' and Similarity.contains(sound.id): sound.similarity_state = 'OK' changed = True if changed: sound.save() counter += 1 if counter % 1000 == 0: print 'Processed %s sounds' % counter
def handle(self, *args, **options): sounds_ok = 0 sounds_set_to_pending = 0 sounds = Sound.objects.filter(analysis_state='OK', moderation_state='OK').exclude(similarity_state='PE').only("id","similarity_state") print "Iterating over sounds with similarity_state != 'PE' (%i)..."%len(sounds) sys.stdout.flush() for sound in sounds: sys.stdout.write("\r%i of %i"%(sounds_ok+sounds_set_to_pending+1,len(sounds))) sys.stdout.flush() is_in_similarity_index = Similarity.contains(sound.id) if not is_in_similarity_index: sound.set_similarity_state('PE') sounds_set_to_pending += 1 else: sounds_ok += 1 print "\t- %i sounds set again to Pending"%sounds_set_to_pending print "\t- %i already Ok"%sounds_ok
def handle(self, **options): # Update sounds max_sound_id = Sound.objects.all().aggregate(Max('id'))['id__max'] counter = 0 for sound_id in xrange(max_sound_id+1): changed = False try: sound = Sound.objects.get(id=sound_id) except Sound.DoesNotExist: continue # check some random paths if sound.processing_state != 'OK' and \ os.path.exists(sound.locations('path')) and \ os.path.exists(sound.locations('preview.HQ.mp3.path')) and \ os.path.exists(sound.locations('display.spectral.L.path')): sound.processing_state = 'OK' changed = True if sound.analysis_state != 'OK' and \ os.path.exists(sound.locations('analysis.statistics.path')) and \ os.path.exists(sound.locations('analysis.frames.path')): sound.analysis_state = 'OK' changed = True if sound.analysis_state == 'OK' and Similarity.contains(sound.id): sound.similarity_state = 'OK' changed = True if changed: sound.save() counter += 1 if counter % 1000 == 0: print 'Processed %s sounds' % counter
if str(e)[0:6] == u"Target" or str(e)[0:6] == u"Filter": raise ReturnError(400, "BadRequest", {'explanation':e}) else: raise ReturnError(500, "ContentBasedSearchError", {'explanation':'Unknown error 500'}) paginator = paginate(request, results, min(int(request.GET.get('sounds_per_page', settings.SOUNDS_PER_API_RESPONSE)),settings.MAX_SOUNDS_PER_API_RESPONSE),'p') page = paginator['page'] sounds = [] if int(request.GET.get("p", "1")) <= paginator['paginator'].num_pages: # This is to mimic solr paginator behavior for result in page.object_list: try: sound = prepare_collection_sound(Sound.objects.select_related('user').get(id=int(result[0])), include_user=False, custom_fields = request.GET.get('fields', False)) sounds.append(sound) except Exception, e: # Delete sound from gaia index so it does not appear again in similarity searches if Similarity.contains(int(result[0])): Similarity.delete(int(result[0])) # Invalidate similarity search cache cache_key = "content-based-search-t-%s-f-%s-nr-%s" % (t.replace(" ",""),f.replace(" ",""),int(request.GET.get('max_results', settings.SOUNDS_PER_PAGE))) cache.delete(cache_key) #sounds = [prepare_collection_sound(Sound.objects.select_related('user').get(id=int(result[0])), include_user=False, custom_fields = request.GET.get('fields', False)) for result in page.object_list] result = {'sounds': sounds, 'num_results': paginator['paginator'].count, 'num_pages': paginator['paginator'].num_pages} if int(request.GET.get("p", "1")) <= paginator['paginator'].num_pages: # This is to mimic solr paginator behavior if page.has_other_pages(): if page.has_previous(): result['previous'] = self.__construct_pagination_link(str(t), str(f), page.previous_page_number(), request.GET.get('sounds_per_page',None), int(request.GET.get('max_results', False)), request.GET.get('fields', False)) if page.has_next(): result['next'] = self.__construct_pagination_link(str(t), str(f), page.next_page_number(), request.GET.get('sounds_per_page',None), int(request.GET.get('max_results', False)), request.GET.get('fields', False))