예제 #1
0
 def to_python(self, value):
     value = super(TagField, self).to_python(value)
     alphanum_only = re.compile(r"[^ a-zA-Z0-9-,]")
     if alphanum_only.search(value):
         raise ValidationError(
             "Tags must contain only letters a-z, digits 0-9 and hyphen")
     return clean_and_split_tags(value)
예제 #2
0
파일: tests.py 프로젝트: kant/freesound
def create_user_and_sounds(num_sounds=1,
                           num_packs=0,
                           user=None,
                           count_offset=0,
                           tags=None):
    count_offset = count_offset + next(sound_counter)
    if user is None:
        user = User.objects.create_user("testuser",
                                        password="******",
                                        email='*****@*****.**')
    packs = list()
    for i in range(0, num_packs):
        pack = Pack.objects.create(user=user,
                                   name="Test pack %i" % (i + count_offset))
        packs.append(pack)
    sounds = list()
    for i in range(0, num_sounds):
        pack = None
        if packs:
            pack = packs[i % len(packs)]
        sound = Sound.objects.create(
            user=user,
            original_filename="Test sound %i" % (i + count_offset),
            base_filename_slug="test_sound_%i" % (i + count_offset),
            license=License.objects.all()[0],
            pack=pack,
            md5="fakemd5_%i" % (i + count_offset))
        if tags is not None:
            sound.set_tags(clean_and_split_tags(tags))
        sounds.append(sound)
    return user, packs, sounds
예제 #3
0
def validate_tags(value):
    tags = clean_and_split_tags(value)
    if len(tags) < 3:
        raise serializers.ValidationError('You should add at least 3 tags...')
    elif len(tags) > 30:
        raise serializers.ValidationError('There can be maximum 30 tags, please select the most relevant ones!')
    return value
예제 #4
0
    def test_upload_sounds(self):
        # create new sound files
        filenames = ['file1.wav', 'file2.wav']
        user = User.objects.create_user("testuser", password="******")
        user_upload_path = settings.UPLOADS_PATH + '/%i/' % user.id
        os.mkdir(user_upload_path)
        for filename in filenames:
            f = open(user_upload_path + filename, 'a')
            f.write(os.urandom(
                1024))  # Add random content to the file to avoid equal md5
            f.close()

        copyfile(user_upload_path + filenames[0],
                 user_upload_path + "copy.wav")

        license = License.objects.all()[0]
        sound_fields = {
            'name': 'new sound',
            'dest_path': user_upload_path + filenames[0],
            'license': license.name,
            'description': 'new sound',
            'tags': clean_and_split_tags('tag1, tag2, tag3'),
        }
        sound = utils.sound_upload.create_sound(user,
                                                sound_fields,
                                                process=False)
        self.assertEqual(user.sounds.all().count(), 1)

        #Now the file has been removed so it should fail
        try:
            sound = utils.sound_upload.create_sound(user,
                                                    sound_fields,
                                                    process=False)
        except utils.sound_upload.NoAudioException:
            # If we try to upload the same file again it shuld also fail
            sound_fields['dest_path'] = user_upload_path + "copy.wav"
            try:
                sound = utils.sound_upload.create_sound(user,
                                                        sound_fields,
                                                        process=False)
            except utils.sound_upload.AlreadyExistsException:
                pass

        self.assertEqual(user.sounds.all().count(), 1)

        #Upload file with geotag and pack
        sound_fields['dest_path'] = user_upload_path + filenames[1]
        sound_fields['geotag'] = '41.2222,31.0000,17'
        sound_fields['pack'] = 'new pack'
        sound_fields['name'] = filenames[1]
        sound = utils.sound_upload.create_sound(user,
                                                sound_fields,
                                                process=False)
        self.assertEqual(user.sounds.all().count(), 2)
        self.assertEqual(Pack.objects.filter(name='new pack').exists(), True)
        self.assertEqual(
            user.sounds.get(original_filename=filenames[1]).tags.count(), 3)
        self.assertNotEqual(
            user.sounds.get(original_filename=filenames[1]).geotag, None)
예제 #5
0
파일: forms.py 프로젝트: MTG/freesound
 def clean(self, value):
     tags = clean_and_split_tags(str(value))
     if len(tags) < 3:
         raise forms.ValidationError('You should add at least 3 different tags. Tags must be separated by spaces '
                                     '(and/or commas).')
     elif len(tags) > 30:
         raise forms.ValidationError('There can be maximum 30 tags, please select the most relevant ones!')
     return tags
