def timetableSet(request, id, school = 0): from forms import TimetableForm from django import forms render = {} if school: school = get_object_or_404(School, id = school) else: school = request.user.school render['school'] = school grade = get_object_or_404(Grade, id = id, school = school) form = TimetableForm() for day in settings.WORKDAYS: if not school.saturday and day[0] == 6: continue for lesson in settings.LESSON_NUMBERS: for i in xrange(1, 3): room = '' subject = None if UsalTimetable.objects.filter(grade = grade, number = lesson[0], group = i, school = school, workday = day[0]).count() != 0: u = UsalTimetable.objects.get(grade = grade, number = lesson[0], group = i, school = school, workday = day[0]) room = u.room subject = u.subject.id form.fields['l_r_%s_%s_%d' % (day[0], lesson[0], i)] = forms.CharField(initial = room, required = False) form.fields['l_s_%s_%s_%d' % (day[0], lesson[0], i)] = forms.ModelChoiceField(initial = subject, queryset = grade.get_subjects(), required = False) if request.method == 'POST': form.initial = request.POST for day in settings.WORKDAYS: if not school.saturday and day[0] == 6: continue for lesson in settings.LESSON_NUMBERS: for i in xrange(1, 3): subject = request.POST.get('l_s_%s_%s_%d' % (day[0], lesson[0], i), '') if subject == '': if UsalTimetable.objects.filter(grade = grade, number = lesson[0], group = i, school = school, workday = day[0]).count() != 0: UsalTimetable.objects.filter(grade = grade, number = lesson[0], group = i, school = school, workday = day[0]).delete() continue subject = get_object_or_404(Subject, id = subject, school = school) if UsalTimetable.objects.filter(grade = grade, number = lesson[0], group = i, school = school, workday = day[0]).count() == 0: tt = UsalTimetable(grade = grade, number = lesson[0], group = i, school = school, workday = day[0]) else: tt = UsalTimetable.objects.get(grade = grade, number = lesson[0], group = i, school = school, workday = day[0]) tt.subject = subject tt.room = request.POST.get('l_r_%s_%s_%d' % (day[0], lesson[0], i), '') tt.save() render['form'] = form return render_to_response('attendance/timetableSet.html', render, context_instance = RequestContext(request))
def timetable_set(request, id, school = 0): u''' Форма заполнения расписания. Внимание: много непонятного ''' render = {} if school: school = get_object_or_404(School, id = school) else: school = request.user.school render['school'] = school render['grade'] = grade = get_object_or_404(Grade, id = id, school = school) try: current_workday = int(request.GET.get('workday', '1')) except ValueError: current_workday = 1 if current_workday not in school.get_workdays(): current_workday = 1 render['workdays'] = school.get_workdays_tuple() render['current_workday'] = current_workday = school.get_workdays_dict()[current_workday] render['lessons'] = settings.LESSON_NUMBERS render['subjects'] = grade.get_subjects() render['attendance'] = TimetableDayGrade(workday = current_workday[0], grade = grade) if request.is_ajax(): subject = get_object_or_404(Subject, id = request.GET.get('subject'), school = school) if request.GET.get('method') == 'add': timetable = UsalTimetable( grade = grade, number = request.GET.get('lesson'), subject = subject, group = request.GET.get('group'), school = school, workday = current_workday[0], room = request.GET.get('room', ''), ) timetable.save() return HttpResponse(str(timetable.id)) elif request.GET.get('method') == 'set_room': timetable = get_object_or_404(UsalTimetable, id = request.GET.get('lesson_id'), grade = grade, number = request.GET.get('lesson'), subject = subject, group = request.GET.get('group'), school = school, workday = current_workday[0], ) timetable.room = request.GET.get('room', '') timetable.save() else: timetable = get_object_or_404(UsalTimetable, id = request.GET.get('lesson_id'), grade = grade, number = request.GET.get('lesson'), subject = subject, group = request.GET.get('group'), school = school, workday = current_workday[0], ) timetable.delete() return HttpResponse('ok') return render_to_response('timetableSet.html', render, context_instance = RequestContext(request))
def timetable_set(request, id, school=0): u''' Форма заполнения расписания. Внимание: много непонятного ''' render = {} if school: school = get_object_or_404(School, id=school) else: school = request.user.school render['school'] = school render['grade'] = grade = get_object_or_404(Grade, id=id, school=school) try: current_workday = int(request.GET.get('workday', '1')) except ValueError: current_workday = 1 if current_workday not in school.get_workdays(): current_workday = 1 render['workdays'] = school.get_workdays_tuple() render['current_workday'] = current_workday = school.get_workdays_dict( )[current_workday] render['lessons'] = settings.LESSON_NUMBERS render['subjects'] = grade.get_subjects() render['attendance'] = TimetableDayGrade(workday=current_workday[0], grade=grade) if request.is_ajax(): subject = get_object_or_404(Subject, id=request.GET.get('subject'), school=school) if request.GET.get('method') == 'add': timetable = UsalTimetable( grade=grade, number=request.GET.get('lesson'), subject=subject, group=request.GET.get('group'), school=school, workday=current_workday[0], room=request.GET.get('room', ''), ) timetable.save() return HttpResponse(str(timetable.id)) elif request.GET.get('method') == 'set_room': timetable = get_object_or_404( UsalTimetable, id=request.GET.get('lesson_id'), grade=grade, number=request.GET.get('lesson'), subject=subject, group=request.GET.get('group'), school=school, workday=current_workday[0], ) timetable.room = request.GET.get('room', '') timetable.save() else: timetable = get_object_or_404( UsalTimetable, id=request.GET.get('lesson_id'), grade=grade, number=request.GET.get('lesson'), subject=subject, group=request.GET.get('group'), school=school, workday=current_workday[0], ) timetable.delete() return HttpResponse('ok') return render_to_response('timetableSet.html', render, context_instance=RequestContext(request))
def import_timetable(request, school): ''' Представление для импорта расписания. Не тестировалась. ''' import csv render = {} if request.user.type == 'EduAdmin': if request.user.school.id != int(school): raise Http404 render['school'] = school = get_object_or_404(School, id=school) if request.method == 'GET': render['form'] = form = ImportForm() else: render['form'] = form = ImportForm(request.POST, request.FILES) if form.is_valid(): workday = 0 last_grade = None last_number = None render['errors'] = errors = [] objects = [] rows = csv.reader(form.cleaned_data['file'], delimiter=';') i = 0 for row in rows: i += 1 if len(row) < 5: errors.append({ 'line': i, 'column': 0, 'error': u'неверное количество столбцов' }) continue try: row = ";".join(row) row = row.decode('cp1251') except UnicodeError: errors.append({ 'line': i, 'column': 0, 'error': u'некорректное значение (невозможно определить кодировку)' }) continue row = row.split(';') if last_grade != row[0]: workday = 0 try: if int(row[2]) == 1 and last_number != 1: workday += 1 except ValueError: pass number = int(row[2]) last_number = number last_grade = grade = row[0] try: grade = Grade.objects.get(school=school, **get_grade(grade)) except Grade.DoesNotExist: errors.append({ 'line': i, 'column': 0, 'error': u'неизвестный класс %s' % grade }) continue try: subject = Subject.objects.get(school=school, name=row[3].strip()) except Subject.DoesNotExist: errors.append({ 'line': i, 'column': 4, 'error': u'неизвестный предмет %s' % row[3] }) continue try: int(row[1]) except ValueError: errors.append({ 'line': i, 'column': 2, 'error': u'неправильный формат номера группы %s' % row[1] }) continue objects.append( UsalTimetable(school=school, grade=grade, number=number, subject=subject, room=row[4], group=row[1], workday=workday)) # _subjects = row[2].split(',') # subjects = [] # for sbj in _subjects: # try: # subjects.append(Subject.objects.get(school = school, name = sbj.strip())) # except Subject.DoesNotExist: # errors.append({'line': i, 'column': 2, 'error': u'неизвестный предмет %s' % sbj}) # # rooms = row[3].split(',') # # ut_kwargs = {'school': school, 'grade': grade, 'workday': workday, 'number': number} # if len(subjects) == 1 and len(rooms)==1: # for j in xrange(1, 3): # objects.append(UsalTimetable(subject = subjects[0], room = rooms[0], group = j, **ut_kwargs)) # # elif len(subjects) == 1 and len(rooms)==2: # for j in xrange(1, 3): # objects.append(UsalTimetable(subject = subjects[0], room = rooms[j-1], group = j, **ut_kwargs)) # # elif (len(subjects) == 2 and len(rooms)==2) or (len(subjects) == 2 and len(rooms)==3): # for j in xrange(1, 3): # objects.append(UsalTimetable(subject = subjects[j-1], room = rooms[j-1], # group = j, **ut_kwargs)) # # elif len(subjects) == 1 and len(rooms)==3: # for j in xrange(1, 4): # objects.append(UsalTimetable(subject = subjects[0], room = rooms[j-1], group = j, **ut_kwargs)) # # else: # errors.append({'line': i, 'column': 0, 'error': u'неверный формат строки'}) if len(errors) == 0: for obj in objects: obj.save() messages.success(request, u'Расписание импортировано') return HttpResponseRedirect('..') return render_to_response('~attendance/timetableImport.html', render, context_instance=RequestContext(request))