Esempio n. 1
0
    def process(self, request, filename, db_session):
        errMsg = ""
        parser = NotebookParser()

        try:
            before = request.form['beforeJSON']
            after = request.form['afterJSON']
        except BadRequestKeyError:
            errMsg = """Invalid notebook Diff Request. <br/>
                Please return to the home page and submit the request again."""
            return render_template('Error.html', err=errMsg)

        try:
            nb_before = parser.parseString(before)
        except nbformat.NotJSONError:
            errMsg = errMsg + """The Before notebook contains
                invalid JSON data. <br/>"""
        try:
            nb_after = parser.parseString(after)
        except nbformat.NotJSONError:
            errMsg = errMsg + """The After notebook contains
                invalid JSON data. <br/>"""

        if len(errMsg) == 0:

            diffNotebook = notebook_diff(nb_before, nb_after)

            # bitarray used to convert notebook to binary for BLOB
            ba = bitarray.bitarray()
            ba.fromstring(json.dumps(diffNotebook, indent=2))

            # object to be saved to database
            obj = nbdiffModel(ba.to01())

            # add to database and commit it.
            try:
                db_session.add(obj)
                db_session.commit()
            except OperationalError:
                db_session.rollback()
                print """The database is not initialized.
                    Please restart server with argument init_db"""
                errMsg = """There was an error with the database. <br/>
                   Please contact administrator to resolve this issue."""
                return render_template('Error.html', err=errMsg)
            except:
                db_session.rollback()
                errMsg = """There was an unexpected error with the database.
                    <br/>Please try again later. <br/>
                    If this problem persists please contact administrator."""
                return render_template('Error.html', err=errMsg)

            # return the id of the object.
            nb_id = obj.id

            # redirect is used because we want
            # user to have a easier url to return to.
            return redirect("/Comparison/"+str(nb_id), code=302)
        else:
            return render_template('Error.html', err=errMsg)
Esempio n. 2
0
    def process(self, request, filename, db_session):
        errMsg = ""
        parser = NotebookParser()

        try:
            before = request.form['beforeJSON']
            after = request.form['afterJSON']
        except BadRequestKeyError:
            errMsg = """Invalid notebook Diff Request. <br/>
                Please return to the home page and submit the request again."""
            return render_template('Error.html', err=errMsg)

        try:
            nb_before = parser.parseString(before)
        except nbformat.NotJSONError:
            errMsg = errMsg + """The Before notebook contains
                invalid JSON data. <br/>"""
        try:
            nb_after = parser.parseString(after)
        except nbformat.NotJSONError:
            errMsg = errMsg + """The After notebook contains
                invalid JSON data. <br/>"""

        if len(errMsg) == 0:

            diffNotebook = notebook_diff(nb_before, nb_after)

            # bitarray used to convert notebook to binary for BLOB
            ba = bitarray.bitarray()
            ba.fromstring(json.dumps(diffNotebook, indent=2))

            # object to be saved to database
            obj = nbdiffModel(ba.to01())

            # add to database and commit it.
            try:
                db_session.add(obj)
                db_session.commit()
            except OperationalError:
                db_session.rollback()
                print """The database is not initialized.
                    Please restart server with argument init_db"""
                errMsg = """There was an error with the database. <br/>
                   Please contact administrator to resolve this issue."""
                return render_template('Error.html', err=errMsg)
            except:
                db_session.rollback()
                errMsg = """There was an unexpected error with the database.
                    <br/>Please try again later. <br/>
                    If this problem persists please contact administrator."""
                return render_template('Error.html', err=errMsg)

            # return the id of the object.
            nb_id = obj.id

            # redirect is used because we want
            # user to have a easier url to return to.
            return redirect("/Comparison/" + str(nb_id), code=302)
        else:
            return render_template('Error.html', err=errMsg)
Esempio n. 3
0
 def test_process(self):
     ccmd.render_template = mock_render_template
     session = db.db_session()
     ba = bitarray.bitarray()
     ba.fromstring("test")
     obj = nbdiffModel(ba.to01())
     session.add(obj)
     session.commit()
     filename = obj.id
     response = ccmd.ComparisonCommand().process(None, filename, session)
     assert response == "nbdiff.html"
     response = app.get("/Comparision/" + str(filename))
     assert response.status == "200 OK"
