Skip to content

Commit

Permalink
Changes sample file name at upload and download
Browse files Browse the repository at this point in the history
  • Loading branch information
qtipee committed Jul 23, 2020
1 parent 563fb9b commit 6f82932
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/safm_api/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models
from django.contrib.auth.models import User

import os, unicodedata
import os
import audiofile as af
import numpy as np
from aubio import source, tempo
Expand Down Expand Up @@ -33,10 +33,8 @@ class Mode(models.TextChoices):
MAJOR = 'maj'

def user_directory_path(instance, filename):
# File will be uploaded to MEDIA_ROOT/samples/<username>/<filename>
filename = unicodedata.normalize('NFKD', filename).encode('ascii', 'ignore')
ext = os.path.splitext(filename)[1]
return 'samples/{0}/{1}{2}'.format(instance.user.id, filename, ext)
# File will be uploaded to MEDIA_ROOT/samples/<username>/<sample_name>
return 'samples/{0}/{1}'.format(instance.user.id, filename)

# Table columns
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
Expand Down
11 changes: 6 additions & 5 deletions src/safm_api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,20 +318,21 @@ def test_sample_filename(self):
'''
Checks that the filename of an uploaded sample is correctly converted.
'''
filename = self.user.username
file = SimpleUploadedFile('test.wav', b'This is the file content.')
name = 'Sample_Test_File'
filename = 'test.wav'
file = SimpleUploadedFile(filename, b'This is the file content.')

sample = Sample(
user=self.user,
name=filename,
name=name,
file=file,
key=Sample.Key.A,
mode=Sample.Mode.MAJOR
)
sample.save()

sample = Sample.objects.get(name=filename)
expected_filename = 'samples/{0}/{1}.wav'.format(self.user.id, filename)
sample = Sample.objects.get(name=name)
expected_filename = 'samples/{0}/{1}'.format(self.user.id, filename)
self.assertEqual(sample.file, expected_filename)

@tag('sample_null_user_delete')
Expand Down
1 change: 0 additions & 1 deletion src/safm_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ class UserDownloads(APIView):

def get(self, request):
user = request.user
# From most recent to oldest
user_downloads = UserSampleDownload.objects.filter(user=user).order_by('-datetime_download')
user_downloads_serializer = UserDownloadSerializer(user_downloads, many=True)

Expand Down

0 comments on commit 6f82932

Please sign in to comment.