feat: play_history
This commit is contained in:
parent
9d7edd63b2
commit
9b485d14e1
|
@ -10,7 +10,7 @@ app_name = 'courses'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.IndexView.as_view(), name='index'),
|
path('', views.IndexView.as_view(), name='index'),
|
||||||
path('course/<int:pk>/', views.CourseDetailView.as_view(), name='course-detail'),
|
path('course/<int:pk>/', views.CourseDetailView.as_view(), name='course-detail'),
|
||||||
path('course/<int:pk>/lesson/<int:lesson_pk>/', views.LessonDetailView.as_view(), name='lesson-detail'),
|
path('course/<int:course_pk>/lesson/<int:lesson_pk>/', views.LessonDetailView.as_view(), name='lesson-detail'),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
from django.http import HttpResponse, JsonResponse
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.views import View
|
from django.views import View
|
||||||
from apps.courses.models import Course, Lesson
|
from apps.courses.models import Course, Lesson
|
||||||
|
from apps.users.models import User, PlayHistory
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
@ -24,7 +29,27 @@ class CourseDetailView(View):
|
||||||
|
|
||||||
|
|
||||||
class LessonDetailView(View):
|
class LessonDetailView(View):
|
||||||
def get(self, request, pk, lesson_pk):
|
def get(self, request, course_pk, lesson_pk):
|
||||||
lesson = Lesson.objects.get(pk=lesson_pk)
|
lesson = Lesson.objects.get(pk=lesson_pk)
|
||||||
context = {'lesson': lesson}
|
play_history = PlayHistory.objects.filter(lesson=lesson, user=request.user).first()
|
||||||
|
if play_history:
|
||||||
|
play_time = play_history.time
|
||||||
|
else:
|
||||||
|
play_time = 0
|
||||||
|
print(play_time)
|
||||||
|
|
||||||
|
context = {'lesson': lesson, 'play_time': play_time}
|
||||||
return render(request, 'courses/lesson_detail.html', context)
|
return render(request, 'courses/lesson_detail.html', context)
|
||||||
|
|
||||||
|
def post(self, request, course_pk, lesson_pk):
|
||||||
|
# print the post data
|
||||||
|
print(json.loads(request.body))
|
||||||
|
play_time = json.loads(request.body)['time']
|
||||||
|
|
||||||
|
PlayHistory.objects.update_or_create(
|
||||||
|
lesson=Lesson.objects.get(pk=lesson_pk),
|
||||||
|
user=User.objects.get(id=request.user.id),
|
||||||
|
defaults={'time': play_time}
|
||||||
|
)
|
||||||
|
|
||||||
|
return JsonResponse({'status': 'ok'}, status=200)
|
|
@ -0,0 +1,26 @@
|
||||||
|
# Generated by Django 5.0.3 on 2024-03-19 13:19
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('courses', '0004_lesson_cover'),
|
||||||
|
('users', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='PlayHistory',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('time', models.IntegerField()),
|
||||||
|
('update_time', models.DateTimeField(auto_now=True)),
|
||||||
|
('lesson', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='courses.lesson')),
|
||||||
|
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,7 +1,19 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
|
|
||||||
|
from apps.courses.models import Course, Lesson
|
||||||
|
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class User(AbstractUser):
|
class User(AbstractUser):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PlayHistory(models.Model):
|
||||||
|
lesson = models.ForeignKey(Lesson, on_delete=models.SET_NULL, null=True)
|
||||||
|
time = models.IntegerField()
|
||||||
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
update_time = models.DateTimeField(auto_now=True)
|
||||||
|
|
|
@ -18,10 +18,54 @@
|
||||||
<script src="https://cdn.plyr.io/3.7.8/plyr.polyfilled.js"></script>
|
<script src="https://cdn.plyr.io/3.7.8/plyr.polyfilled.js"></script>
|
||||||
<script>
|
<script>
|
||||||
const player = new Plyr('#player');
|
const player = new Plyr('#player');
|
||||||
|
player.currentTime = {{ play_time }};
|
||||||
|
console.log('Current time:', player.currentTime);
|
||||||
|
|
||||||
|
function getCookie(name) {
|
||||||
|
let cookieValue = null;
|
||||||
|
if (document.cookie && document.cookie !== '') {
|
||||||
|
let cookies = document.cookie.split(';');
|
||||||
|
for (let i = 0; i < cookies.length; i++) {
|
||||||
|
let cookie = cookies[i].trim();
|
||||||
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
||||||
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cookieValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let csrftoken = getCookie('csrftoken');
|
||||||
|
|
||||||
|
let url = '{{ lesson.get_absolute_url }}';
|
||||||
|
|
||||||
|
function postData(url = '', data = {}) {
|
||||||
|
return fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRFToken': csrftoken
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
})
|
||||||
|
.then(response => response.json());
|
||||||
|
}
|
||||||
|
|
||||||
// print current time on play every 10 seconds
|
// print current time on play every 10 seconds
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
console.log('Current time:', player.currentTime);
|
console.log('Current time:', player.currentTime);
|
||||||
|
let data = {
|
||||||
|
user: '{{ request.user.id }}',
|
||||||
|
lesson: '{{ lesson.id }}',
|
||||||
|
time: player.currentTime};
|
||||||
|
|
||||||
|
postData(url, data)
|
||||||
|
.then(data => {
|
||||||
|
console.log(data);
|
||||||
|
});
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
player.on('pause', event => {
|
player.on('pause', event => {
|
||||||
console.log('Video paused', player.currentTime);
|
console.log('Video paused', player.currentTime);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue