Beispiel #1
0
def grid_changed():
    """ REST method to determine whether the grid has changed """

    # Compare the original grid loaded to its current values.
    # Return True if they are different, False if they are the same.
    jsonstr_initial_sha = session.get('grid.initial.sha', sha256(None))
    jsonstr_current_sha = sha256(session.get('grid', None))
    changed = not (jsonstr_current_sha == jsonstr_initial_sha)
    obj = {"changed": changed}

    # Send this back to the client in JSON
    resp = make_response(json.dumps(obj), HTTPStatus.OK)
    resp.headers['Content-Type'] = "application/json"
    return resp
Beispiel #2
0
def puzzle_new():
    """ Creates a new puzzle and redirects to puzzle screen """

    # Get the chosen grid name from the query parameters
    gridname = request.args.get('gridname')

    # Open the corresponding file and read its contents as json
    # and recreate the grid from it
    userid = 1  # TODO replace hard-coded user ID
    query = DBGrid.query.filter_by(userid=userid, gridname=gridname)
    jsonstr = query.first().jsonstr
    grid = Grid.from_json(jsonstr)

    # Now pass this grid to the Puzzle() constructor
    puzzle = Puzzle(grid)

    # Save puzzle in the session
    jsonstr = puzzle.to_json()
    session['puzzle'] = jsonstr
    session['puzzle.initial.sha'] = sha256(jsonstr)

    # Remove any leftover puzzle name in the session
    session.pop('puzzlename', None)

    return redirect(url_for('uipuzzle.puzzle_screen'))
Beispiel #3
0
def puzzle_changed():
    """ REST method that returns whether the puzzle has changed since it was opened """

    current_puzzle_json = session.get('puzzle', None)
    if current_puzzle_json:
        current_puzzle = Puzzle.from_json(current_puzzle_json)
        jsonstr_initial_sha = session.get('puzzle.initial.sha', sha256(None))
        jsonstr_current_sha = sha256(current_puzzle.to_json())
        changed = not (jsonstr_current_sha == jsonstr_initial_sha)
    else:
        changed = False

    obj = {"changed": changed}

    # Send this back to the client in JSON
    resp = make_response(json.dumps(obj), HTTPStatus.OK)
    resp.headers['Content-Type'] = "application/json"
    return resp
Beispiel #4
0
def puzzle_save_common(puzzlename):
    """ Common method used by both puzzle_save and puzzle_save_as """

    # Recreate the puzzle from the JSON in the session
    # and validate it
    jsonstr = session.get('puzzle', None)
    puzzle = Puzzle.from_json(jsonstr)
    jsonstr = puzzle.to_json()
    ok, messages = puzzle.validate()
    if not ok:
        flash("Puzzle not saved")
        for message_type in messages:
            message_list = messages[message_type]
            if len(message_list) > 0:
                flash(f"*** {message_type} ***")
                for message in message_list:
                    flash("   " + message)
    else:
        # Save the file
        userid = 1  # TODO Replace hard coded user id
        query = DBPuzzle.query.filter_by(userid=userid, puzzlename=puzzlename)
        if not query.all():
            # No puzzle in the database. This is an insert
            logging.debug(f"Inserting puzzle {puzzlename} into puzzles table")
            created = modified = datetime.now().isoformat()
            newpuzzle = DBPuzzle(userid=userid,
                                 puzzlename=puzzlename,
                                 created=created,
                                 modified=modified,
                                 jsonstr=jsonstr)
            db.session.add(newpuzzle)
            db.session.commit()
        else:
            # Existing puzzle. This is an update
            logging.debug(f"Updating puzzle {puzzlename} in puzzles table")
            oldpuzzle = query.first()
            oldpuzzle.modified = datetime.now().isoformat()
            oldpuzzle.jsonstr = jsonstr
            db.session.commit()

        # Send message about the save
        flash(f"Puzzle saved as {puzzlename}")

        # Store the sha of the saved version of the puzzle
        # in the session as 'puzzle.initial.sha' so that we can
        # detect whether it has been changed since it was last saved
        session['puzzle.initial.sha'] = sha256(puzzle.to_json())

    # Show the puzzle screen
    return redirect(url_for('uipuzzle.puzzle_screen'))
