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.core import validators
from django.conf import settings from django.conf import settings
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.core.urlresolvers import reverse
from rest_framework import serializers
from core.models import User from core.models import User
@ -58,6 +61,9 @@ class UV(models.Model):
return int(sum(comments.values_list(field, flat=True)) / comments.count()) 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 @cached_property
def grade_global_average(self): def grade_global_average(self):
return self.__grade_average_generic("grade_global") return self.__grade_average_generic("grade_global")
@ -302,3 +308,30 @@ class UVCommentReport(models.Model):
User, related_name="reported_uv_comment", verbose_name=_("reporter") User, related_name="reported_uv_comment", verbose_name=_("reporter")
) )
reason = models.TextField(_("reason")) 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="SPRING">P
<input type="checkbox" name="semester" value="AUTUMN_AND_SPRING"> <input type="checkbox" name="semester" value="AUTUMN_AND_SPRING">
</p> </p>
<input type="text" name="json" hidden>
</form> </form>
{% if can_create_uv(user) %} {% if can_create_uv(user) %}
<p> <p>
@ -38,7 +39,7 @@
</p> </p>
<br> <br>
{% endif %} {% endif %}
<table> <table id="dynamic_view">
<thead> <thead>
<tr> <tr>
<td>{% trans %}UV{% endtrans %}</td> <td>{% trans %}UV{% endtrans %}</td>
@ -51,7 +52,7 @@
{% endif %} {% endif %}
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="dynamic_view_content">
{% for uv in object_list %} {% for uv in object_list %}
<tr> <tr>
<td><a href="{{ url('pedagogy:uv_detail', uv_id=uv.id) }}">{{ uv.code }}</a></td> <td><a href="{{ url('pedagogy:uv_detail', uv_id=uv.id) }}">{{ uv.code }}</a></td>
@ -68,21 +69,38 @@
</table> </table>
<script> <script>
// Auto fill from get arguments
var urlParams = new URLSearchParams(window.location.search);
function autofillCheckboxRadio(name){ function autofillCheckboxRadio(name){
if (urlParams.has(name)){ $("input[name='" + name + "']").each(function(){ if (urlParams.has(name)){ $("input[name='" + name + "']").each(function(){
if ($(this).attr("value") == urlParams.get(name)) if ($(this).attr("value") == urlParams.get(name))
$(this).prop("checked", true); $(this).prop("checked", true);
}); });
} }
} }
if (urlParams.has("search"))
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")); $("input[name='search']").first().prop("value", urlParams.get("search"));
autofillCheckboxRadio("department"); autofillCheckboxRadio("department");
autofillCheckboxRadio("credit_type"); autofillCheckboxRadio("credit_type");
autofillCheckboxRadio("semester"); autofillCheckboxRadio("semester");
// Allow unchecking a radio button when we click on it // Allow unchecking a radio button when we click on it
// Keep a state of what is checked // Keep a state of what is checked
@ -91,6 +109,8 @@ autofillCheckboxRadio("semester");
if (formStates[this.name] == this.value){ if (formStates[this.name] == this.value){
this.checked = false; this.checked = false;
formStates[this.name] = ""; formStates[this.name] = "";
// Fire an update since the browser does not do it in this situation
$("#search_form").submit();
return; return;
} }
formStates[this.name] = this.value; formStates[this.name] = this.value;
@ -111,6 +131,9 @@ autofillCheckboxRadio("semester");
// Make autumn and spring hidden if js is enabled // Make autumn and spring hidden if js is enabled
autumn_and_spring.hide(); 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 // Set correctly state of what is checked
if (autumn_and_spring.prop("checked")){ if (autumn_and_spring.prop("checked")){
autumn.prop("checked", true); autumn.prop("checked", true);
@ -126,7 +149,38 @@ autofillCheckboxRadio("semester");
autumn.prop("checked", false); autumn.prop("checked", false);
spring.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> </script>
{% endblock content %} {% endblock content %}

View File

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

View File

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