Commenting my code except the two most important ones: views.py and model.py

master
Corentin LEMAIRE 6 months ago
parent af39563341
commit b76325c76d

@ -20,6 +20,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
# Fell free to get this key, I won't publish my project into a release, and I didn't want to take the time to hide it.
SECRET_KEY = 'django-insecure-3ib(8=@cwv%48&0@r1c)inr_*l*-ky$q!8*f_x2&*8a(1_mxb#'
# SECURITY WARNING: don't run with debug turned on in production!
@ -37,7 +38,9 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My project, where all my code is defined
'redis_app',
# I love using debug toolbar
'debug_toolbar',
]
@ -52,12 +55,15 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# Localhost website
INTERNAL_IPS = [
'127.0.0.1',
]
# All my URLs are in this file, kinda Router
ROOT_URLCONF = 'CourseMaster.urls'
# Declaring redis
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
@ -80,6 +86,7 @@ WSGI_APPLICATION = 'CourseMaster.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
# Useless, I don't use it in my project because I'm using Redis
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
@ -106,6 +113,7 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
# Defining the cache in redis. Not very important here, just played with it after I saw a YouTube video showcasing this feature.
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
@ -120,8 +128,8 @@ CACHES = {
# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/
# I'm telling my project that le language is french (even if all my texts are in english...)
LANGUAGE_CODE = 'fr-fr'
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
@ -132,6 +140,8 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
# Allows me to use css into my templates. I followed the django docs.
STATIC_URL = '/static/'
# Default primary key field type

@ -19,7 +19,10 @@ from django.urls import path, include
import debug_toolbar
urlpatterns = [
# Useless, there's no admin part in my project
path('admin/', admin.site.urls),
# The main project
path('redis/', include('redis_app.urls')),
# For debug toolbar
path('__debug__/', include(debug_toolbar.urls))
]

@ -1,6 +1 @@
from django.contrib import admin
from .models import Course, Person
admin.site.register(Course)
admin.site.register(Person)

@ -1,6 +1,6 @@
from django.apps import AppConfig
# Configuring redis as I saw it on the doc.
class RedisAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'redis_app'

@ -1,3 +1,6 @@
/* I don't think commenting css would be relevant enough. My comments could litteraly only describe every line, so it would be faster to just read each part */
/* This css is not really good, I just spent 2 hours to make it because the main part is not the UX/UI. Sorry if you thinks it's ugly :( */
body {
margin: 0;
font-family: Arial, sans-serif;

@ -1,38 +1,71 @@
<html>
<!--
Every head markup will be the same. I know in twig we can do some templating, but didn't take the time to search if it was possible with django.
This head doesn't have anything, not even the title part because I didn't want to take the time for it.
I won't repeat this comment for all HTML file because it would be the same.
-->
<head>
{% load static %}
<link rel="stylesheet" href="{% static 'redis_app/style.css' %}">
</head>
<body>
<!--
The navbar is also the same for each HTML. There's all important links for the navigation.
Also for this one, I won't repeat this for each HTML.
-->
<nav>
<a href="{% url 'home' %}">Home</a>
<!-- If you're not logged in -->
{% if person == '' %}
<a href="{% url 'login_form' %}">Login</a>
<a href="{% url 'register_form' %}">Register</a>
<!-- If you're logged in -->
{% else %}
<a href="{% url 'courses' %}">Courses</a>
<a href="{% url 'profile' %}">Profile</a>
<a href="{% url 'search' %}">Search</a>
<a href="{% url 'notifications_view' %}">Notifications</a>
<!-- If you're a teacher -->
{% if person.role == 'Teacher' %}
<a href="{% url 'publish_message' %}">Publish a message</a>
{% endif %}
<a href="{% url 'logout' %}">Logout</a>
{% endif %}
</nav>
<!--
Main part of the HTML. there's 3 cascading div to help me creating a nice layout with flex display (because I love flex.
Also for this one, I won't repeat this for each HTML.
-->
<div class="body">
<div class="container">
<div class="div-container">
<!--
Errors and successes section. It helps me enhance the UX by informing the user if something bad happened.
Also for this one, I won't repeat this for each HTML.
-->
{% if error_message %}
<p class="error-message">{{ error_message }}</p>
{% endif %}
{% if success_message %}
<p class="success-message">{{ success_message }}</p>
{% endif %}
<div class="create-container">
{% if person %}
{% if person.role == "Teacher" %}
<!--
This scary code is only here to display + Create with a nice style (again to use flex display). this will let the user creating a course.
It's only for teachers.
-->
<div class="create-container">
<div>
<!--
These a href redirects to url 'name' 'arguments' .
Indeed, this is a cool feature of django defining routes with names and arguments to retrieve it very fast in the code.
-->
<a href="{% url 'create_course' %}" class="create-course">
<p class="plus">
+
@ -42,19 +75,26 @@
</p>
</a>
</div>
</div>
{% endif %}
{% endif %}
</div>
<h1>Courses for {{ person.name }}</h1>
<!-- If there's courses -->
{% if courses %}
<div class="courses">
{% for course in courses %}
<!-- Displaying each course as a button with the course name redirecting to the course detail -->
<a href="{% url 'details' course.id %}" class="course-link">{{ course.title }}</a>
{% endfor %}
</div>
<!-- If there's not -->
{% else %}
<p class="no-courses">No course yet</p>
{% endif %}
<!-- Letting the user registering to a course with a link to Search feature. Also works for teacher if they want to copy other teachers' courses -->
<p class="register-link">
Register to a course by <a href="{% url 'search' %}">Searching</a>
</p>

@ -22,7 +22,15 @@
</nav>
<div class="body">
<div class="container">
<!--
Redirects to my view called create_course_form which handles the POST result of the form to create a course.
I won't repeat this part because for all form, it will be the same
-->
<form action="{% url 'create_course_form' %}" method="POST">
<!--
What a great feature... As we seen in Security module, csrf are really dangerous attacks on form. We have to prevent them.
And Django helped us with this really short code, but soooo useful!
-->
{% csrf_token %}
<fieldset>
<legend><h1>Create a course</h1></legend>

@ -29,6 +29,8 @@
{% if success_message %}
<p class="success-message">{{ success_message }}</p>
{% endif %}
<!-- Displaying information about the course, each of course information -->
{% if course %}
<h1>Welcome in the course {{ course.title }}!</h1>
<p>
@ -43,17 +45,28 @@
<p>
The course has been created by {{ course.teacher_name }}
</p>
<!-- When someone tries to go directly into a detail page but failed to type the correct course id into the URL. -->
{% else %}
<h1>No course found!</h1>
<p class="no-courses">No course found!</p>
{% endif %}
<!-- All actions. -->
{% if person %}
<!-- Teachers actions -->
{% if person.role == "Teacher" %}
<a href="{% url 'delete_course' course.id %}">Delete</a>
<a href="{% url 'update_course' course.id %}">Update</a>
<!-- Students actions -->
{% else %}
<!-- If he's registered to the ourse, he can unregister from it -->
{% if register %}
<a href="{% url 'course_unregister' course.id %}">Unregister</a>
<!-- If he's not registered to the course, he can register to it -->
{% else %}
<!-- But if the course if full (if there's as many students as the number of places defined in course creatin/update), he can't register-->
{% if full == False %}
<a href="{% url 'course_register' course.id %}">Register</a>
{% else %}

@ -29,6 +29,8 @@
{% if success_message %}
<p class="success-message">{{ success_message }}</p>
{% endif %}
<!-- Displaying a welcome message with his name if he's login. Helps us to know when we're logged in or not, and with which account -->
{% if person != '' %}
<h1>Welcome {{ person.name }}!</h1>
{% else %}

@ -32,6 +32,7 @@
{% if success_message %}
<p class="success-message">{{ success_message }}</p>
{% endif %}
<label for="name">Name</label>
<input name="name" id="name" value="{{ person.name }}" class="input">
<label for="role">Role</label>

@ -25,8 +25,11 @@
<div class="notifications">
<h1>Notifications</h1>
<!-- To clear all old notifications. -->
<a href="{% url 'clear_notifications' %}">Clear notifications</a>
</div>
<!-- Displaying every message as a link with the mesage as title and redirects to the course associated to the message. -->
{% for message in messages %}
<a href="{% url 'details' message.channel %}">{{ message.data }}</a>
{% endfor %}

@ -32,10 +32,12 @@
{% if success_message %}
<p class="success-message">{{ success_message }}</p>
{% endif %}
<label for="name">Name</label>
<input name="name" id="name" value="{{ person.name }}" class="input">
<label for="role">Role</label>
<select name="role" id="role">
<!-- Selecting if he's Student or Teacher. This is cool when you arrive to this page, everything is well selected! -->
<option value="Student" {% if person.role == 'Student' %}selected{% endif %}>Student</option>
<option value="Teacher" {% if person.role == 'Teacher' %}selected{% endif %}>Teacher</option>
</select>

@ -34,6 +34,7 @@
<p class="success-message">{{ success_message }}</p>
{% endif %}
<!-- Choose the course related to the news you want to publish. -->
<label for="course_id">Course</label>
<select name="course_id" id="course_id" required>
{% for course in courses %}

@ -32,6 +32,7 @@
{% if success_message %}
<p class="success-message">{{ success_message }}</p>
{% endif %}
<label for="name">Name</label>
<input name="name" id="name" value="{{ person.name }}" class="input">
<label for="role">Role</label>

@ -32,6 +32,14 @@
{% if success_message %}
<p class="success-message">{{ success_message }}</p>
{% endif %}
<!--
Asking for keywords my project will search with to title, description and level attributes of every courses.
You have to type the exact word to match.
You can search for multiples courses by typing multiple keywords separated with spaces.
This displays the search for each keyword. It won't try to match for the specific search of two keywords. It will handle one keyword at once.
However, if a course has 'test' as a title and has 'test' in description, the search will display the course 2 times, for each time there's the match.
-->
<label for="keywords">Keywords</label>
<input name="keywords" id="keywords" placeholder="Keywords separated by spaces" class="input">
</fieldset>
@ -39,12 +47,16 @@
<input type="submit" value="Search" class="submit">
</div>
</form>
<!-- Displaying all courses found. -->
{% if courses %}
{% for course in courses %}
<a href="{% url 'details' course.id %}">{{ course.title }}</a>
{% endfor %}
<!-- When no course is found :( -->
{% else %}
<p>No course found</p>
<p class="no-courses">No course found.</p>
{% endif %}
</div>
</div>

@ -39,6 +39,7 @@
<label for="level">Course Level</label>
<select name="level" id="level" required>
<!-- To auto select again the level :sunglasses: -->
{% if course.level == 'Beginner' %}
<option value="Beginner" selected>Beginner</option>
<option value="Intermediate">Intermediate</option>

@ -1,3 +1,4 @@
from django.test import TestCase
# Create your tests here.
# Testing = doubting. (Joking, but didn't take the time to test my app even if it's maybe one of the most important think to do in a project)

@ -1,6 +1,7 @@
from django.urls import path
from . import views
# Defining my routes here, to associate my URLs, my routes names and my views functions.
urlpatterns = [
path('', views.home, name='home'),
path('register_form', views.register_form, name='register_form'),

@ -9,9 +9,9 @@ def create_index(redis_connection):
'SCHEMA',
'title', 'TEXT',
'summary', 'TEXT',
'level', 'TEXT', # Found Tag here, it's for enums, when we know the possibility
'level', 'TEXT',
'places', 'NUMERIC',
'teacher', 'TEXT', # ToString of the teacher because it's a primary key
'teacher', 'TEXT', # Id of the teacher, without the teacher:
)
# Same for persons

Loading…
Cancel
Save