diff --git a/myproject/docker-compose.yml b/myproject/docker-compose.yml index 18aa0078ba37d93c0e8027d37dc2f374dc0c31b0..646ed6edf7373383aaeadede99e06f9863a8b147 100644 --- a/myproject/docker-compose.yml +++ b/myproject/docker-compose.yml @@ -52,15 +52,11 @@ services: - --rest_api_port=8501 - --model_name=instrument_model - --monitoring_config_file=/etc/tensorflow_serving/monitoring_config.txt - ports: - - "8501:8501" prometheus: image: prom/prometheus volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml - ports: - - "9090:9090" volumes: static_volume: diff --git a/myproject/myapp/templates/_base.html b/myproject/myapp/templates/_base.html index 11c6ad581d8058aa77270adc5aed2e5239f0489f..c067bc86e58e68fffeb953848fd0cb076cf1a0e7 100644 --- a/myproject/myapp/templates/_base.html +++ b/myproject/myapp/templates/_base.html @@ -93,6 +93,13 @@ </nav> {% endblock navbar %} <div class="container mx-auto mt-6"> + {% if messages %} + <div class="space-y-4"> + {% for message in messages %} + <div class="{% if message.tags %}alert-{{ message.tags }}{% endif %}">{{ message }}</div> + {% endfor %} + </div> + {% endif %} {% block content %} {% endblock content %} {% block scripts %} {% endblock scripts %} diff --git a/myproject/myapp/urls.py b/myproject/myapp/urls.py index 2126860b3c400284aabb580e009e908ca98ebac2..999a2ba94e399fecd24a2000c6015a1a7858a0d0 100644 --- a/myproject/myapp/urls.py +++ b/myproject/myapp/urls.py @@ -1,6 +1,6 @@ from django.urls import path from .views import InstrumentDetectionView, ModelPerformanceView, index, log_fileupload, users, maintenance, \ -handler404, handler500, terms_conditions, privacy_policy, handling_music_file, pricing, generate_pdf, admin_table,\ +handler404, handler500, terms_conditions, privacy_policy, pricing, generate_pdf, admin_table,\ change_user_type, submit_feedback from .payments import create_payment, execute_payment, payment_cancelled, payment_success from django.contrib.auth import views as auth_views @@ -20,7 +20,6 @@ urlpatterns = [ path('terms_conditions/', terms_conditions, name='terms_conditions'), path('pricay_policy/', privacy_policy, name='privacy_policy'), path('pricing/', pricing, name='pricing'), - path('uploading_file/', handling_music_file, name='uploading_file'), path('submit_feedback/', submit_feedback, name='submit_feedback'), path('generate_pdf/', generate_pdf, name='generate_pdf'), path('pricing/', pricing, name='pricing'), diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index d7354e9b7cdb803af11d3b5672fcbadfdc6e1258..b916e7337b20e00bbb31d7b464942a76a3e3ec3e 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -52,20 +52,6 @@ def get_log_data(user, action, status='success', file=None, description=None, fe def create_log(user, log_data): Log.objects.create(user=user, log=log_data, feedback=log_data.get('feedback')) -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(request.user ,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) - log_data = get_log_data(request.user ,Action.INVALID_FILE, 'error') - create_log(None, log_data) - return HttpResponse('File invalid',log_data) - @csrf_exempt def log_fileupload(request): if request.method == 'POST': @@ -105,55 +91,66 @@ def submit_feedback(request): return redirect('index') def admin_table(request): - # Execute the query and fetch all rows - query = """SELECT date, log, user_id, feedback FROM myapp_log ORDER BY date DESC""" - with connection.cursor() as cursor: - cursor.execute(query) - rows = cursor.fetchall() - - # Create a list of dictionaries from the query results - data = [] - for row in rows: - # Parse the JSON string into a dictionary - log = json.loads(row[1]) - # Get the user object based on the user_id - user_id = row[2] - # Get the feedback value - feedback = row[3] - # Create a dictionary with the date, user, JSON fields, and feedback - date = row[0].strftime('%Y-%m-%d %H:%M:%S') - entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status'], - 'description': log['description'], 'feedback': feedback} - data.append(entry) - - # Return the data as a JSON response - return JsonResponse({'data': data}, safe=False) - + if request.user.is_authenticated: + if request.user.profile.user_type != 0 or request.user.is_superuser: + # Execute the query and fetch all rows + query = """SELECT date, log, user_id, feedback FROM myapp_log ORDER BY date DESC""" + with connection.cursor() as cursor: + cursor.execute(query) + rows = cursor.fetchall() + + # Create a list of dictionaries from the query results + data = [] + for row in rows: + # Parse the JSON string into a dictionary + log = json.loads(row[1]) + # Get the user object based on the user_id + user_id = row[2] + # Get the feedback value + feedback = row[3] + # Create a dictionary with the date, user, JSON fields, and feedback + date = row[0].strftime('%Y-%m-%d %H:%M:%S') + entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status'], + 'description': log['description'], 'feedback': feedback} + data.append(entry) + + # Return the data as a JSON response + return JsonResponse({'data': data}, safe=False) + else: + messages.info(request, 'Must be logged in as a non-basic user to access this page.') + return redirect('index') + else: + messages.info(request, 'Must be logged in as a non-basic user to access this page.') + return redirect('login') def user_table(request): - user_id = request.user.id - # Only display user logs code below - query = """SELECT date, log, user_id, feedback FROM myapp_log WHERE user_id = {} ORDER BY date DESC""".format(user_id) - with connection.cursor() as cursor: - cursor.execute(query) - rows = cursor.fetchall() - - # Create a list of dictionaries from the query results - data = [] - for row in rows: - # Parse the JSON string into a dictionary - log = json.loads(row[1]) - # Get the user object based on the user_id - user_id = row[2] - # Get the feedback value - feedback = row[3] - # Create a dictionary with the date, user, JSON fields, and feedback - date = row[0].strftime('%Y-%m-%d %H:%M:%S') - entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status'], - 'description': log['description'], 'feedback': feedback} - data.append(entry) - - # Return the data as a JSON response - return JsonResponse({'data': data}, safe=False) + if request.user.is_authenticated: + user_id = request.user.id + # Only display user logs code below + query = """SELECT date, log, user_id, feedback FROM myapp_log WHERE user_id = {} ORDER BY date DESC""".format(user_id) + with connection.cursor() as cursor: + cursor.execute(query) + rows = cursor.fetchall() + + # Create a list of dictionaries from the query results + data = [] + for row in rows: + # Parse the JSON string into a dictionary + log = json.loads(row[1]) + # Get the user object based on the user_id + user_id = row[2] + # Get the feedback value + feedback = row[3] + # Create a dictionary with the date, user, JSON fields, and feedback + date = row[0].strftime('%Y-%m-%d %H:%M:%S') + entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status'], + 'description': log['description'], 'feedback': feedback} + data.append(entry) + + # Return the data as a JSON response + return JsonResponse({'data': data}, safe=False) + else: + messages.info(request, 'Must be logged in as a user to access this page.') + return redirect('login') def index(request): # Initialize default context @@ -208,9 +205,7 @@ def users(request): if request.user.is_authenticated: # Make a request to the admin_table view to get the data context = {} - data_admin = admin_table(request) data_user = user_table(request) - admin_dict = json.loads(data_admin.content) user_dict = json.loads(data_user.content) token_count = UserTokenCount.objects.get(user=request.user).token_count user_profile = request.user.profile @@ -219,7 +214,12 @@ def users(request): # Pass the data as a context variable to the template # !!! ADMIN DATA ONLY DISPLAYED AND GET IF USER IS ADMIN !!! - context['admin_data'] = admin_dict['data'] + if request.user.profile.user_type != 0 or request.user.is_superuser: + data_admin = admin_table(request) + admin_dict = json.loads(data_admin.content) + context['admin_data'] = admin_dict['data'] + + context['user_data'] = user_dict['data'] context['token_count'] = token_count context['user_profile'] = user_profile @@ -227,7 +227,9 @@ def users(request): context['all_user_profiles'] = all_user_profiles # Add all_user_profiles to the context return render(request, 'user_page.html', context) - return redirect('login') + else: + messages.info(request, 'Must be logged in as a user to access this page.') + return redirect('login') def handler404(request, *args, **kwargs): response = render(request, '404.html', {}) @@ -249,6 +251,12 @@ class RegisterView(generic.CreateView): success_url = reverse_lazy('index') template_name = 'registration/register.html' + def dispatch(self, request, *args, **kwargs): + if request.user.is_authenticated: + messages.info(request, 'You are already logged in.') + return redirect('index') + return super().dispatch(request, *args, **kwargs) + def form_valid(self, form): response = super().form_valid(form) user = self.object # Grab the user instance @@ -271,6 +279,12 @@ class CustomLoginView(LoginView): authentication_form = LoginAuthenticationForm template_name = 'registration/login.html' + def dispatch(self, request, *args, **kwargs): + if request.user.is_authenticated: + messages.info(request, 'You are already logged in.') + return redirect('index') + return super().dispatch(request, *args, **kwargs) + def form_valid(self, form): # Create log if user is authenticated login(self.request, form.get_user()) @@ -281,6 +295,8 @@ class CustomLoginView(LoginView): return super().form_valid(form) + + def terms_conditions(request): return render(request, 'terms_conditions.html') @@ -304,14 +320,21 @@ def generate_pdf(request): # Running the audio file through the model class InstrumentDetectionView(APIView): + + def dispatch(self, request, *args, **kwargs): + user_token_count = UserTokenCount.objects.get(user=request.user) + if request.user.is_anonymous: + messages.info(request, 'Must be logged in as a user to access this page.') + return redirect('login') + elif user_token_count.token_count < 1: + messages.info(request, 'You do not have enough tokens to make a prediction.') + return redirect('pricing') + else: return super().dispatch(request, *args, **kwargs) + def post(self, request): # Get the user's token count user_token_count = UserTokenCount.objects.get(user=request.user) - # Check if the user has more than one token - if user_token_count.token_count < 1: - return Response({'error': 'Insufficient tokens'}, status=status.HTTP_403_FORBIDDEN) - # Decrease the user's token count by one user_token_count.token_count -= 1 user_token_count.save() @@ -370,6 +393,16 @@ class InstrumentDetectionView(APIView): class ModelPerformanceView(UserPassesTestMixin, TemplateView): template_name = 'model_performance.html' + def dispatch(self, request, *args, **kwargs): + if request.user.is_anonymous: + messages.info(request, 'Must be logged in as an ML Engineer or Admin to access this page.') + return redirect('users') + elif request.user.profile.user_type != 2 or not request.user.is_superuser: + messages.info(request, 'Must be logged in as an ML Engineer or Admin to access this page.') + return redirect('users') + else: + return super().dispatch(request, *args, **kwargs) + def test_func(self): return self.request.user.is_authenticated and (self.request.user.is_superuser or self.request.user.profile.user_type == 2)