def edit(request: HttpRequest, lesson_id) -> HttpResponse: """Render the edit page""" if request.META.get('HTTP_REFERER') is None: return redirect('/calendar/') if request.method == 'POST': form = EditForm(request.POST) if form.is_valid(): if is_ajax(request): return render(request, 'popup.html', context={"form": form}) past_conflicts = list(Conflict.objects.all()) lesson = Lesson.objects.get(id=form.cleaned_data['id']) lesson.name = form.cleaned_data['name'] professor = form.cleaned_data['professor'].strip().split() lesson.professor = get_professor(professor[0], professor[1]) lesson.room = get_room(form.cleaned_data['room']) lesson.group = get_group(form.cleaned_data['group']) lesson.start_time = form.cleaned_data['start_time'] lesson.end_time = form.cleaned_data['end_time'] lesson.save() db_conflicts() context = generate_full_index_context_with_date(form.cleaned_data['start_time']) current_conflicts = list(context['conflicts']) context.update(generate_context_for_conflicts_report(past_conflicts, current_conflicts)) return render(request, 'index.html', context=context) return render(request, 'popup.html', context={"form": form}) lesson = Lesson.objects.get(id=lesson_id) form = EditForm( initial={'id': lesson.id, 'name': lesson.name, 'professor': lesson.professor, 'room': lesson.room, 'group': lesson.group, 'start_time': lesson.start_time, 'end_time': lesson.end_time}) return render(request, 'popup.html', context={"form": form})
def create(request: HttpRequest) -> HttpResponse: """Render the create page""" if request.META.get('HTTP_REFERER') is None: return redirect('/calendar/') if request.method == 'POST': form = EditForm(request.POST) if form.is_valid(): if is_ajax(request): return render(request, 'popup.html', context={"form": form}) past_conflicts = list(Conflict.objects.all()) professor = form.cleaned_data['professor'].strip().split() professor = get_professor(professor[0], professor[1]) room = get_room(form.cleaned_data['room']) group = get_group(form.cleaned_data['group']) Lesson.objects.get_or_create( name=form.cleaned_data['name'], professor=professor, room=room, group=group, start_time=form.cleaned_data['start_time'], end_time=form.cleaned_data['end_time'] ) db_conflicts() context = generate_full_index_context_with_date(form.cleaned_data['start_time']) current_conflicts = list(context['conflicts']) context.update(generate_context_for_conflicts_report(past_conflicts, current_conflicts)) return render(request, 'index.html', context=context) return render(request, 'popup.html', context={"form": form}) return render(request, 'popup.html', context={"form": EditForm()})
def test_lesson_starts_and_ends_during_another(self): """Test when one lesson starts and ends during another""" db_conflicts() self.assertEqual( len( Conflict.objects.filter(first_lesson=self.middle_lesson, second_lesson=self.last_lesson, conflict_type='PROFESSOR', object_id=self.professor.id)), 1)
def test_lesson_overlaps_another(self): """Test when one lesson starts before another and then overlaps at it""" db_conflicts() self.assertEqual( len( Conflict.objects.filter(first_lesson=self.first_lesson, second_lesson=self.middle_lesson, conflict_type='PROFESSOR', object_id=self.professor.id)), 1)
def test_overlapping_group(self): """Test when conflict is for group""" db_conflicts() self.assertEqual( len( Conflict.objects.filter(first_lesson=self.first_lesson, second_lesson=self.second_lesson, conflict_type='GROUP', object_id=self.group.id)), 1)
def test_not_overlapping_lessons(self): """Test when times of lessons do not overlap""" db_conflicts() self.assertQuerysetEqual( Conflict.objects.filter(first_lesson=self.first_lesson, second_lesson=self.last_lesson, conflict_type='PROFESSOR', object_id=self.professor.id), Conflict.objects.none())
def test_overlapping_room(self): """Test when conflict is for room""" db_conflicts() self.assertEqual( len( Conflict.objects.filter(first_lesson=self.first_lesson, second_lesson=self.second_lesson, conflict_type='ROOM', object_id=self.room.id)), 1)
def test_overlapping_professor(self): """Test when conflict is for professor""" db_conflicts() self.assertEqual( len( Conflict.objects.filter(first_lesson=self.first_lesson, second_lesson=self.second_lesson, conflict_type='PROFESSOR', object_id=self.professor.id)), 1)
def delete_lessons(request: HttpRequest) -> HttpResponse: """Logic for mass delete of conflicts""" if request.method == 'POST': past_conflicts = list(Conflict.objects.all()) checks = request.POST.getlist('checks[]') Lesson.objects.filter(id__in=checks).delete() db_conflicts() context = generate_full_index_context() current_conflicts = list(context['conflicts']) context.update(generate_context_for_conflicts_report(past_conflicts, current_conflicts)) return render(request, 'index.html', context=context) context = generate_full_index_context() return render(request, 'index.html', context=context)
def test_duplication_of_conflicts(self): """Test if the conflicts are not duplicated when found""" db_conflicts() self.assertEqual( len( Conflict.objects.filter(first_lesson=self.first_lesson, second_lesson=self.second_lesson, conflict_type='PROFESSOR', object_id=self.professor.id)) + len( Conflict.objects.filter(first_lesson=self.second_lesson, second_lesson=self.first_lesson, conflict_type='PROFESSOR', object_id=self.professor.id)), 1)
def remove(request: HttpRequest, lesson_id) -> HttpResponse: """Remove event and redirect to index page""" try: if request.method == 'POST': past_conflicts = list(Conflict.objects.all()) lesson = Lesson.objects.get(id=lesson_id) lesson.delete() db_conflicts() context = generate_full_index_context() current_conflicts = list(context['conflicts']) context.update(generate_context_for_conflicts_report(past_conflicts, current_conflicts)) return render(request, 'index.html', context=context) return redirect('/calendar/') except Lesson.DoesNotExist: return redirect('/calendar/')
def upload_schedule(request: HttpRequest) -> HttpResponse: """Render schedule upload page""" filename = None context: dict = {} try: if request.method == 'POST' and request.FILES['uploaded_file']: file = request.FILES['uploaded_file'] if isinstance(file.name, str): filename = default_storage.save(file.name, file) ext = os.path.splitext(file.name)[1] if ext == '.csv': data = pd.read_csv(file.name) elif ext == '.xlsx': data = pd.read_excel(file.name) else: return render(request, "upload_schedule.html", {'error': "Error: Extension not supported"}) added_lessons, incorrect, duplicate = imp.parse_data(data, ext) data_html = data.style \ .set_table_attributes('class="table table-striped table-hover table-bordered"')\ .apply(lambda x: [('background: lightcoral' if x.name in incorrect else ('background: lightblue' if x.name in duplicate else '')) for _ in x], axis=1) \ .render() db_conflicts() context = {'loaded_data': data_html, 'added': added_lessons} except MultiValueDictKeyError: context = {'error': "Error: You didn't select a file"} except XLRDError: context = {'error': "Error: Corrupted file"} except UnicodeDecodeError: context = {'error': "Error: File contains weird symbols"} except imp.ImportSizeException: context = {'error': "Error: Incorrect number of columns"} finally: if filename: default_storage.delete(filename) return render(request, "upload_schedule.html", context)
def test_database_with_no_conflicts(self): """Test on a database with two lessons that are not overlapping""" room = Room.objects.create(number="1.11a") professor = Professor.objects.create(name="John", surname="Doe") group = Group.objects.create(name="1") Lesson.objects.create(name="Lesson1", professor=professor, room=room, group=group, start_time=datetime.datetime( 2019, 5, 11, 12, 00), end_time=datetime.datetime(2019, 5, 11, 13, 30)) Lesson.objects.create(name="Lesson2", professor=professor, room=room, group=group, start_time=datetime.datetime( 2019, 5, 11, 14, 00), end_time=datetime.datetime(2019, 5, 11, 16, 30)) db_conflicts() self.assertQuerysetEqual(Conflict.objects.all(), Conflict.objects.none())
def edit_lessons(request: HttpRequest) -> HttpResponse: """Logic for mass edit of conflicts""" if request.method == 'POST': form = MassEditForm(request.POST) if form.is_valid(): changes = {} past_conflicts = list(Conflict.objects.all()) if form.cleaned_data['lesson_name']: changes['name'] = form.cleaned_data['lesson_name'] if form.cleaned_data['professor']: professor = form.cleaned_data['professor'].strip().split() changes['professor'] = get_professor(professor[0], professor[1]) if form.cleaned_data['room']: changes['room'] = get_room(form.cleaned_data['room']) if form.cleaned_data['group']: changes['group'] = get_group(form.cleaned_data['group']) if form.cleaned_data['start_time']: changes['start_time'] = form.cleaned_data['start_time'] if form.cleaned_data['end_time']: changes['end_time'] = form.cleaned_data['end_time'] checks = request.POST.getlist('checks[]') if changes != {}: lessons = Lesson.objects.filter(id__in=checks) lessons.update(**changes) db_conflicts() context_after_edit = generate_full_index_context() current_conflicts = list(context_after_edit['conflicts']) context_after_edit.update( generate_context_for_conflicts_report(past_conflicts, current_conflicts)) return render(request, 'index.html', context=context_after_edit) context: dict = {} context.update(generate_conflicts_context()) context.update(generate_full_schedule_context()) context.update({'form': form}) return render(request, 'index.html', context=context) return index(request)
def test_clean_database(self): """Test on a clean database""" db_conflicts() self.assertQuerysetEqual(Conflict.objects.all(), Conflict.objects.none())