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 delete_sound_from_gaia(sound): logger.info("Deleting sound from gaia with id %d" % sound.id) try: Similarity.delete(sound.id) except Exception, e: logger.warn("Could not delete sound from gaia with id %d (%s)" % (sound.id, str(e)))
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)
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)) add_request_id(request,result)