diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py index b09b3f1348e1803ab24326caa517da4d3ac5937b..edc97afd52e944d8c8d7dc32fffdd873dc0c94a7 100644 --- a/myproject/myapp/models.py +++ b/myproject/myapp/models.py @@ -122,6 +122,7 @@ class Log(models.Model): user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) log = models.JSONField() + # # LOGIN # log_data = get_log_data(Action.LOGIN, 'success', user=request.user.username) # create_log(log_data) diff --git a/myproject/myapp/templates/index1.html b/myproject/myapp/templates/index1.html index 530819ec9591a2a77e2bacb4665127f434eff3e0..fda1160729533a50f503412da44b80ca7fd4f15c 100644 --- a/myproject/myapp/templates/index1.html +++ b/myproject/myapp/templates/index1.html @@ -66,15 +66,26 @@ <script> function loadAudioFile(event) { - var file = event.target.files[0]; - if (file) { - wavesurfer.loadBlob(file); - document.getElementById('player').classList.remove('hidden'); - wavesurfer.on('ready', function () { - document.getElementById('playButton').disabled = false; - }); - } + var file = event.target.files[0]; + if (file) { + wavesurfer.loadBlob(file); + document.getElementById('player').classList.remove('hidden'); + wavesurfer.on('ready', function () { + document.getElementById('playButton').disabled = false; + + // After player is loaded, make an AJAX request to create a log + var xhr = new XMLHttpRequest(); + xhr.open('POST', '/log_fileupload', true); + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.send(JSON.stringify({ + 'action': 'UPLOAD', + 'status': 'success', + 'file': file.name, + 'description': 'File uploaded and player loaded' + })); + }); } +} function submitForm() { var form = document.getElementById('uploadForm'); diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index a04cd1bbaca8bd0515bf55761b3ff87d3d048318..3aab0b85ae4a7ae0220e8dadf6c6656f6e5b3542 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -30,6 +30,7 @@ from django.views import generic from .models import Profile from .forms import UserRegisterForm, LoginAuthenticationForm from django.contrib.auth.views import LoginView +from django.views.decorators.csrf import csrf_exempt logger = logging.getLogger(__name__) @@ -59,6 +60,22 @@ def handling_music_file(request): create_log(None, log_data) return HttpResponse('File invalid',log_data) +@csrf_exempt +def log_fileupload(request): + if request.method == 'POST': + data = json.loads(request.body) + status = data.get('status') + file = data.get('file') + description = data.get('description') + + if request.user.is_authenticated: + log_data = get_log_data(Action.UPLOAD_FILE, status, file, description) + create_log(request.user, log_data) + + return JsonResponse({'message': 'Log created successfully'}, status=201) + + return JsonResponse({'error': 'Invalid request'}, status=400) + def admin_table(request): # Execute the query and fetch all rows query = """SELECT date, user, log FROM myapp_log ORDER BY date DESC""" @@ -124,6 +141,10 @@ def index(request): # Ensure there's a response and it contains predictions before updating context if response and hasattr(response, 'data') and 'predictions' in response.data: context['predictions'] = response.data['predictions'] + if request.user.is_authenticated: + log_data = get_log_data(Action.RUN_ALGORITHM, 'success', file=uploaded_file.name,\ + description=response.data["predictions"]) + create_log(request.user, log_data) else: context['form'] = form # For GET requests or if form is not valid, render the page with the default or updated context @@ -231,7 +252,16 @@ class RegisterView(generic.CreateView): class CustomLoginView(LoginView): authentication_form = LoginAuthenticationForm - template_name = 'registration/login.html' + template_name = 'registration/login.html' + + def form_valid(self, form): + # Create log if user is authenticated + login(self.request, form.get_user()) + + log_data = get_log_data(Action.LOGIN, 'success') + create_log(form.get_user(), log_data) + + return super().form_valid(form) def terms_conditions(request): @@ -288,15 +318,6 @@ class InstrumentDetectionView(APIView): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - def convert_to_percentages(self, predictions): - # Assuming predictions is a list of lists - percentage_predictions = [] - for prediction in predictions: - total = sum(prediction) - # Convert each number to a percentage of the total, rounded to 2 decimal places - percentages = [round((number / total) * 100, 2) for number in prediction] - percentage_predictions.append(percentages) - return percentage_predictions def format_predictions(self, predictions): instruments = ['Guitar', 'Drum', 'Violin', 'Piano']