def import_data_csv(data_to_insert, data_to_update, keep_database): """ Import Order data into the database from a CSV file that has been processed by the function 'process_csv' below. Insert new data or update existing one. If the boolean 'keep_database' is False, the data imported completely overwrite current data in the database. :param data_to_insert: Array of Order data to insert into the database (cf. Section VIII - C in Documentation backend routes) :param data_to_update: Array of Order data to update in the database (cf. Section VIII - C in Documentation backend routes) :param keep_database: Boolean used to delete the database or not before importing the data """ if not keep_database: query_order.delete_all_orders() order_formatted = [] for order in data_to_insert: new_order = dict(orderNumber=order['number'], orderDate=convert_date_string_to_datetime( order['date']), orderTotalAmount=order['totalAmount'], clientNumber=order['clientNumber'], quoteNumber=order['quoteNumber']) order_formatted.append(new_order.copy()) query_order.bulk_insert_orders(order_formatted) for order in data_to_update: order_to_update = query_order.get_order_by_number(order['number']) if order_to_update is not None: query_order.update_order( order_to_update, order['number'], convert_date_string_to_datetime(order['date']), order['totalAmount'], order['quoteNumber'], order['clientNumber']) if keep_database: query_log.insert_log( 'Des données ont été ajoutées et/ou remplacées dans la base de données de commandes', 'database') else: query_log.insert_log( 'La base de données de commandes à été supprimée et remplacée par une autre', 'database')
def import_data_csv(data_to_insert, data_to_update, keep_database): """ Import Invoice data into the database from a CSV file that has been processed by the function 'process_csv' below. Insert new data or update existing one. If the boolean 'keep_database' is False, the data imported completely overwrite current data in the database. :param data_to_insert: Array of Invoice data to insert into the database (cf. Section VIII - C in Documentation backend routes) :param data_to_update: Array of Invoice data to update in the database (cf. Section VIII - C in Documentation backend routes) :param keep_database: Boolean used to delete the database or not before importing the data """ if not keep_database: query_invoice.delete_all_invoices() invoice_formatted = [] for invoice in data_to_insert: new_invoice = dict(invoiceNumber=invoice['number'], invoiceDate=convert_date_string_to_datetime( invoice['date']), clientNumber=invoice['clientNumber'], orderNumber=invoice['orderNumber']) invoice_formatted.append(new_invoice.copy()) query_invoice.bulk_insert_invoices(invoice_formatted) for invoice in data_to_update: invoice_to_update = query_invoice.get_invoice_by_number( invoice['number']) if invoice_to_update is not None: query_invoice.update_invoice( invoice_to_update, invoice['number'], convert_date_string_to_datetime(invoice['date']), invoice['orderNumber'], invoice['clientNumber']) if keep_database: query_log.insert_log( 'Des données ont été ajoutées et/ou remplacées dans la base de données de factures', 'database') else: query_log.insert_log( 'La base de données de factures à été supprimée et remplacée par une autre', 'database')
def update_quote(id_quote): data = json.loads(request.form['data']) data["quoteDate"] = convert_date_string_to_datetime(data["quoteDate"]) quote = query.get_quote_by_id(id_quote) if quote is None: return "Quote not found", 404 if quote.quoteNumber != data['quoteNumber']: if query.quote_exist_by_number(data['quoteNumber']): return "This quote number already exists", 400 old_quote_state = quote.quoteState quote_pdf_name = quote.quotePdfName query.update_quote(quote, data['quoteNumber'], data["quoteDate"], data["quoteTotalAmount"], data["quoteState"], data["clientNumber"]) if old_quote_state == 'waiting' and (data["quoteState"] == 'validated' or data["quoteState"] == 'cancelled'): Quote.delete_quote_pdf_file_reminder(quote_pdf_name, data['quoteState']) query_log.insert_log('Le devis ' + str(data['quoteNumber']) + ' a été mis à jour', 'database') resp = jsonify({"success": True}) resp.status_code = 200 return resp
def update_order(id_order): data = json.loads(request.form['data']) data["orderDate"] = convert_date_string_to_datetime(data["orderDate"]) order = query.get_order_by_id(id_order) if order is None: return "Order not found", 404 if order.orderNumber != data['orderNumber']: if query.order_exist_by_number(data['orderNumber']): return "This order number already exists", 400 query.update_order(order, data["orderNumber"], data["orderDate"], data["orderTotalAmount"], data["quoteNumber"], data["clientNumber"]) query_log.insert_log( 'La commande ' + str(data['orderNumber']) + ' a été mise à jour', 'database') resp = jsonify({"success": True}) resp.status_code = 201 return resp
def insert_document(): data = json.loads(request.form['data']) data['documentData']['creationDate'] = convert_date_string_to_datetime( data['documentData']['creationDate']) document = pdf.get_object_from_document_type( data['documentData']['documentType'], data) # If the document already exists in the database, return 400 BAD REQUEST if document.exist_in_database(): return "This document is already in the database", 400 document_client = client.Client(data['clientData']['clientNumber'], data['clientData']['clientName'], data['clientData']['clientMail']) document_client.insert_or_update_if_exists_database() document.insert_database() query.insert_log( "Les données du document " + data["documentData"]["documentName"] + " ont été insérées dans la base de données", 'pdf-processing') response = { "documentNumber": data["documentData"]["documentNumber"], "type": data["documentData"]["documentType"] } return jsonify(response), 201
def insert_quote(): data = json.loads(request.form['data']) data["quoteDate"] = convert_date_string_to_datetime(data["quoteDate"]) if query.quote_exist_by_number(data['quoteNumber']): return "This quote number already exists", 400 query.insert_quote(data['quoteNumber'], data["quoteDate"], data["quoteTotalAmount"], data["clientNumber"]) query_log.insert_log('Un nouveau devis ' + str(data['quoteNumber']) + ' a été inséré manuellement dans la base de données', 'database') resp = jsonify({"success": True}) resp.status_code = 201 return resp
def insert_order(): data = json.loads(request.form['data']) data["orderDate"] = convert_date_string_to_datetime(data["orderDate"]) if query.order_exist_by_number(data['orderNumber']): return "This order number already exists", 400 query.insert_order(data["orderNumber"], data["orderDate"], data["orderTotalAmount"], data["clientNumber"], data["quoteNumber"]) query_log.insert_log( 'Une nouvelle commande ' + str(data['orderNumber']) + ' a été insérée manuellement dans la base de données', 'database') resp = jsonify({"success": True}) resp.status_code = 200 return resp
def process_csv(csv_data, keep_database): """ Loop through every rows of the csv_data extracted from a CSV file to check the data's validity. The rules to check the data's validity can be found in the functions below. (cf. Section VIII - B in Documentation backend routes) If the boolean 'keep_database' is True, the function will also check for conflicts between the current database and the data in the CSV. If 'keep_database' is False, the current database will be overwritten so it doesn't need to check for conflicts. :param csv_data: The CSV data in string format extracted from a CSV file :param keep_database: A boolean used to know if the function needs to check for conflicts :return: (cf. Section VIII - B in Documentation backend routes) """ csv_has_error = False rows = csv_data.split('\r\n') headers = rows[0].split(',') if len(headers) != NUMBER_OF_CSV_HEADERS: return { 'code': 0, 'message': 'The number of headers does not match the number of data required for this dataset' }, True else: data_csv = [] errors = [] data_numbers = set() total_number_of_data = len(rows) - 1 for i in range(1, len(rows)): data = rows[i].split(',') error_line = {'line': i + 1, 'messages': []} check_data_result = __check_data(data, data_numbers, error_line) error_line = check_data_result[0] csv_has_error = csv_has_error or check_data_result[1] errors.append(error_line) if not (data[0] == '' and len(data) == 1): data_numbers.add(data[0]) if not csv_has_error: if data[4] == '' or data[4] == 'None': quote_number = None else: quote_number = int(data[4]) data_csv.append({ 'number': int(data[0]), 'date': convert_date_string_to_datetime(data[1]), 'totalAmount': float(data[2]), 'clientNumber': int(data[3]), 'quoteNumber': quote_number }) else: total_number_of_data -= 1 if csv_has_error: return { 'code': 1, 'message': 'The CSV contains errors.', 'data': errors }, True else: if keep_database: result = __check_for_conflicts(data_csv) data_csv = result[0] conflicts = result[1] has_conflicts = len(conflicts) > 0 if has_conflicts: return { 'hasConflicts': True, 'totalNumberOfData': total_number_of_data, 'data': { 'conflicts': conflicts, 'dataToInsert': data_csv } }, False return { 'hasConflicts': False, 'totalNumberOfData': total_number_of_data, 'data': data_csv }, False