def upload_solution(self, request, pk=None): problem = self.get_object() if not request.user.is_authenticated: raise exceptions.PermissionDenied('Je potrebné sa prihlásiť') event_registration = EventRegistration.get_registration_by_profile_and_event( request.user.profile, problem.series.semester) if event_registration is None: raise exceptions.MethodNotAllowed(method='upload-solutuion') if 'file' not in request.data: raise exceptions.ParseError(detail='Request neobsahoval súbor') file = request.data['file'] if mime_type(file) != 'application/pdf': raise exceptions.ParseError( detail='Riešenie nie je vo formáte pdf') late_tag = problem.series.get_actual_late_flag() solution = Solution.objects.create( problem=problem, semester_registration=event_registration, late_tag=late_tag, is_online=True) solution.solution.save(solution.get_solution_file_name(), file, save=True) return Response(status=status.HTTP_201_CREATED)
def post(self, request, **kwargs): # pylint: disable=unused-argument self.object = self.get_object() form = SeriesSolutionForm(self.object, data=request.POST, files=request.FILES) try: registration = EventRegistration.get_registration_by_profile_and_event( self.request.user.profile, self.object.semester) assert form.is_valid() except AttributeError: messages.error(request, 'Na odovzdávanie riešení je potrebné sa prihlásiť') except (EventRegistration.DoesNotExist, AssertionError): messages.error(request, 'Odovzdávanie riešení zlyhalo') else: for field in form.fields: if field not in request.FILES: continue problem = Problem.objects.get(pk=int(field)) solution, _ = Solution.objects.get_or_create( semester_registration=registration, problem=problem) solution.solution = request.FILES[field] solution.is_online = True solution.save() messages.success( request, f'{problem.pk}: Riešenie bolo úspešne odovzdané') return self.get(request)
def my_solution(self, request, pk=None): problem = self.get_object() if not request.user.is_authenticated: raise exceptions.PermissionDenied('Je potrebné sa prihlásiť') event_registration = EventRegistration.get_registration_by_profile_and_event( request.user.profile, problem.series.semester) if event_registration is None: raise exceptions.MethodNotAllowed(method='my-solutuion') # TODO: vráti riešenie užívateľa return Response([], status=status.HTTP_501_NOT_IMPLEMENTED)
def my_solution(self, request, pk=None): problem = self.get_object() if not request.user.is_authenticated: raise exceptions.PermissionDenied('Je potrebné sa prihlásiť') event_registration = EventRegistration.get_registration_by_profile_and_event( request.user.profile, problem.series.semester) if event_registration is None: raise exceptions.MethodNotAllowed(method='my-solution') solution = Solution.objects.filter( problem=problem, semester_registration=event_registration).first() serializer = SolutionSerializer(solution) return Response(serializer.data, status=status.HTTP_200_OK)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) if "profile" in dir(self.request.user): context['profile'] = self.request.user.profile context[ 'registration'] = EventRegistration.get_registration_by_profile_and_event( self.request.user.profile, self.object.semester) else: context['profile'] = context['registration'] = None context['results'] = self.object.results_with_ranking() context['results_type'] = 'series' return context
def get(self, request, pk, cont, **kwargs): # pylint: disable=unused-argument try: profile = self.request.user.profile semester = Semester.objects.get(pk=pk) assert not EventRegistration.get_registration_by_profile_and_event( profile, semester) EventRegistration(profile=profile, school=profile.school, grade=Grade.get_grade_by_year_of_graduation( profile.year_of_graduation), event=semester).save() except AssertionError: messages.info(request, 'Do semestra sa dá registrovať iba jedenkrát') except AttributeError: messages.error(request, 'Na registráciu je potrebné sa prihlásiť') else: messages.success( request, f'{Semester.objects.get(pk=pk)}: Registrácia do semestra prebehla úspešne' ) return redirect(cont)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # TODO: táto logika sa opakuje na viacerých miestach if hasattr(self.request.user, 'profile'): context['profile'] = self.request.user.profile context[ 'registration'] = EventRegistration.get_registration_by_profile_and_event( self.request.user.profile, self.object.semester) else: context['profile'] = context['registration'] = None context['form'] = form = SeriesSolutionForm(self.object) context['problems'] = zip(self.object.problem_set.all(), form) return context
def register(self, request, pk=None): event = self.get_object() profile = request.user.profile # TODO: Overiť či sa môže registrovať ... či nie je starý if EventRegistration.get_registration_by_profile_and_event( profile, event): return Response(status=status.HTTP_409_CONFLICT) EventRegistration.objects.create( event=event, school=profile.school, profile=profile, grade=Grade.get_grade_by_year_of_graduation( profile.year_of_graduation), ) return Response(status=status.HTTP_201_CREATED)