Example #1
0
    def test_source_description_submit(self):
        desc_submit = Submit.objects.create(
            task=self.task,
            user=self.user,
            points=5,
            submit_type=submit_constants.SUBMIT_TYPE_DESCRIPTION,
            filepath=path.join(path.dirname(__file__), "test_data", "submits", "description.txt"),
        )
        desc_submit.time = self.task.round.start_time + timezone.timedelta(0, 5)
        desc_submit.save()
        submit = Submit.objects.create(
            task=self.task,
            user=self.user,
            points=5,
            submit_type=submit_constants.SUBMIT_TYPE_SOURCE,
            filepath=path.join(path.dirname(__file__), "test_data", "submits", "source.cpp"),
        )
        submit.time = self.task.round.start_time + timezone.timedelta(0, 5)
        submit.save()

        submit_file = helpers.submit_source_download_filename(submit, desc_submit.id, 0)

        self.client.force_login(self.staff)
        url = reverse(self.url_name, kwargs={"task_pk": self.task.id})
        response = self.client.get(url)
        f = io.BytesIO(b"".join(response.streaming_content))
        zipped_file = zipfile.ZipFile(f, "a")

        self.assertIsNone(zipped_file.testzip())
        self.assertIn(submit_file, zipped_file.namelist())
        zipped_file.close()
        f.close()
Example #2
0
File: views.py Project: lampask/web
def download_all_submits(request, task_pk, download_reviews=False):
    task = get_object_or_404(Task, pk=task_pk)
    submits = get_latest_submits_for_task(task).values()

    if (not request.user.is_superuser
            and task.round.semester.competition.organizers_group
            not in request.user.groups.all()):
        raise PermissionDenied

    path = os.path.join(settings.SUBMIT_PATH, "reviews")
    if not os.path.isdir(path):
        os.makedirs(path)
        os.chmod(path, 0o777)

    path = os.path.join(
        path,
        "Uloha-%s-%s-%s.zip" %
        (slugify(task.name), int(time()), slugify(request.user.username)),
    )

    errors = []

    with zipfile.ZipFile(path, "w") as zipper:
        for i, user in enumerate(submits):
            if "description" in user:
                submit = (user["review"] if download_reviews
                          and "review" in user else user["description"])
                description_submit_id = submit.pk
                if not os.path.isfile(submit.filepath):
                    errors += [
                        _("Missing file of user %s") %
                        submit.user.get_full_name()
                    ]
                else:
                    zipper.write(submit.filepath,
                                 submit_download_filename(submit, i))
                last_review_points = str(int(
                    user["review"].points)) if "review" in user else ""
                last_review_comment = user[
                    "review"].reviewer_comment if "review" in user else ""
                zipper.writestr(
                    submit_directory(submit, i) + REVIEW_POINTS_FILENAME,
                    last_review_points)
                zipper.writestr(
                    submit_directory(submit, i) + REVIEW_COMMENT_FILENAME,
                    last_review_comment.encode("utf-8"),
                )

            if "sources" in user:
                for submit in user["sources"]:
                    if os.path.isfile(submit.filepath):
                        zipper.write(
                            submit.filepath,
                            submit_source_download_filename(
                                submit, description_submit_id, i),
                        )
                    else:
                        errors += [
                            _("Missing source file of user %s") %
                            submit.user.get_full_name()
                        ]
                    if submit.protocol:
                        zipper.writestr(
                            submit.protocol,
                            submit_protocol_download_filename(
                                submit, description_submit_id, i),
                        )
                    else:
                        errors += [
                            _("Missing protocol of user %s") %
                            submit.user.get_full_name()
                        ]

        if errors:
            zipper.writestr(REVIEW_ERRORS_FILENAME,
                            "\n".join(errors).encode("utf8"))

    return sendfile(request, path, attachment=True)