Queryset default ordering and more

This commit is contained in:
papi 2023-12-31 12:13:22 +03:00
parent e0c122016a
commit 7c8fc91e73
12 changed files with 224 additions and 34 deletions

1
.gitignore vendored
View File

@ -129,6 +129,7 @@ dmypy.json
.pyre/ .pyre/
# Custome # Custome
staticfiles/
datadump.json datadump.json
local_note.txt local_note.txt
docs/ docs/

View File

@ -0,0 +1,29 @@
# Generated by Django 4.0.8 on 2023-12-31 07:50
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('accounts', '0016_alter_user_managers'),
]
operations = [
migrations.AlterModelOptions(
name='departmenthead',
options={'ordering': ('-user__date_joined',)},
),
migrations.AlterModelOptions(
name='parent',
options={'ordering': ('-user__date_joined',)},
),
migrations.AlterModelOptions(
name='student',
options={'ordering': ('-student__date_joined',)},
),
migrations.AlterModelOptions(
name='user',
options={'ordering': ('-date_joined',)},
),
]

View File

@ -71,6 +71,9 @@ class User(AbstractUser):
objects = CustomUserManager() objects = CustomUserManager()
class Meta:
ordering = ("-date_joined",)
@property @property
def get_full_name(self): def get_full_name(self):
full_name = self.username full_name = self.username
@ -140,6 +143,9 @@ class Student(models.Model):
objects = StudentManager() objects = StudentManager()
class Meta:
ordering = ("-student__date_joined",)
def __str__(self): def __str__(self):
return self.student.get_full_name return self.student.get_full_name
@ -168,6 +174,9 @@ class Parent(models.Model):
# the parent (i.e. father, mother, brother, sister) # the parent (i.e. father, mother, brother, sister)
relation_ship = models.TextField(choices=RELATION_SHIP, blank=True) relation_ship = models.TextField(choices=RELATION_SHIP, blank=True)
class Meta:
ordering = ("-user__date_joined",)
def __str__(self): def __str__(self):
return self.user.username return self.user.username
@ -176,5 +185,8 @@ class DepartmentHead(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.ForeignKey(Program, on_delete=models.CASCADE, null=True) department = models.ForeignKey(Program, on_delete=models.CASCADE, null=True)
class Meta:
ordering = ("-user__date_joined",)
def __str__(self): def __str__(self):
return "{}".format(self.user) return "{}".format(self.user)

View File

@ -294,33 +294,37 @@ def semester_delete_view(request, pk):
return redirect("semester_list") return redirect("semester_list")
# ########################################################
# from django.shortcuts import render_to_response
# from django.template import RequestContext
# def handler404(request, exception, template_name="common/404.html"):
# response = render_to_response("common/404.html")
# response.status_code = 404
# return response
# def handler500(request, *args, **argv):
# response = render_to_response('common/500.html', {}, context_instance=RequestContext(request))
# response.status_code = 500
# return response
# def handler400(request, exception, template_name="common/400.html"):
# response = render_to_response('common/400.html', context_instance=RequestContext(request))
# response.status_code = 400
# return response
@login_required @login_required
@admin_required @admin_required
def dashboard_view(request): def dashboard_view(request):
return render(request, "app/dashboard.html") return render(request, "app/dashboard.html")
# ########################################################
def handler404(request, exception, template_name="common/404.html"):
response = render(request, "common/404.html")
response.status_code = 404
return response
def handler500(request, *args, **argv):
response = render(request, "common/500.html")
response.status_code = 500
return response
def handler400(request, exception, template_name="common/400.html"):
response = render(request, "common/400.html")
response.status_code = 400
return response
def handler403(request, exception, template_name="common/403.html"):
response = render(request, "common/403.html")
response.status_code = 403
return response

View File

@ -11,7 +11,6 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
""" """
import os import os
import posixpath
from decouple import config from decouple import config
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -24,9 +23,9 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = "o!ld8nrt4vc*h1zoey*wj48x*q0#ss12h=+zh)kk^6b3aygg=!" SECRET_KEY = "o!ld8nrt4vc*h1zoey*wj48x*q0#ss12h=+zh)kk^6b3aygg=!"
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = config("DEBUG", default=True, cast=bool)
ALLOWED_HOSTS = [] ALLOWED_HOSTS = ["*"]
# change the default user models to our custom model # change the default user models to our custom model
AUTH_USER_MODEL = "accounts.User" AUTH_USER_MODEL = "accounts.User"
@ -164,12 +163,21 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/ # https://docs.djangoproject.com/en/2.2/howto/static-files/
# https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/" STATIC_URL = "/static/"
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [ STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"), os.path.join(BASE_DIR, "static"),
] ]
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
# Media files config
MEDIA_URL = "/media/" MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media") MEDIA_ROOT = os.path.join(BASE_DIR, "media")
@ -203,3 +211,27 @@ REST_FRAMEWORK = {
# Strip payment config # Strip payment config
STRIPE_SECRET_KEY = config("STRIPE_SECRET_KEY", default="") STRIPE_SECRET_KEY = config("STRIPE_SECRET_KEY", default="")
STRIPE_PUBLISHABLE_KEY = config("STRIPE_PUBLISHABLE_KEY", default="") STRIPE_PUBLISHABLE_KEY = config("STRIPE_PUBLISHABLE_KEY", default="")
# LOGGING
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
# See https://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "%(levelname)s %(asctime)s %(module)s "
"%(process)d %(thread)d %(message)s"
}
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "verbose",
}
},
"root": {"level": "INFO", "handlers": ["console"]},
}

