def response(pupilID): Pupil.markAsWaiting(pupilID, True) out = {"done": True} outJ = json.dumps(out) return """Status: 200 OK Content-Type: application/json Content-Length: {} {}""".format(len(outJ), outJ)
def response(pupilID): g = Pupil.getWaitingGame(pupilID) out = {"found": bool(g)} if g: Pupil(pupilID=pupilID).isWaiting(False) out.update(g) outJ = json.dumps(out) return """Status: 200 OK Content-Type: application/json Content-Length: {} {}""".format(len(outJ), outJ)
def test_markAsWaiting(self): Pupil.markAsWaiting(1, True) with BoggleDBCursor() as cur: cur.execute("SELECT waitingForGame FROM pupil " \ "WHERE pupilID=1;") res = cur.fetchone() self.assertEqual(res[0], "True") Pupil.markAsWaiting(1, False) with BoggleDBCursor() as cur: cur.execute("SELECT waitingForGame FROM pupil " \ "WHERE pupilID=1;") res = cur.fetchone() self.assertEqual(res[0], "False")
def test_getWaitingGame_gameExists(self): self._ensureGameExists(gameID=1, date='2020-04-14', board='rirehgaoesoldivs') self._ensurePlayerExists(gameID=1, pupilID=1, submitted="False") g = Pupil.getWaitingGame(pupilID=1) self.assertEqual(g, {"gameID": 1, "board": "rirehgaoesoldivs"}) self._ensurePlayerDoesNotExist(1, 1)
def response(gameID): g = Game(gameID) g.markAbsentPlayersScored() available = g.allPlayersScored() out = {"available": available} if available: possibleWords = g.possibleWords() out.update({ "gameID": gameID, "board": "".join(l for sublist in g.board for l in sublist), "date": g.dateStarted, "possibleWords": possibleWords, "players": [] }) for pupilID in g.players(): p = Player(pupilID=pupilID, gameID=gameID, fromDatabase=True) pWords = p.allWords() otherWords = p.otherPlayersWords() pup = Pupil(pupilID=pupilID) out["players"].append({ "name": "{} {}".format(pup.forename, pup.surname), "score": p.score, "words": [{ "word": w, "unique": w not in otherWords, "legit": l and w in possibleWords, "score": p.wordScore((w, l), pWords, otherWords) } for w, l in pWords] }) out = json.dumps(out) return """Status: 200 OK Content-Type: application/json Content-Length: {} {}""".format(len(out), out)
cookie.load(cookieString) if "teacherID" in cookie: teacherID = int(cookie["teacherID"].value) else: raise Exception("A cookie expired") else: raise Exception("A cookie expired") searchBy = post["pupilDataType"].value searchData = post["pupilSearchData"].value.lower() result = getFromDatabase("SELECT DISTINCT pupilID FROM pupil \ WHERE LOWER(%s)='%s';" % (searchBy, searchData)) pupilList = [Pupil(pupilID=row[0]) for row in result] for i in range(len(pupilList)): if i % 3 == 0: resultTable += "<tr>" resultTable += "<td class='objectContainer'>" + pupilList[ i].pupilToHTML(teacherID) + "</td>" if i % 3 == 2 or i == len(pupilList) - 1: resultTable += "</tr>" print page % (teacherMenuBar(), resultTable) except Exception as e: page = errorPage()
if "HTTP_COOKIE" in os.environ: cookieString = os.environ.get("HTTP_COOKIE") c = Cookie.SimpleCookie() c.load(cookieString) if "teacherID" in c: teacherID = int(c["teacherID"].value) else: raise Exception("A cookie expired") else: raise Exception("You do not have permission to view this page") pupil = Pupil(pupilID=pupilID) playerList = pupil.players() if not teacherID is None: body = teacherMenuBar() else: body = "<a href='/'><div class='centreForeground' style='font-size: 20px;'>Go home</div></a>" body += "<div class='centreForegroundWide' style='overflow: hidden'>" body += "<h1>Pupil: %s %s </h1>" % (pupil.forename, pupil.surname) body += "<div class='objectContainer' style='margin-left: 34px;'>" + pupil.pupilToHTML( teacherID) + "</div>" body += "<table id='objectTable'>"
def test_getWaitingGame_noGameIsFalsy(self): self._ensurePlayerDoesNotExist(pupilID=1) g = Pupil.getWaitingGame(pupilID=1) self.assertFalse(g)
</div> </body> </html>""" try: post = cgi.FieldStorage() isTeacher = "email" in post if isTeacher: newUser = Teacher(post["email"].value, post["username"].value, post["forename"].value, post["surname"].value) else: newUser = Pupil(post["username"].value, post["forename"].value, post["surname"].value) successMessage = newUser.save() if not isTeacher: newUser.addTeacher(post["teacherID"].value) print page % successMessage except Exception as e: if e[0] == 1452: print page % ( "A key constraint failed. If you signed up as a pupil this is probably because teacher {} does not exist. Get your teacher to find you from their control panel" .format(post["teacherID"].value)) else: page = errorPage() print page % ("", str(e))
from boggleGame import getFromDatabase try: post = cgi.FieldStorage() teacherID = post["teacherID"].value result = getFromDatabase("""SELECT pupil.pupilID FROM pupil, teaches WHERE teaches.teacherID='{}' AND pupil.pupilID=teaches.pupilID AND waitingForGame='True' AND ({} - lastSeen < 3 OR lastSeen = 0) ORDER BY pupil.pupilID;""". format(teacherID, time.time())) pupils = [Pupil(pupilID=row[0]) for row in result] pupilsTable = """<tr id='pupilTable'> <th>Pupil ID</th> <th>First Name</th> <th>Surname</th> <th>Average Score</th> <th>Select</th> <th>View Pupil</th> <tr>""" for pupil in pupils: pupilsTable += pupil.generateTR() print pupilsTable