diff --git a/sauce/controllers/submissions.py b/sauce/controllers/submissions.py index 43cb9506..800f210b 100644 --- a/sauce/controllers/submissions.py +++ b/sauce/controllers/submissions.py @@ -20,7 +20,7 @@ #from tg.i18n import ugettext as _ from repoze.what.predicates import not_anonymous, Any, has_permission from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound -from sqlalchemy.exc import IntegrityError +from sqlalchemy.exc import SQLAlchemyError from chardet import detect from pygmentize.widgets import Pygmentize @@ -246,6 +246,32 @@ def edit(self, **kwargs): return dict(page=['submissions', 'edit'], event=self.event, assignment=self.assignment, submission=self.submission) + @expose() + def delete(self): + subm_id = self.submission.id + subm_url = self.submission.url + try: + if (hasattr(request, 'teacher') and request.teacher or + hasattr(request, 'user') and request.user == self.submission.user): + DBSession.delete(self.submission) + DBSession.flush() + else: + #abort(403) + flash('You have no permission to delete this Submission', 'warning') + redirect(url(self.submission.url + '/show')) + except SQLAlchemyError: + DBSession.rollback() + flash('Submission could not be deleted', 'error') + log.warn('Submission could not be deleted', exc_info=True) + redirect(url(self.submission.url + '/show')) + else: + flash('Submission %d deleted' % (subm_id), 'ok') + if request.referer and (set(request.referer.split('/')) >= set(subm_url.split('/'))): + redirect(url(self.assignment.url)) + else: + redirect(request.referer) + redirect(url(self.assignment.url)) + @expose('sauce.templates.submission_result') def result(self, force_test=False): compilation = None diff --git a/sauce/lib/base.py b/sauce/lib/base.py index 3e9c483f..58fcaafa 100644 --- a/sauce/lib/base.py +++ b/sauce/lib/base.py @@ -63,7 +63,9 @@ def __call__(self, environ, start_response): c.user = request.user c.student = request.student c.teacher = request.teacher - + + request.referer = request.environ.get('HTTP_REFERER', None) + # Initialize other tmpl_context variables c.sub_menu = [] c.side_menu = [] diff --git a/sauce/model/submission.py b/sauce/model/submission.py index 8e9b0aa7..179aff15 100644 --- a/sauce/model/submission.py +++ b/sauce/model/submission.py @@ -186,7 +186,7 @@ class Judgement(DeclarativeBase): '''Date of judgement''' submission_id = Column(Integer, ForeignKey('submissions.id'), nullable=False) - submission = relationship('Submission', backref=backref('judgement', uselist=False)) + submission = relationship('Submission', backref=backref('judgement', uselist=False, cascade='all,delete-orphan')) teacher_id = Column(Integer, ForeignKey('teachers.id'), nullable=False) teacher = relationship('Teacher', backref=backref('judgements')) diff --git a/sauce/model/test.py b/sauce/model/test.py index 0132974b..ddfc3438 100644 --- a/sauce/model/test.py +++ b/sauce/model/test.py @@ -260,7 +260,7 @@ class Testrun(DeclarativeBase): '''Test that was run in this testrun''' submission_id = Column(Integer, ForeignKey('submissions.id'), nullable=False) - submission = relationship('Submission', backref=backref('testruns')) + submission = relationship('Submission', backref=backref('testruns', cascade='all,delete-orphan')) '''Submission that was run in this testrun''' __mapper_args__ = {'order_by': asc(date)} diff --git a/sauce/templates/submission.mak b/sauce/templates/submission.mak index 5042ad70..4f6e27d2 100644 --- a/sauce/templates/submission.mak +++ b/sauce/templates/submission.mak @@ -60,6 +60,12 @@  Judge % endif + % if hasattr(request, 'teacher') and request.teacher or \ + hasattr(request, 'user') and request.user == submission.user: +
  • +  Delete +
  • + % endif
    diff --git a/sauce/widgets/submission_table.py b/sauce/widgets/submission_table.py index a74ec18c..ab303f2f 100644 --- a/sauce/widgets/submission_table.py +++ b/sauce/widgets/submission_table.py @@ -35,6 +35,10 @@ def _actions(filler, subm): if hasattr(request, 'teacher') and request.teacher: result.append(u'' '' % (subm.url)) + if (hasattr(request, 'teacher') and request.teacher or + hasattr(request, 'user') and request.user == subm.user): + result.append(u'' + '' % (subm.url)) return literal('
    ' % (len(result)*30) + ''.join(result) + '
    ')