Optimisation des requêtes js

This commit is contained in:
Klafyvel 2018-03-28 17:05:18 +02:00
parent 808e779237
commit 6976d3547c
6 changed files with 70 additions and 10 deletions

View file

@ -1,3 +1,13 @@
from django.contrib import admin
from .models import Playlist, Link
# Register your models here.
class PlaylistAdmin(admin.ModelAdmin):
list_display = ['name', 'date']
ordering = ['date']
class LinkAdmin(admin.ModelAdmin):
pass
admin.site.register(Playlist, PlaylistAdmin)
admin.site.register(Link, LinkAdmin)

View file

@ -1,5 +1,5 @@
from urllib.parse import urlparse
import datetime
import django.utils.timezone as timezone
from django import forms
@ -11,7 +11,9 @@ class PlaylistForm(forms.ModelForm):
fields = ['name']
def is_valid(self):
self.instance.date = datetime.datetime.now()
self.instance.date = timezone.now()
self.instance.last_get = timezone.now()
self.instance.last_update = timezone.now()
return super().is_valid()
class LinkForm(forms.Form):

View file

@ -0,0 +1,26 @@
# Generated by Django 2.0.3 on 2018-03-28 14:12
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('player', '0002_auto_20180324_2340'),
]
operations = [
migrations.AddField(
model_name='playlist',
name='last_get',
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='Dernière écoute'),
preserve_default=False,
),
migrations.AddField(
model_name='playlist',
name='last_update',
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='Dernière mise à jour'),
preserve_default=False,
),
]

View file

@ -9,6 +9,12 @@ class Playlist(models.Model):
max_length=255,
verbose_name="Nom de la playlist"
)
last_update = models.DateTimeField(
verbose_name="Dernière mise à jour"
)
last_get = models.DateTimeField(
verbose_name="Dernière écoute"
)
@staticmethod
def reverse_token(token):
@ -36,4 +42,4 @@ class Link(models.Model):
)
def __str__(self):
return "Link : " + self.url + " of " + str(self.playlist)
return "Link : " + self.token + " of " + str(self.playlist)

View file

@ -53,7 +53,7 @@ var current_link = -1;
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var last_sync = new Date().getTime();
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
@ -128,6 +128,10 @@ function addLink() {
return false;
}
function updateLinks(data) {
if (!data.updated) {
return;
}
last_sync = new Date().getTime();
var links = document.getElementById("links");
while (links.firstChild) {
links.removeChild(links.firstChild);
@ -135,9 +139,9 @@ function updateLinks(data) {
var model = document.getElementById('link_template');
var rerun = (current_link >= tracks.length ) || current_link < 0;
tracks = [];
for (var i=0; i<data.length; i++)
for (var i=0; i<data.tokens.length; i++)
{
var token = data[i].fields.token;
var token = data.tokens[i];
tracks.push(token);
var card = model.cloneNode(true);
card.style.display = 'block';
@ -161,6 +165,7 @@ function loadLinks() {
type: "get",
url: "{% url 'player:list' playlist.get_token %}",
async: true,
data: {last_sync: last_sync},
success: updateLinks
})
}

View file

@ -1,9 +1,10 @@
import datetime
import json
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.serializers import serialize
import django.utils.timezone as timezone
from player.models import Playlist, Link
from player.forms import PlaylistForm, LinkForm
@ -23,9 +24,17 @@ def new_playlist(request):
@csrf_exempt
def get_list(request, token):
p = get_object_or_404(Playlist, pk=Playlist.reverse_token(token))
p.last_get = timezone.now()
p.save()
s = serialize('json', p.link_set.all(), fields=('token',))
return HttpResponse(s, content_type='application/json')
d = {'tokens':[], 'updated':False}
last_up = p.last_update.timestamp()
last_sync = (int(request.GET['last_sync'])/1000)
if p.last_update.timestamp() >= int(request.GET['last_sync'])/1000:
d['updated'] = True
d['tokens'] = [l.token for l in p.link_set.all()]
return HttpResponse(json.dumps(d), content_type='application/json')
@csrf_exempt
@ -33,6 +42,8 @@ def add_link(request, token):
p = get_object_or_404(Playlist, pk=Playlist.reverse_token(token))
l = LinkForm(request.POST or None)
if l.is_valid():
p.last_update = timezone.now()
p.save()
yt_token = l.get_token()
link = Link()
link.token = yt_token