def more_info(suborder_number: str) -> Response: """ GET: {suborder_number :param suborder_number: :return: json of all the info """ if request.method == 'POST': order_info = SearchFunctions.format_first_result( search_queries(suborder_number=suborder_number, search_type='print')) return jsonify(order_info=order_info), 200
def get_orders() -> Response: """ Retrieves the data for orders to be displayed. If a form is submitted, the parameters including order_number, suborder_number, order_type, billing_name, date_received_start, and date_receieved_end will be retrieved from the form data and used in a function called get_orders_by_fields to filter orders. Else, orders are filtered with today's date. As a user, I want to be able to search for specific orders. GET {order_number, suborder_number, order_type, billing_name, user, date_received_start, date_received_end}, Search functionality should be in utils.py :return {orders, 200} """ if request.method == 'POST': # makes it so we get a post method to receive the info put in on the form json = request.get_json(force=True) order_number = json.get('order_number') suborder_number = json.get('suborder_number') order_type = json.get('order_type') delivery_method = json.get('delivery_method') status = json.get('status') billing_name = json.get('billing_name') date_received_start = json.get('date_received_start') date_received_end = json.get('date_received_end') date_submitted_start = json.get('date_submitted_start') date_submitted_end = json.get('date_submitted_end') start = json.get('start') size = json.get('size') multiple_items = '' if order_type == 'multiple_items': multiple_items = True order_type = 'all' # Overwrite order_type value from 'multiple_items' to 'all' for search orders = search_queries(order_number, suborder_number, order_type, delivery_method, status, billing_name, date_received_start, date_received_end, date_submitted_start, date_submitted_end, multiple_items, start, size) # formatting results formatted_orders = SearchFunctions.format_results(orders) suborder_total = orders['hits']['total'] order_total = orders['aggregations']['order_count']['value'] return jsonify( order_count=order_total, suborder_count=suborder_total, all_orders=formatted_orders, ), 200 else: orders = search_queries( date_received_start=date.today().strftime('%m/%d/%Y')) formatted_orders = SearchFunctions.format_results(orders) suborder_total = orders['hits']['total'] order_total = orders['aggregations']['order_count']['value'] return jsonify( order_count=order_total, suborder_count=suborder_total, all_orders=formatted_orders, ), 200
def generate_csv(search_params: Dict[str, str]) -> str: """Generates CSV from search result. Args: search_params: Dictionary of attributes to search by. Returns: URL string of the CSV. """ order_type = search_params.get('order_type') suborder_results = search_queries( order_number=search_params.get('order_number'), suborder_number=search_params.get('suborder_number'), order_type=order_type, delivery_method=search_params.get('delivery_method'), status=search_params.get('status'), billing_name=search_params.get('billing_name'), date_received_start=search_params.get('date_received_start'), date_received_end=search_params.get('date_received_end'), date_submitted_start=search_params.get('date_submitted_start'), date_submitted_end=search_params.get('date_submitted_end'), search_type='csv', ) filename = 'orders_{}.csv'.format( datetime.now().strftime('%m_%d_%Y_at_%I_%M_%p')) file = open(join(current_app.static_folder, 'files', filename), 'w') writer = csv.writer(file) if order_type == order_types.PHOTOS: writer.writerow([ 'Order Number', 'Suborder Number', 'Date Received', 'Customer Name', 'Phone', 'Email', 'Customer Address', 'Delivery Method', 'Size', 'Copy', 'Image Identifier', 'Building Number', 'Street', 'Collection', 'Borough', 'Block', 'Lot', 'Comment', 'Description', ]) for suborder in suborder_results: writer.writerow([ '="{}"'.format(suborder['_source']['order_number']), suborder['_source']['suborder_number'], suborder['_source'].get('date_received')[:8], suborder['_source'].get('customer')['billing_name'], suborder['_source'].get('customer').get('phone'), suborder['_source'].get('customer').get('email'), suborder['_source'].get('customer').get('address'), suborder['_source'].get('metadata').get('delivery_method'), suborder['_source'].get('metadata').get('size'), suborder['_source'].get('metadata').get('num_copies'), suborder['_source'].get('metadata').get('image_id', ''), suborder['_source'].get('metadata').get('building_number', ''), suborder['_source'].get('metadata').get('street', ''), suborder['_source'].get('metadata').get('collection', ''), suborder['_source'].get('metadata').get('borough', ''), suborder['_source'].get('metadata').get('block', ''), suborder['_source'].get('metadata').get('lot', ''), 'Yes' if suborder['_source'].get('metadata').get('comment') else '', suborder['_source'].get('metadata').get('description', ''), ]) elif order_type == order_types.VITAL_RECORDS: writer.writerow([ 'Order Number', 'Suborder Number', 'Date Received', 'Customer Name', 'Email', 'Delivery Method', 'Certificate Type', 'Certificate Number', 'Borough', 'Years', ]) for suborder in suborder_results: if suborder['_source'].get('metadata') is None: print(suborder) writer.writerow([ '="{}"'.format(suborder['_source']['order_number']), suborder['_source']['suborder_number'], suborder['_source'].get('date_received') [:8], # Remove time from string suborder['_source'].get('customer')['billing_name'], suborder['_source'].get('customer').get('email'), suborder['_source'].get('metadata').get('delivery_method'), suborder['_source'].get('order_type'), suborder['_source'].get('metadata').get('certificate_number'), suborder['_source'].get('metadata').get('borough'), '="{}"'.format( suborder['_source'].get('metadata').get('years')), ]) file.close() return url_for('static', filename='files/{}'.format(filename), _external=True)
def _print_orders(search_params: Dict[str, str]) -> str: """ Generates a PDF of order sheets. Args: search_params: Dictionary of attributes to search by. Returns: URL string of the PDF. """ order_number = search_params.get('order_number') suborder_number = search_params.get('suborder_number') order_type = search_params.get('order_type') delivery_method = search_params.get('delivery_method') status = search_params.get('status') billing_name = search_params.get('billing_name') date_received_start = search_params.get('date_received_start') date_received_end = search_params.get('date_received_end') date_submitted_start = search_params.get('date_submitted_start') date_submitted_end = search_params.get('date_submitted_end') multiple_items = '' if order_type == 'multiple_items': # Since multiple_items is parsed from the order_type field, we must overwrite the order_type field multiple_items = True order_type = 'all' suborders = search_queries(order_number, suborder_number, order_type, delivery_method, status, billing_name, date_received_start, date_received_end, date_submitted_start, date_submitted_end, multiple_items, 0, ELASTICSEARCH_MAX_SIZE, 'print') suborder = SearchFunctions.format_results(suborders) order_type_template_handler = { 'Birth Search': 'birth_search.html', 'Birth Cert': 'birth_cert.html', 'Marriage Search': 'marriage_search.html', 'Marriage Cert': 'marriage_cert.html', 'Death Search': 'death_search.html', 'Death Cert': 'death_cert.html', 'Tax Photo': 'tax_photo.html', 'Photo Gallery': 'photo_gallery.html', 'Property Card': 'property_card.html', } html = '' for item in suborder: html += render_template('orders/{}'.format( order_type_template_handler[item['order_type']]), order_info=item, customer_info=item['customer']) filename = 'order_sheets_{username}_{time}.pdf'.format( username=current_user.email, time=datetime.now().strftime('%Y%m%d-%H%M%S')) with open(join(current_app.static_folder, 'files', filename), 'w+b') as file_: CreatePDF(src=html, dest=file_) return url_for('static', filename='files/{}'.format(filename), _external=True)
def _print_large_labels(search_params: Dict[str, str]) -> str: """ :param search_params: :return: """ order_number = search_params.get('order_number') suborder_number = search_params.get('suborder_number') order_type = search_params.get('order_type') delivery_method = search_params.get('delivery_method') status = search_params.get('status') billing_name = search_params.get('billing_name') date_received_start = search_params.get('date_received_start') date_received_end = search_params.get('date_received_end') date_submitted_start = search_params.get('date_submitted_start') date_submitted_end = search_params.get('date_submitted_end') multiple_items = '' if order_type == 'multiple_items': # Since multiple_items is parsed from the order_type field, we must overwrite the order_type field multiple_items = True order_type = 'all' suborder_results = search_queries(order_number, suborder_number, order_type, delivery_method, status, billing_name, date_received_start, date_received_end, date_submitted_start, date_submitted_end, multiple_items, 0, ELASTICSEARCH_MAX_SIZE, 'search') # Only want suborder_number, and order type suborders = SearchFunctions.format_results(suborder_results) customers = [] for item in suborders: customers.append(item['customer']) customers = sorted(customers, key=lambda customer: customer['billing_name']) labels = [ customers[i:i + printing.LARGE_LABEL_COUNT] for i in range(0, len(customers), printing.LARGE_LABEL_COUNT) ] html = '' for page in labels: if len(page) < printing.LARGE_LABEL_COUNT: for i in range(printing.LARGE_LABEL_COUNT - len(page)): page.append(EMPTY_CUSTOMER) html += render_template('orders/large_labels.html', labels=page) filename = 'large_labels_{username}_{time}.pdf'.format( username=current_user.email, time=datetime.now().strftime('%Y%m%d-%H%M%S')) with open(join(current_app.static_folder, 'files', filename), 'w+b') as file_: CreatePDF(src=html, dest=file_) return url_for('static', filename='files/{}'.format(filename), _external=True)