Adeel 4 years ago
parent
commit
b974a72cfb

+ 25 - 14
README.md

@@ -1,9 +1,5 @@
 # PDF Comparison Microservice
 
-### Root Folder
-
-Please go into pdf_comaprison_flask_api folder. 
-
 ### Installation
 
 ```
@@ -11,11 +7,26 @@ pip install requirements.txt
 ```
 
 ### Quick Start
+First we need to set Flask in our environment
+
+For Windows ```set FLASK_APP=app.py```
 
-To run the microservice, simply use the command
+For Linux ``` export FLASK_APP=app.py```
+
+Now for Database creation
+```
+flask db init
+```
+```
+flask db migrate
+```
+```
+flask db upgrade
+```
 
+To run the microservice, simply use these command
 ```
-python app.py
+flask run
 ```
 
 The app will start running on server ```127.0.0.1:5000```
@@ -46,14 +57,14 @@ This section contains three fields
 * Click on ```Delete``` link data will be deleted from database and updated in the below table.
 
 ### API ENDPOINTS
-* Upload Reference File : http://127.0.0.1:5000/upload_reference_ajax
-* View Reference Files : http://127.0.0.1:5000/upload_reference_ajax
-* Update Reference File : http://127.0.0.1:5000/update_reference_ajax
-* Delete Reference File : http://127.0.0.1:5000/upload_reference_ajax
-* Upload Original File : http://127.0.0.1:5000/upload_original_ajax
-* View Original Files : http://127.0.0.1:5000/upload_original_ajax
-* Delete Original File : http://127.0.0.1:5000/upload_original_ajax
-* Compare Files : http://127.0.0.1:5000/comparison?uuid1=&uuid2=
+* Upload Reference File : http://127.0.0.1:5000/upload_reference
+* View Reference Files : http://127.0.0.1:5000/upload_reference
+* Update Reference File : http://127.0.0.1:5000/update_reference
+* Delete Reference File : http://127.0.0.1:5000/upload_reference
+* Upload Original File : http://127.0.0.1:5000/upload_original
+* View Original Files : http://127.0.0.1:5000/upload_original
+* Delete Original File : http://127.0.0.1:5000/upload_original
+* Compare Files : http://127.0.0.1:5000/comparison
 
 
 ### Screenshots

+ 22 - 13
api_frontend/index.html

@@ -39,8 +39,9 @@
                             
                             <textarea name="description" class="form-control" id="exampleFormControlTextarea2" rows="3" placeholder="Enter description" style="margin-top: 10px;"></textarea>
                             
-                            <div class="form-group">
-                                <input id='taginput' type="text" class="form-control" name="planets", placeholder="Enter Regex" style="margin-top: 10px;">
+                            <div class="form-group inner-addon left-addon">
+                                <i class="glyphicon glyphicon-user"></i>
+                                <input id='taginput' type="text" class="form-control" name="regex", placeholder="Enter Regex" style="margin-top: 10px;">
                             </div>
         
                         </div>
