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)

  from __future__ import unicode_literals
  
  import os
  import logging
+ import sqlite3
  
  from . import helper
  
  
  def readStatus(config, student):
-     student = student.lower()
-     path = config("attachment_path")
+     database = getStatusTable(config)
+     cursor = database.cursor()
  
-     if not os.path.exists(path):
-         return
+     cursor.execute("SELECT status FROM status WHERE identifier = ?", (student,))
+     statusRow = cursor.fetchone()[0]  # just get first status
  
+     if statusRow:
+         return statusRow
+     else:
-     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()
+     database = getStatusTable(config)
+     cursor = database.cursor()
  
-     path = os.path.join(config("attachment_path"), student)
+     # Check if we need to create a new row first
+     cursor.execute("SELECT status FROM status WHERE identifier = ?", (student,))
+     statusRow = cursor.fetchone()[0]
  
-     if not os.path.exists(path):
-         logging.error("Requested student '%s' hasn't submitted anything yet.")
-         return
+     if statusRow:
+         cursor.execute("UPDATE status SET status = ? WHERE identifier = ?", (status, student,))
+     else:
+         cursor.execute("INSERT INTO status VALUES(?, ?)", (student, status, ))
+     database.commit()
  
-     path = os.path.join(path, "korrekturstatus.txt")
  
-     with open(path, "w") as statusfile:
-         statusfile.write(status)
+ 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)
## Code Before:
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)

## Instruction:
Move status to database instead of status file (server side)
## Code After:
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
