text: from typing import Counter
  from django.http import request
  from django.contrib.auth.decorators import login_required
  from django.shortcuts import render, redirect, reverse
  from django.contrib import messages
  from django.contrib.auth.context_processors import *
  from .models import (
      Departement,
      Semester,
      Module,
      Course
  )
  from room.models import (
      Sujet,
      Room,
      Message
  )
  from .forms import (
      CoursesForm,
      ModuleForm,
  )
  from django.views.decorators.csrf import csrf_exempt
  
  # Create your views here.
  @csrf_exempt
  def semesterList(request,departement,slug):
      user = request.user
      has_participents = "N"
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = slug, departement = departementList.id)
      moduleList = Module.objects.filter(semester = semesterList.pk)
      # room = Room.objects.get(slug = "mecanique-analytique-et-vibrations-room")
      # roomparticipents = room.participants.all()
      # roomList = Room.objects.filter()
      # 
      # for user in roomparticipents:
      #     print(user.username)
      #     if user.username == request.user:
      #         pass
      room_messages = Message.objects.all()
      participents = semesterList.semester_participants.all()
      # for user in  participents:
          # print(user.username)
  
      participents_nbr = len(participents)
      print(f"nbr of participents :{participents_nbr}")
      for user in participents:
          if request.user == user:
              has_participents = "O"
              
        
      if request.method == "POST":
          s_qs = semesterList
          if request.user.is_authenticated: 
              s_qs.semester_participants.add(request.user)
              s_qsre = semesterList
              s_qsre.numberOfParticipent = s_qsre.numberOfParticipent + 1         
              s_qsre.save()
              # print(f"{request.user.name} inscrit ")
              messages.success(request, f"You  Are Inscrit in {s_qs.name}")
              return redirect("formation:semester-List",departement,slug)
          else:
              messages.error(request, "You  Are Not Login")
      context = {
          'room_messages': room_messages,
          'participents_nbr':participents_nbr,
          'has_participents':has_participents,
          'participents':participents,
          'departement':departementList,
          'semesters':semesterList,
          'modules':moduleList,
          'semesterListSuj': Semester.objects.all(),
          'departementsSuj':Departement.objects.all(),
      }
      return render(request,'formation/pages/semesterList.html',context)
  # @login_required(redirect_field_name='next', login_url='/login')
  @csrf_exempt
  def moduleList(request,departement,semester_slug,slug):
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = semester_slug,departement = departementList.id)
      moduleList = Module.objects.get(slug = slug, semester = semesterList.id)
      coursesList = Course.objects.filter(module = moduleList.id)
      context = {
          'departement':departementList,
          'semester':semesterList,
          'modules':moduleList,
          'courses':coursesList,
          'moduleListSuj': Module.objects.all().order_by('-count')[:6] ,
          'semesterListSuj':Semester.objects.all(),
      }
      return render(request,'formation/pages/moduleList.html',context)
  
  #************************************ MODULES CRUD
  @csrf_exempt
  def createModule(request,departement,slug):
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = slug, departement = departementList.id)
      moduleList = Module.objects.filter(semester = semesterList.id)
  
      form = ModuleForm()
      if request.method == "POST":
          form = ModuleForm(request.POST )
          if form.is_valid():
              name = form.cleaned_data['name']
              form.instance.semester = semesterList
              m_qs = Module.objects.filter(semester = semesterList.id,name = name)
              if len(m_qs) == 0:
                  form.save()
                  counter = len(Module.objects.filter(semester = semesterList.id))
                  s_qs = semesterList
                  s_qs.count = counter
                  s_qs.save()
                  messages.success(request, f"You have successfully created {name}")
                  return redirect("formation:semester-List",departement,slug)
              else:
                  messages.warning(request, f"You have created {name} befor !")
                  return redirect("formation:semester-List",departement,slug)
          else:
              messages.warning(request, "Form Not Validate")
                  
      context = {
          'title':"Create Module",
          'form':form,
          'departement':departementList,
          'semester':semesterList,
          'modules':moduleList,
      }
      return render(request,'base/create.html',context)
  @csrf_exempt
  def updateModule(request,departement,slug,pk):
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = slug, departement=departementList.id )
      moduleList = Module.objects.get(pk= pk)
  
      form = ModuleForm(instance=moduleList)
      # name = form.cleaned_data['name']
      if request.method == "POST":
          form = ModuleForm(request.POST ,instance=moduleList)
  
          if form.is_valid():
              form.save()
              messages.success(request, f"You have successfully updated {moduleList.name}")
              return redirect("formation:semester-List",departement,slug)
  
      context = {
          'update':'update',
          'form':form,
          'departement':departementList,
          'semester':semesterList,
          'modules':moduleList,
      }
      return render(request,'base/create.html',context)
  
  @csrf_exempt
  def deleteModule(request,departement,slug, pk):
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = slug, departement=departementList.id )
      moduleList = Module.objects.get(pk= pk)
      coursesList = Course.objects.filter(module = moduleList.id)
  
      if request.method == "POST":
          moduleList.delete()
          coursesList.delete()
          counter = len(Module.objects.filter(semester = semesterList.id))
          s_qs = semesterList
          s_qs.count = counter
          s_qs.save()
          messages.success(request, "delete successfully")
          return redirect("formation:semester-List", departement ,slug )
  
      context = {
          'title':"Module Delete",
          'departement':departementList,
          'semester':semesterList,
          'modules':moduleList,
          'dcourses':coursesList,
          'objs': moduleList,
      }
      return render(request,'base/delete.html',context)
  #************************************ COURSES CRUD
  @csrf_exempt
  def createCourses(request,departement,semester_slug,slug):
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = semester_slug,departement = departementList.id)
      moduleList = Module.objects.get(slug = slug, semester = semesterList.id)
      coursesList = Course.objects.filter(module = moduleList.id)
  
      form = CoursesForm()
      if request.method == "POST":
          form = CoursesForm(request.POST , request.FILES )
          if form.is_valid():
              name = form.cleaned_data['name']
              form.instance.module = moduleList
              form.save()
              Counter = len(coursesList)
              qs = moduleList
              qs.count = Counter 
              qs.save()
              messages.success(request, f"You have successfully created {name}")
              return redirect("formation:module-List",departement,semester_slug,slug)
  
      context = {
          'title':'Create course ',
          'form':form,
          'departement':departementList,
          'semester':semesterList,
          'modules':moduleList,
          'courses':coursesList,
      }
      return render(request,'base/create.html',context)
  @csrf_exempt
  def updateCourses(request,departement,semester_slug,slug,pk):
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = semester_slug,departement = departementList.id)
      moduleList = Module.objects.get(slug = slug, semester = semesterList.id)
      coursesList = Course.objects.get(module = moduleList.id,pk=pk)
  
      form = CoursesForm(instance=coursesList)
      # name = form.cleaned_data['name']
      if request.method == "POST":
          form = CoursesForm(request.POST , request.FILES, instance=coursesList)
  
          if form.is_valid():
              form.save()
              messages.success(request, f"You have successfully updated {coursesList.name}")
              return redirect("formation:module-List",departement,semester_slug,slug)
  
      context = {
          'update':'update',
          'form':form,
          'departement':departementList,
          'semester':semesterList,
          'modules':moduleList,
          'courses':coursesList,
      }
      return render(request,'base/create.html',context)
  
  @csrf_exempt
  def deleteCourses(request,departement,semester_slug,slug, pk):
      departementList = Departement.objects.get(name = departement)
      semesterList = Semester.objects.get(slug = semester_slug,departement = departementList.id)
      moduleList = Module.objects.get(slug = slug, semester = semesterList.id)
      coursesList = Course.objects.get(pk=pk)
  
      if request.method == "POST":
          coursesList.delete()
          messages.success(request, "delete successfully")
          return redirect("formation:module-List", departement ,semester_slug,slug )
  
      context = {
          'title':"Courses Delete",
          'departement':departementList,
          'semester':semesterList,
          'modules':moduleList,
          'courses':coursesList,
          'objs':coursesList,
      }
      return render(request,'base/delete.html',context)
  
  @csrf_exempt
  def departement_page(request):
      context = {
          'semesterListSuj': Semester.objects.all(),
          'departementsSuj':Departement.objects.all(),
      }
      return render(request,"formation/pages/departement.html",context)