from flask import render_template, flash, redirect, url_for, request, send_from_directory, make_response
from iliasCorrector import app, db
from iliasCorrector.models import Exercise, Submission, File, Student
from iliasCorrector import utils
from werkzeug import secure_filename
import os


@app.route('/')
def index():
    exercises = Exercise.query.all()
    return render_template('index.html', exercises=exercises)


@app.route('/exercise/<exercise_id>/')
def exercise(exercise_id):
    exercise = Exercise.query.get_or_404(exercise_id)
    submissions = exercise.submissions.join(Student).order_by('student.ident')
    return render_template('exercise.html', exercise=exercise, submissions=submissions)


@app.route('/exercise/<exercise_id>/submission/')
@app.route('/exercise/<exercise_id>/submission/<submission_id>/', methods=['GET', 'POST'])
def submission(exercise_id=None, submission_id=None):
    if not submission_id:
        submission = Submission.query.filter_by(exercise_id=exercise_id, grade=None).join(Student).order_by('student.ident').first()
        return redirect(url_for('submission', exercise_id=exercise_id, submission_id=submission.id))
    submission = Submission.query.get_or_404(submission_id)
    if request.method == 'POST':
        grade = request.form.get('grade', None)
        remarks = request.form.get('remarks', None)
        flash('Successfully graded {} with {} points'.format(submission.student, grade), 'success')
        submission.grade = grade
        submission.remarks = remarks.replace(';', '-')
        db.session.add(submission)
        db.session.commit()
        next_submission = submission.exercise.submissions.filter_by(grade=None).join(Student).order_by('student.ident').first()
        if not next_submission:
            flash('Finished correcting submissions for exercise {}'.format(submission.exercise), 'success')
            return redirect(url_for('exercise', exercise_id=exercise_id))
        return redirect(url_for('submission', exercise_id=exercise_id, submission_id=next_submission.id))
    return render_template('submission.html', submission=submission)


@app.route('/files/<file_id>/')
def file(file_id):
    f = File.query.get_or_404(file_id)
    return send_from_directory(f.path, f.name)


@app.route('/sync')
def update_exercises():
    utils.update_exercises()
    return "done"

@app.route('/exercise/<exercise_id>/export/')
def export_grades(exercise_id):
    exercise = Exercise.query.get_or_404(exercise_id)
    response = make_response('\n'.join(utils.export_grades(exercise)))
    response.headers["Content-Disposition"] = "attachment; filename=points.csv"
    return response

@@ -700,24 +700,283 @@
 missions)%0A%0A%0A
+def get_next_submission(exercise_id, ident=''):%0A    exercise = Exercise.query.filter_by(id=exercise_id).first()%0A    return exercise.submissions.filter_by(grade=None).join(Student).order_by(%0A            'student.ident').filter(Student.ident %3E ident).first()%0A%0A%0A
 @app.route('
@@ -1209,118 +1209,39 @@
 n = 
-Submission.query.filter_by(exercise_id=exercise_id, grade=None).join(Student).order_by('student.ident').first(
+get_next_submission(exercise_id
 )%0A  
@@ -1395,24 +1395,147 @@
 mission_id)%0A
+    next_submission = get_next_submission(exercise_id,%0A                                          submission.student.ident)%0A
     if reque
@@ -1910,136 +1910,8 @@
 t()%0A
-        next_submission = submission.exercise.submissions.filter_by(grade=None).join(Student).order_by('student.ident').first()%0A
     
@@ -2222,24 +2222,24 @@
 ission.id))%0A
-
     return r
@@ -2293,16 +2293,76 @@
 bmission
+,%0A                           next_submission=next_submission
 )%0A%0A%0A@app

Improve selection of next submission to grade