Exemplo n.º 1
0
    def submissions_edit(self):
        # Prepare parameters
        form = SubmissionForm(self.request.POST, csrf_context=self.request)
        submissions = get_submissions()
        page = self.page(submissions)
        redirect = self.redirect("admin_submissions", page.page)

        # Cancel button pressed?
        if not form.submit.data:
            return redirect

        is_new = not bool(form.challenge.data and form.team.data)
        # Form errors?
        if not form.validate():
            return self._list_retparams(page, form, is_new=is_new)

        # New item or existing one?
        try:
            submission = (
                DBSession.query(Submission)
                .filter(Submission.challenge_id == form.challenge.data.id)
                .filter(Submission.team_id == form.team.data.id)
                .one()
            )
            self.request.session.flash("Submission edited!")
        except NoResultFound:
            submission = Submission()
            DBSession.add(submission)
            self.request.session.flash("Submission added!")

        # Transfer edits into databse
        form.populate_obj(submission)
        return redirect
Exemplo n.º 2
0
    def test_get_submissions(self, make_team, make_challenge):
        c = make_challenge()
        t = make_team()
        s = Submission(challenge=c, team=t)
        self.dbsession.add(s)

        query = get_submissions()
        assert query.count() == 1
        assert query[0] == s
Exemplo n.º 3
0
    def submissions(self):
        """
        List, add or edit a submission. This is different because it consists
        of composite foreign keys and thus needs separate though similar logic.
        But in the end it is basically the same functionality as with the other
        list views.
        """
        # Prepare parameters
        submissions = get_submissions()
        page = self.page(submissions)
        redirect = self.redirect("admin_submissions", page.page)
        challenge_id = None
        team_id = None
        if self.request.method == "POST":
            # Someone wants to edit so make sure to load and check the form
            # for CSRF and get the ID.
            edit_form = SubmissionButtonForm(self.request.POST, csrf_context=self.request)
            if not edit_form.validate():
                # Error, most likely CSRF
                return redirect
            challenge_id = edit_form.challenge_id.data
            team_id = edit_form.team_id.data

        # Either load a new form or load old data into it.
        if challenge_id is None or team_id is None:
            form = SubmissionForm(self.request.POST, csrf_context=self.request)
        else:
            submission = (
                DBSession.query(Submission)
                .filter(Submission.team_id == team_id)
                .filter(Submission.challenge_id == challenge_id)
                .one()
            )
            form = SubmissionForm(None, submission, csrf_context=self.request)

        # Display the page
        is_new = not bool(form.challenge.data and form.team.data)
        return self._list_retparams(page, form, is_new=is_new)