Skip to content

Commit

Permalink
[#N/A] Fixed user links on fyp to update with JS. Removed sounds inde…
Browse files Browse the repository at this point in the history
…x and made for_you the root. Made comment form reset after a user comments or goes to a different sound.
  • Loading branch information
paulmckissock committed Jul 29, 2024
1 parent ea5845a commit fb99c65
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 41 deletions.
14 changes: 8 additions & 6 deletions audioapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class Meta:
model = Comment
fields = ["text"]
widgets = {
'text': forms.Textarea(attrs={
'rows': 5, # Number of rows
'cols': 30, # Number of columns
'class': 'w-full p-2 border border-gray-300 rounded', # Tailwind CSS classes
'placeholder': 'Write your comment here...',
}),
"text": forms.Textarea(
attrs={
"rows": 5, # Number of rows
"cols": 30, # Number of columns
"class": "w-full p-2 border border-gray-300 rounded", # Tailwind CSS classes
"placeholder": "Write your comment here...",
}
),
}
1 change: 0 additions & 1 deletion audioapp/templates/audioapp/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<header class="bg-white shadow">
<nav class="container mx-auto flex justify-between items-center p-4">
<div class="flex items-center space-x-4">
<a href="{% url 'index' %}" class="text-blue-500 hover:text-blue-700">Home</a>
<a href="{% url 'for_you' %}" class="text-blue-500 hover:text-blue-700">For You</a>
<a href="{% url 'upload_audio' %}" class="text-blue-500 hover:text-blue-700">Upload Sound</a>
</div>
Expand Down
26 changes: 19 additions & 7 deletions audioapp/templates/audioapp/for_you.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ <h1 class="text-2xl font-bold text-center my-8">For You</h1>
<div class="flex items-center space-x-4">
<h2 id="audio-title" class="text-xl font-bold">{{ audio_file.title }}</h2>
<p id="audio-username" class="text-sm text-gray-600">
By <a href="{% url 'user_detail' audio_file.user.username %}" class="text-sm text-gray-600 hover:underline">{{ audio_file.user.username }}</a>
</p>
By <a id="audio-user-link" href="{% url 'user_detail' audio_file.user.username %}" class="text-sm text-gray-600 hover:underline">{{ audio_file.user.username }}</a>
</p>
</div>
<div class="flex items-center space-x-4 ml-auto">
<p id="audio-like-count">{{ audio_file.like_count }} Likes</p>
Expand Down Expand Up @@ -84,14 +84,16 @@ <h2 class="text-xl font-bold">Comments</h2>
audioPlayer.load();
if (document.getElementById('autoplay-checkbox').checked) {
audioPlayer.play();
}

}
}
document.getElementById('comment-form').reset();
}
var data = JSON.parse(xhr.responseText);
document.getElementById('audio-title').textContent = data.title;
document.getElementById('audio-description').textContent = data.description;
document.getElementById('audio-like-count').textContent = data.like_count + ' Likes';
document.getElementById('audio-username').textContent = 'By ' + data.username;
var userProfileLink = document.getElementById('audio-user-link');
userProfileLink.href = data.profile_url;
userProfileLink.textContent = data.username;

document.getElementById('previous-button').disabled = data.fyp_index <= 0;
document.getElementById('next-button').disabled = data.reached_end;
Expand All @@ -107,11 +109,20 @@ <h2 class="text-xl font-bold">Comments</h2>
likeButton.classList.remove('text-red-500');
likeButton.classList.add('text-black-500');
}

var commentsList = document.getElementById('comments-list');
commentsList.innerHTML = '';
data.comments.forEach(function(comment) {
var li = document.createElement('li');
li.textContent = comment.user + ': ' + comment.text;

var userLink = document.createElement('a');
userLink.href = comment.profile_url;
userLink.className = 'text-m text-black-600 hover:underline';
userLink.textContent = comment.user;

li.appendChild(userLink);
li.appendChild(document.createTextNode(': ' + comment.text));

commentsList.appendChild(li);
});

Expand Down Expand Up @@ -148,6 +159,7 @@ <h2 class="text-xl font-bold">Comments</h2>
var formData = new FormData(this);
formData.append('action', 'comment');
sendAudioRequest('comment', 'POST', formData);
document.getElementById('comment-form').reset();
});

document.getElementById('autoplay-checkbox').addEventListener('change', function() {
Expand Down
12 changes: 0 additions & 12 deletions audioapp/templates/audioapp/index.html

This file was deleted.

6 changes: 0 additions & 6 deletions audioapp/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ def setUp(self):
self.audio_files = [AudioFileFactory(user=self.user) for _ in range(5)]
self.client.login(username="testuser", password="password")

def test_index_view(self):
response = self.client.get(reverse("index"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "index.html")
self.assertContains(response, self.audio_file.title)

def test_profile_view_authenticated(self):
response = self.client.get(reverse("profile"))
self.assertEqual(response.status_code, 200)
Expand Down
2 changes: 1 addition & 1 deletion audioapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


urlpatterns = [
path("", views.index, name="index"),
path("", for_you, name="for_you"),
path("accounts/", include("allauth.urls")),
path("accounts/profile/", views.profile_view, name="profile"),
path("upload/", upload_audio, name="upload_audio"),
Expand Down
18 changes: 10 additions & 8 deletions audioapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@


# Create your views here.
def index(request):
audio_files = AudioFile.objects.all()
return render(request, "index.html", {"audio_files": audio_files})


@login_required
def profile_view(request):
audio_files = AudioFile.objects.filter(user=request.user)
Expand Down Expand Up @@ -138,9 +133,9 @@ def handle_like(request, audio_file):
print("Like created:", like)
else:
Like.objects.filter(user=request.user, audio_file=audio_file).delete()

def has_liked(request, audio_file):
return Like.objects.filter(user=request.user, audio_file=audio_file).exists()
return Like.objects.filter(user=request.user, audio_file=audio_file).exists()

def fyp_xml_http_request(audio_file, reached_end, fyp_index, request):
return JsonResponse(
Expand All @@ -153,8 +148,15 @@ def fyp_xml_http_request(audio_file, reached_end, fyp_index, request):
"has_liked": has_liked(request, audio_file),
"username": audio_file.user.username,
"like_count": audio_file.like_set.count(),
"profile_url": reverse("user_detail", args=[audio_file.user.username]),
"comments": [
{"user": comment.user.username, "text": comment.text}
{
"user": comment.user.username,
"text": comment.text,
"profile_url": reverse(
"user_detail", args=[comment.user.username]
),
}
for comment in audio_file.comments.all()
],
}
Expand Down

0 comments on commit fb99c65

Please sign in to comment.