def get_changeform_initial_data(self, request): """Custom form default values""" if 'add-extra-multi' in request.path: return { 'group': Group.objects.get(semester=get_ongoing_semester(), name=settings.SELF_TRAINING_GROUP_NAME) } elif 'add-extra' in request.path: return { 'group': Group.objects.get(semester=get_ongoing_semester(), name=settings.EXTRA_EVENTS_GROUP_NAME) } return {}
def self_sport_upload(request, **kwargs): serializer = SelfSportReportUploadSerializer(data=request.data) serializer.is_valid(raise_exception=True) student = request.user # user.pk == user.student.pk if request.user.student.medical_group_id \ < settings.SELFSPORT_MINIMUM_MEDICAL_GROUP_ID: return Response( status=400, data=error_detail(*SelfSportErrors.MEDICAL_DISALLOWANCE), ) image = None link = serializer.validated_data.get('link', None) if 'image' in serializer.validated_data: image, error = process_image(serializer.validated_data['image']) if error is not None: return error serializer.save( image=image, link=link, semester=get_ongoing_semester(), student_id=student.pk ) return Response({})
def process_med_group_form(request, *args, **kwargs): form = MedicalGroupReferenceForm(request.POST, request.FILES) if form.is_valid(): obj, created = MedicalGroupReference.objects.get_or_create( student_id=request.user.pk, semester=get_ongoing_semester(), defaults={ "image": form.cleaned_data["reference"], }, ) if created: set_session_notification( request, "Medical group reference successfully submitted", "success", ) return redirect('profile') else: set_session_notification( request, "You have already submitted medical group reference", "error", ) else: set_session_notification(request, "Form is invalid", "error") return redirect('profile')
def test_get_ongoing_semester(semester_factory): semester_factory(name="S19", start=date(2020, 1, 1), end=date(2020, 1, 3)) s2 = semester_factory(name="S20", start=date(2020, 1, 4), end=date(2020, 1, 24)) assert get_ongoing_semester() == s2
def test_primary_group_export( student_factory, sport_factory, semester_factory, group_factory, training_factory, attendance_factory, ): student = student_factory("A").student sport = sport_factory(name="Sport") s1 = semester_factory(name="S19", start=dummy_date, end=dummy_date) g1 = group_factory(name="G11", sport=sport, semester=s1, capacity=20) g2 = group_factory(name="G12", sport=sport, semester=s1, capacity=20) trainings = [] for i, g in enumerate([g1, g2, ]): t = training_factory(group=g, start=timezone.now(), end=timezone.now()) trainings.append(t) attendance_factory(training=t, student=student, hours=i + 1) # g1 - 1 hour, # g2 - 2 hours primary_groups = get_primary_groups(get_ongoing_semester().pk) assertMembers( primary_groups, { student.pk: g2.pk, } )
def reference_upload(request, **kwargs): serializer = ReferenceUploadSerializer(data=request.data) serializer.is_valid(raise_exception=True) image = image_optimizer( serializer.validated_data['image'], resize_method="thumbnail", ) if image.size > settings.MAX_IMAGE_SIZE: return Response( status=status.HTTP_400_BAD_REQUEST, data=error_detail(*ReferenceErrors.IMAGE_FILE_SIZE_TOO_BIG)) width, height = image.image.size if not (settings.MIN_IMAGE_DIMENSION <= width <= settings.MAX_IMAGE_DIMENSION and settings.MIN_IMAGE_DIMENSION <= height <= settings.MAX_IMAGE_DIMENSION): return Response(status=status.HTTP_400_BAD_REQUEST, data=error_detail(*ReferenceErrors.INVALID_IMAGE_SIZE)) student = request.user # user.pk == user.student.pk try: with transaction.atomic(): ref = serializer.save(semester=get_ongoing_semester(), student_id=student.pk) count = Reference.objects.filter( student_id=student.pk, uploaded__date=ref.uploaded.date()).count() assert count == 1 except AssertionError: return Response( status=status.HTTP_400_BAD_REQUEST, data=error_detail(*ReferenceErrors.TOO_MUCH_UPLOADS_PER_DAY)) return Response({})
def get_changeform_initial_data(self, request): """Custom form default values""" if 'extra' in request.GET: return { "group": Group.objects.get(semester=get_ongoing_semester(), name=settings.EXTRA_EVENTS_GROUP_NAME), "start": timezone.now().replace(minute=0, second=0), "end": timezone.now().replace(minute=30, second=0), } return {}
def reference_upload(request, **kwargs): serializer = ReferenceUploadSerializer(data=request.data) serializer.is_valid(raise_exception=True) image, error = process_image(serializer.validated_data['image']) if error is not None: return error student = request.user # user.pk == user.student.pk try: with transaction.atomic(): ref = serializer.save(semester=get_ongoing_semester(), student_id=student.pk) count = Reference.objects.filter( student_id=student.pk, uploaded__date=ref.uploaded.date()).count() assert count == 1 except AssertionError: return Response( status=status.HTTP_400_BAD_REQUEST, data=error_detail(*ReferenceErrors.TOO_MUCH_UPLOADS_PER_DAY)) return Response({})
def profile_view(request, **kwargs): user = request.user student = Student.objects.filter(pk=user.pk).select_related( "medical_group").first() # type: Optional[Student] trainer = getattr(user, "trainer", None) # type: Optional[Trainer] current_semester = get_ongoing_semester() utc_date = timezone.localdate(timezone=timezone.utc) context = { "user": request.user, "common": { "semester_name": current_semester.name }, "forms": { "medical_group_reference": MedicalGroupReferenceForm() }, } if "notify" in request.session: msg_type, msg = request.session["notify"] context.update({"notify": { "msg": msg, "type": msg_type, }}) del request.session["notify"] if student is not None: student_groups = get_student_groups(student) student_groups_parsed = list(map(parse_group, student_groups)) student_brief_hours_info = get_brief_hours(student) student_data = student.__dict__ has_med_group_submission = MedicalGroupReference.objects.filter( student=student, semester=current_semester, ).exists() context.update({ "student": { "student_id": student.pk, "sport_groups": student_groups_parsed, "group_choices_left": max( 0, settings.STUDENT_MAXIMUM_GROUP_COUNT - len(student_groups_parsed)), "semesters": student_brief_hours_info, "obj": student, "has_med_group_submission": has_med_group_submission, **student_data, }, }) if trainer is not None: training_groups = list(map(parse_group, get_trainer_groups(trainer))) trainer_data = trainer.__dict__ context.update({ "trainer": { "sport_groups": training_groups, "obj": trainer, **trainer_data, }, }) return render(request, "profile.html", context)
def get_queryset(self, request): qs = super().get_queryset(request) if 'extra' in request.META.get('HTTP_REFERER', []): return qs.filter(semester=get_ongoing_semester(), sport__name=settings.OTHER_SPORT_NAME).order_by('name') return qs.annotate(enroll_count=RawSQL('select count(*) from enroll where group_id = "group".id', ()))