From 22b21327b3a86c304c414e9abedfad215d5d40be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20J=C3=B3zef=20Drupisz?= <kdrupisz@meta.com> Date: Sat, 9 Mar 2024 14:19:05 +0000 Subject: [PATCH] Logs updated audio play. --- myproject/debug.log | 128 ++++++++++++++++++++++++++ myproject/myapp/models.py | 48 +++++++++- myproject/myapp/templates/index1.html | 120 +++++++++++++++--------- myproject/myapp/urls.py | 7 +- myproject/myapp/views.py | 26 +++++- myproject/myproject/settings.py | 16 ++++ 6 files changed, 297 insertions(+), 48 deletions(-) create mode 100644 myproject/debug.log diff --git a/myproject/debug.log b/myproject/debug.log new file mode 100644 index 0000000..f59a078 --- /dev/null +++ b/myproject/debug.log @@ -0,0 +1,128 @@ +User uploaded file +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +User uploaded file +Error +Warning +/usr/src/app/myapp/urls.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/urls.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/urls.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/urls.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/urls.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +User uploaded file +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +Not Found: /favicon.ico +User uploaded file has been received +Internal Server Error: /uploading_file/ +Traceback (most recent call last): + File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner + response = get_response(request) + ^^^^^^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 204, in _get_response + self.check_response(response, callback) + File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 332, in check_response + raise ValueError( +ValueError: The view myapp.views.handling_music_file didn't return an HttpResponse object. It returned None instead. +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +User uploaded file has been received +Internal Server Error: /uploading_file/ +Traceback (most recent call last): + File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner + response = get_response(request) + ^^^^^^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.11/site-packages/django/utils/deprecation.py", line 136, in __call__ + response = self.process_response(request, response) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.11/site-packages/django/middleware/clickjacking.py", line 27, in process_response + if response.get("X-Frame-Options") is not None: + ^^^^^^^^^^^^ +AttributeError: 'str' object has no attribute 'get' +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +User uploaded file has been received +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +User uploaded file has been received +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +User uploaded file has been received ethereal-vistas-191254.mp3 +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +Internal Server Error: /uploading_file/ +Traceback (most recent call last): + File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner + response = get_response(request) + ^^^^^^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response + response = wrapped_callback(request, *callback_args, **callback_kwargs) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/usr/src/app/myapp/views.py", line 14, in handling_music_file + logger.info('Unauthenticated user uploaded file %s at %s', request.FILES['audio_file'].name, timezone.now()) + ^^^^^^^^ +NameError: name 'timezone' is not defined +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +Unauthenticated user uploaded file ethereal-vistas-191254.mp3 at 2024-03-09 13:53:20.754075+00:00 +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/models.py changed, reloading. +Watching for file changes with StatReloader +/usr/src/app/myapp/views.py changed, reloading. +Watching for file changes with StatReloader diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py index 4e65c8b..7a6ee44 100644 --- a/myproject/myapp/models.py +++ b/myproject/myapp/models.py @@ -1,5 +1,21 @@ from django.db import models +from django.contrib.auth.models import User +from django.contrib.postgres.fields import JSONField +from enum import Enum +class Action(Enum): + UPLOAD_FILE = 'upload_file' + LOGIN = 'login' + REGISTER = 'register' + PAYMENT = 'payment' + GENERATE_FINANCIAL_STATEMENT = 'generate_financial_statement' + CHANGE_MLA = 'change_MLA' + RUN_ALGORITHM = 'run_algorithm' + INVALID_FILE = 'invalid_file' + INVALID_PASSWORD = 'invalid_password' + USER_DOES_NOT_EXIST = 'user_does_not_exist' + DOWNLOAD_BREAKDOWN = 'download_breakdown' + UNKNOWN = 'unknown' # # Usertypes # # --------- # # 0 - Basic User @@ -56,6 +72,34 @@ from django.db import models # date = models.DateTimeField() # data = models.CharField(max_length=2000) # uploader = models.ForeignKey("User", on_delete=models.CASCADE) - + class Audio(models.Model): - file = models.FileField('audio', upload_to='audio') \ No newline at end of file + file = models.FileField('audio', upload_to='audio') + +class Log(models.Model): + date = models.DateTimeField(auto_now_add=True) + user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) + log = models.JSONField() + +def get_log_data(action, status='success', file=None, **additional_field): + log_data = { + 'action': action, + 'status': status, + 'file': file, + } + log_data.update(additional_fields) + return log_data + + +# # LOGIN +# log_data = get_log_data(Action.LOGIN, 'success', user=request.user.username) +# create_log(log_data) +# # REGISTER +# log_data = get_log_data(Action.REGISTER, 'success', user=request.user.username) +# create_log(log_data) +# # INVALID_PASSWORD +# log_data = get_log_data(Action.INVALID_PASSWORD, 'error', user=request.user.username) +# create_log(log_data) +# # GENERATE_FINANCIAL_STATEMENT +# log_data = get_log_data(Action.GENERATE_FINANCIAL_STATEMENT, 'success', user=request.user.username) +# create_log(log_data) diff --git a/myproject/myapp/templates/index1.html b/myproject/myapp/templates/index1.html index 6e3f604..ef78841 100644 --- a/myproject/myapp/templates/index1.html +++ b/myproject/myapp/templates/index1.html @@ -2,11 +2,13 @@ {% block content %} <section class="bg-white dark:bg-gray-900"> + <script src="https://unpkg.com/wavesurfer.js"></script> + <div class="gap-16 items-center py-8 px-4 mx-auto max-w-screen-xl lg:grid lg:grid-cols-2"> <div class="font-light text-gray-500 sm:text-lg dark:text-gray-400"> <h2 class="mb-4 text-4xl tracking-tight font-extrabold text-gray-900 dark:text-white">An Intelligent System for Instrument Detection</h2> - <p class="mb-4">*placeholder input* We present to you a intelligent system for instrument detection. Using audio processing techinques and a convolutionsal - neural network we are able to classify instruments used in a song. other exciting words that might catch peoples attention and make them use our product. + <p class="mb-4">*placeholder input* We present to you a intelligent system for instrument detection. Using audio processing techinques and a convolutionsal + neural network we are able to classify instruments used in a song. other exciting words that might catch peoples attention and make them use our product. To use our service upload an mp3 file below. *placeholder input* </p> @@ -16,47 +18,83 @@ <img class="w-240 h-60 rounded-lg" src="{% static 'src/images/0_IPKn3dedq86U4UqP.png' %}" alt="CNN for audio"> </div> </div> - <div > - <form enctype="multipart/form-data" method="post" class="gap-16 items-center py-8 px-4 mx-auto max-w-screen-xl lg:grid lg:grid-cols-2"> + <div class="gap-16 items-center py-8 px-4 mx-auto max-w-screen-xl lg:grid lg:grid-cols-2" > + <form enctype="multipart/form-data" method="post" id="uploadForm"> {% csrf_token %} - <input - class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400" - id="audio_file" - name="audio_file" - type="file"> - - - <button type="submit" class="text-white bg-gray-800 hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:hover:bg-gray-700 dark:focus:ring-gray-700 dark:border-gray-700"> - Run Algorithm - </button> - </form> - </div> - - <audio id="myAudio"> - <source src="{% static 'src/media/G53-44104-1111-00083.wav' %}" type="audio/wav"> - Your browser does not support the audio element. - </audio> - - <div class="gap-16 items-center py-8 px-4 mx-auto max-w-screen-xl lg:grid lg:grid-cols-2 lg:py-8"> - <button onclick="playAudio()" type="button" class="text-white bg-gray-800 hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:hover:bg-gray-700 dark:focus:ring-gray-700 dark:border-gray-700"> - Play Audio</button> - <button onclick="pauseAudio()" type="button" class="text-white bg-gray-800 hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:hover:bg-gray-700 dark:focus:ring-gray-700 dark:border-gray-700"> - Pause Audio</button> + <input + class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400" + id="audio_file" + name="audio_file" + type="file" + accept=".mp3,.wav" + onchange="loadAudioFile(event)" + > + </form> + + <button type="submit" class="text-white bg-gray-800 hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:hover:bg-gray-700 dark:focus:ring-gray-700 dark:border-gray-700"> + Run Algorithm + </button> </div> - <script> - var x = document.getElementById("myAudio"); - - function playAudio() { - x.play(); - } - - function pauseAudio() { - x.pause(); - } - </script> - + + <div id="player" class="py-8 px-4 mx-auto max-w-screen-xl lg:py-8 hidden"> + <div id="waveform" class="w-full h-32 m-4"></div> + <button id="playButton" class="text-white bg-gray-800 hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:hover:bg-gray-700 dark:focus:ring-gray-700 dark:border-gray-700 w-full" disabled>Play</button> + </div> + <script> + var wavesurfer = WaveSurfer.create({ + container: '#waveform', + waveColor: 'gray', + progressColor: '#f9f1f1' + }); + function loadAudioFile(event) { + var file = event.target.files[0]; + var reader = new FileReader(); + reader.onload = function(e) { + wavesurfer.loadBlob(file); + document.getElementById('player').classList.remove('hidden'); + submitForm(); // Submit the form + } + reader.readAsDataURL(file); + } + wavesurfer.on('ready', function () { + document.getElementById('playButton').disabled = false; + }); + document.getElementById('playButton').addEventListener('click', function () { + wavesurfer.playPause(); + this.textContent = wavesurfer.isPlaying() ? 'Stop' : 'Play'; + }); + wavesurfer.on('play', function () { + document.getElementById('playButton').textContent = 'Stop'; + }); + wavesurfer.on('pause', function () { + document.getElementById('playButton').textContent = 'Play'; + }); + function submitForm() { + var form = document.getElementById('uploadForm'); + var formData = new FormData(form); + var xhr = new XMLHttpRequest(); + xhr.open('POST', '/uploading_file/', true); + xhr.setRequestHeader('X-CSRFToken', getCookie('csrftoken')); // Include the CSRF token + xhr.send(formData); + } + // Function to get the CSRF token + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie !== '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i].trim(); + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + </script> </section> - -{% endblock content %} \ No newline at end of file + +{% endblock content %} diff --git a/myproject/myapp/urls.py b/myproject/myapp/urls.py index 727aa51..4b7449c 100644 --- a/myproject/myapp/urls.py +++ b/myproject/myapp/urls.py @@ -8,7 +8,7 @@ from .views import register from .views import login from .views import terms_conditions from .views import privacy_policy - +from .views import handling_music_file from .views import pricing @@ -24,6 +24,7 @@ urlpatterns = [ path('login/', login, name='login'), path('terms_conditions/', terms_conditions, name='terms_conditions'), path('pricay_policy/', privacy_policy, name='privacy_policy'), - path('pricing/', pricing, name='pricing') -] + path('pricing/', pricing, name='pricing'), + path('uploading_file/', handling_music_file, name='uploading_file'), +] diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index afaaf33..44d758a 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -1,5 +1,27 @@ from django.shortcuts import render from django.template import RequestContext +import logging +from django.http import HttpResponse +from django.utils import timezone + +logger = logging.getLogger(__name__) + +def create_log(user, log_data): + Log.objects.create(user=user, log=log_data) + +def handling_music_file(request): + if request.method == 'POST': + if 'audio_file' in request.FILES: + log_data = { + 'action': 'File uploaded', + 'file': request.FILES['audio_file'].name, + } + log_data = get_log_data(Action.UPLOAD_FILE, 'success', file=request.FILES['audio_file'].name) + create_log(request.user if request.user.is_authenticated else None, log_data) + return HttpResponse('File uploaded successfully!') + log_data = get_log_data(Action.invalid_file, 'error') + create_log(None, log_data) + return HttpResponse('File invalid') def index(request): #for now this authenication just returns the main view @@ -14,7 +36,7 @@ def index(request): return render(request, 'index1.html') else: return render(request, 'index1.html') - + def users(request): return render(request, 'user_page.html') @@ -44,4 +66,4 @@ def privacy_policy(request): return render(request, 'privacy_policy.html') def pricing(request): - return render(request, 'pricing.html') \ No newline at end of file + return render(request, 'pricing.html') diff --git a/myproject/myproject/settings.py b/myproject/myproject/settings.py index 41a1691..ae24b76 100644 --- a/myproject/myproject/settings.py +++ b/myproject/myproject/settings.py @@ -65,6 +65,22 @@ MIDDLEWARE = [ ROOT_URLCONF = 'myproject.urls' +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'file': { + 'level': 'INFO', + 'class': 'logging.FileHandler', + 'filename': 'debug.log', + }, + }, + 'root': { + 'handlers': ['file'], + 'level': 'INFO', + } +} + TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', -- GitLab