diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py index 7dc56b8baaceda6431a612b5bf489da923978f3a..9d728a7ef58e7ef556b7b06dd4b245fbe7ef2a6f 100644 --- a/myproject/myapp/models.py +++ b/myproject/myapp/models.py @@ -102,6 +102,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 db04949e1d2a937f992abe928c21330c2c815937..b521f12d4f606c73627d57b41695b13eb593f422 100644 --- a/myproject/myapp/templates/index1.html +++ b/myproject/myapp/templates/index1.html @@ -54,15 +54,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/urls.py b/myproject/myapp/urls.py index 8e0269800f768e72ec60ddb8174e9262a0095816..ff5404ea65fe5b9ef049ff1cecbaf14eb91f29c6 100644 --- a/myproject/myapp/urls.py +++ b/myproject/myapp/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from .views import InstrumentDetectionView, index, users, maintenance, handler404, handler500, terms_conditions, privacy_policy, handling_music_file, pricing, generate_pdf, admin_table +from .views import InstrumentDetectionView, index, log_fileupload, users, maintenance, handler404, handler500, terms_conditions, privacy_policy, handling_music_file, pricing, generate_pdf, admin_table from django.contrib.auth import views as auth_views # Authentication @@ -27,6 +27,7 @@ urlpatterns = [ path('password_change/', auth_views.PasswordChangeView.as_view(template_name='password_change_form.html'), name='password_change'), path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html'), name='password_change_done'), # path('user_logout/', auth_views.LogoutView.as_view(next_page='index'), name='user_logout') + path('log_fileupload', log_fileupload, name='log_fileupload'), # Authentication path('login/', CustomLoginView.as_view(), name='login'), diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index 34e3e538fceba7e9c1439b0ccc81bcfa817fbc14..17fd4ac84776d6b30d9d9d31fc6727a314679e23 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""" @@ -122,6 +139,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 @@ -223,7 +244,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): @@ -280,15 +310,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']