From 9484d9d5a91149c8c7f2fd18b14d0bf0041cf864 Mon Sep 17 00:00:00 2001 From: papi Date: Thu, 28 Dec 2023 00:09:59 +0300 Subject: [PATCH] Follow few pylint conventions --- accounts/views.py | 9 ++--- course/forms.py | 6 --- course/models.py | 20 +++++----- quiz/admin.py | 4 +- quiz/models.py | 2 +- quiz/views.py | 26 ++++++------- result/models.py | 97 ++++++++++++++++++++--------------------------- result/views.py | 54 +++++++++++++------------- search/views.py | 42 +++----------------- 9 files changed, 103 insertions(+), 157 deletions(-) diff --git a/accounts/views.py b/accounts/views.py index ae07ef9..5ce6d3e 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -6,14 +6,11 @@ from django.contrib.auth import update_session_auth_hash from django.views.generic import CreateView, ListView from django.db.models import Q from django.utils.decorators import method_decorator -from django.contrib.auth.forms import ( - PasswordChangeForm, -) - -from .decorators import admin_required +from django.contrib.auth.forms import PasswordChangeForm +from app.models import Session, Semester from course.models import Course from result.models import TakenCourse -from app.models import Session, Semester +from .decorators import admin_required from .forms import StaffAddForm, StudentAddForm, ProfileUpdateForm, ParentAddForm from .models import User, Student, Parent diff --git a/course/forms.py b/course/forms.py index c9003ff..863d11f 100644 --- a/course/forms.py +++ b/course/forms.py @@ -1,13 +1,7 @@ from django import forms -from django.db import transaction -from django.conf import settings -from django.contrib.auth.models import User - from accounts.models import User from .models import Program, Course, CourseAllocation, Upload, UploadVideo -# User = settings.AUTH_USER_MODEL - class ProgramForm(forms.ModelForm): class Meta: diff --git a/course/models.py b/course/models.py index 7bab27a..cfeafa3 100644 --- a/course/models.py +++ b/course/models.py @@ -41,13 +41,13 @@ SEMESTER = ( class ProgramManager(models.Manager): def search(self, query=None): - qs = self.get_queryset() + queryset = self.get_queryset() if query is not None: or_lookup = Q(title__icontains=query) | Q(summary__icontains=query) - qs = qs.filter( + queryset = queryset.filter( or_lookup ).distinct() # distinct() is often necessary with Q lookups - return qs + return queryset class Program(models.Model): @@ -65,7 +65,7 @@ class Program(models.Model): class CourseManager(models.Manager): def search(self, query=None): - qs = self.get_queryset() + queryset = self.get_queryset() if query is not None: or_lookup = ( Q(title__icontains=query) @@ -73,10 +73,10 @@ class CourseManager(models.Manager): | Q(code__icontains=query) | Q(slug__icontains=query) ) - qs = qs.filter( + queryset = queryset.filter( or_lookup ).distinct() # distinct() is often necessary with Q lookups - return qs + return queryset class Course(models.Model): @@ -169,15 +169,15 @@ class Upload(models.Model): ext = str(self.file).split(".") ext = ext[len(ext) - 1] - if ext == "doc" or ext == "docx": + if ext in ("doc", "docx"): return "word" elif ext == "pdf": return "pdf" - elif ext == "xls" or ext == "xlsx": + elif ext in ("xls", "xlsx"): return "excel" - elif ext == "ppt" or ext == "pptx": + elif ext in ("ppt", "pptx"): return "powerpoint" - elif ext == "zip" or ext == "rar" or ext == "7zip": + elif ext in ("zip", "rar", "7zip"): return "archive" def delete(self, *args, **kwargs): diff --git a/quiz/admin.py b/quiz/admin.py index e023877..15f62ba 100644 --- a/quiz/admin.py +++ b/quiz/admin.py @@ -9,7 +9,7 @@ from .models import ( Question, MCQuestion, Choice, - Essay_Question, + EssayQuestion, Sitting, ) @@ -89,5 +89,5 @@ class EssayQuestionAdmin(admin.ModelAdmin): admin.site.register(Quiz, QuizAdmin) admin.site.register(MCQuestion, MCQuestionAdmin) admin.site.register(Progress, ProgressAdmin) -admin.site.register(Essay_Question, EssayQuestionAdmin) +admin.site.register(EssayQuestion, EssayQuestionAdmin) admin.site.register(Sitting) diff --git a/quiz/models.py b/quiz/models.py index 46b28bb..f5c4cbb 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -540,7 +540,7 @@ class Choice(models.Model): verbose_name_plural = _("Choices") -class Essay_Question(Question): +class EssayQuestion(Question): def check_if_correct(self, guess): return False diff --git a/quiz/views.py b/quiz/views.py index 0887ab4..194acf7 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -1,6 +1,4 @@ -import random - -from django.contrib.auth.decorators import login_required, permission_required +from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404, render, redirect from django.utils.decorators import method_decorator @@ -11,18 +9,20 @@ from django.views.generic import ( FormView, CreateView, FormView, - DeleteView, UpdateView, ) from django.contrib import messages -from django.urls import reverse_lazy from django.db import transaction -from django.forms import inlineformset_factory -from django.http import HttpResponseRedirect -from accounts.decorators import student_required, lecturer_required -from .models import * -from .forms import * +from accounts.decorators import lecturer_required +from .models import Course, Progress, Sitting, EssayQuestion, Quiz, MCQuestion, Question +from .forms import ( + QuizAddForm, + MCQuestionForm, + MCQuestionFormSet, + QuestionForm, + EssayForm, +) @method_decorator([login_required, lecturer_required], name="dispatch") @@ -173,7 +173,7 @@ class QuizUserProgressView(TemplateView): def get_context_data(self, **kwargs): context = super(QuizUserProgressView, self).get_context_data(**kwargs) - progress, c = Progress.objects.get_or_create(user=self.request.user) + progress, _ = Progress.objects.get_or_create(user=self.request.user) context["cat_scores"] = progress.list_all_cat_scores context["exams"] = progress.show_exams() context["exams_counter"] = progress.show_exams().count() @@ -275,7 +275,7 @@ class QuizTake(FormView): self.question = self.sitting.get_first_question() self.progress = self.sitting.progress() - if self.question.__class__ is Essay_Question: + if self.question.__class__ is EssayQuestion: form_class = EssayForm else: form_class = self.form_class @@ -308,7 +308,7 @@ class QuizTake(FormView): return context def form_valid_user(self, form): - progress, c = Progress.objects.get_or_create(user=self.request.user) + progress, _ = Progress.objects.get_or_create(user=self.request.user) guess = form.cleaned_data["answers"] is_correct = self.question.check_if_correct(guess) diff --git a/result/models.py b/result/models.py index 39973b8..a88bfd3 100644 --- a/result/models.py +++ b/result/models.py @@ -2,7 +2,7 @@ from django.db import models from django.urls import reverse from accounts.models import Student -from app.models import Session, Semester +from app.models import Semester from course.models import Course YEARS = ( @@ -34,29 +34,29 @@ SEMESTER = ( (THIRD, "Third"), ) -A_plus = "A+" +A_PLUS = "A+" A = "A" -A_minus = "A-" -B_plus = "B+" +A_MINUS = "A-" +B_PLUS = "B+" B = "B" -B_minus = "B-" -C_plus = "C+" +B_MINUS = "B-" +C_PLUS = "C+" C = "C" -C_minus = "C-" +C_MINUS = "C-" D = "D" F = "F" NG = "NG" GRADE = ( - (A_plus, "A+"), + (A_PLUS, "A+"), (A, "A"), - (A_minus, "A-"), - (B_plus, "B+"), + (A_MINUS, "A-"), + (B_PLUS, "B+"), (B, "B"), - (B_minus, "B-"), - (C_plus, "C+"), + (B_MINUS, "B-"), + (C_PLUS, "C+"), (C, "C"), - (C_minus, "C-"), + (C_MINUS, "C-"), (D, "D"), (F, "F"), (NG, "NG"), @@ -72,21 +72,6 @@ COMMENT = ( class TakenCourseManager(models.Manager): - def new_or_get(self, request): - cart_id = request.session.get("cart_id", None) - qs = self.get_queryset().filter(id=cart_id) - if qs.count() == 1: - new_obj = False - cart_obj = qs.first() - if request.user.is_authenticated() and cart_obj.user is None: - cart_obj.user = request.user - cart_obj.save() - else: - cart_obj = Cart.objects.new(user=request.user) - new_obj = True - request.session["cart_id"] = cart_obj.id - return cart_obj, new_obj - def new(self, user=None): user_obj = None if user is not None: @@ -132,23 +117,23 @@ class TakenCourse(models.Model): # total = self.get_total(assignment=assignment, mid_exam=mid_exam, quiz=quiz, attendance=attendance, final_exam=final_exam) # total = total if total >= 90: - grade = A_plus + grade = A_PLUS elif total >= 85: grade = A elif total >= 80: - grade = A_minus + grade = A_MINUS elif total >= 75: - grade = B_plus + grade = B_PLUS elif total >= 70: grade = B elif total >= 65: - grade = B_minus + grade = B_MINUS elif total >= 60: - grade = C_plus + grade = C_PLUS elif total >= 55: grade = C elif total >= 50: - grade = C_minus + grade = C_MINUS elif total >= 45: grade = D elif total < 45: @@ -172,23 +157,23 @@ class TakenCourse(models.Model): # point = 0 # for i in student: credit = self.course.credit - if self.grade == A_plus: + if self.grade == A_PLUS: point = 4 elif self.grade == A: point = 4 - elif self.grade == A_minus: + elif self.grade == A_MINUS: point = 3.75 - elif self.grade == B_plus: + elif self.grade == B_PLUS: point = 3.5 elif self.grade == B: point = 3 - elif self.grade == B_minus: + elif self.grade == B_MINUS: point = 2.75 - elif self.grade == C_plus: + elif self.grade == C_PLUS: point = 2.5 elif self.grade == C: point = 2 - elif self.grade == C_minus: + elif self.grade == C_MINUS: point = 1.75 elif self.grade == D: point = 1 @@ -208,23 +193,23 @@ class TakenCourse(models.Model): point = 0 for i in student: credit = i.course.credit - if i.grade == A_plus: + if i.grade == A_PLUS: point = 4 elif i.grade == A: point = 4 - elif i.grade == A_minus: + elif i.grade == A_MINUS: point = 3.75 - elif i.grade == B_plus: + elif i.grade == B_PLUS: point = 3.5 elif i.grade == B: point = 3 - elif i.grade == B_minus: + elif i.grade == B_MINUS: point = 2.75 - elif i.grade == C_plus: + elif i.grade == C_PLUS: point = 2.5 elif i.grade == C: point = 2 - elif i.grade == C_minus: + elif i.grade == C_MINUS: point = 1.75 elif i.grade == D: point = 1 @@ -242,10 +227,10 @@ class TakenCourse(models.Model): previousResult = Result.objects.filter( student__id=self.student.id, level__lt=self.student.level ) - previousCGPA = 0 + previous_cgpa = 0 for i in previousResult: if i.cgpa is not None: - previousCGPA += i.cgpa + previous_cgpa += i.cgpa cgpa = 0 if str(current_semester) == SECOND: first_sem_gpa = 0.0 @@ -269,22 +254,22 @@ class TakenCourse(models.Model): taken_courses = TakenCourse.objects.filter( student=self.student, student__level=self.student.level ) - TCC = 0 - TCP = 0 + taken_course_credits = 0 + taken_course_points = 0 for i in taken_courses: - TCP += float(i.point) + taken_course_points += float(i.point) for i in taken_courses: - TCC += int(i.course.credit) + taken_course_credits += int(i.course.credit) # cgpa = (first_sem_gpa + sec_sem_gpa) / 2 - print("TCP = ", TCP) - print("TCC = ", TCC) + print("taken_course_points = ", taken_course_points) + print("taken_course_credits = ", taken_course_credits) print("first_sem_gpa = ", first_sem_gpa) print("sec_sem_gpa = ", sec_sem_gpa) - print("cgpa = ", round(TCP / TCC, 2)) + print("cgpa = ", round(taken_course_points / taken_course_credits, 2)) try: - cgpa = TCP / TCC + cgpa = taken_course_points / taken_course_credits return round(cgpa, 2) except ZeroDivisionError: return 0 diff --git a/result/views.py b/result/views.py index 4c92322..27a2e29 100644 --- a/result/views.py +++ b/result/views.py @@ -1,20 +1,11 @@ -from django.shortcuts import render, redirect, get_object_or_404 +from django.shortcuts import render, get_object_or_404 from django.contrib import messages from django.http import HttpResponseRedirect from django.urls import reverse_lazy from django.conf import settings from django.contrib.auth.decorators import login_required -from django.core.paginator import Paginator - -from accounts.models import User, Student -from app.models import Session, Semester -from course.models import Course -from accounts.decorators import lecturer_required, student_required -from .models import TakenCourse, Result - -# pdf from django.core.files.storage import FileSystemStorage -from django.http import HttpResponse, JsonResponse +from django.http import HttpResponse from reportlab.platypus import ( SimpleDocTemplate, @@ -23,14 +14,19 @@ from reportlab.platypus import ( Table, TableStyle, Image, - LongTable, ) -from reportlab.lib.styles import getSampleStyleSheet, black, ParagraphStyle +from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER, TA_RIGHT from reportlab.platypus.tables import Table from reportlab.lib.units import inch from reportlab.lib import colors -from .models import * + +from accounts.models import Student +from app.models import Session, Semester +from course.models import Course +from accounts.decorators import lecturer_required, student_required +from .models import TakenCourse, Result, FIRST, SECOND + cm = 2.54 @@ -42,15 +38,16 @@ cm = 2.54 @lecturer_required def add_score(request): """ - Shows a page where a lecturer will select a course allocated to him for score entry. - in a specific semester and session - + Shows a page where a lecturer will select a course allocated + to him for score entry. in a specific semester and session """ current_session = Session.objects.get(is_current_session=True) current_semester = get_object_or_404( Semester, is_current_semester=True, session=current_session ) - # semester = Course.objects.filter(allocated_course__lecturer__pk=request.user.id, semester=current_semester) + # semester = Course.objects.filter( + # allocated_course__lecturer__pk=request.user.id, + # semester=current_semester) courses = Course.objects.filter( allocated_course__lecturer__pk=request.user.id ).filter(semester=current_semester) @@ -66,8 +63,8 @@ def add_score(request): @lecturer_required def add_score_for(request, id): """ - Shows a page where a lecturer will add score for students that are taking courses allocated to him - in a specific semester and session + Shows a page where a lecturer will add score for students that + are taking courses allocated to him in a specific semester and session """ current_session = Session.objects.get(is_current_session=True) current_semester = get_object_or_404( @@ -81,9 +78,11 @@ def add_score_for(request, id): # myclass = Class.objects.get(lecturer__pk=request.user.id) # myclass = get_object_or_404(Class, lecturer__pk=request.user.id) - # students = TakenCourse.objects.filter(course__allocated_course__lecturer__pk=request.user.id).filter( - # course__id=id).filter(student__allocated_student__lecturer__pk=request.user.id).filter( - # course__semester=current_semester) + # students = TakenCourse.objects.filter( + # course__allocated_course__lecturer__pk=request.user.id).filter( + # course__id=id).filter( + # student__allocated_student__lecturer__pk=request.user.id).filter( + # course__semester=current_semester) students = ( TakenCourse.objects.filter( course__allocated_course__lecturer__pk=request.user.id @@ -186,12 +185,14 @@ def add_score_for(request, id): ) # try: - # a = Result.objects.get(student=student.student, semester=current_semester, level=student.student.level) + # a = Result.objects.get(student=student.student, + # semester=current_semester, level=student.student.level) # a.gpa = gpa # a.cgpa = cgpa # a.save() # except: - # Result.objects.get_or_create(student=student.student, gpa=gpa, semester=current_semester, level=student.student.level) + # Result.objects.get_or_create(student=student.student, gpa=gpa, + # semester=current_semester, level=student.student.level) messages.success(request, "Successfully Recorded! ") return HttpResponseRedirect(reverse_lazy("add_score_for", kwargs={"id": id})) @@ -723,7 +724,8 @@ def course_registration_form(request): has been duly registered for the " + student.level + " level of study in the department\ - of COMPUTER SICENCE & ENGINEERING and that the courses and credits registered are as approved by the senate of the University" + of COMPUTER SICENCE & ENGINEERING and that the courses and credits \ + registered are as approved by the senate of the University" ) certification_text = Paragraph(certification_text, certification) Story.append(certification_text) diff --git a/search/views.py b/search/views.py index 0321db1..eb863eb 100644 --- a/search/views.py +++ b/search/views.py @@ -1,39 +1,5 @@ -# from django.shortcuts import render -# from django.views.generic import ListView -# from app.models import NewsAndEvents - - -# class SearchNewsAndEventsView(ListView): -# template_name = "search/search_view.html" - -# def get_context_data(self, *args, **kwargs): -# context = super(SearchNewsAndEventsView, self).get_context_data(*args, **kwargs) -# query = self.request.GET.get('q') -# context['query'] = query -# context['obj_counter'] = NewsAndEvents.objects.search(query).count() -# # SearchQuery.objects.create(query=query) -# return context - -# def get_queryset(self, *args, **kwargs): -# request = self.request -# method_dict = request.GET -# query = method_dict.get('q', None) # method_dict['q'] -# if query is not None: -# return NewsAndEvents.objects.search(query) -# return NewsAndEvents.objects.all() -# ''' -# __icontains = field contains this -# __iexact = fields is exactly this -# ''' - - -# search.views.py from itertools import chain from django.views.generic import ListView - -from django.db.models import Q - -from accounts.models import User, Student from app.models import NewsAndEvents from course.models import Program, Course from quiz.models import Quiz @@ -64,7 +30,9 @@ class SearchView(ListView): queryset_chain = chain( news_events_results, program_results, course_results, quiz_results ) - qs = sorted(queryset_chain, key=lambda instance: instance.pk, reverse=True) - self.count = len(qs) # since qs is actually a list - return qs + queryset = sorted( + queryset_chain, key=lambda instance: instance.pk, reverse=True + ) + self.count = len(queryset) # since queryset is actually a list + return queryset return NewsAndEvents.objects.none() # just an empty queryset as default