def get(self, request, id): subject = get_object_or_404(Subject, id=id) subject_form = SubjectForm(instance=subject) subject_students = Student.objects.filter(subject__title=subject.title) no_subject_students = Student.objects.filter(subject=None) context = { 'subject_form': subject_form, 'subject': subject, 'subject_students': subject_students, 'no_subject_students': no_subject_students, } return render(request, 'subject_update.html', context=context)
def subject_add(request): if request.method == 'POST': form = SubjectForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('subject')) else: form = SubjectForm() context = { 'form': form } return render(request, 'appTemp/subject/subject_add.html', context=context)
def add(): form = SubjectForm() if form.validate_on_submit(): date = form.date.data month = date.month day = date.day year = date.year date_str = date.strftime('%Y-%m-%d') time = form.time.data time_str = '' if (form.time.data is not None): hour = time.hour minute = time.minute time_str = time.strftime('%H:%M') jform = { "subject": form.subject.data, "type": form.type.data, "date": date_str, "time": time_str, "other": form.other.data } global db Load() eventID = 'e' x = 0 while (True): x = random.randint(1, 100) eventID += str(x) if (eventID not in db): break tmp = dict(**{'events': {eventID: jform}}) tmps = db for i in tmp['events']: tmps['events'].update({i: tmp['events'][i]}) db = tmps Save() flash('Dodano {} - {}!'.format(form.subject.data, form.type.data)) return redirect('/') return make_response(render_template('add.html', title='Add', form=form), 201)
def post(self, request, id): subject = get_object_or_404(Subject, id=id) subject_form = SubjectForm(request.POST, instance=subject) if request.POST.get('delete_student'): student_id = request.POST['delete_student'] student = Student.objects.get(id=student_id) subject.students.remove(student) subject_form = SubjectForm(instance=subject) subject_students = Student.objects.filter( subject__title=subject.title) no_subject_students = Student.objects.filter(subject=None) context = { 'subject_form': subject_form, 'subject': subject, 'subject_students': subject_students, 'no_subject_students': no_subject_students, } return render(request, 'subject_update.html', context=context) if request.POST.get('add_student'): student_id = request.POST['add_student'] student = Student.objects.get(id=student_id) subject.students.add(student) subject_form = SubjectForm(instance=subject) subject_students = Student.objects.filter( subject__title=subject.title) no_subject_students = Student.objects.filter(subject=None) context = { 'subject_form': subject_form, 'subject': subject, 'subject_students': subject_students, 'no_subject_students': no_subject_students, } return render(request, 'subject_update.html', context=context) if subject_form.is_valid(): subject_form.save() return redirect(reverse('subjects'))
def subject_edit(request, subject_id): if request.method == 'POST': subject = Subject.objects.get(pk=subject_id) form = SubjectForm(request.POST, instance=subject) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('subject')) else: subject = Subject.objects.get(pk=subject_id) fields = model_to_dict(subject) form = SubjectForm(initial=fields, instance=subject) context = { 'form': form, 'type': 'edit', } return render(request, 'appTemp/subject/subject_add.html', context)
def lab_3(request): if request.method == "POST": form = SubjectForm(request.POST) if form.is_valid(): if form.cleaned_data['hour'] > 0: subject = form.save(commit=False) subject.name = form.cleaned_data['name'] subject.hour = form.cleaned_data['hour'] subject.semester = random.randint(1, 12) subject.save() else: form = SubjectForm() return render(request, 'main/lab_3.html', {'form': form})
def subject(subject_id): """Страничка для отображения конкретного предмета обучения""" if subject_id == 'new': new_subject = Subject() else: try: subject_id = int(subject_id) except ValueError: return 'Bad Request', 400 new_subject = DB.session.query(Subject).filter( Subject.id == subject_id).one_or_none() if new_subject is None: return 'Not Found', 404 form = SubjectForm(request.form, obj=new_subject) if form.button_delete.data: form.validate() if DB.session.query(CurriculumUnit) \ .filter(CurriculumUnit.subject_id == new_subject.id) \ .count() > 0: form.button_delete.errors.append( 'Невозможно удалить предмет, к которому привязаны единицы учебного плана' ) if len(form.button_delete.errors) == 0: DB.session.delete(new_subject) DB.session.flush() return redirect(url_for('subjects')) if form.button_save.data and form.validate(): form.populate_obj(new_subject) DB.session.add(new_subject) if subject_id == 'new': DB.session.flush() return redirect(url_for('subject', id=new_subject.id)) return render_template('public/subject.html', subject=new_subject, form=form)