Esempio n. 4
0
 def test_process(self):
     ccmd.render_template = mock_render_template
     session = db.db_session()
     ba = bitarray.bitarray()
     ba.fromstring("test")
     obj = nbdiffModel(ba.to01())
     session.add(obj)
     session.commit()
     filename = obj.id
     response = ccmd.ComparisonCommand().process(None, filename, session)
     assert response == "nbdiff.html"
     response = app.get("/Comparison/" + str(filename))
     assert response.status == "200 OK"
Esempio n. 5
0
 def test_process(self):
     session = db.db_session()
     localStream = open(os.path.join(MERGE_NB_DIR, "local.ipynb"), "r")
     data = localStream.read()
     ba = bitarray.bitarray()
     ba.fromstring(data)
     obj = nbdiffModel(ba.to01())
     session.add(obj)
     session.commit()
     filename = obj.id
     response = nrcmd.NotebookRequestCommand().process(None, filename, session)
     assert response == data
     response = app.get("/notebooks/" + str(filename))
     assert response.data == data
Esempio n. 6
0
 def test_process(self):
     session = db.db_session()
     localStream = open(os.path.join(MERGE_NB_DIR, "local.ipynb"), 'r')
     data = localStream.read()
     ba = bitarray.bitarray()
     ba.fromstring(data)
     obj = nbdiffModel(ba.to01())
     session.add(obj)
     session.commit()
     filename = obj.id
     response = nrcmd.NotebookRequestCommand().process(
         None, filename, session)
     assert response == data
     response = app.get("/notebooks/" + str(filename))
     assert response.data == data
Esempio n. 7
0
    def process(self, request, filename, db_session):
        errMsg = ""
        parser = NotebookParser()
        # Max Size of a notebook accepted is 20M.
        max_size = 20*1024*1024

        try:
            beforeURL = request.form['beforeURL']
            afterURL = request.form['afterURL']
        except BadRequestKeyError:
            errMsg = """Invalid notebook Diff Request. <br/>
                Please return to the home page and submit the request again."""
            return render_template('Error.html', err=errMsg)

        try:
            beforeFile = urllib2.urlopen(beforeURL)
            if int(beforeFile.info()['Content-Length']) > max_size:
                errMsg = errMsg + """The Before notebook exceeds 20MB.
                    Only notebooks below 20MB are accepted.<br/>"""
        except:
            errMsg = errMsg + """We are unable to access the Before
                notebook file from the given URL.<br/>"""

        try:
            afterFile = urllib2.urlopen(afterURL)
            if int(afterFile.info()['Content-Length']) > max_size:
                errMsg = errMsg + """The After notebook exceeds 20MB.
                    Only notebooks below 20MB are accepted.<br/>"""
        except:
            errMsg = errMsg + """We are unable to access the After
                notebook file from the given URL.<br/>"""

        if len(errMsg) == 0:
            try:
                nb_before = parser.parse(beforeFile)
            except nbformat.NotJSONError:
                errMsg = errMsg + """The Before notebook contains
                    invalid JSON data. <br/>"""
            try:
                nb_after = parser.parse(afterFile)
            except nbformat.NotJSONError:
                errMsg = errMsg + """The After notebook contains
                    invalid JSON data. <br/>"""

            beforeFile.close()
            afterFile.close()

        if len(errMsg) == 0:
            diffedNotebook = notebook_diff(nb_before, nb_after)

            # bitarray used to convert notebook to binary for BLOB
            ba = bitarray.bitarray()
            ba.fromstring(json.dumps(diffedNotebook, indent=2))

            # object to be saved to database
            obj = nbdiffModel(ba.to01())

            # add to database and commit it.
            try:
                db_session.add(obj)
                db_session.commit()
            except OperationalError:
                db_session.rollback()
                print """The database is not initialized.
                    Please restart server with argument init_db."""
                errMsg = """There was an error with the database. <br/>
                   Please contact administrator to resolve this issue."""
                return render_template('Error.html', err=errMsg)
            except:
                db_session.rollback()
                errMsg = """There was an unexpected error with the database.
                    <br/>Please try again later. <br/>
                    If this problem persists please contact administrator."""
                return render_template('Error.html', err=errMsg)

            # return the id of the object.
            nb_id = obj.id

            # redirect is used because we want
            # user to have a easier url to return to.
            return redirect("/Comparison/"+str(nb_id), code=302)
        else:
            return render_template('Error.html', err=errMsg)
