Пример #1
0
class TrainingRequestCreateView(CreateView):
  form_class = TrainingRequestForm
  template_name = None
  user = None
  tpid = None

  @method_decorator(group_required("Organiser"))
  def dispatch(self, *args, **kwargs):
    if 'tpid' in self.kwargs:
      self.tpid = self.kwargs['tpid']
      training_planner = TrainingPlanner.objects.filter(pk=self.tpid, organiser_id = self.request.user.organiser.id)
      if not training_planner.exists():
        # message say Not found
        messages.warning(self.request, 'Invalid Training Planner ID passed')
        return HttpResponseRedirect("/events2/training-planner")
      else:
        training_planner = training_planner.first()
        if not training_planner.is_current_planner() and not training_planner.is_next_planner():
          messages.warning(self.request, 'Selected Training Planner is already expired')
          return HttpResponseRedirect("/events2/training-planner")
    return super(TrainingRequestCreateView, self).dispatch(*args, **kwargs)
  
  def get_context_data(self, **kwargs):
    context = super(TrainingRequestCreateView, self).get_context_data(**kwargs)
    context['training_planner_id'] = self.tpid
    return context

  def get_form_kwargs(self):
    kwargs = super(TrainingRequestCreateView, self).get_form_kwargs()
    kwargs.update({'user' : self.request.user})
    return kwargs

  def post(self, request, *args, **kwargs):
    self.object = None
    self.user = request.user
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    if form.is_valid():
      return self.form_valid(form)
    else:
      return self.form_invalid(form)

  def form_valid(self, form, **kwargs):
    # Check if all student participate in selected foss
    try:
      form_data = form.save(commit=False)
      sb = StudentBatch.objects.get(pk=form_data.batch.id)
      if sb.is_foss_batch_acceptable(form_data.course.id):
        form_data.training_planner_id = self.kwargs['tpid']
        form_data.participants = StudentMaster.objects.filter(batch_id = form_data.batch_id).count()
        form_data.save()
      else:
        messages.error(self.request, 'This student batch already taken the selected course.')
        return self.form_invalid(form)
    except:
      messages.error(self.request, 'Something went wrong, Contact site administrator.')
      return self.form_invalid(form)
    context = {}
    return HttpResponseRedirect('/events2/training-planner/')
Пример #2
0
class TrainingPlannerListView(ListView):
  queryset = None
  paginate_by = 20
  user = None
  template_name = None
  @method_decorator(group_required("Organiser"))
  def dispatch(self, *args, **kwargs):
    self.user = self.request.user
    self.get_current_planner()
    self.queryset = TrainingPlanner.objects.filter(
        organiser_id = self.request.user.organiser.id,
        academic_id = self.request.user.organiser.academic.id,
      ).order_by('-year')
    return super(TrainingPlannerListView, self).dispatch(*args, **kwargs)
  
  def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context
    context = super(TrainingPlannerListView, self).get_context_data(**kwargs)
    # Add in a QuerySet of all the books
    context['current_planner'] = self.get_current_planner()
    context['next_planner'] = self.get_next_planner(context['current_planner'])
    return context

  #def get(self, request):
  #  return render(request, self.template_name)

  def post(self, request):
    return HttpResponse('Post')

  def is_even_sem(self, month):
    # 0 => odd sem, 1 => even sem
    if month > 6 and month < 13:
      return 0
    return 1

  def get_year(self, sem, year):
    if sem:
      return year - 1
    return year

  def get_semester(self, sem):
    return Semester.objects.get(even=sem)

  def get_current_planner(self):
    now = datetime.now()
    sem = self.is_even_sem(now.month)
    year = self.get_year(sem, now.year)
    try:
      return TrainingPlanner.objects.get(year=year, semester__even=sem, \
        academic=self.user.organiser.academic, organiser=self.user.organiser)
    except ObjectDoesNotExist:
      return TrainingPlanner.objects.create(year=year, \
        semester=self.get_semester(sem), academic=self.user.organiser.academic,\
          organiser=self.user.organiser)
    except Exception, e:
      print e
    return False
