hello am facing an error below as shown on the image which i did not exist before i dont know where i messed things up
view.py
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from accounts.decorators import admin_required, lecturer_required
from accounts.models import AbstractUser, Student
from .forms import SessionForm, SemesterForm, NewsAndEventsForm
from .models import NewsAndEvents, ActivityLog, Session, Semester
# ########################################################
# News & Events
# ########################################################
@login_required
def home_view(request):
items = NewsAndEvents.objects.all().order_by("-updated_date")
context = {
"title": "News & Events",
"items": items,
}
return render(request, "core/index.html", context)
@login_required
@admin_required
def dashboard_view(request):
logs = ActivityLog.objects.all().order_by("-created_at")[:10]
gender_count = Student.get_gender_count()
context = {
"student_count": User.objects.get_student_count(),
"lecturer_count": User.objects.get_lecturer_count(),
"superuser_count": User.objects.get_superuser_count(),
"males_count": gender_count["M"],
"females_count": gender_count["F"],
"logs": logs,
}
return render(request, "core/dashboard.html", context)
@login_required
def post_add(request):
if request.method == "POST":
form = NewsAndEventsForm(request.POST)
title = request.POST.get("title")
if form.is_valid():
form.save()
messages.success(request, (title + " has been uploaded."))
return redirect("home")
else:
messages.error(request, "Please correct the error(s) below.")
else:
form = NewsAndEventsForm()
return render(
request,
"core/post_add.html",
{
"title": "Add Post",
"form": form,
},
)
@login_required
@lecturer_required
def edit_post(request, pk):
instance = get_object_or_404(NewsAndEvents, pk=pk)
if request.method == "POST":
form = NewsAndEventsForm(request.POST, instance=instance)
title = request.POST.get("title")
if form.is_valid():
form.save()
messages.success(request, (title + " has been updated."))
return redirect("home")
else:
messages.error(request, "Please correct the error(s) below.")
else:
form = NewsAndEventsForm(instance=instance)
return render(
request,
"core/post_add.html",
{
"title": "Edit Post",
"form": form,
},
)
@login_required
@lecturer_required
def approve_post(request, pk):
post = get_object_or_404(NewsAndEvents, pk=pk)
title = post.title
approve = NewsAndEvents.objects.get(pk=pk)
approve.approved_status = "1"
approve.save()
messages.success(request, (title + " has been Approved."))
return redirect("home")
@login_required
@lecturer_required
def delete_post(request, pk):
post = get_object_or_404(NewsAndEvents, pk=pk)
title = post.title
post.delete()
messages.success(request, (title + " has been deleted."))
return redirect("home")
# ########################################################
# Session
# ########################################################
@login_required
@lecturer_required
def session_list_view(request):
"""Show list of all sessions"""
sessions = Session.objects.all().order_by("-is_current_session", "-session")
return render(request, "core/session_list.html", {"sessions": sessions})
@login_required
@lecturer_required
def session_add_view(request):
"""check request method, if POST we add session otherwise show empty form"""
if request.method == "POST":
form = SessionForm(request.POST)
if form.is_valid():
data = form.data.get(
"is_current_session"
) # returns string of 'True' if the user selected Yes
print(data)
if data == "true":
sessions = Session.objects.all()
if sessions:
for session in sessions:
if session.is_current_session == True:
unset = Session.objects.get(is_current_session=True)
unset.is_current_session = False
unset.save()
form.save()
else:
form.save()
else:
form.save()
messages.success(request, "Session added successfully. ")
return redirect("session_list")
else:
form = SessionForm()
return render(request, "core/session_update.html", {"form": form})
@login_required
@lecturer_required
def session_update_view(request, pk):
session = Session.objects.get(pk=pk)
if request.method == "POST":
form = SessionForm(request.POST, instance=session)
data = form.data.get("is_current_session")
if data == "true":
sessions = Session.objects.all()
if sessions:
for session in sessions:
if session.is_current_session == True:
unset = Session.objects.get(is_current_session=True)
unset.is_current_session = False
unset.save()
if form.is_valid():
form.save()
messages.success(request, "Session updated successfully. ")
return redirect("session_list")
else:
form = SessionForm(request.POST, instance=session)
if form.is_valid():
form.save()
messages.success(request, "Session updated successfully. ")
return redirect("session_list")
else:
form = SessionForm(instance=session)
return render(request, "core/session_update.html", {"form": form})
@login_required
@lecturer_required
def session_delete_view(request, pk):
session = get_object_or_404(Session, pk=pk)
if session.is_current_session:
messages.error(request, "You cannot delete current session")
return redirect("session_list")
else:
session.delete()
messages.success(request, "Session successfully deleted")
return redirect("session_list")
# ########################################################
# ########################################################
# Semester
# ########################################################
@login_required
@lecturer_required
def semester_list_view(request):
semesters = Semester.objects.all().order_by("-is_current_semester", "-semester")
return render(
request,
"core/semester_list.html",
{
"semesters": semesters,
},
)
@login_required
@lecturer_required
def semester_add_view(request):
if request.method == "POST":
form = SemesterForm(request.POST)
if form.is_valid():
data = form.data.get(
"is_current_semester"
) # returns string of 'True' if the user selected Yes
if data == "True":
semester = form.data.get("semester")
ss = form.data.get("session")
session = Session.objects.get(pk=ss)
try:
if Semester.objects.get(semester=semester, session=ss):
messages.error(
request,
semester
+ " semester in "
+ session.session
+ " session already exist",
)
return redirect("add_semester")
except:
semesters = Semester.objects.all()
sessions = Session.objects.all()
if semesters:
for semester in semesters:
if semester.is_current_semester == True:
unset_semester = Semester.objects.get(
is_current_semester=True
)
unset_semester.is_current_semester = False
unset_semester.save()
for session in sessions:
if session.is_current_session == True:
unset_session = Session.objects.get(
is_current_session=True
)
unset_session.is_current_session = False
unset_session.save()
new_session = request.POST.get("session")
set_session = Session.objects.get(pk=new_session)
set_session.is_current_session = True
set_session.save()
form.save()
messages.success(request, "Semester added successfully.")
return redirect("semester_list")
form.save()
messages.success(request, "Semester added successfully. ")
return redirect("semester_list")
else:
form = SemesterForm()
return render(request, "core/semester_update.html", {"form": form})
@login_required
@lecturer_required
def semester_update_view(request, pk):
semester = Semester.objects.get(pk=pk)
if request.method == "POST":
if (
request.POST.get("is_current_semester") == "True"
): # returns string of 'True' if the user selected yes for 'is current semester'
unset_semester = Semester.objects.get(is_current_semester=True)
unset_semester.is_current_semester = False
unset_semester.save()
unset_session = Session.objects.get(is_current_session=True)
unset_session.is_current_session = False
unset_session.save()
new_session = request.POST.get("session")
form = SemesterForm(request.POST, instance=semester)
if form.is_valid():
set_session = Session.objects.get(pk=new_session)
set_session.is_current_session = True
set_session.save()
form.save()
messages.success(request, "Semester updated successfully !")
return redirect("semester_list")
else:
form = SemesterForm(request.POST, instance=semester)
if form.is_valid():
form.save()
return redirect("semester_list")
else:
form = SemesterForm(instance=semester)
return render(request, "core/semester_update.html", {"form": form})
@login_required
@lecturer_required
def semester_delete_view(request, pk):
semester = get_object_or_404(Semester, pk=pk)
if semester.is_current_semester:
messages.error(request, "You cannot delete current semester")
return redirect("semester_list")
else:
semester.delete()
messages.success(request, "Semester successfully deleted")
return redirect("semester_list")
model.py
from django.db import models
from django.urls import reverse
from django.core.validators import FileExtensionValidator
from django.contrib.auth.models import AbstractUser, UserManager
from django.db.models import Q
NEWS = "News"
EVENTS = "Event"
POST = (
(NEWS, "News"),
(EVENTS, "Event"),
)
ASSIGNMENT = "Assignment"
QUI = "Quiz"
EXAM = "Exam"
GENERAL = "General"
CATEGORY = (
(ASSIGNMENT, "Assignment"),
(QUI, "Quiz"),
(EXAM, "Exam"),
(GENERAL, "General"),
)
FIRST = "First"
SECOND = "Second"
THIRD = "Third"
SEMESTER = (
(FIRST, "First"),
(SECOND, "Second"),
(THIRD, "Third"),
)
class NewsAndEventsQuerySet(models.query.QuerySet):
def search(self, query):
lookups = (
Q(title__icontains=query)
| Q(summary__icontains=query)
| Q(posted_as__icontains=query)
)
return self.filter(lookups).distinct()
class NewsAndEventsManager(models.Manager):
def get_queryset(self):
return NewsAndEventsQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset()
def get_by_id(self, id):
qs = self.get_queryset().filter(
id=id
) # NewsAndEvents.objects == self.get_queryset()
if qs.count() == 1:
return qs.first()
return None
def search(self, query):
return self.get_queryset().search(query)
class NewsAndEvents(models.Model):
title = models.CharField(max_length=200, null=True)
summary = models.TextField(max_length=200, blank=True, null=True)
posted_as = models.CharField(choices=POST, max_length=10)
approved_status = models.CharField(max_length=20, default=0, null=False)
updated_date = models.DateTimeField(auto_now=True, auto_now_add=False, null=True)
upload_time = models.DateTimeField(auto_now=False, auto_now_add=True, null=True)
objects = NewsAndEventsManager()
def __str__(self):
return self.title
class Session(models.Model):
session = models.CharField(max_length=200, unique=True)
is_current_session = models.BooleanField(default=False, blank=True, null=True)
next_session_begins = models.DateField(blank=True, null=True)
def __str__(self):
return self.session
class Semester(models.Model):
semester = models.CharField(max_length=10, choices=SEMESTER, blank=True)
is_current_semester = models.BooleanField(default=False, blank=True, null=True)
session = models.ForeignKey(
Session, on_delete=models.CASCADE, blank=True, null=True
)
next_semester_begins = models.DateField(null=True, blank=True)
def __str__(self):
return self.semester
class ActivityLog(models.Model):
message = models.TextField()
created_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"[{self.created_at}]{self.message}"