예제 #6
0
def create_user_and_sounds(num_sounds=1,
                           num_packs=0,
                           user=None,
                           count_offset=0,
                           tags=None,
                           processing_state='PE',
                           moderation_state='PE',
                           type='wav'):
    """Creates User, Sound and Pack objects useful for testing.

    A counter is used to make sound names unique as well as other fields like md5 (see `sound_counter` variable).
    NOTE: creating sounds requires License objects to exist in DB. Do that by making sure your test case loads
    'licenses' fixture, i.e. "fixtures = ['licenses']".

    Args:
        num_sounds (int): N sounds to generate.
        num_packs (int): N packs in which the sounds above will be grouped.
        user (User): user owner of the created sounds (if not provided, a new user will be created).
        count_offset (int): start counting sounds at X.
        tags (str): string of tags to be added to the sounds (all sounds will have the same tags).
        processing_state (str): processing state of the created sounds.
        moderation_state (str): moderation state of the created sounds.
        type (str): type of the sounds to be created (e.g. 'wav').

    Returns:
        (Tuple(User, List[Pack], List[Sound]): 3-element tuple containing the user owning the sounds,
            a list of the packs created and a list of the sounds created.
    """
    count_offset = count_offset + next(sound_counter)
    if user is None:
        user = User.objects.create_user("testuser",
                                        password="******",
                                        email='*****@*****.**')
    packs = list()
    for i in range(0, num_packs):
        pack = Pack.objects.create(user=user,
                                   name="Test pack %i" % (i + count_offset))
        packs.append(pack)
    sounds = list()
    for i in range(0, num_sounds):
        pack = None
        if packs:
            pack = packs[i % len(packs)]
        sound = Sound.objects.create(
            user=user,
            original_filename="Test sound %i" % (i + count_offset),
            base_filename_slug="test_sound_%i" % (i + count_offset),
            license=License.objects.all()[0],
            pack=pack,
            md5="fakemd5_%i" % (i + count_offset),
            type=type,
            processing_state=processing_state,
            moderation_state=moderation_state)

        if tags is not None:
            sound.set_tags(clean_and_split_tags(tags))
        sounds.append(sound)
    return user, packs, sounds
예제 #7
0
 def validate_tags(self, attrs, source):
     value = attrs[source]
     tags = clean_and_split_tags(value)
     if len(tags) < 3:
         raise serializers.ValidationError('You should at least have 3 tags...')
     elif len(tags) > 30:
         raise serializers.ValidationError('There can be maximum 30 tags, please select the most relevant ones!')
     attrs[source] = tags
     return attrs
예제 #8
0
파일: forms.py 프로젝트: Sciss/freesound
    def clean(self, value):
        tags = clean_and_split_tags(value)

        if len(tags) < 3:
            raise forms.ValidationError('You should at least have 3 tags...')
        elif len(tags) > 30:
            raise forms.ValidationError('There can be maximum 30 tags, please select the most relevant ones!')

        return tags
예제 #9
0
    def clean(self, value):
        tags = clean_and_split_tags(value)

        if len(tags) < 3:
            raise forms.ValidationError('You should at least have 3 tags...')
        elif len(tags) > 30:
            raise forms.ValidationError('There can be maximum 30 tags, please select the most relevant ones!')

        return tags
예제 #10
0
def get_recommended_tags_view(request):
    if request.is_ajax() and request.method == 'POST':
        input_tags = request.POST.get('input_tags', False)
        if input_tags:
            input_tags = list(clean_and_split_tags(input_tags))
            if len(input_tags) > 0:
                tags, community = get_recommended_tags(input_tags)
                return HttpResponse(json.dumps([tags, community]), content_type='application/javascript')

    return HttpResponse(json.dumps([[],"-"]), content_type='application/javascript')
예제 #11
0
 def clean(self, value):
     tags = clean_and_split_tags(str(value))
     if len(tags) < 3:
         raise forms.ValidationError(
             'You should add at least 3 different tags. Tags must be separated by spaces '
             '(and/or commas).')
     elif len(tags) > 30:
         raise forms.ValidationError(
             'There can be maximum 30 tags, please select the most relevant ones!'
         )
     return tags
def get_recommended_tags_view(request):
    if request.is_ajax() and request.method == 'POST':
        input_tags = request.POST.get('input_tags', False)
        if input_tags:
            input_tags = list(clean_and_split_tags(input_tags))
            if len(input_tags) > 0:
                tags, community = get_recommended_tags(input_tags)
                return HttpResponse(json.dumps([tags, community]),
                                    mimetype='application/javascript')

    return HttpResponse(json.dumps([[], "-"]),
                        mimetype='application/javascript')
예제 #13
0
 def validate_tags(self, attrs, source):
     value = attrs[source]
     tags = clean_and_split_tags(value)
     if len(tags) < 3:
         raise serializers.ValidationError(
             'You should at least have 3 tags...')
     elif len(tags) > 30:
         raise serializers.ValidationError(
             'There can be maximum 30 tags, please select the most relevant ones!'
         )
     attrs[source] = tags
     return attrs
