diff --git a/.pylintrc b/.pylintrc index 99d3a19..c27897b 100644 --- a/.pylintrc +++ b/.pylintrc @@ -451,7 +451,8 @@ disable=raw-checker-failed, arguments-differ, invalid-overridden-method, unsupported-binary-operation, - attribute-defined-outside-init + attribute-defined-outside-init, + duplicate-code # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/config/settings.py b/config/settings.py index cc45d3a..7f03b05 100644 --- a/config/settings.py +++ b/config/settings.py @@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/2.2/ref/settings/ import os from decouple import config +from django.utils.translation import gettext_lazy as _ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -251,3 +252,32 @@ STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" STUDENT_ID_PREFIX = config("STUDENT_ID_PREFIX", "ugr") LECTURER_ID_PREFIX = config("LECTURER_ID_PREFIX", "lec") + + +# Constants +YEARS = ( + (1, "1"), + (2, "2"), + (3, "3"), + (4, "4"), + (5, "5"), + (6, "6"), +) + +BACHELOR_DEGREE = "Bachelor" +MASTER_DEGREE = "Master" + +LEVEL_CHOICES = ( + (BACHELOR_DEGREE, _("Bachelor Degree")), + (MASTER_DEGREE, _("Master Degree")), +) + +FIRST = "First" +SECOND = "Second" +THIRD = "Third" + +SEMESTER_CHOICES = ( + (FIRST, _("First")), + (SECOND, _("Second")), + (THIRD, _("Third")), +) diff --git a/core/utils.py b/core/utils.py index 19bf670..5bd2d5d 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,7 +1,11 @@ +import random +import string +from django.utils.text import slugify from django.core.mail import send_mail from django.template.loader import render_to_string from django.utils.html import strip_tags from django.conf import settings +from django.utils.text import slugify def send_email(user, subject, msg): @@ -30,3 +34,27 @@ def send_html_email(subject, recipient_list, template, context): recipient_list, html_message=html_message, ) + + +def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits): + return "".join(random.choice(chars) for _ in range(size)) + + +def unique_slug_generator(instance, new_slug=None): + """ + Assumes the instance has a model with a slug field and a title + character (char) field. + """ + if new_slug is not None: + slug = new_slug + else: + slug = slugify(instance.title) + + Klass = instance.__class__ + qs_exists = Klass.objects.filter(slug=slug).exists() + if qs_exists: + new_slug = "{slug}-{randstr}".format( + slug=slug, randstr=random_string_generator(size=4) + ) + return unique_slug_generator(instance, new_slug=new_slug) + return slug diff --git a/course/models.py b/course/models.py index 89c900e..3737745 100644 --- a/course/models.py +++ b/course/models.py @@ -7,37 +7,8 @@ from django.dispatch import receiver from django.urls import reverse from django.utils.translation import gettext_lazy as _ -from core.models import ActivityLog -from core.models import Semester -from .utils import unique_slug_generator - -# Constants -YEARS = ( - (1, "1"), - (2, "2"), - (3, "3"), - (4, "4"), - (5, "5"), - (6, "6"), -) - -BACHELOR_DEGREE = "Bachelor" -MASTER_DEGREE = "Master" - -LEVEL_CHOICES = ( - (BACHELOR_DEGREE, _("Bachelor Degree")), - (MASTER_DEGREE, _("Master Degree")), -) - -FIRST = "First" -SECOND = "Second" -THIRD = "Third" - -SEMESTER_CHOICES = ( - (FIRST, _("First")), - (SECOND, _("Second")), - (THIRD, _("Third")), -) +from core.models import ActivityLog, Semester +from core.utils import unique_slug_generator class ProgramManager(models.Manager): @@ -94,9 +65,9 @@ class Course(models.Model): credit = models.IntegerField(default=0) summary = models.TextField(max_length=200, blank=True) program = models.ForeignKey(Program, on_delete=models.CASCADE) - level = models.CharField(max_length=25, choices=LEVEL_CHOICES) - year = models.IntegerField(choices=YEARS, default=1) - semester = models.CharField(choices=SEMESTER_CHOICES, max_length=200) + level = models.CharField(max_length=25, choices=settings.LEVEL_CHOICES) + year = models.IntegerField(choices=settings.YEARS, default=1) + semester = models.CharField(choices=settings.SEMESTER_CHOICES, max_length=200) is_elective = models.BooleanField(default=False) objects = CourseManager() diff --git a/course/utils.py b/course/utils.py index 48d1108..e69de29 100644 --- a/course/utils.py +++ b/course/utils.py @@ -1,30 +0,0 @@ -import datetime -import os -import random -import string - -from django.utils.text import slugify - - -def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits): - return "".join(random.choice(chars) for _ in range(size)) - - -def unique_slug_generator(instance, new_slug=None): - """ - This is for a Django project and it assumes your instance - has a model with a slug field and a title character (char) field. - """ - if new_slug is not None: - slug = new_slug - else: - slug = slugify(instance.title) - - Klass = instance.__class__ - qs_exists = Klass.objects.filter(slug=slug).exists() - if qs_exists: - new_slug = "{slug}-{randstr}".format( - slug=slug, randstr=random_string_generator(size=4) - ) - return unique_slug_generator(instance, new_slug=new_slug) - return slug diff --git a/quiz/forms.py b/quiz/forms.py index b54448c..a2dde4a 100644 --- a/quiz/forms.py +++ b/quiz/forms.py @@ -2,11 +2,7 @@ from django import forms from django.forms.widgets import RadioSelect, Textarea from django.contrib.admin.widgets import FilteredSelectMultiple from django.utils.translation import gettext_lazy as _ -from django.db import transaction - from django.forms.models import inlineformset_factory - -from accounts.models import User from .models import Question, Quiz, MCQuestion, Choice diff --git a/quiz/models.py b/quiz/models.py index 97b6e57..527fe3f 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -17,7 +17,7 @@ from django.dispatch import receiver from model_utils.managers import InheritanceManager from course.models import Course -from .utils import unique_slug_generator +from core.utils import unique_slug_generator CHOICE_ORDER_OPTIONS = ( ("content", _("Content")), diff --git a/quiz/utils.py b/quiz/utils.py index 48d1108..e69de29 100644 --- a/quiz/utils.py +++ b/quiz/utils.py @@ -1,30 +0,0 @@ -import datetime -import os -import random -import string - -from django.utils.text import slugify - - -def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits): - return "".join(random.choice(chars) for _ in range(size)) - - -def unique_slug_generator(instance, new_slug=None): - """ - This is for a Django project and it assumes your instance - has a model with a slug field and a title character (char) field. - """ - if new_slug is not None: - slug = new_slug - else: - slug = slugify(instance.title) - - Klass = instance.__class__ - qs_exists = Klass.objects.filter(slug=slug).exists() - if qs_exists: - new_slug = "{slug}-{randstr}".format( - slug=slug, randstr=random_string_generator(size=4) - ) - return unique_slug_generator(instance, new_slug=new_slug) - return slug diff --git a/result/models.py b/result/models.py index d60a546..a24dfc0 100644 --- a/result/models.py +++ b/result/models.py @@ -1,4 +1,5 @@ from decimal import Decimal +from django.conf import settings from django.db import models from django.urls import reverse @@ -7,34 +8,6 @@ from accounts.models import Student from core.models import Semester from course.models import Course -# Constants -YEARS = ( - (1, "1"), - (2, "2"), - (3, "3"), - (4, "4"), - (5, "5"), - (6, "6"), -) - -BACHELOR_DEGREE = "Bachelor" -MASTER_DEGREE = "Master" - -LEVEL_CHOICES = ( - (BACHELOR_DEGREE, "Bachelor Degree"), - (MASTER_DEGREE, "Master Degree"), -) - -FIRST = "First" -SECOND = "Second" -THIRD = "Third" - -SEMESTER_CHOICES = ( - (FIRST, "First"), - (SECOND, "Second"), - (THIRD, "Third"), -) - A_PLUS = "A+" A = "A" A_MINUS = "A-" @@ -208,9 +181,9 @@ class Result(models.Model): student = models.ForeignKey(Student, on_delete=models.CASCADE) gpa = models.FloatField(null=True) cgpa = models.FloatField(null=True) - semester = models.CharField(max_length=100, choices=SEMESTER_CHOICES) + semester = models.CharField(max_length=100, choices=settings.SEMESTER_CHOICES) session = models.CharField(max_length=100, blank=True, null=True) - level = models.CharField(max_length=25, choices=LEVEL_CHOICES, null=True) + level = models.CharField(max_length=25, choices=settings.LEVEL_CHOICES, null=True) def __str__(self): return f"Result for {self.student} - Semester: {self.semester}, Level: {self.level}"