@@ -116,7 +117,7 @@
             var URL = 'http://127.0.0.1:5000';
             
             $(document).ready(function(){
-                $('input[name="planets"]').amsifySuggestags({
+                $('input[name="regex"]').amsifySuggestags({
                     suggestions: [], //'Mercury', 'Venus', 'Earth', 'Mars', 'Jupitor', 'Uranus', 'Neptune', 'Pluto'
                 });
             });
@@ -140,14 +141,14 @@
                 var uuid = data_tables_reference.row( $(this).parents('tr')).data()[0];
                 var json_text = {'uuid':uuid};
                 $.ajax({
-                    url: URL + '/upload_reference_ajax',
+                    url: URL + '/upload_reference',
                     type:'POST',
                     data:JSON.stringify(json_text),
                     contentType: 'application/json;charset=UTF-8',
                     success: function() {
                         console.log('deleted')
                         $.ajax({
-                            url: URL + '/upload_reference_ajax',
+                            url: URL + '/upload_reference',
                             type:'GET',
                             success: function(data) {
                                 var newOptionsHtml = '<option selected="true" disabled="disabled">Select Reference File UUID</option>';
@@ -188,7 +189,7 @@
             
                     $.ajax({
                         type: 'POST',
-                        url: URL + '/update_reference_ajax',
+                        url: URL + '/update_reference',
                         data: form_data,
                         contentType: false,
                         cache: false,
@@ -201,7 +202,7 @@
                             data_tables_reference.clear().draw();
         
                             $.ajax({
-                                url: URL + '/upload_reference_ajax',
+                                url: URL + '/upload_reference',
                                 type:'GET',
                                 success: function(data) {
                                     $.each(data, function (key, item) {
@@ -228,7 +229,7 @@
                 var uuid = data_tables_original.row( $(this).parents('tr')).data()[0];
                 var json_text = {'uuid':uuid};
                 $.ajax({
-                    url: URL + '/upload_original_ajax',
+                    url: URL + '/upload_original',
                     type:'POST',
                     data:JSON.stringify(json_text),
                     contentType: 'application/json;charset=UTF-8',
@@ -246,7 +247,7 @@
             $(document).ready(function() {
                 $("#msg").hide();
                 $.ajax({
-                    url: URL + '/upload_reference_ajax',
+                    url: URL + '/upload_reference',
                     type:'GET',
                     success: function(data) {
                         var newOptionsHtml = '<option selected="true" disabled="disabled">Select Reference File UUID</option>';
@@ -266,7 +267,7 @@
             $(document).ready(function() {
                 $("#msg").hide();
                 $.ajax({
-                    url: URL + '/upload_original_ajax',
+                    url: URL + '/upload_original',
                     type:'GET',
                     success: function(data) {
                            
@@ -289,7 +290,7 @@
                     
                     $.ajax({
                         type: 'POST',
-                        url: URL + '/upload_reference_ajax',
+                        url: URL + '/upload_reference',
                         data: form_data,
                         contentType: false,
                         cache: false,
@@ -306,7 +307,7 @@
                               
                             });
                             $.ajax({
-                                url: URL + '/upload_reference_ajax',
+                                url: URL + '/upload_reference',
                                 type:'GET',
                                 success: function(data) {
                                     var newOptionsHtml = '<option selected="true" disabled="disabled">Select Reference File UUID</option>';
@@ -340,7 +341,7 @@
 
                     $.ajax({
                         type: 'POST',
-                        url: URL + '/upload_original_ajax',
+                        url: URL + '/upload_original',
                         data: form_data1,
                         contentType: false,
                         cache: false,
@@ -378,6 +379,14 @@
                             });
                             $("#compare_link").trigger("click");
 
+                            // $.ajax({
+                            //     url: URL + '/comparison_results',
+                            //     type:'GET',
+                            //     success: function(data) {
+                            //         console.log(data.diff_file_1);
+                            //         console.log(data.diff_file_2);
+                            //     }
+                            // });
 
                         },
                         statusCode: {

+ 142 - 167
pdf_comaprison_flask_api/app.py

@@ -1,4 +1,4 @@
-from flask import Flask,request,jsonify,render_template,send_file,Response,redirect,url_for
+from flask import Flask,request,jsonify,render_template,send_file,Response
 import base64
 from flask_bootstrap import Bootstrap
 import sqlite3
@@ -13,89 +13,81 @@ import uuid
 import sys
 import os
 from pathlib import Path
+import logging
+from flask_migrate import Migrate
+from config import Config
+from flask_sqlalchemy import SQLAlchemy
 
 ALLOWED_EXTENSIONS = {'pdf'}
 app = Flask(__name__)
+
+app.config.from_object(Config)
+db = SQLAlchemy(app)
+migrate = Migrate(app, db)
+
+from models import ReferenceFile, OriginalFile
+
 CORS(app)
 Bootstrap(app)
 app.secret_key = '12345'
+
+
 def allowed_file(filename):
     return '.' in filename and \
            filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
 
-@app.route('/upload_reference_ajax', methods=['GET', 'POST'])
+@app.route('/upload_reference', methods=['GET', 'POST'])
 def upload_reference_file():
     uu_id = uuid.uuid1()
-    sqliteConnection = sqlite3.connect('baangt.db')
-    cursor = sqliteConnection.cursor()
-    print("Connected to SQLite")
-    cursor.execute('''CREATE TABLE IF NOT EXISTS reference_file (
-                        UUID NOT NULL,
-                        reference_pdf_name NOT NULL, 
-                        reference_pdf text NOT NULL,
-                        description text NOT NULL 
-                    );''')
-    query = """ INSERT INTO reference_file
-                                  (UUID,reference_pdf_name,reference_pdf, description) VALUES (?,?,?,?)"""
     #### POST REQUEST FOR UPLOAD, DELETE
     if request.method == 'POST':
         #### UPLOAD
         if request.files:
-            print('sdddssd')
             desc = request.form['description']
+            regex = request.form['regex']
             file_ref = request.files['reference']
             files_json = [{'file_ref':file_ref.filename,'uuid':str(uu_id.int), 'desc':desc}]
-            print(files_json)
-            # print(allowed_file(file_orig.filename))
             try:
                 if  allowed_file(file_ref.filename):
                     blob_ref = base64.b64encode(file_ref.read())
-                    data_tuple = (str(uu_id.int),file_ref.filename,blob_ref,desc)
-                    cursor.execute(query,data_tuple)
-                    sqliteConnection.commit()
-                    cursor.close()
+                    ref = ReferenceFile(
+                        uuid=str(uu_id.int), 
+                        reference_pdf_name=file_ref.filename,
+                        reference_pdf=blob_ref,
+                        description=desc,
+                        regex=regex)
+                    db.session.add(ref)
+                    db.session.commit()
                     return jsonify(files_json)
                 else:
                     return Response("All fields must be selected", status=400, mimetype='application/json')
-            except Exception as e:
-                exc_type, exc_obj, exc_tb = sys.exc_info()
-                fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
-                print(exc_type, fname, exc_tb.tb_lineno)
-                print(e)
+            except Exception:
+                # exc_type, exc_obj, exc_tb = sys.exc_info()
+                # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
+                # print(exc_type, fname, exc_tb.tb_lineno)
+                logging.error("Exception occurred", exc_info=True)
                 return Response("Error in uploading", status=400, mimetype='application/json')
         #### DELETE
         else:
             uuid_value = request.json['uuid']
-            print(str(uuid_value))
-            sql = 'Delete from reference_file where UUID = "{}"'.format(str(uuid_value))
-            print(sql)
-            cursor.execute(sql)
-            sqliteConnection.commit()
-            return jsonify('') #Response("Deleted", mimetype='application/json')
+            ref = ReferenceFile.query.filter_by(uuid = uuid_value).one()
+            db.session.delete(ref)
+            db.session.commit()
+            return jsonify('')
     #### GET REQUEST FOR GET RESULTS
     else:
         try:
-            cursor.execute('Select UUID, reference_pdf_name, description from reference_file')
-            db_data = [i for i in cursor.fetchall()]
-            print(db_data)
-            cursor.close()
-            # print(db_data)
+            refs = ReferenceFile.query.all()
+            db_data = []
+            for ref in refs:
+                db_data.append((ref.uuid, ref.reference_pdf_name, ref.description))
+            
             return jsonify(db_data)
-        except:
+        except Exception:
             return jsonify('')
-    # return render_template('index.html')
 
-@app.route('/update_reference_ajax', methods=['GET', 'POST'])
+@app.route('/update_reference', methods=['GET', 'POST'])
 def update_reference_file():
-    
-    sqliteConnection = sqlite3.connect('baangt.db')
-    cursor = sqliteConnection.cursor()
-    print("Connected to SQLite")
-    
-    query = """ UPDATE reference_file
-                SET reference_pdf_name = :name , reference_pdf = :file , description = :desc
-                WHERE UUID = :uuid        
-            """
     #### POST REQUEST FOR UPDATE
     if request.method == 'POST':
         if request.files:
@@ -104,46 +96,34 @@ def update_reference_file():
                 desc = request.form['desc']
                 file_ref = request.files['reference']
                 files_json = [{'file_ref':file_ref.filename,'uuid':str(uuid),'desc':desc}]
-                # print(files_json)
-                # print(allowed_file(file_orig.filename))
                 try:
                     if  allowed_file(file_ref.filename):
                         blob_ref = base64.b64encode(file_ref.read())
-                        data_tuple = (file_ref.filename,blob_ref,str(uuid))
-                        data = {'name' : file_ref.filename, 'file' : blob_ref, 'desc' : desc, 'uuid' : str(uuid)}
-                        cursor.execute(query,data)
-                        sqliteConnection.commit()
-                        cursor.close()
+                        
+                        ref = ReferenceFile.query.filter_by(uuid = uuid).one()
+                        ref.reference_pdf_name = file_ref.filename
+                        ref.reference_pdf = blob_ref
+                        ref.description = desc
+                        db.session.commit()
+                        
                         return jsonify(files_json)
                     else:
                         return Response("All fields must be selected", status=400, mimetype='application/json')
-                except Exception as e:
-                    exc_type, exc_obj, exc_tb = sys.exc_info()
-                    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
-                    print(exc_type, fname, exc_tb.tb_lineno)
-                    print(e)
+                except Exception:
+                    # exc_type, exc_obj, exc_tb = sys.exc_info()
+                    # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
+                    # print(exc_type, fname, exc_tb.tb_lineno)
+                    logging.error("Exception occurred", exc_info=True)
                     return Response("Error in uploading", status=400, mimetype='application/json')
-            except:
+            except Exception:
                 return Response("All fields must be selected", status=400, mimetype='application/json')
     else:
         return Response("Bad request", status=400, mimetype='application/json')    
-    # return render_template('index.html')
 
-@app.route('/upload_original_ajax', methods=['GET', 'POST'])
+
+@app.route('/upload_original', methods=['GET', 'POST'])
 def upload_original_file():
     uu_id = uuid.uuid1()
-    sqliteConnection = sqlite3.connect('baangt.db')
-    cursor = sqliteConnection.cursor()
-    print("Connected to SQLite")
-    cursor.execute('''CREATE TABLE IF NOT EXISTS original_file (
-        UUID NOT NULL,
-        original_pdf_name NOT NULL, 
-        original_pdf text NOT NULL,
-        description text NOT NULL,
-        reference_uuid NOT NULL
-    );''')
-    query = """ INSERT INTO original_file
-                                  (UUID,original_pdf_name,original_pdf, description, reference_uuid) VALUES (?,?,?,?,?)"""
     if request.method == 'POST':
         if request.files:
             try:
@@ -151,152 +131,142 @@ def upload_original_file():
                 ref_uuid = request.form['reference_uuid']
                 file_orig = request.files['original']
                 files_json = [{'file_orig':file_orig.filename,'uuid':str(uu_id.int), 'desc':desc, 'ref_uuid':ref_uuid}]
-                print(files_json)
-                # print(allowed_file(file_orig.filename))
-            # try:
                 if  allowed_file(file_orig.filename):
                     blob_orig = base64.b64encode(file_orig.read())
-                    data_tuple = (str(uu_id.int),file_orig.filename,blob_orig, desc, ref_uuid)
-                    cursor.execute(query,data_tuple)
-                    sqliteConnection.commit()
-                    cursor.close()
+                    orig = OriginalFile(
+                        uuid=str(uu_id.int), 
+                        original_pdf_name=file_orig.filename,
+                        original_pdf=blob_orig,
+                        description=desc,
+                        uuid_ref=ref_uuid)
+                    db.session.add(orig)
+                    db.session.commit()
                     return jsonify(files_json)
                 else:
                     return Response("All fields must be selected", status=422, mimetype='application/json')
-            except Exception as e:
-                exc_type, exc_obj, exc_tb = sys.exc_info()
-                fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
-                print(exc_type, fname, exc_tb.tb_lineno)
-                print(e)
+            except Exception:
+                # exc_type, exc_obj, exc_tb = sys.exc_info()
+                # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
+                # print(exc_type, fname, exc_tb.tb_lineno)
+                logging.error("Exception occurred", exc_info=True)
                 return Response("All fields must be selected", status=422, mimetype='application/json')
         else:
             uuid_value = request.json['uuid']
-            print(str(uuid_value))
-            sql = 'Delete from original_file where UUID = "{}"'.format(str(uuid_value))
-            print(sql)
-            cursor.execute(sql)
-            sqliteConnection.commit()
+            orig = OriginalFile.query.filter_by(uuid = uuid_value).one()
+            db.session.delete(orig)
+            db.session.commit()
             return jsonify('')
     else:
         try:
-            cursor.execute('Select UUID, original_pdf_name, description, reference_uuid from original_file')
-            db_data = [i for i in cursor.fetchall()]
-            print(db_data)
-            cursor.close()
-            # print(db_data)
+            origs = OriginalFile.query.all()
+            db_data = []
+            for orig in origs:
+                db_data.append((orig.uuid, orig.original_pdf_name, orig.description, orig.uuid_ref))
+            
             return jsonify(db_data)
-        except:
+        except Exception:
             return jsonify('')
-    # return render_template('index.html')
+    
 
 @app.route('/comparison',methods=['POST', 'GET'])
 def comparison_():
     if request.method == 'GET':
         try:
-            root_dir = Path.cwd()
+            basedir = Path.cwd()
             uuid1 = request.args.get('uuid1', None)
             uuid2 = request.args.get('uuid2', None)
-            print(uuid1,uuid2)
-            
-            sqliteConnection = sqlite3.connect('baangt.db')
-            cursor = sqliteConnection.cursor()
             
-            orig_sql = 'Select original_pdf from original_file where UUID = "{}"'.format(str(uuid1))
-            cursor.execute(orig_sql)
-            blob = cursor.fetchone()
-            blob_orig = base64.b64decode(blob[0])
+            orig = OriginalFile.query.filter_by(uuid = uuid1).one()
+            blob = orig.original_pdf
+            blob_orig = base64.b64decode(blob)
 
-            with open(root_dir.joinpath('temp', 'temp_orig.pdf'), 'wb') as f:
+            with open(basedir.joinpath('temp', 'temp_orig.pdf'), 'wb') as f:
                 f.write(blob_orig)
 
-            ref_sql = 'Select reference_pdf from reference_file where UUID = "{}"'.format(str(uuid2))
-            cursor.execute(ref_sql)
-            blob = cursor.fetchone()
-            blob_ref = base64.b64decode(blob[0])
+            ref = ReferenceFile.query.filter_by(uuid = uuid2).one()
+            blob = ref.reference_pdf
+            blob_ref = base64.b64decode(blob)
 
-            with open(root_dir.joinpath('temp', 'temp_ref.pdf'), 'wb') as f:
+            with open(basedir.joinpath('temp', 'temp_ref.pdf'), 'wb') as f:
                 f.write(blob_ref)
 
 
-            input_file1 = root_dir.joinpath('temp', 'temp_orig.pdf')
-            input_file2 = root_dir.joinpath('temp', 'temp_ref.pdf')
+            input_file1 = Path() / 'temp' / 'temp_orig.pdf'
+            input_file2 = Path() / 'temp' / 'temp_ref.pdf'
 
             output_file1 = Path() / 'output' / 'Original_file.pdf'
             output_file2 = Path() / 'output' / 'Reference_file.pdf'
 
-            print('Comparing files ', input_file1, ' and ', input_file2, '.....')
+            msg = 'Comparing files ' + str(input_file1) + ' and ' + str(input_file2) + '.....'
+            logging.info(msg)
 
-            fullText1 = ""
-
-            pdfFileObj = open(input_file1, 'rb')
+            pdf_file_obj = open(input_file1, 'rb')
 
             #The pdfReader variable is a readable object that will be parsed
-            pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
+            pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)
 
             #discerning the number of pages will allow us to parse through all #the pages
-            num_pages = pdfReader.numPages
+            num_pages = pdf_reader.numPages
             count = 0
             text = ""
             pages1 = []
 
             #The while loop will read each page
             while count < num_pages:
-                pageObj = pdfReader.getPage(count)
+                page_obj = pdf_reader.getPage(count)
                 count +=1
-                temp = pageObj.extractText()
+                temp = page_obj.extractText()
                 text += temp
                 pages1.append(temp)
 
-            fullText1 = text
-            fullText1 = fullText1.replace('\n', ' ')
-            fullText1 = fullText1.replace(' \n', ' ')
-            fullText1 = re.sub(' +', ' ', fullText1)
+            full_text1 = text
+            full_text1 = full_text1.replace('\n', ' ')
+            full_text1 = full_text1.replace(' \n', ' ')
+            full_text1 = re.sub(' +', ' ', full_text1)
 
             while True:
                 try:
-                    inz = fullText1.index('Seite')
-                    temp = ' '.join(fullText1[inz:].split()[:4])
-                    fullText1 = fullText1.replace(temp, '')
-                except:
+                    inz = full_text1.index('Seite')
+                    temp = ' '.join(full_text1[inz:].split()[:4])
+                    full_text1 = full_text1.replace(temp, '')
+                except Exception:
                     break
 
-            fullText2 = ""
-
-            pdfFileObj = open(input_file2, 'rb')
+            pdf_file_obj = open(input_file2, 'rb')
 
             #The pdfReader variable is a readable object that will be parsed
-            pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
+            pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)
 
             #discerning the number of pages will allow us to parse through all #the pages
-            num_pages = pdfReader.numPages
+            num_pages = pdf_reader.numPages
             count = 0
             text = ""
             pages2 = []
 
             #The while loop will read each page
             while count < num_pages:
-                pageObj = pdfReader.getPage(count)
+                page_obj = pdf_reader.getPage(count)
                 count +=1
-                temp = pageObj.extractText()
+                temp = page_obj.extractText()
                 text += temp
                 pages2.append(temp)
 
 
-            fullText2 = text
-            fullText2 = fullText2.replace('\n', ' ')
-            fullText2 = fullText2.replace(' \n', ' ')
-            fullText2 = re.sub(' +', ' ', fullText2)
+            full_text2 = text
+            full_text2 = full_text2.replace('\n', ' ')
+            full_text2 = full_text2.replace(' \n', ' ')
+            full_text2 = re.sub(' +', ' ', full_text2)
 
             while True:
                 try:
-                    inz = fullText2.index('Seite')
-                    temp = ' '.join(fullText2[inz:].split()[:4])
-                    fullText2 = fullText2.replace(temp, '')
-                except:
+                    inz = full_text2.index('Seite')
+                    temp = ' '.join(full_text2[inz:].split()[:4])
+                    full_text2 = full_text2.replace(temp, '')
+                except Exception:
                     break
 
-            str1 = fullText1
-            str2 = fullText2
+            str1 = full_text1
+            str2 = full_text2
 
             delta = difflib.Differ().compare(str1.split(), str2.split())
             # difflist = []
@@ -331,7 +301,7 @@ def comparison_():
             for match in matches:
                 sen = one_text[match.a:match.a + match.size]
                 if len(sen) > 6:
-                    # print(sen)
+                    
                     one_final = one_final.replace(sen, ' ', 1)
                     two_final = two_final.replace(sen, ' ', 1)
 
@@ -342,11 +312,15 @@ def comparison_():
             for match in matches:
                 sen = two_text[match.a:match.a + match.size]
                 if len(sen) > 6:
-                    # print(sen)
+                    
                     one_final = one_final.replace(sen, ' ', 1)
                     two_final = two_final.replace(sen, ' ', 1)
 
-            print('Generating', output_file1, '.....')
+            
+            msg = 'Generating ' + str(output_file1) + '.....'
+            logging.info(msg)
+
+
             one_list = one_final.split()
 
             doc1 = fitz.open(input_file1)
@@ -370,11 +344,13 @@ def comparison_():
                         for inst in text_instances:
                             highlight = page.addHighlightAnnot(inst)
                             break
-            except:
+            except Exception:
                 pass
             doc1.save(output_file1, garbage=4, deflate=True, clean=True)
 
-            print('Generating', output_file2, '.....')
+            
+            msg = 'Generating ' + str(output_file2) + '.....'
+            logging.info(msg)
             two_list = two_final.split()
 
             # for i, page in enumerate(pages1):
@@ -399,31 +375,30 @@ def comparison_():
                         for inst in text_instances:
                             highlight = page.addHighlightAnnot(inst)
                             break
-            except:
+            except Exception:
                 pass
             doc2.save(output_file2, garbage=4, deflate=True, clean=True)
-            zipObj = ZipFile(root_dir.joinpath('output', 'output.zip'), 'w')
-            zipObj.write(output_file1)
-            zipObj.write(output_file2)
-            zipObj.close()
-            print('Finish')
-        except Exception as e:
-
-            print('error in comparison')
-            print(e)
+            zip_obj = ZipFile(basedir.joinpath('output', 'output.zip'), 'w')
+            zip_obj.write(output_file1)
+            zip_obj.write(output_file2)
+            zip_obj.close()
+
+            msg = 'Finish'
+            logging.info(msg)
+        except Exception:
+            logging.error("Exception occurred", exc_info=True)
             return Response("Error in Comparison", status=400, mimetype='application/json')
         
         
-        return send_file(root_dir.joinpath('output', 'output.zip'), as_attachment=True)
+        return send_file(basedir.joinpath('output', 'output.zip'), as_attachment=True)
     else:
-        print('sss')
         return Response("Bad Request", status=400, mimetype='application/json')
 
 
-
 @app.route('/')
 def index():
     return render_template('default.html')
 
 if __name__ == "__main__":
+    logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
     app.run()

+ 9 - 0
pdf_comaprison_flask_api/config.py

@@ -0,0 +1,9 @@
+import os
+from pathlib import Path
+# basedir = os.path.abspath(os.path.dirname(__file__))
+basedir = Path.cwd()
+class Config(object):
+    # ...
+    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
+        'sqlite:///' + str(basedir.joinpath(basedir, 'baangt.db'))
+    SQLALCHEMY_TRACK_MODIFICATIONS = False

+ 22 - 0
pdf_comaprison_flask_api/models.py

@@ -0,0 +1,22 @@
+from api import db
+import datetime
+class ReferenceFile(db.Model):
+    uuid = db.Column(db.String(64), primary_key=True)
+    reference_pdf_name = db.Column(db.String(64), nullable=False)
+    reference_pdf = db.Column(db.LargeBinary, nullable=False)
+    description = db.Column(db.String(250), nullable=False)
+    regex = db.Column(db.String(250), nullable=True) 
+    # original_files = db.relationship('Original_File', backref='reference_pdf', lazy=True)
+    def __repr__(self):
+        return '<UUID {}>'.format(self.uuid)
+
+class OriginalFile(db.Model):
+    uuid = db.Column(db.String(64), primary_key=True)
+    original_pdf_name = db.Column(db.String(64), nullable=False)
+    original_pdf = db.Column(db.LargeBinary, nullable=False)
+    description = db.Column(db.String(250), nullable=False)
+    uuid_ref = db.Column(db.String(64), nullable=False)
+    # timestamp = db.Column(db.DateTime, index=True, default=datetime.datetime.utcnow())
+    # UUID_F = db.Column(db.Integer, db.ForeignKey('reference_file.UUID'), nullable=False)
+    def __repr__(self):
+        return '<UUID {}>'.format(self.uuid)

+ 3 - 1
pdf_comaprison_flask_api/requirements.txt

@@ -44,4 +44,6 @@ traits==6.0.0
 urllib3==1.25.8
 visitor==0.1.3
 Werkzeug==1.0.1
-flask-cors
+flask-cors
+flask_migrate
+flask_sqlalchemy

+ 61 - 14
pdf_comaprison_flask_api/templates/default.html

@@ -1,14 +1,61 @@
-<h1>URLS</h1>
-<h3>
-  <ul>
-      <li>Upload Reference File(POST REQ along with data) : http://127.0.0.1:5000/upload_reference_ajax</li>
-      <li>View Reference Files(GET REQ) : http://127.0.0.1:5000/upload_reference_ajax</li>
-      <li>Update Reference File(POST REQ along with data) : http://127.0.0.1:5000/update_reference_ajax</li>
-      <li>Delete Reference File(POST REQ along with UUID) : http://127.0.0.1:5000/upload_reference_ajax</li>
-      <li>Upload Original File(POST REQ along with data) : http://127.0.0.1:5000/upload_original_ajax</li>
-      <li>View Original Files(GET REQ) : http://127.0.0.1:5000/upload_original_ajax</li>
-      <li>Delete Original File(POST REQ along with UUID) : http://127.0.0.1:5000/upload_original_ajax</li>
-      <li>Compare Files(GET REQ along with UUID) : http://127.0.0.1:5000/comparison?uuid1=&uuid2=</li>
-  </ul>
-</h3>
-
+<!DOCTYPE html><html><head><meta charset="utf-8"><title>PDF Comparison Microservice.md</title><style></style></head><body id="preview">
+  <h1 class="code-line" data-line-start=0 data-line-end=1><a id="PDF_Comparison_Microservice_0"></a>PDF Comparison Microservice</h1>
+  <h3 class="code-line" data-line-start=2 data-line-end=3><a id="Installation_2"></a>Installation</h3>
+  <pre><code class="has-line-data" data-line-start="5" data-line-end="7">pip install requirements.txt
+  </code></pre>
+  <h3 class="code-line" data-line-start=8 data-line-end=9><a id="Quick_Start_8"></a>Quick Start</h3>
+  <p class="has-line-data" data-line-start="9" data-line-end="10">First we need to set Flask in our environment</p>
+  <p class="has-line-data" data-line-start="11" data-line-end="12">For Windows <code>set FLASK_APP=app.py</code></p>
+  <p class="has-line-data" data-line-start="13" data-line-end="14">For Linux <code>export FLASK_APP=app.py</code></p>
+  <p class="has-line-data" data-line-start="15" data-line-end="16">Now for Database creation</p>
+  <pre><code class="has-line-data" data-line-start="17" data-line-end="19">flask db init
+  </code></pre>
+  <pre><code class="has-line-data" data-line-start="20" data-line-end="22">flask db migrate
+  </code></pre>
+  <pre><code class="has-line-data" data-line-start="23" data-line-end="25">flask db upgrade
+  </code></pre>
+  <p class="has-line-data" data-line-start="26" data-line-end="27">To run the microservice, simply use these command</p>
+  <pre><code class="has-line-data" data-line-start="28" data-line-end="30">flask run
+  </code></pre>
+  <p class="has-line-data" data-line-start="31" data-line-end="32">The app will start running on server <code>127.0.0.1:5000</code></p>
+  <p class="has-line-data" data-line-start="33" data-line-end="34">The <code>APP</code> contains two sections</p>
+  <h3 class="code-line" data-line-start=35 data-line-end=36><a id="Upload_Reference_File_35"></a>Upload Reference File</h3>
+  <p class="has-line-data" data-line-start="37" data-line-end="38">This section contains three fields</p>
+  <ul>
+  <li class="has-line-data" data-line-start="38" data-line-end="39">Reference File</li>
+  <li class="has-line-data" data-line-start="39" data-line-end="40">Description</li>
+  <li class="has-line-data" data-line-start="40" data-line-end="42">Regex</li>
+  </ul>
+  <h3 class="code-line" data-line-start=42 data-line-end=43><a id="Actions_42"></a>Actions</h3>
+  <ul>
+  <li class="has-line-data" data-line-start="43" data-line-end="44">Fill in the info. After you upload the form by clicking <code>Upload</code> button, data will be stored in database and updated in the below table.</li>
+  <li class="has-line-data" data-line-start="44" data-line-end="45">Click on <code>Delete</code> link data will be deleted from database and updated in the below table.</li>
+  <li class="has-line-data" data-line-start="45" data-line-end="47">Click on <code>Update</code> link after taht choose a file and data will be updated in database and in below table.</li>
+  </ul>
+  <h3 class="code-line" data-line-start=47 data-line-end=48><a id="Upload_Original_File_47"></a>Upload Original File</h3>
+  <p class="has-line-data" data-line-start="49" data-line-end="50">This section contains three fields</p>
+  <ul>
+  <li class="has-line-data" data-line-start="50" data-line-end="51">Original File</li>
+  <li class="has-line-data" data-line-start="51" data-line-end="52">Description</li>
+  <li class="has-line-data" data-line-start="52" data-line-end="54">UUID of Reference File</li>
+  </ul>
+  <h3 class="code-line" data-line-start=54 data-line-end=55><a id="Actions_54"></a>Actions</h3>
+  <ul>
+  <li class="has-line-data" data-line-start="55" data-line-end="56">Fill in the info. After you upload the form by clicking <code>Upload</code> button, data will be stored in database and updated in the below table, on success it will automatically call the <code>compare</code> function to get the comparison of files.</li>
+  <li class="has-line-data" data-line-start="56" data-line-end="58">Click on <code>Delete</code> link data will be deleted from database and updated in the below table.</li>
+  </ul>
+  <h3 class="code-line" data-line-start=58 data-line-end=59><a id="API_ENDPOINTS_58"></a>API ENDPOINTS</h3>
+  <ul>
+  <li class="has-line-data" data-line-start="59" data-line-end="60">Upload Reference File : <a href="http://127.0.0.1:5000/upload_reference">http://127.0.0.1:5000/upload_reference</a></li>
+  <li class="has-line-data" data-line-start="60" data-line-end="61">View Reference Files : <a href="http://127.0.0.1:5000/upload_reference">http://127.0.0.1:5000/upload_reference</a></li>
+  <li class="has-line-data" data-line-start="61" data-line-end="62">Update Reference File : <a href="http://127.0.0.1:5000/update_reference">http://127.0.0.1:5000/update_reference</a></li>
+  <li class="has-line-data" data-line-start="62" data-line-end="63">Delete Reference File : <a href="http://127.0.0.1:5000/upload_reference">http://127.0.0.1:5000/upload_reference</a></li>
+  <li class="has-line-data" data-line-start="63" data-line-end="64">Upload Original File : <a href="http://127.0.0.1:5000/upload_original">http://127.0.0.1:5000/upload_original</a></li>
+  <li class="has-line-data" data-line-start="64" data-line-end="65">View Original Files : <a href="http://127.0.0.1:5000/upload_original">http://127.0.0.1:5000/upload_original</a></li>
+  <li class="has-line-data" data-line-start="65" data-line-end="66">Delete Original File : <a href="http://127.0.0.1:5000/upload_original">http://127.0.0.1:5000/upload_original</a></li>
+  <li class="has-line-data" data-line-start="66" data-line-end="67">Compare Files : <a href="http://127.0.0.1:5000/comparison">http://127.0.0.1:5000/comparison</a></li>
+  </ul>
+  <h3 class="code-line" data-line-start=69 data-line-end=70><a id="Screenshots_69"></a>Screenshots</h3>
+  <p class="has-line-data" data-line-start="71" data-line-end="72"><img src="https://github.com/mudasar477/Beach/blob/master/a.png" alt="alt Picture"></p>
+  <p class="has-line-data" data-line-start="73" data-line-end="74"><img src="https://github.com/mudasar477/Beach/blob/master/b.png" alt="alt Picture"></p>
+  </body></html>