Esempio n. 8
0
    def process(self, request, filename, db_session):
        errMsg = ""
        parser = NotebookParser()
        # Max Size of a notebook accepted is 20M.
        max_size = 20 * 1024 * 1024

        try:
            localURL = request.form['localURL']
            baseURL = request.form['baseURL']
            remoteURL = request.form['remoteURL']
        except BadRequestKeyError:
            errMsg = """Invalid notebook Merge Request.
                <br/>Please return to the home page and
                submit the request again."""
            return render_template('Error.html', err=errMsg)

        try:
            localFile = urllib2.urlopen(localURL)
            if int(localFile.info()['Content-Length']) > max_size:
                errMsg = errMsg + """The Local notebook
                    exceeds 20MB. Only notebooks below
                    20MB are accepted.<br/>"""
        except:
            errMsg = errMsg + """We are unable to access
                the Local notebook file from the
                given URL.<br/>"""

        try:
            baseFile = urllib2.urlopen(baseURL)
            if int(baseFile.info()['Content-Length']) > max_size:
                errMsg = errMsg + """The Base notebook
                    exceeds 20MB. Only notebooks below
                    20MB are accepted.<br/>"""
        except:
            errMsg = errMsg + """We are unable to access
                the Base notebook file from the given
                URL.<br/>"""

        try:
            remoteFile = urllib2.urlopen(remoteURL)
            if int(remoteFile.info()['Content-Length']) > max_size:
                errMsg = errMsg + """The Remote notebook
                    exceeds 20MB. Only notebooks below
                    20MB are accepted.<br/>"""
        except:
            errMsg = errMsg + """We are unable to access
                the Remote notebook file from
                the given URL.<br/>"""

        if len(errMsg) == 0:
            try:
                nb_local = parser.parse(localFile)
            except nbformat.NotJSONError:
                errMsg = errMsg + """The Local notebook
                    contains invalid JSON data. <br/>"""
            try:
                nb_base = parser.parse(baseFile)
            except nbformat.NotJSONError:
                errMsg = errMsg + """The Base notebook
                    contains invalid JSON data. <br/>"""
            try:
                nb_remote = parser.parse(remoteFile)
            except nbformat.NotJSONError:
                errMsg = errMsg + """The Remote notebook
                    contains invalid JSON data. <br/>"""

            localFile.close()
            baseFile.close()
            remoteFile.close()

        if len(errMsg) == 0:
            mergedNotebook = notebook_merge(nb_local, nb_base, nb_remote)

            # bitarray used to convert notebook to binary for BLOB
            ba = bitarray.bitarray()
            ba.fromstring(json.dumps(mergedNotebook, indent=2))

            # object to be saved to database
            obj = nbdiffModel(ba.to01())

            # add to database and commit it.
            try:
                db_session.add(obj)
                db_session.commit()
            except OperationalError:
                db_session.rollback()
                print """The database is not initialized.
                    Please restart server with argument init_db"""
                errMsg = """There was an error with the database. <br/>
                   Please contact administrator to resolve this issue."""
                return render_template('Error.html', err=errMsg)
            except:
                db_session.rollback()
                errMsg = """There was an unexpected error with the database.
                    <br/>Please try again later. <br/>
                    If this problem persists please contact administrator."""
                return render_template('Error.html', err=errMsg)

            # return the id of the object.
            nb_id = obj.id

            # redirect is used because we want users
            # to have a easier url to return to.
            return redirect("/Comparison/" + str(nb_id), code=302)
        else:
            return render_template('Error.html', err=errMsg)