pedagogy: live uv update on guide

This commit is contained in:
Antoine Bartuccio 2019-07-05 20:11:33 +02:00
parent 2aa1314fac
commit 3e3c576ad7
Signed by: klmp200
GPG Key ID: E7245548C53F904B
4 changed files with 136 additions and 50 deletions

View File

@ -28,6 +28,9 @@ from django.utils import timezone
from django.core import validators
from django.conf import settings
from django.utils.functional import cached_property
from django.core.urlresolvers import reverse
from rest_framework import serializers
from core.models import User
@ -58,6 +61,9 @@ class UV(models.Model):
return int(sum(comments.values_list(field, flat=True)) / comments.count())
def get_absolute_url(self):
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.id})
@cached_property
def grade_global_average(self):
return self.__grade_average_generic("grade_global")
@ -302,3 +308,30 @@ class UVCommentReport(models.Model):
User, related_name="reported_uv_comment", verbose_name=_("reporter")
)
reason = models.TextField(_("reason"))
# Custom serializers
class UVSerializer(serializers.ModelSerializer):
"""
Custom seralizer for UVs
Allow adding more informations like absolute_url
"""
class Meta:
model = UV
fields = "__all__"
absolute_url = serializers.SerializerMethodField()
update_url = serializers.SerializerMethodField()
delete_url = serializers.SerializerMethodField()
def get_absolute_url(self, obj):
return obj.get_absolute_url()
def get_update_url(self, obj):
return reverse("pedagogy:uv_update", kwargs={"uv_id": obj.id})
def get_delete_url(self, obj):
return reverse("pedagogy:uv_delete", kwargs={"uv_id": obj.id})

View File

@ -31,6 +31,7 @@
<input type="checkbox" name="semester" value="SPRING">P
<input type="checkbox" name="semester" value="AUTUMN_AND_SPRING">
</p>
<input type="text" name="json" hidden>
</form>
{% if can_create_uv(user) %}
<p>
@ -38,7 +39,7 @@
</p>
<br>
{% endif %}
<table>
<table id="dynamic_view">
<thead>
<tr>
<td>{% trans %}UV{% endtrans %}</td>
@ -51,7 +52,7 @@
{% endif %}
</tr>
</thead>
<tbody>
<tbody id="dynamic_view_content">
{% for uv in object_list %}
<tr>
<td><a href="{{ url('pedagogy:uv_detail', uv_id=uv.id) }}">{{ uv.code }}</a></td>
@ -68,9 +69,6 @@
</table>
<script>
// Auto fill from get arguments
var urlParams = new URLSearchParams(window.location.search);
function autofillCheckboxRadio(name){
if (urlParams.has(name)){ $("input[name='" + name + "']").each(function(){
if ($(this).attr("value") == urlParams.get(name))
@ -78,6 +76,26 @@
});
}
}
function uvJSONToHTML(uv){
var html = `
<tr>
<td><a href="${uv.absolute_url}">${uv.code}</a></td>
<td>${uv.title}</td>
<td>${uv.department}</td>
<td>${uv.credit_type}</td>
`;
{% if can_create_uv(user) %}
html += `
<td><a href="${uv.update_url}">{% trans %}Edit{% endtrans %}</a></td>
<td><a href="${uv.delete_url}">{% trans %}Delete{% endtrans %}</a></td>
`;
{% endif %}
return html + "</td>";
}
// Auto fill from get arguments
var urlParams = new URLSearchParams(window.location.search);
if (urlParams.has("search"))
$("input[name='search']").first().prop("value", urlParams.get("search"));
autofillCheckboxRadio("department");
@ -91,6 +109,8 @@ autofillCheckboxRadio("semester");
if (formStates[this.name] == this.value){
this.checked = false;
formStates[this.name] = "";
// Fire an update since the browser does not do it in this situation
$("#search_form").submit();
return;
}
formStates[this.name] = this.value;
@ -111,6 +131,9 @@ autofillCheckboxRadio("semester");
// Make autumn and spring hidden if js is enabled
autumn_and_spring.hide();
// Fill json field if js is enabled
$("input[name='json']").first().prop("value", "true");
// Set correctly state of what is checked
if (autumn_and_spring.prop("checked")){
autumn.prop("checked", true);
@ -126,7 +149,38 @@ autofillCheckboxRadio("semester");
autumn.prop("checked", false);
spring.prop("checked", false);
}
this.submit();
// Do query
var xhr = new XMLHttpRequest();
$.ajax({
type: "GET",
url: "{{ url('pedagogy:guide') }}",
data: $(this).serialize(),
xhr: function(){
return xhr;
},
success: function(data){
// Update URL
history.pushState({}, null, xhr.responseURL.replace("&json=true", ""));
// Update content
$("#dynamic_view_content").html("");
for (key in data){
$("#dynamic_view_content").append(uvJSONToHTML(data[key]));
}
},
});
// Restore autumn and spring for perfect illusion
if (autumn_and_spring.prop("checked")){
autumn_and_spring.prop("checked", false);
autumn.prop("checked", true);
spring.prop("checked", true);
}
});
// Auto send on change
$("#search_form").on("change", function(e){
$(this).submit();
});
</script>
{% endblock content %}

View File

@ -673,9 +673,10 @@ class UVSearchTest(TestCase):
response.content,
[
{
"model": "pedagogy.uv",
"pk": 1,
"fields": {
"id": 1,
"absolute_url": "/pedagogy/uv/1",
"update_url": "/pedagogy/uv/1/edit",
"delete_url": "/pedagogy/uv/1/delete",
"code": "PA00",
"author": 0,
"credit_type": "OM",
@ -694,7 +695,6 @@ class UVSearchTest(TestCase):
"hours_TP": 0,
"hours_THE": 121,
"hours_TE": 4,
},
}
],
)

View File

@ -25,13 +25,11 @@
from django.views.generic import (
CreateView,
DeleteView,
DetailView,
UpdateView,
ListView,
FormView,
View,
)
from django.core import serializers
from django.utils import html
from django.http import HttpResponse
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
@ -39,6 +37,9 @@ from django.core.urlresolvers import reverse_lazy, reverse
from django.shortcuts import get_object_or_404
from django.conf import settings
from haystack.query import SearchQuerySet
from rest_framework.renderers import JSONRenderer
from core.views import (
DetailFormView,
CanCreateMixin,
@ -48,15 +49,13 @@ from core.views import (
)
from core.models import RealGroup, Notification
from haystack.query import SearchQuerySet
from pedagogy.forms import (
UVForm,
UVCommentForm,
UVCommentReportForm,
UVCommentModerationForm,
)
from pedagogy.models import UV, UVComment, UVCommentReport
from pedagogy.models import UV, UVComment, UVCommentReport, UVSerializer
# Some mixins
@ -166,7 +165,7 @@ class UVListView(CanViewMixin, CanCreateUVFunctionMixin, ListView):
# Return serialized response
return HttpResponse(
serializers.serialize("json", self.get_queryset()),
JSONRenderer().render(UVSerializer(self.get_queryset(), many=True).data),
content_type="application/json",
)