Пример #3
0
class StudentListView(ListView):
  queryset = Student.objects.none()
  paginate_by = 20
  template_name = None
  batch = None
  @method_decorator(group_required("Organiser"))
  def dispatch(self, *args, **kwargs):
    self.batch = StudentBatch.objects.filter(pk=kwargs['bid'])
    if not self.batch.exists():
      return HttpResponseRedirect('/student-batch')
    self.batch = self.batch.first()
    return super(StudentListView, self).dispatch(*args, **kwargs)
    
  def get_context_data(self, **kwargs):
    context = super(StudentListView, self).get_context_data(**kwargs)
    collectionSet = self.queryset = Student.objects.filter(
      id__in = StudentMaster.objects.filter(
        batch_id=self.batch.id,
        moved=False
      ).values_list(
        'student'
      )
    )
    header = {
        1: SortableHeader('#', False),
        2: SortableHeader('', False, 'Department'),
        3: SortableHeader('', False, 'Year'),
        4: SortableHeader('user__first_name', True, 'First Name'),
        5: SortableHeader('user__last_name', True, 'Last Name'),
        6: SortableHeader('user__email', True, 'Email'),
        7: SortableHeader('gender', True, 'Gender'),
        8: SortableHeader('', False, 'Status'),
        9: SortableHeader('', False, ''),
    }
    
    raw_get_data = self.request.GET.get('o', None)
    collection = get_sorted_list(self.request, collectionSet, header, raw_get_data)
    ordering = get_field_index(raw_get_data)
    #collection = TrainingFilter(self.request.GET, user = user, queryset=collection)
    
    page = self.request.GET.get('page')
    collection = get_page(collection, page)
    context['object_list'] = collection
    context['header'] = header
    context['ordering'] = ordering
    context['batch'] = self.batch
    return context
Пример #4
0
class TrainingRequestEditView(CreateView):
  form_class = TrainingRequestEditForm
  template_name = None
  user = None
  training = None
  @method_decorator(group_required("Organiser"))
  def dispatch(self, *args, **kwargs):
    if 'trid' in self.kwargs:
      self.training = TrainingRequest.objects.get(pk=self.kwargs['trid'])
    if not self.training.can_edit():
      messages.error(self.request, "Training has attendance, edit is not permitted for training.")
      return HttpResponseRedirect('/events2/training-planner/')
    return super(TrainingRequestEditView, self).dispatch(*args, **kwargs)

  def get_form_kwargs(self):
    kwargs = super(TrainingRequestEditView, self).get_form_kwargs()
    kwargs.update({'training' : self.training})
    kwargs.update({'user' : self.request.user})
    return kwargs

  def get_context_data(self, **kwargs):
    context = super(TrainingRequestEditView, self).get_context_data(**kwargs)
    context['training'] = self.training
    return context

  def form_valid(self, form, **kwargs):
    # Check if all student participate in selected foss
    try:
      if self.training.batch.is_foss_batch_acceptable(form.cleaned_data['course']):
        self.training.sem_start_date = form.cleaned_data['sem_start_date']
        self.training.course_id = form.cleaned_data['course']
        self.training.save()
      else:
        messages.error(self.request, 'This student batch already taken the selected course.')
        return self.form_invalid(form)
    except Exception, e:
      print e
      messages.error(self.request, 'Something went wrong, Contact site administrator.')
      return self.form_invalid(form)
    context = {}
    return HttpResponseRedirect('/events2/training-planner/')
Пример #5
0
class StudentBatchCreateView(CreateView):
  form_class = None
  template_name = None
  user = None
  batch = None

  @method_decorator(group_required("Organiser"))
  def dispatch(self, *args, **kwargs):
    if 'bid' in kwargs:
      sb = StudentBatch.objects.filter(pk=kwargs['bid'])
      if sb.exists():
        self.batch = sb.first()
    return super(StudentBatchCreateView, self).dispatch(*args, **kwargs)

  #def get_form_kwargs(self):
  #  kwargs = super(StudentBatchCreateView, self).get_form_kwargs()
  #  kwargs.update({'user' : self.request.user})
  #  return kwargs
  
  def get_context_data(self, **kwargs):
    context = super(StudentBatchCreateView, self).get_context_data(**kwargs)
    if self.batch:
      existing_student = Student.objects.filter(
        id__in=StudentMaster.objects.filter(
          batch_id=self.batch.id,
          moved=False
        ).values_list(
          'student_id'
        )
      )
      context['batch'] = self.batch
      context['existing_student'] = existing_student
    return context
    
  def post(self, request, *args, **kwargs):
    self.object = None
    self.user = request.user
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    if form.is_valid():
      return self.form_valid(form)
    else:
      return self.form_invalid(form)

  def form_valid(self, form, **kwargs):
    form_data = form.save(commit=False)
    form_data.academic = self.user.organiser.academic
    form_data.organiser = self.user.organiser
    try:
      if 'bid' in self.kwargs:
        form_data = StudentBatch.objects.get(pk=self.kwargs['bid'])
      else:
        form_data = StudentBatch.objects.get(year=form_data.year, academic=form_data.academic, department=form_data.department)
    except ObjectDoesNotExist:
      form_data.save()
    except Exception, e:
      print e
      return HttpResponseRedirect("/events2/student-batch/")
    skipped, error, warning, write_flag = \
      self.csv_email_validate(self.request.FILES['csv_file'], form_data.id)
    context = {'error' : error, 'warning' : warning, 'batch':form_data}
    
    if error or warning:
      return render_to_response(self.template_name, context, context_instance=RequestContext(self.request))
    messages.success(self.request, "Student Batch added successfully.")
    return HttpResponseRedirect('/events2/student-batch/')