From: Gael Le Mignot Date: Mon, 26 Nov 2018 09:08:09 +0000 (+0100) Subject: Optimized notes, added stat status, fixed a few bugs X-Git-Tag: 1.4.0~21^2 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=ad38537f49796062952777b129fe49fd7b441ea2;p=teleforma.git Optimized notes, added stat status, fixed a few bugs --- diff --git a/teleforma/exam/models.py b/teleforma/exam/models.py old mode 100644 new mode 100755 index c6cda6ec..bab2130b --- a/teleforma/exam/models.py +++ b/teleforma/exam/models.py @@ -81,7 +81,8 @@ crocodoc.api_token = settings.BOX_API_TOKEN # box_client = Client(oauth) SCRIPT_STATUS = ((0, _('rejected')), (1, _('draft')), (2, _('submitted')), - (3, _('pending')),(4, _('marked')), (5, _('read')), (6, _('backup')) ) + (3, _('pending')),(4, _('marked')), (5, _('read')), + (6, _('backup')), (7, _('stat')) ) REJECT_REASON = (('unreadable', _('unreadable')), ('bad orientation', _('bad orientation')), @@ -353,9 +354,11 @@ class Script(BaseResource): self.save() def save(self, *args, **kwargs): - if self.status == 4 and self.score: + if not self.file and self.score: + self.status = 7 + elif self.status == 4 and self.score: self.mark() - if self.status == 0 and self.reject_reason: + elif self.status == 0 and self.reject_reason: self.reject() super(Script, self).save(*args, **kwargs) diff --git a/teleforma/exam/views.py b/teleforma/exam/views.py index 2f29edd6..e38b2f5f 100755 --- a/teleforma/exam/views.py +++ b/teleforma/exam/views.py @@ -123,6 +123,7 @@ class ScriptsView(ScriptsListMixinView, ListView): model = Script template_name='exam/scripts.html' + status_filter = None def get_form_queryset(self): QT = ~Q(pk=None) @@ -132,87 +133,67 @@ class ScriptsView(ScriptsListMixinView, ListView): course = self.request.GET.get('course') platform_only = self.request.GET.get('platform_only') if type: - QT = Q(type__id=int(type)) & QT + QT &= Q(type__id=int(type)) if session: - QT = Q(session=session) & QT + QT &= Q(session=session) if course: - QT = Q(course__id=int(course)) & QT + QT &= Q(course__id=int(course)) if corrector: - QT = Q(corrector__id=int(corrector)) & QT + QT &= Q(corrector__id=int(corrector)) if platform_only: - QT = Q(author__student__platform_only = int(platform_only)) & QT + QT &= Q(author__student__platform_only = int(platform_only)) return QT - - @method_decorator(login_required) - def dispatch(self, *args, **kwargs): - return super(ScriptsView, self).dispatch(*args, **kwargs) - - -class ScriptsPendingView(ScriptsView): - + def get_base_queryset(self): - period = Period.objects.get(id=self.kwargs['period_id']) - QT = Q(status__in=(2, 3), period=period) + QT = self.get_form_queryset() & Q(period_id=self.kwargs['period_id']) + if self.status_filter: + QT &= Q(status__in=self.status_filter) return Script.objects.filter(QT) - def get_queryset(self): user = self.request.user - period = Period.objects.get(id=self.kwargs['period_id']) + base_qs = self.get_base_queryset() - if self.request.GET.get('corrector') is not None: - QT = Q(status__in=(2, 3), period=period) - else: - QT = Q(status=2, author=user, period=period) - QT = Q(status=3, author=user, period=period) | QT - QT = Q(status=3, corrector=user, period=period) | QT + if self.request.GET.get('corrector') is None: + QT = Q(author=user) | Q(corrector=user) professor = user.professor.all() if professor: professor = professor[0] - for course in professor.courses.all(): - QT = Q(status=2, period=period, course=course) | QT - QT = Q(status=3, period=period, course=course) | QT + courses_id = [ c['id'] for c in professor.courses.values('id') ] + QT |= Q(course_id__in=courses_id) - QT = self.get_form_queryset() & QT + base_qs = base_qs.filter(QT) + return base_qs - return Script.objects.filter(QT) - - def get_context_data(self, **kwargs): - context = super(ScriptsPendingView, self).get_context_data(**kwargs) - context['title'] = ugettext('Pending scripts') - return context + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(ScriptsView, self).dispatch(*args, **kwargs) -class ScriptsTreatedView(ScriptsView): +class ScriptsPendingView(ScriptsView): - def get_base_queryset(self): - period = Period.objects.get(id=self.kwargs['period_id']) - QT = Q(status__in=(4, 5), period=period) - return Script.objects.filter(QT) + status_filter = (2, 3) def get_queryset(self): - user = self.request.user - period = Period.objects.get(id=self.kwargs['period_id']) + qs = super(ScriptsPendingView, self).get_queryset() + + if self.request.GET.get('corrector') is None: + # Exclude status=3 but not author=user + qs = qs.filter(~Q(status=3) | Q(author=user)) - if self.request.GET.get('corrector') is not None: - QT = Q(status__in=(4, 5), period=period) - else: - QT = Q(status=4, author=user, period=period) - QT = Q(status=5, author=user, period=period) | QT - QT = Q(status=4, corrector=user, period=period) | QT - QT = Q(status=5, corrector=user, period=period) | QT + return qs - professor = user.professor.all() - if professor: - professor = professor[0] - for course in professor.courses.all(): - QT = Q(status=4, period=period, course=course) | QT - QT = Q(status=5, period=period, course=course) | QT + def get_context_data(self, **kwargs): + context = super(ScriptsPendingView, self).get_context_data(**kwargs) + context['title'] = ugettext('Pending scripts') + return context - QT = self.get_form_queryset() & QT - return Script.objects.filter(QT) +class ScriptsTreatedView(ScriptsView): + + status_filter = (4, 5 ,7) + def get_context_data(self, **kwargs): context = super(ScriptsTreatedView, self).get_context_data(**kwargs) period = Period.objects.get(id=self.kwargs['period_id']) @@ -221,29 +202,7 @@ class ScriptsTreatedView(ScriptsView): class ScriptsRejectedView(ScriptsView): - - def get_base_queryset(self): - period = Period.objects.get(id=self.kwargs['period_id']) - QT = Q(status=0) - return Script.objects.filter(QT) - - def get_queryset(self): - user = self.request.user - period = Period.objects.get(id=self.kwargs['period_id']) - if self.request.GET.get('corrector') is not None: - QT = Q(status=0) - else: - QT = Q(status=0, author=user) - QT = Q(status=0, corrector=user) | QT - - professor = user.professor.all() - if professor: - professor = professor[0] - for course in professor.courses.all(): - QT = Q(status=0, period=period, course=course) | QT - - QT = self.get_form_queryset() & QT - return Script.objects.filter(QT) + status_filter = (0,) def get_context_data(self, **kwargs): context = super(ScriptsRejectedView, self).get_context_data(**kwargs) @@ -312,6 +271,8 @@ class QuotasView(ListView): class ScriptsScoreAllView(ScriptsTreatedView): + perso_name = 'Moyenne personnelle' + template_name='exam/scores.html' def score_data_setup(self, x, y): @@ -342,100 +303,63 @@ class ScriptsScoreAllView(ScriptsTreatedView): def get_context_data(self, **kwargs): context = super(ScriptsScoreAllView, self).get_context_data(**kwargs) user = self.request.user - period = Period.objects.get(id=self.kwargs['period_id']) + period_id = self.kwargs['period_id'] + + if 'course_id' in self.kwargs: + course = Course.objects.get(id=self.kwargs['course_id']) + else: + course = None + + is_staff = user.is_staff + staff_or_teacher = is_staff or user.professor.exists() - if self.request.user.is_staff: - QT = Q(status=4, period=period) - QT = Q(status=5, period=period) | QT - QT = Q(date_added__gte=self.period.date_begin) | QT + QT = Q(period_id=period_id, status__in = self.status_filter) + all_scripts = Script.objects.filter(QT).exclude(score=None) + + if is_staff: + QT |= Q(date_added__gte=self.period.date_begin) scripts = Script.objects.filter(QT).exclude(score=None) else: scripts = self.get_queryset() - sessions = [] + if course: + scripts = scripts.filter(course=course) + all_scripts = all_scripts.filter(course=course) + + sessions = set() scores = [] - for script in scripts: - if not script.session in sessions: - sessions.append(script.session) - sessions = map(str, sorted(map(int, sessions))) + for script in scripts.values('session'): + sessions.add(script['session']) + sessions = sorted(sessions, key = lambda v: int(v)) sessions_x = {'x': sessions} - if not (self.request.user.is_staff or self.request.user.professor.all()): - data = [] - for session in sessions: - data.append(np.mean([float(script.score) for script in scripts.filter(session=session) if script.score])) - scores.append({'name': 'Moyenne personnelle' + ' (' + str(len(sessions)) + ')', 'data': data}) - - data = [] - counter = 0 - for session in sessions: - scripts = Script.objects.filter(session=session, period=self.period).exclude(score=None) - counter += scripts.count() - data.append(np.mean([s.score for s in scripts if script.score])) - scores.append({'name': 'Moyenne generale' + ' (' + str(counter) + ')', 'data': data}) + def by_session(scripts): + res = { s: [] for s in sessions } + for script in scripts.values('score', 'session'): + if script['session'] in res: + res[script['session']].append(script['score']) + return [ np.mean(res[s]) for s in sessions ] + + if not staff_or_teacher: + scores.append({'name': self.perso_name, + 'data': by_session(scripts)}) + + scores.append({'name': 'Moyenne generale', + 'data': by_session(all_scripts)}) for script_type in ScriptType.objects.all(): - data = [] - counter = 0 - for session in sessions: - scripts = Script.objects.filter(session=session, period=self.period, type=script_type).exclude(score=None) - data.append(np.mean([s.score for s in scripts if script.score])) - counter += scripts.count() - scores.append({'name': 'Moyenne ' + script_type.name + ' (' + str(counter) + ')', 'data': data}) + scripts = all_scripts.filter(type=script_type) + scores.append({'name': 'Moyenne ' + script_type.name, + 'data': by_session(scripts)}) context['data'] = self.score_data_setup(sessions_x, scores) - context['course'] = ugettext('all courses') + context['course'] = course and course.title or ugettext('all courses') return context class ScriptsScoreCourseView(ScriptsScoreAllView): - - def get_context_data(self, **kwargs): - context = super(ScriptsScoreCourseView, self).get_context_data(**kwargs) - course = Course.objects.get(id=self.kwargs['course_id']) - period = Period.objects.get(id=self.kwargs['period_id']) - - if self.request.user.is_staff or self.request.user.professor.all(): - scripts = Script.objects.all().filter(course=course, period=self.period).exclude(score=None) - else: - scripts = self.get_queryset().filter(course=course) - - sessions = [] - scores = [] - - for script in scripts: - if not script.session in sessions: - sessions.append(script.session) - sessions = sorted(sessions) - sessions_x = {'x': sessions} - - if not (self.request.user.is_staff or self.request.user.professor.all()): - data = [] - for session in sessions: - data.append(np.mean([float(script.score) for script in scripts.filter(session=session) if script.score])) - scores.append({'name':'Note personnelle' , 'data': data}) - - data = [] - counter = 0 - for session in sessions: - scripts = Script.objects.filter(session=session, course=course, period=self.period).exclude(score=None) - counter += scripts.count() - data.append(np.mean([s.score for s in scripts if script.score])) - scores.append({'name':'Moyenne generale' + ' (' + str(counter) + ')', 'data': data}) - - for script_type in ScriptType.objects.all(): - data = [] - counter = 0 - for session in sessions: - scripts = Script.objects.filter(session=session, type=script_type, course=course, period=self.period).exclude(score=None) - counter += scripts.count() - data.append(np.mean([s.score for s in scripts if script.score])) - scores.append({'name': 'Moyenne ' + script_type.name + ' (' + str(counter) + ')', 'data': data}) - - context['data'] = self.score_data_setup(sessions_x, scores) - context['course'] = course.title - return context + perso_name = 'Note personnelle' class ScoreCreateView(ScriptCreateView):