예제 #14
0
 def validate_tags(self, attrs, source):
     if not self.is_providing_description(attrs):
         attrs[source] = None
     else:
         if source not in attrs:
             raise serializers.ValidationError('This field is required.')
         value = attrs[source]
         tags = clean_and_split_tags(value)
         if len(tags) < 3:
             raise serializers.ValidationError('You should at least have 3 tags...')
         elif len(tags) > 30:
             raise serializers.ValidationError('There can be maximum 30 tags, please select the most relevant ones!')
         attrs[source] = tags
     return attrs
예제 #15
0
def get_recommended_tags_view(request):
    if request.is_ajax() and request.method == 'POST':
        input_tags = request.POST.get('input_tags', False)
        if input_tags:
            input_tags = list(clean_and_split_tags(input_tags))
            if len(input_tags) > 0:
                try:
                    tags, community = get_recommended_tags(input_tags)
                    return HttpResponse(json.dumps([tags, community]), content_type='application/javascript')
                except urllib2.URLError as e:
                    logger.error('Could not get a response from the tagrecommendation service (%s)\n\t%s' % \
                         (e, traceback.format_exc()))
                    return HttpResponseUnavailabileError()

    return HttpResponse(json.dumps([[],"-"]), content_type='application/javascript')
예제 #16
0
def get_recommended_tags_view(request):
    if request.is_ajax() and request.method == 'POST':
        input_tags = request.POST.get('input_tags', False)
        if input_tags:
            input_tags = list(clean_and_split_tags(input_tags))
            if len(input_tags) > 0:
                try:
                    tags, community = get_recommended_tags(input_tags)
                    return HttpResponse(json.dumps([tags, community]), content_type='application/javascript')
                except urllib2.URLError as e:
                    logger.error('Could not get a response from the tagrecommendation service (%s)\n\t%s' % \
                         (e, traceback.format_exc()))
                    return HttpResponseUnavailabileError()

    return HttpResponse(json.dumps([[],"-"]), content_type='application/javascript')
예제 #17
0
 def validate_tags(self, attrs, source):
     if not self.is_providing_description(attrs):
         attrs[source] = None
     else:
         if source not in attrs:
             raise serializers.ValidationError('This field is required.')
         value = attrs[source]
         tags = clean_and_split_tags(value)
         if len(tags) < 3:
             raise serializers.ValidationError(
                 'You should at least have 3 tags...')
         elif len(tags) > 30:
             raise serializers.ValidationError(
                 'There can be maximum 30 tags, please select the most relevant ones!'
             )
         attrs[source] = tags
     return attrs
예제 #18
0
파일: tests.py 프로젝트: qubodup/freesound
    def test_upload_sounds(self):
        # create new sound files
        filenames = ['file1.wav', 'file2.wav']
        user = User.objects.create_user("testuser", password="******")
        user_upload_path = settings.UPLOADS_PATH + '/%i/' % user.id
        os.mkdir(user_upload_path)
        for filename in filenames:
            f = open(user_upload_path + filename, 'a')
            f.write(os.urandom(1024))  # Add random content to the file to avoid equal md5
            f.close()

        shutil.copyfile(user_upload_path + filenames[0], user_upload_path + "copy.wav")

        license = License.objects.all()[0]
        sound_fields = {
            'name': 'new sound',
            'dest_path': user_upload_path + filenames[0],
            'license': license.name,
            'description': 'new sound',
            'tags': clean_and_split_tags('tag1, tag2, tag3'),
        }
        create_sound(user, sound_fields, process=False)
        self.assertEqual(user.sounds.all().count(), 1)

        # Now the file has been removed so it should fail
        try:
            create_sound(user, sound_fields, process=False)
        except NoAudioException:
            # If we try to upload the same file again it shuld also fail
            sound_fields['dest_path'] = user_upload_path + "copy.wav"
            try:
                create_sound(user, sound_fields, process=False)
            except AlreadyExistsException:
                pass
        self.assertEqual(user.sounds.all().count(), 1)

        # Upload file with geotag and pack
        sound_fields['dest_path'] = user_upload_path + filenames[1]
        sound_fields['geotag'] = '41.2222,31.0000,17'
        sound_fields['pack'] = 'new pack'
        sound_fields['name'] = filenames[1]
        create_sound(user, sound_fields, process=False)
        self.assertEqual(user.sounds.all().count(), 2)
        self.assertEqual(Pack.objects.filter(name='new pack').exists(), True)
        self.assertEqual(user.sounds.get(original_filename=filenames[1]).tags.count(), 3)
        self.assertNotEqual(user.sounds.get(original_filename=filenames[1]).geotag, None)
