Esempio n. 1
0
 def __unicode__(self):
     template = "{name} ({competition} {year}-{number})"
     return template.format(competition=remove_accents(self.competition),
                            year=self.year,
                            number=self.number,
                            name=remove_accents(self.name),
                            )
Esempio n. 2
0
 def __unicode__(self):
     template = "{name} ({competition} {year}-{number})"
     return template.format(competition=remove_accents(self.competition),
                            year=self.year,
                            number=self.number,
                            name=remove_accents(self.name),
                            )
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            try:
                file_data = form.cleaned_data['zipfile']
                filepath = get_uploaded_filepath(file_data)
                solutions_zip = ZipFile(filepath)

                # Check for any corrupted files in the zip
                # testzip returns the list of corrupted ones
                if solutions_zip.testzip():
                    messages.error(request,
                                   '"%s" in the .zip archive is corrupt.'
                                   % solutions_zip.testzip())
                    raise Exception('Corrpted archive.')

                # We loop over all PDF files in the zip
                for filename in [name for name in solutions_zip.namelist()
                                 if name.endswith('.pdf')]:

                    # Check that the name is of the form
                    # <score>-<username>-<problem_pk>.pdf
                    try:
                        parts = filename.rstrip('.pdf').split('-')

                        score = int(parts[0])
                        username = '******'.join(parts[1:-1])
                        problem_pk = int(parts[-1])

                    except (IndexError, ValueError, AssertionError):
                        messages.error(request,
                                       '"%s" is not of the correct form '
                                       '<score>-<username>-<problem_pk>.pdf'
                                       % remove_accents(filename))
                        continue

                    # Find the UserSolution and modify it
                    try:
                        user = User.objects.get(username=username)
                        solution = UserSolution.objects.get(user=user,
                                                            problem=problem_pk)
                    except User.DoesNotExist:
                        messages.error('User %s does not exist' % username)
                        continue
                    except UserSolution.DoesNotExist:
                        messages.error(request,
                                       'Solution for user %s and problem '
                                       '%d does not exist.'
                                       % (username, problem_pk))
                        continue

                    extracted_path = solutions_zip.extract(filename, path='/tmp')
                    new_path = os.path.join(settings.SENDFILE_ROOT, solution.get_corrected_solution_path())
                    file_move_safe(extracted_path, new_path, allow_overwrite=True)

                    solution.score = score
                    solution.corrected_solution = solution.get_corrected_solution_path()
                    solution.save()

                    messages.success(
                        request,
                        _("%s assigned %d points") % (solution, score)
                    )

            except Exception, e:
                # If any exceptions happened, errors should be in messages
                messages.error(request, 'exception happened: %s' % e)
            finally:
Esempio n. 4
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            try:
                file_data = form.cleaned_data['zipfile']
                filepath = get_uploaded_filepath(file_data)
                solutions_zip = ZipFile(filepath)

                # Check for any corrupted files in the zip
                # testzip returns the list of corrupted ones
                if solutions_zip.testzip():
                    messages.error(
                        request, '"%s" in the .zip archive is corrupt.' %
                        solutions_zip.testzip())
                    raise Exception('Corrpted archive.')

                # We loop over all PDF files in the zip
                for filename in [
                        name for name in solutions_zip.namelist()
                        if name.endswith('.pdf')
                ]:

                    # Check that the name is of the form
                    # <score>-<username>-<problem_pk>.pdf
                    try:
                        parts = filename.rstrip('.pdf').split('-')

                        score = int(parts[0])
                        username = '******'.join(parts[1:-1])
                        problem_pk = int(parts[-1])

                    except (IndexError, ValueError, AssertionError):
                        messages.error(
                            request, '"%s" is not of the correct form '
                            '<score>-<username>-<problem_pk>.pdf' %
                            remove_accents(filename))
                        continue

                    # Find the UserSolution and modify it
                    try:
                        user = User.objects.get(username=username)
                        solution = UserSolution.objects.get(user=user,
                                                            problem=problem_pk)
                    except User.DoesNotExist:
                        messages.error('User %s does not exist' % username)
                        continue
                    except UserSolution.DoesNotExist:
                        messages.error(
                            request, 'Solution for user %s and problem '
                            '%d does not exist.' % (username, problem_pk))
                        continue

                    extracted_path = solutions_zip.extract(filename,
                                                           path='/tmp')
                    new_path = os.path.join(
                        settings.SENDFILE_ROOT,
                        solution.get_corrected_solution_path())
                    file_move_safe(extracted_path,
                                   new_path,
                                   allow_overwrite=True)

                    solution.score = score
                    solution.corrected_solution = solution.get_corrected_solution_path(
                    )
                    solution.save()

                    messages.success(
                        request,
                        _("%s assigned %d points") % (solution, score))

            except Exception, e:
                # If any exceptions happened, errors should be in messages
                messages.error(request, 'exception happened: %s' % e)
            finally: