from __future__ import unicode_literals

import os
import logging

from . import helper


def readStatus(config, student):
    student = student.lower()
    path = config("attachment_path")

    if not os.path.exists(path):
        return

    path = os.path.join(path, student)

    if not os.path.exists(path):
        return "Student ohne Abgabe"

    path = os.path.join(path, "korrekturstatus.txt")

    if not os.path.exists(path):
        return "Unbearbeitet"

    statusfile = open(path, "r")
    status = statusfile.read()
    statusfile.close()
    return status


def writeStatus(config, student, status):
    student = student.lower()
    status = status.lower()

    path = os.path.join(config("attachment_path"), student)

    if not os.path.exists(path):
        logging.error("Requested student '%s' hasn't submitted anything yet.")
        return

    path = os.path.join(path, "korrekturstatus.txt")

    with open(path, "w") as statusfile:
        statusfile.write(status)

from __future__ import unicode_literals

import os
import logging
import sqlite3

from . import helper


def readStatus(config, student):
    database = getStatusTable(config)
    cursor = database.cursor()

    cursor.execute("SELECT status FROM status WHERE identifier = ?", (student,))
    statusRow = cursor.fetchone()[0]  # just get first status

    if statusRow:
        return statusRow
    else:
        return "Unbearbeitet"


def writeStatus(config, student, status):
    database = getStatusTable(config)
    cursor = database.cursor()

    # Check if we need to create a new row first
    cursor.execute("SELECT status FROM status WHERE identifier = ?", (student,))
    statusRow = cursor.fetchone()[0]

    if statusRow:
        cursor.execute("UPDATE status SET status = ? WHERE identifier = ?", (status, student,))
    else:
        cursor.execute("INSERT INTO status VALUES(?, ?)", (student, status, ))
    database.commit()


def getStatusTable(config):
    statusDatabasePath = config("database_path")
    statusDatabase = sqlite3.connect(statusDatabasePath)
    cursor = statusDatabase.cursor()
    cursor.execute("""CREATE TABLE IF NOT EXISTS status
         (`identifier` text UNIQUE, `status` text, PRIMARY KEY (`identifier`));""")
    return statusDatabase

Move status to database instead of status file (server side)
Move status to database instead of status file (server side)