Beispiel #5
0
def grid_save_common(gridname):
    """ Common method used by both grid_save and grid_save_as """

    # Recreate the grid from the JSON in the session
    # and validate it
    jsonstr = session.get('grid', None)
    grid = Grid.from_json(jsonstr)
    ok, messages = grid.validate()
    if not ok:
        flash("Grid not saved")
        for message_type in messages:
            message_list = messages[message_type]
            if len(message_list) > 0:
                flash(f"*** {message_type} ***")
                for message in message_list:
                    flash("   " + message)
    else:
        # Save the file
        userid = 1  # TODO Replace hard coded user id
        query = DBGrid.query.filter_by(userid=userid, gridname=gridname)
        if not query.all():
            # No grid in the database. This is an insert
            logging.debug(f"Inserting grid '{gridname}' into grids table")
            created = modified = datetime.now().isoformat()
            newgrid = DBGrid(userid=userid,
                             gridname=gridname,
                             created=created,
                             modified=modified,
                             jsonstr=jsonstr)
            db.session.add(newgrid)
            db.session.commit()
        else:
            # There is a grid. This is an update
            logging.debug(f"Updating grid '{gridname}' in grids table")
            oldgrid = query.first()
            oldgrid.modified = datetime.now().isoformat()
            oldgrid.jsonstr = jsonstr
            db.session.commit()

        # Send message about save
        flash(f"Grid saved as {gridname}")

        # Store the sha256 of the saved version of the grid
        # in the session as 'grid.initial.sha' so that we can detect
        # whether it has been changed since it was last saved
        session['grid.initial.sha'] = sha256(jsonstr)

    # Show the grid screen
    return redirect(url_for('uigrid.grid_screen'))
Beispiel #6
0
def grid_new():
    """ Creates a new grid and redirects to grid screen """

    # Get the grid size from the form
    n = int(request.args.get('n'))

    # Remove any leftover grid name in the session
    session.pop('gridname', None)

    # Create the grid
    grid = Grid(n)
    jsonstr = grid.to_json()
    session['grid'] = jsonstr
    session['grid.initial.sha'] = sha256(jsonstr)

    return redirect(url_for('uigrid.grid_screen'))
Beispiel #7
0
 def __init__(self):
     self.id = 1
     self.username = "******"
     self.password = sha256('My Password')
     dateformat = "%Y-%m-%dT%H:%M:%S.%f"
     self.created = datetime(2020, 5, 31, 18, 23, 56).strftime(dateformat)
     self.modified = datetime(2020, 6, 28, 1, 2, 3).strftime(dateformat)
     self.last_access = datetime(2020, 7, 20, 4, 5, 6).strftime(dateformat)
     self.email = "*****@*****.**"
     self.confirmed = datetime(2020, 5, 31, 19, 0, 0).strftime(dateformat)
     self.author_name = "John Q. Puzzlemaker"
     self.address_line_1 = "123 Main Street"
     self.address_line_2 = "Apt. 456"
     self.address_city = "Anytown"
     self.address_state = "NY"
     self.address_zip = "12345"
Beispiel #8
0
def grid_open():
    """ Opens a new grid and redirects to grid screen """

    # Get the chosen grid name from the query parameters
    gridname = request.args.get('gridname')

    # Open the corresponding file and read its contents as json
    # and recreate the grid from it
    userid = 1  # TODO Replace hard coded user id
    jsonstr = grid_load_common(userid, gridname)

    # Store the grid and grid name in the session
    session['grid'] = jsonstr
    session['grid.initial.sha'] = sha256(jsonstr)
    session['gridname'] = gridname

    return redirect(url_for('uigrid.grid_screen'))
Beispiel #9
0
def puzzle_open():
    """ Opens a new puzzle and redirects to puzzle screen """

    # Get the chosen puzzle name from the query parameters
    puzzlename = request.args.get('puzzlename')

    # Open the corresponding file and read its contents as json
    # and recreate the puzzle from it
    userid = 1  # TODO Replace hard coded user id
    jsonstr = puzzle_load_common(userid, puzzlename)
    puzzle = Puzzle.from_json(jsonstr)

    # Store the puzzle and puzzle name in the session
    session['puzzle'] = jsonstr
    session['puzzle.initial.sha'] = sha256(jsonstr)
    session['puzzlename'] = puzzlename

    return redirect(url_for('uipuzzle.puzzle_screen'))
Beispiel #10
0
def grid_new_from_puzzle():
    """ Creates a new grid from the specified puzzle """

    puzzlename = request.args.get('puzzlename')
    userid = 1  # TODO replace hard-coded user ID
    query = DBPuzzle.query.filter_by(userid=userid, puzzlename=puzzlename)
    jsonstr = query.first().jsonstr
    grid = Grid.from_json(jsonstr)
    grid.undo_stack = []
    grid.redo_stack = []
    jsonstr = grid.to_json()

    # Save grid in the session
    session['grid'] = jsonstr
    session['grid.initial.sha'] = sha256(jsonstr)

    # Remove any leftover grid name in the session
    session.pop('gridname', None)

    return redirect(url_for('uigrid.grid_screen'))
Beispiel #11
0
 def test_same(self):
     cs1 = sha256("Hello")
     cs2 = sha256("Hello")
     self.assertEqual(cs1, cs2)
Beispiel #12
0
 def test_obj(self):
     x = object()
     sha = sha256(x)
     self.assertIsNotNone(sha)
Beispiel #13
0
 def test_int(self):
     x = 17
     sha = sha256(x)
     self.assertIsNotNone(sha)
Beispiel #14
0
 def test_none(self):
     x = None
     sha = sha256(x)
     self.assertIsNotNone(sha)
Beispiel #15
0
 def test_different(self):
     cs1 = sha256("Hello1")
     cs2 = sha256("Hello2")
     self.assertNotEqual(cs1, cs2)