예제 #19
0
def create_user_and_sounds(num_sounds=1, num_packs=0, user=None, count_offset=0, tags=None,
                           processing_state='PE', moderation_state='PE', type='wav'):
    """
    Creates User, Sound and Pack objects useful for testing. A 'sound_counter' is used to make sound names unique as
    well as other fields like md5.
    NOTE: creating sounds requires License objects to exist in DB. Do that by making sure your test case loads
    'initial_data' fixture, i.e. "fixtures = ['initial_data']".
    :param num_sounds: N sounds to generate
    :param num_packs: N packs in which the sounds above will be grouped
    :param user: user owner of the created sounds (if not provided, a new user will be created)
    :param count_offset: start counting sounds at X
    :param tags: tags added to the sounds (all sounds will have the same tags)
    :param processing_state: processing state of the created sounds
    :param moderation_state: moderation state of the created sounds
    :param type: type of the sounds to be created (e.g. 'wav')
    :return: tuple with (User object, list of Pack objcts, list of Sound objects)
    """
    count_offset = count_offset + next(sound_counter)
    if user is None:
        user = User.objects.create_user("testuser", password="******", email='*****@*****.**')
    packs = list()
    for i in range(0, num_packs):
        pack = Pack.objects.create(user=user, name="Test pack %i" % (i + count_offset))
        packs.append(pack)
    sounds = list()
    for i in range(0, num_sounds):
        pack = None
        if packs:
            pack = packs[i % len(packs)]
        sound = Sound.objects.create(user=user,
                                     original_filename="Test sound %i" % (i + count_offset),
                                     base_filename_slug="test_sound_%i" % (i + count_offset),
                                     license=License.objects.all()[0],
                                     pack=pack,
                                     md5="fakemd5_%i" % (i + count_offset),
                                     type=type,
                                     processing_state=processing_state,
                                     moderation_state=moderation_state)

        if tags is not None:
            sound.set_tags(clean_and_split_tags(tags))
        sounds.append(sound)
    return user, packs, sounds
예제 #20
0
def create_user_and_sounds(num_sounds=1, num_packs=0, user=None, count_offset=0, tags=None):
    if user is None:
        user = User.objects.create_user("testuser", password="******")
    packs = list()
    for i in range(0, num_packs):
        pack = Pack.objects.create(user=user, name="Test pack %i" % (i + count_offset))
        packs.append(pack)
    sounds = list()
    for i in range(0, num_sounds):
        pack = None
        if packs:
            pack = packs[i % len(packs)]
        sound = Sound.objects.create(user=user,
                                     original_filename="Test sound %i" % (i + count_offset),
                                     base_filename_slug="test_sound_%i" % (i + count_offset),
                                     license=License.objects.all()[0],
                                     pack=pack,
                                     md5="fakemd5_%i" % (i + count_offset))
        if tags is not None:
            sound.set_tags(clean_and_split_tags(tags))
        sounds.append(sound)
    return user, packs, sounds
예제 #21
0
    # 7 create geotag objects
    # format: lat#lon#zoom
    if 'geotag' in sound_fields:
        if sound_fields['geotag']:
            lat, lon, zoom = sound_fields['geotag'].split(',')
            geotag = GeoTag(user=user,
                            lat=float(lat),
                            lon=float(lon),
                            zoom=int(zoom))
            geotag.save()
            sound.geotag = geotag

    # 8 set description, tags
    sound.description = sound_fields['description']
    sound.set_tags(clean_and_split_tags(sound_fields['tags']))
    #sound.set_tags([t.lower() for t in sound_fields['tags'].split(" ") if t])

    # 8.5 set uploaded apiv2 client
    sound.uploaded_with_apiv2_client = apiv2_client

    # 9 save!
    sound.save()

    # 10 create moderation tickets if needed
    if user.profile.is_whitelisted:
        sound.change_moderation_state('OK', do_not_update_related_stuff=True)
    else:
        # create moderation ticket!
        sound.create_moderation_ticket()
        invalidate_template_cache("user_header", user.id)
예제 #22
0
    # 7 create geotag objects
    # format: lat#lon#zoom
    if 'geotag' in sound_fields:
        if sound_fields['geotag']:
            lat, lon, zoom = sound_fields['geotag'].split(',')
            geotag = GeoTag(user=user,
                lat=float(lat),
                lon=float(lon),
                zoom=int(zoom))
            geotag.save()
            sound.geotag = geotag

    # 8 set description, tags
    sound.description = sound_fields['description']
    sound.set_tags(clean_and_split_tags(sound_fields['tags']))
    #sound.set_tags([t.lower() for t in sound_fields['tags'].split(" ") if t])

    # 8.5 set uploaded apiv2 client
    sound.uploaded_with_apiv2_client = apiv2_client

    # 9 save!
    sound.save()

    # 10 create moderation tickets if needed
    if user.profile.is_whitelisted:
        sound.change_moderation_state('OK', do_not_update_related_stuff=True)
    else:
        # create moderation ticket!
        sound.create_moderation_ticket()
        invalidate_template_cache("user_header", user.id)