Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #240 from SalientCRGT-FEC/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
rsanchez-s authored Jan 19, 2021
2 parents ad76b72 + 79449a4 commit 94dfac8
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 95 deletions.
5 changes: 3 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@
FECFILE_UTIL_PRINT_API_URL = os.environ.get('FECFILE_UTIL_URL', 'https://dev-efile-api.efdev.fec.gov/printpdf')
FECFILE_UTIL_API_VERSION = "/v1/fecfileutil"

NXG_FEC_PARSER_API_URL = os.environ.get('PARSER_URL', 'https://dev-efile-api.efdev.fec.gov/parser')
# NXG_FEC_PARSER_API_URL = os.environ.get('PARSER_URL', 'http://localhost:8080')
NXG_FEC_PARSER_API_URL = os.environ.get('PARSER_URL', 'https://dev-efile-api.efdev.fec.gov/receiver')
# NXG_FEC_PARSER_API_URL = os.environ.get('PARSER_URL', 'http://localhost:8090')
NXG_FEC_PARSER_API_VERSION = "/v1"


# SQS Details
DATA_RECEIVER_SQS_QUEUE = os.environ.get('DATA_RECEIVER_SQS_QUEUE', 'datareceiver-dev')
VALIDATION_SQS_QUEUE = os.environ.get('VALIDATION_SQS_QUEUE', 'validator-dev')
IMAGE_NUMBER_SQS_QUEUE = os.environ.get('IMAGE_NUMBER_SQS_QUEUE', 'imaging')
# IMAGE_NUMBER_SQS_QUEUE = os.environ.get('IMAGE_NUMBER_SQS_QUEUE', 'validator-dev')
IMAGE_GENERATOR_SQS_QUEUE = os.environ.get('IMAGE_GENERATOR_SQS_QUEUE', 'imaging-generator')
# Timeout for the message in queue, after the set time interval other process will see it
MESSAGE_VISIBILITY_TIMEOUT = 30 # setting it to 0 for testing
4 changes: 3 additions & 1 deletion routes/src/f3x/form3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject
from routes.src import tmoflask, utils, common, form
from routes.src.utils import md5_for_text, md5_for_file, directory_files, merge, error
from routes.src.utils import md5_for_text, md5_for_file, directory_files, merge, error, delete_directory
from routes.src.f3x.helper import (
calculate_page_count,
calculate_sh3_page_count,
Expand Down Expand Up @@ -197,6 +197,8 @@ def print_pdftk(
# return flask.jsonify(**envelope), status_code
#

# deleting directory if it exists and has any content
delete_directory(md5_directory)
if not page_count and not paginate:
os.makedirs(md5_directory, exist_ok=True)

Expand Down
22 changes: 15 additions & 7 deletions routes/src/form1m.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from flask import request, current_app
from flask_api import status
from routes.src import common
from routes.src.utils import md5_for_text, md5_for_file, error
from routes.src.utils import md5_for_text, md5_for_file, error, delete_directory


# stamp_print is a flag that will be passed at the time of submitting a report.
Expand Down Expand Up @@ -93,6 +93,10 @@ def print_pdftk(
md5_directory = current_app.config["OUTPUT_DIR_LOCATION"].format(
json_file_md5
)

# deleting directory if it exists and has any content
delete_directory(md5_directory)

os.makedirs(md5_directory, exist_ok=True)
infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("F1M")
outfile = md5_directory + json_file_md5 + "_temp.pdf"
Expand Down Expand Up @@ -179,13 +183,17 @@ def print_pdftk(
extraArgs = {"ContentType": "application/pdf", "ACL": "public-read"}

if silent_print:
response["pdf_url"] = current_app.config['S3_FILE_URL'] + rep_id + '.pdf'
response["pdf_url"] = (
current_app.config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"],
rep_id + ".pdf",
)

s3.upload_file(
md5_directory + 'all_pages.pdf',
current_app.config['AWS_FECFILE_COMPONENTS_BUCKET_NAME'],
current_app.config['AWS_FECFILE_OUTPUT_DIRECTORY'] + '/' +
str(rep_id) + '.pdf',
ExtraArgs=extraArgs)
md5_directory + str(f1m_data["reportId"]) + "/F1M.pdf",
current_app.config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"],
rep_id + ".pdf",
ExtraArgs=extraArgs,
)
else:
response["pdf_url"] = (
current_app.config["PRINT_OUTPUT_FILE_URL"].format(
Expand Down
22 changes: 15 additions & 7 deletions routes/src/form24.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from flask import request, current_app
from flask_api import status
from routes.src import common
from routes.src.utils import md5_for_text, md5_for_file, error
from routes.src.utils import md5_for_text, md5_for_file, error, delete_directory
from routes.src.f3x.helper import calculate_page_count, map_txn_img_num

name_list = ["LastName", "FirstName", "MiddleName", "Prefix", "Suffix"]
Expand Down Expand Up @@ -93,6 +93,10 @@ def print_pdftk(
f24_json = json.loads(file_content)

md5_directory = current_app.config["OUTPUT_DIR_LOCATION"].format(json_file_md5)

# deleting directory if it exists and has any content
delete_directory(md5_directory)

os.makedirs(md5_directory, exist_ok=True)

# setting timestamp and imgno to empty as these needs to show up after submission
Expand Down Expand Up @@ -347,13 +351,17 @@ def print_pdftk(
extraArgs = {"ContentType": "application/pdf", "ACL": "public-read"}

if silent_print:
response["pdf_url"] = current_app.config['S3_FILE_URL'] + rep_id + '.pdf'
response["pdf_url"] = (
current_app.config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"],
rep_id + ".pdf",
)

s3.upload_file(
md5_directory + 'all_pages.pdf',
current_app.config['AWS_FECFILE_COMPONENTS_BUCKET_NAME'],
current_app.config['AWS_FECFILE_OUTPUT_DIRECTORY'] + '/' +
str(rep_id) + '.pdf',
ExtraArgs=extraArgs)
md5_directory + reportId + "/F24.pdf",
current_app.config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"],
rep_id + ".pdf",
ExtraArgs=extraArgs,
)
else:
response["pdf_url"] = (
current_app.config["PRINT_OUTPUT_FILE_URL"].format(json_file_md5)
Expand Down
168 changes: 90 additions & 78 deletions routes/src/schedules/sb_schedule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import pypdftk
import sys
import traceback
import logging

from flask import current_app
from os import path
Expand All @@ -18,86 +21,95 @@ def print_sb_line(
total_no_of_pages,
image_num=None,
):
try:
if sb_list:
last_page_cnt = 3 if len(sb_list) % 3 == 0 else len(sb_list) % 3
schedule_total = 0
os.makedirs(md5_directory + "SB/" + line_number, exist_ok=True)
sb_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("SB")

for page_num in range(page_cnt):
current_page_num += 1
memo_array = []
last_page = False
schedule_page_dict = {}
schedule_page_dict["lineNumber"] = line_number
schedule_page_dict["pageNo"] = current_page_num
schedule_page_dict["totalPages"] = total_no_of_pages

if image_num:
schedule_page_dict["IMGNO"] = image_num
image_num += 1

page_start_index = page_num * 3
if page_num + 1 == page_cnt:
last_page = True

# This call prepares data to render on PDF
build_sb_per_page_schedule_dict(
last_page,
last_page_cnt,
page_start_index,
schedule_page_dict,
sb_list,
memo_array,
)
try:
schedule_total += float(schedule_page_dict["pageSubtotal"])

if page_cnt == page_num + 1:
schedule_page_dict["scheduleTotal"] = "{0:.2f}".format(schedule_total)
schedule_page_dict["committeeName"] = f3x_data["committeeName"]
sb_outfile = (
md5_directory + "SB/" + line_number + "/page_" + str(page_num) + ".pdf"
)
pypdftk.fill_form(sb_infile, schedule_page_dict, sb_outfile)

# Memo text changes and build memo pages and return updated current_page_num
current_page_num, image_num = build_memo_page(
memo_array,
md5_directory,
line_number,
current_page_num,
page_num,
total_no_of_pages,
sb_outfile,
name="SB",
image_num=image_num,
)

if sb_list:
last_page_cnt = 3 if len(sb_list) % 3 == 0 else len(sb_list) % 3
schedule_total = 0
os.makedirs(md5_directory + "SB/" + line_number, exist_ok=True)
sb_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("SB")

for page_num in range(page_cnt):
current_page_num += 1
memo_array = []
last_page = False
schedule_page_dict = {}
schedule_page_dict["lineNumber"] = line_number
schedule_page_dict["pageNo"] = current_page_num
schedule_page_dict["totalPages"] = total_no_of_pages

if image_num:
schedule_page_dict["IMGNO"] = image_num
image_num += 1

page_start_index = page_num * 3
if page_num + 1 == page_cnt:
last_page = True

# This call prepares data to render on PDF
build_sb_per_page_schedule_dict(
last_page,
last_page_cnt,
page_start_index,
schedule_page_dict,
sb_list,
memo_array,
)

schedule_total += float(schedule_page_dict["pageSubtotal"])

if page_cnt == page_num + 1:
schedule_page_dict["scheduleTotal"] = "{0:.2f}".format(schedule_total)
schedule_page_dict["committeeName"] = f3x_data["committeeName"]
sb_outfile = (
md5_directory + "SB/" + line_number + "/page_" + str(page_num) + ".pdf"
)
pypdftk.fill_form(sb_infile, schedule_page_dict, sb_outfile)

# Memo text changes and build memo pages and return updated current_page_num
current_page_num, image_num = build_memo_page(
memo_array,
md5_directory,
line_number,
current_page_num,
page_num,
total_no_of_pages,
sb_outfile,
name="SB",
image_num=image_num,
)

pypdftk.concat(
directory_files(md5_directory + "SB/" + line_number + "/"),
md5_directory + "SB/" + line_number + "/all_pages.pdf",
)
if path.isfile(md5_directory + "SB/all_pages.pdf"):
pypdftk.concat(
[
md5_directory + "SB/all_pages.pdf",
pypdftk.concat(
directory_files(md5_directory + "SB/" + line_number + "/"),
md5_directory + "SB/" + line_number + "/all_pages.pdf",
],
md5_directory + "SB/temp_all_pages.pdf",
)
os.rename(
md5_directory + "SB/temp_all_pages.pdf",
md5_directory + "SB/all_pages.pdf",
)
else:
os.rename(
md5_directory + "SB/" + line_number + "/all_pages.pdf",
md5_directory + "SB/all_pages.pdf",
)

return current_page_num, image_num
)
if path.isfile(md5_directory + "SB/all_pages.pdf"):
pypdftk.concat(
[
md5_directory + "SB/all_pages.pdf",
md5_directory + "SB/" + line_number + "/all_pages.pdf",
],
md5_directory + "SB/temp_all_pages.pdf",
)
os.rename(
md5_directory + "SB/temp_all_pages.pdf",
md5_directory + "SB/all_pages.pdf",
)
else:
os.rename(
md5_directory + "SB/" + line_number + "/all_pages.pdf",
md5_directory + "SB/all_pages.pdf",
)
except:
logging.error('**** Start - Error inside if condition ****')
# printing stack trace
traceback.print_exception(*sys.exc_info())
logging.error('**** End - Error inside if condition ****')


return current_page_num, image_num
except:
# printing stack trace
traceback.print_exception(*sys.exc_info())


# This method builds data for individual SB page
Expand Down
8 changes: 8 additions & 0 deletions routes/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import flask


from shutil import rmtree
from flask_api import status
from routes.src import common

Expand Down Expand Up @@ -42,3 +44,9 @@ def error(msg):
envelope = common.get_return_envelope("false", msg)
status_code = status.HTTP_400_BAD_REQUEST
return flask.jsonify(**envelope), status_code


# delete directory if it exists
def delete_directory(dir_name):
if os.path.isdir(dir_name) and os.listdir(dir_name):
rmtree(dir_name)

0 comments on commit 94dfac8

Please sign in to comment.