Skip to content

Commit

Permalink
[#8] Added for you page. Still need to setup a previous button.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmckissock committed Jul 25, 2024
1 parent 98943b7 commit 6143709
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
19 changes: 19 additions & 0 deletions audioapp/templates/audioapp/for_you.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'base.html' %}

{% block content %}
<h1>For You</h1>
{% if audio_file %}
<h2>{{ audio_file.title }}</h2>
<p> {{audo_file.description}}</p>
<audio controls autoplay>
<source src="{{ audio_file.file.url }}" type="audio/mp4">
Your browser does not support the audio element.
</audio>
<p>{{ audio_file.description }}</p>
<form method="get" action="{% url 'for_you' %}">
<button type="submit">Next</button>
</form>
{% else %}
<p>No audio files available.</p>
{% endif %}
{% endblock %}
3 changes: 2 additions & 1 deletion audioapp/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import include, path
from . import views
from .views import upload_audio, audio_detail, user_detail, like_audio, unlike_audio
from .views import upload_audio, audio_detail, user_detail, like_audio, unlike_audio, for_you


urlpatterns = [
Expand All @@ -12,4 +12,5 @@
path('user/<str:username>/', user_detail, name='user_detail'),
path('audio/<int:pk>/like/', like_audio, name='like_audio'),
path('audio/<int:pk>/unlike/', unlike_audio, name='unlike_audio'),
path('for_you/', for_you, name='for_you'),
]
12 changes: 10 additions & 2 deletions audioapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from django.contrib.auth.decorators import login_required
import allauth
import allauth, random
from .forms import AudioFileForm, CommentForm
from .models import AudioFile, Like, Comment
from django.contrib.auth.models import User
Expand Down Expand Up @@ -77,4 +77,12 @@ def unlike_audio(request, pk):
audio_file = get_object_or_404(AudioFile, pk=pk)
Like.objects.filter(user=request.user, audio_file=audio_file).delete()
return redirect('audio_detail', pk=audio_file.pk)


@login_required
def for_you(request):
audio_files = list(AudioFile.objects.all())
if audio_files:
audio_file = random.choice(audio_files)
else:
audio_file = None
return render(request, 'for_you.html', {'audio_file': audio_file})

0 comments on commit 6143709

Please sign in to comment.