View File

@ -1,10 +1,10 @@
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.conf.urls import handler404, handler500, handler400
from django.conf import settings from django.conf import settings
from django.conf.urls.static import static from django.conf.urls.static import static
from django.views import defaults as default_views
admin.site.site_header = "Django LMS Admin" admin.site.site_header = "Dj-LMS Admin"
urlpatterns = [ urlpatterns = [
path("jet/", include("jet.urls", "jet")), # Django JET URLS path("jet/", include("jet.urls", "jet")), # Django JET URLS
@ -26,6 +26,28 @@ if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# handler404 = 'app.views.handler404' if settings.DEBUG:
# handler500 = 'app.views.handler500' # This allows the error pages to be debugged during development, just visit
# handler400 = 'app.views.handler400' # these url in browser to see how these error pages look like.
urlpatterns += [
path(
"400/",
default_views.bad_request,
kwargs={"exception": Exception("Bad Request!")},
),
path(
"403/",
default_views.permission_denied,
kwargs={"exception": Exception("Permission Denied")},
),
path(
"404/",
default_views.page_not_found,
kwargs={"exception": Exception("Page not Found")},
),
path("500/", default_views.server_error),
]
handler404 = "app.views.handler404"
handler500 = "app.views.handler500"
handler400 = "app.views.handler400"

View File

@ -0,0 +1,23 @@
# Generated by Django 4.0.8 on 2023-12-31 07:50
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quiz', '0003_rename_essay_question_essayquestion'),
]
operations = [
migrations.AlterField(
model_name='question',
name='figure',
field=models.ImageField(blank=True, help_text="Add an image for the question if it's necessary.", null=True, upload_to='uploads/%Y/%m/%d', verbose_name='Figure'),
),
migrations.AlterField(
model_name='quiz',
name='description',
field=models.TextField(blank=True, help_text='A detailed description of the quiz', verbose_name='Description'),
),
]

View File

@ -0,0 +1,9 @@
{% extends 'common/_base.html' %}
{% block content %}
<div class="text-center">
<h1>Bad request</h1>
<p>Please make sure the form is correctly filled.</p>
<a href="/" class="link">&LeftArrow; Return to the app</a>
</div>
{% endblock %}

View File

@ -0,0 +1,9 @@
{% extends 'common/_base.html' %}
{% block content %}
<div class="text-center">
<h1>403, forbidden</h1>
<p>You need the proper permission to make that request.</p>
<a href="/" class="link">&LeftArrow; Return to the app</a>
</div>
{% endblock %}

View File

@ -0,0 +1,9 @@
{% extends 'common/_base.html' %}
{% block content %}
<div class="text-center">
<h1>404</h1>
<p>Looks like the page you're looking for is does not exist.</p>
<a href="/" class="link">&LeftArrow; Return to the app</a>
</div>
{% endblock %}

View File

@ -0,0 +1,9 @@
{% extends 'common/_base.html' %}
{% block content %}
<div class="text-center">
<h1>Server error</h1>
<p>Please try again later.</p>
<a href="/" class="link">&LeftArrow; Return to the app</a>
</div>
{% endblock %}

View File

@ -0,0 +1,31 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{% block title %}Django Learning Management System{% endblock title %}</title>
<link rel="shortcut icon" href="{% static 'img/favicon.png' %}" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/all.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
</head>
<body>
<div id="main">
<div class="container-fluid" id="main-content">
{% block content %}{% endblock content %}
</div>
</div>
</body>
</html>