Skip to content

Commit a96019c

Browse files
committed
added students list
1 parent a1ed25a commit a96019c

File tree

5 files changed

+98
-3
lines changed

5 files changed

+98
-3
lines changed

django_school/classroom/templates/classroom/students/_header.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ <h2>Quizzes</h2>
1111
<li class="nav-item">
1212
<a class="nav-link{% if active == 'taken' %} active{% endif %}" href="{% url 'students:taken_quiz_list' %}">Taken</a>
1313
</li>
14+
<li class="nav-item">
15+
<a class="nav-link{% if active == 'students' %} active{% endif %}" href="{% url 'students:student_list' %}">Students</a>
16+
</li>
1417
</ul>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!-- To show list of students and teachers tab for both users -->
2+
{% extends 'base.html' %}
3+
{% load quiz_extras %}
4+
5+
{% block content %}
6+
{% include 'classroom/students/_header.html' with active='students' %}
7+
<form method='GET'>
8+
<div class="row">
9+
<div class="col-sm-6">
10+
<div class="input-group mb-3">
11+
<input type="text" class="form-control" name='q' value='{{ request.GET.q }}' placeholder="Filter by name">
12+
<div class="input-group-append">
13+
<button class="btn btn-outline-secondary" type="submit">Search...</button>
14+
</div>
15+
</div>
16+
</div>
17+
</div>
18+
</form>
19+
<br>
20+
<div class="row">
21+
{% for user in users %}
22+
<div class="col-sm-3">
23+
<div class="media">
24+
<a>
25+
<img class="mr-3" src="{{ user.username|gravatar_url:50 }}" alt="{{user.get_full_name}}">
26+
</a>
27+
<div class="media-body" style="font-size: 12px">
28+
<!-- <a href="#">{{user.get_full_name}}</a><br> -->
29+
{{user.username}}<br>
30+
<strong>{{user.student.score}}</strong>
31+
</div>
32+
</div>
33+
</div>
34+
{% empty %}
35+
<div class="col-sm-6"><p class="text-warning">No student matched your search.</p></div>
36+
{% endfor %}
37+
</div>
38+
39+
{% if is_paginated %}
40+
<ul class="pagination float-right">
41+
{% if page_obj.has_previous %}
42+
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
43+
{% else %}
44+
<li class="page-item disabled"><span class="page-link">&laquo;</span></li>
45+
{% endif %}
46+
{% for i in paginator.page_range %}
47+
{% if page_obj.number == i %}
48+
<li class="page-item active"><span class="page-link" >{{ i }} <span class="sr-only">(current)</span></span></li>
49+
{% else %}
50+
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
51+
{% endif %}
52+
{% endfor %}
53+
{% if page_obj.has_next %}
54+
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
55+
{% else %}
56+
<li class="page-item disabled"><span class="page-link">&raquo;</span></li>
57+
{% endif %}
58+
</ul>
59+
{% endif %}
60+
{% endblock %}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from django import template
22
from classroom.models import StudentAnswer
3-
register = template.Library()
4-
3+
import hashlib
54

5+
register = template.Library()
66

77
@register.simple_tag
88
def marked_answer(user,opt):
@@ -12,4 +12,10 @@ def marked_answer(user,opt):
1212
return 'correct'
1313
return 'wrong'
1414

15-
return ''
15+
return ''
16+
17+
@register.filter
18+
def gravatar_url(username, size=40):
19+
# TEMPLATE USE: {{ email|gravatar_url:150 }}
20+
username_hash = hashlib.md5(username.lower().encode('utf-8')).hexdigest()
21+
return f"https://www.gravatar.com/avatar/{username_hash}?s={size}&d=identicon"

django_school/classroom/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
path('students/', include(([
99
path('', students.QuizListView.as_view(), name='quiz_list'),
10+
path('s/', students.StudentList.as_view(), name='student_list'),
1011
path('interests/', students.StudentInterestsView.as_view(), name='student_interests'),
1112
path('taken/', students.TakenQuizListView.as_view(), name='taken_quiz_list'),
1213
path('quiz/<int:pk>/', students.take_quiz, name='take_quiz'),

django_school/classroom/views/students.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from django.contrib import messages
22
from django.contrib.auth import login
33
from django.contrib.auth.decorators import login_required
4+
from django.contrib.auth import get_user_model
5+
46
from django.db import transaction
57
from django.db.models import Count, Sum
8+
from django.db.models.functions import Concat
69
from django.shortcuts import get_object_or_404, redirect, render
710
from django.urls import reverse_lazy
811
from django.utils.decorators import method_decorator
@@ -141,3 +144,25 @@ def take_quiz(request, pk):
141144
'answered_questions': total_questions - total_unanswered_questions,
142145
'total_questions': total_questions
143146
})
147+
148+
149+
@method_decorator([login_required, student_required], name='dispatch')
150+
class StudentList(ListView):
151+
# model = get_user_model()
152+
paginate_by = 36
153+
template_name = 'classroom/students/student_list.html'
154+
context_object_name = 'users'
155+
156+
def get_queryset(self):
157+
query = self.request.GET.get('q','')
158+
User = get_user_model()
159+
160+
queryset = User.objects.filter(is_student = True).order_by('-student__score')
161+
if query:
162+
# queryset = queryset.annotate(
163+
# full_name = Concat('first_name','last_name')
164+
# ).filter(full_name__icontains = query)
165+
queryset = queryset.filter(username__icontains = query)
166+
return queryset
167+
168+

0 commit comments

Comments
 (0)