Skip to content

Commit 3fb6a67

Browse files
committed
added top subject in student list
1 parent f10fb04 commit 3fb6a67

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Django School
1+
# [Django School](https://djangoschools.herokuapp.com/)
22

33
[![Python Version](https://img.shields.io/badge/python-3.6-brightgreen.svg)](https://python.org)
44
[![Django Version](https://img.shields.io/badge/django-3.0-brightgreen.svg)](https://djangoproject.com)

django_school/classroom/templates/classroom/students/student_list.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- To show list of students and teachers tab for both users -->
1+
<!-- To show list of students -->
22
{% extends 'base.html' %}
33
{% load quiz_extras %}
44

@@ -16,7 +16,7 @@
1616
<div class="row">
1717
<div class="col-sm-6">
1818
<div class="input-group mb-3">
19-
<input type="text" class="form-control" name='q' value='{{ request.GET.q }}' placeholder="Filter by name">
19+
<input type="text" class="form-control" name='q' value='{{ request.GET.q }}' placeholder="Filter by username">
2020
<div class="input-group-append">
2121
<button class="btn btn-outline-secondary" type="submit">Search...</button>
2222
</div>
@@ -26,16 +26,17 @@
2626
</form>
2727
<br>
2828
<div class="row">
29-
{% for user in users %}
29+
{% for student in students %}
3030
<div class="col-sm-3">
3131
<div class="media">
3232
<a>
33-
<img class="mr-3" src="{{ user.username|gravatar_url:50 }}" alt="{{user.get_full_name}}">
33+
<img class="mr-3" src="{{ student.user.username|gravatar_url:50 }}" alt="{{student.user.get_full_name}}">
3434
</a>
3535
<div class="media-body" style="font-size: 12px">
3636
<!-- <a href="#">{{user.get_full_name}}</a><br> -->
37-
{{user.username}}<br>
38-
<strong>{{user.student.score}}</strong>
37+
{{student.user.username}}<br>
38+
<strong>{{student.score}}</strong><br>
39+
{{student.taken_quizzes.all|top_subject}}
3940
</div>
4041
</div>
4142
</div>

django_school/classroom/templatetags/quiz_extras.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from django import template
2+
from django.db.models import Sum
3+
24
from classroom.models import StudentAnswer
35
import hashlib
46

@@ -11,11 +13,21 @@ def marked_answer(user,opt):
1113
if opt.is_correct:
1214
return 'correct'
1315
return 'wrong'
14-
1516
return ''
1617

1718
@register.filter
1819
def gravatar_url(username, size=40):
1920
# TEMPLATE USE: {{ email|gravatar_url:150 }}
2021
username_hash = hashlib.md5(username.lower().encode('utf-8')).hexdigest()
21-
return f"https://www.gravatar.com/avatar/{username_hash}?s={size}&d=identicon"
22+
return f"https://www.gravatar.com/avatar/{username_hash}?s={size}&d=identicon"
23+
24+
@register.filter
25+
def top_subject(taken_quizzes):
26+
subjects = taken_quizzes.values('quiz__subject__name') \
27+
.annotate(score = Sum('score')) \
28+
.order_by('-score')
29+
if subjects:
30+
name = subjects[0]['quiz__subject__name']
31+
score = subjects[0]['score']
32+
return f"{name} x {score}"
33+
return ""

django_school/classroom/views/students.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
from ..decorators import student_required
1616
from ..forms import StudentInterestsForm, StudentSignUpForm, TakeQuizForm
17-
from ..models import Quiz, Student, TakenQuiz, User, Question
17+
from ..models import Quiz, Student, TakenQuiz, Question
1818

19+
User = get_user_model()
1920

2021
class StudentSignUpView(CreateView):
2122
model = User
@@ -151,18 +152,18 @@ class StudentList(ListView):
151152
# model = get_user_model()
152153
paginate_by = 36
153154
template_name = 'classroom/students/student_list.html'
154-
context_object_name = 'users'
155+
context_object_name = 'students'
155156

156157
def get_queryset(self):
157158
query = self.request.GET.get('q','')
158159
User = get_user_model()
159160

160-
queryset = User.objects.filter(is_student = True).order_by('-student__score')
161+
queryset = Student.objects.order_by('-score')
161162
if query:
162163
# queryset = queryset.annotate(
163164
# full_name = Concat('first_name','last_name')
164165
# ).filter(full_name__icontains = query)
165-
queryset = queryset.filter(username__icontains = query)
166+
queryset = queryset.filter(user__username__icontains = query)
166167
return queryset
167168

168169

django_school/classroom/views/teachers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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 Avg, Count
68
from django.forms import inlineformset_factory
@@ -12,8 +14,9 @@
1214

1315
from ..decorators import teacher_required
1416
from ..forms import BaseAnswerInlineFormSet, QuestionForm, TeacherSignUpForm
15-
from ..models import Answer, Question, Quiz, User
17+
from ..models import Answer, Question, Quiz
1618

19+
User = get_user_model()
1720

1821
class TeacherSignUpView(CreateView):
1922
model = User

0 commit comments

Comments
 (0)