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,21 +69,38 @@
</table>
<script>
function autofillCheckboxRadio(name){
if (urlParams.has(name)){ $("input[name='" + name + "']").each(function(){
if ($(this).attr("value") == urlParams.get(name))
$(this).prop("checked", true);
});
}
}
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);
function autofillCheckboxRadio(name){
if (urlParams.has(name)){ $("input[name='" + name + "']").each(function(){
if ($(this).attr("value") == urlParams.get(name))
$(this).prop("checked", true);
});
}
}
if (urlParams.has("search"))
$("input[name='search']").first().prop("value", urlParams.get("search"));
autofillCheckboxRadio("department");
autofillCheckboxRadio("credit_type");
autofillCheckboxRadio("semester");
if (urlParams.has("search"))
$("input[name='search']").first().prop("value", urlParams.get("search"));
autofillCheckboxRadio("department");
autofillCheckboxRadio("credit_type");
autofillCheckboxRadio("semester");
// Allow unchecking a radio button when we click on it
// Keep a state of what is checked
@ -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);
@ -119,14 +142,45 @@ autofillCheckboxRadio("semester");
}
// Handle submit here and modify autumn and spring here
$("#search_form").submit(function(e) {
e.preventDefault();
if (autumn.prop("checked") && spring.prop("checked")){
autumn_and_spring.prop("checked", true);
autumn.prop("checked", false);
spring.prop("checked", false);
}
this.submit();
$("#search_form").submit(function(e) {
e.preventDefault();
if (autumn.prop("checked") && spring.prop("checked")){
autumn_and_spring.prop("checked", true);
autumn.prop("checked", false);
spring.prop("checked", false);
}
// 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,28 +673,28 @@ class UVSearchTest(TestCase):
response.content,
[
{
"model": "pedagogy.uv",
"pk": 1,
"fields": {
"code": "PA00",
"author": 0,
"credit_type": "OM",
"semester": "AUTUMN_AND_SPRING",
"language": "FR",
"credits": 5,
"department": "HUMA",
"title": "Participation dans une association \u00e9tudiante",
"manager": "Laurent HEYBERGER",
"objectives": "* Permettre aux \u00e9tudiants de r\u00e9aliser, pendant un semestre, un projet culturel ou associatif et de le valoriser.",
"program": "* Semestre pr\u00e9c\u00e9dent proposition d'un projet et d'un cahier des charges\n* Evaluation par un jury de six membres\n* Si accord r\u00e9alisation dans le cadre de l'UV\n* Compte-rendu de l'exp\u00e9rience\n* Pr\u00e9sentation",
"skills": "* G\u00e9rer un projet associatif ou une action \u00e9ducative en autonomie:\n* en produisant un cahier des charges qui -d\u00e9finit clairement le contexte du projet personnel -pose les jalons de ce projet -estime de mani\u00e8re r\u00e9aliste les moyens et objectifs du projet -d\u00e9finit exactement les livrables attendus\n * en \u00e9tant capable de respecter ce cahier des charges ou, le cas \u00e9ch\u00e9ant, de r\u00e9viser le cahier des charges de mani\u00e8re argument\u00e9e.\n* Relater son exp\u00e9rience dans un rapport:\n* qui permettra \u00e0 d'autres \u00e9tudiants de poursuivre les actions engag\u00e9es\n* qui montre la capacit\u00e9 \u00e0 s'auto-\u00e9valuer et \u00e0 adopter une distance critique sur son action.",
"key_concepts": "* Autonomie\n* Responsabilit\u00e9\n* Cahier des charges\n* Gestion de projet",
"hours_CM": 0,
"hours_TD": 0,
"hours_TP": 0,
"hours_THE": 121,
"hours_TE": 4,
},
"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",
"semester": "AUTUMN_AND_SPRING",
"language": "FR",
"credits": 5,
"department": "HUMA",
"title": "Participation dans une association \u00e9tudiante",
"manager": "Laurent HEYBERGER",
"objectives": "* Permettre aux \u00e9tudiants de r\u00e9aliser, pendant un semestre, un projet culturel ou associatif et de le valoriser.",
"program": "* Semestre pr\u00e9c\u00e9dent proposition d'un projet et d'un cahier des charges\n* Evaluation par un jury de six membres\n* Si accord r\u00e9alisation dans le cadre de l'UV\n* Compte-rendu de l'exp\u00e9rience\n* Pr\u00e9sentation",
"skills": "* G\u00e9rer un projet associatif ou une action \u00e9ducative en autonomie:\n* en produisant un cahier des charges qui -d\u00e9finit clairement le contexte du projet personnel -pose les jalons de ce projet -estime de mani\u00e8re r\u00e9aliste les moyens et objectifs du projet -d\u00e9finit exactement les livrables attendus\n * en \u00e9tant capable de respecter ce cahier des charges ou, le cas \u00e9ch\u00e9ant, de r\u00e9viser le cahier des charges de mani\u00e8re argument\u00e9e.\n* Relater son exp\u00e9rience dans un rapport:\n* qui permettra \u00e0 d'autres \u00e9tudiants de poursuivre les actions engag\u00e9es\n* qui montre la capacit\u00e9 \u00e0 s'auto-\u00e9valuer et \u00e0 adopter une distance critique sur son action.",
"key_concepts": "* Autonomie\n* Responsabilit\u00e9\n* Cahier des charges\n* Gestion de projet",
"hours_CM": 0,
"hours_TD": 0,
"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",
)