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>
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 // Auto fill from get arguments
var urlParams = new URLSearchParams(window.location.search); var urlParams = new URLSearchParams(window.location.search);
function autofillCheckboxRadio(name){ if (urlParams.has("search"))
if (urlParams.has(name)){ $("input[name='" + name + "']").each(function(){ $("input[name='search']").first().prop("value", urlParams.get("search"));
if ($(this).attr("value") == urlParams.get(name)) autofillCheckboxRadio("department");
$(this).prop("checked", true); 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 // 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);
@ -119,14 +142,45 @@ autofillCheckboxRadio("semester");
} }
// Handle submit here and modify autumn and spring here // Handle submit here and modify autumn and spring here
$("#search_form").submit(function(e) { $("#search_form").submit(function(e) {
e.preventDefault(); e.preventDefault();
if (autumn.prop("checked") && spring.prop("checked")){ if (autumn.prop("checked") && spring.prop("checked")){
autumn_and_spring.prop("checked", true); autumn_and_spring.prop("checked", true);
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,28 +673,28 @@ 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",
"code": "PA00", "delete_url": "/pedagogy/uv/1/delete",
"author": 0, "code": "PA00",
"credit_type": "OM", "author": 0,
"semester": "AUTUMN_AND_SPRING", "credit_type": "OM",
"language": "FR", "semester": "AUTUMN_AND_SPRING",
"credits": 5, "language": "FR",
"department": "HUMA", "credits": 5,
"title": "Participation dans une association \u00e9tudiante", "department": "HUMA",
"manager": "Laurent HEYBERGER", "title": "Participation dans une association \u00e9tudiante",
"objectives": "* Permettre aux \u00e9tudiants de r\u00e9aliser, pendant un semestre, un projet culturel ou associatif et de le valoriser.", "manager": "Laurent HEYBERGER",
"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", "objectives": "* Permettre aux \u00e9tudiants de r\u00e9aliser, pendant un semestre, un projet culturel ou associatif et de le valoriser.",
"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.", "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",
"key_concepts": "* Autonomie\n* Responsabilit\u00e9\n* Cahier des charges\n* Gestion de projet", "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.",
"hours_CM": 0, "key_concepts": "* Autonomie\n* Responsabilit\u00e9\n* Cahier des charges\n* Gestion de projet",
"hours_TD": 0, "hours_CM": 0,
"hours_TP": 0, "hours_TD": 0,
"hours_THE": 121, "hours_TP": 0,
"hours_TE": 4, "hours_THE": 121,
}, "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",
) )