nithin81 commited on
Commit
69d3dec
·
verified ·
1 Parent(s): 849f8b8

Update invoice/app/views.py

Browse files
Files changed (1) hide show
  1. invoice/app/views.py +77 -35
invoice/app/views.py CHANGED
@@ -6,46 +6,18 @@ import pandas as pd
6
  import os
7
  from dotenv import load_dotenv
8
  from django.http import JsonResponse
9
-
 
10
  load_dotenv()
11
 
12
  endpoint = os.environ["endpoint"]
13
  key = os.environ["key"]
14
 
15
- from pymongo import MongoClient
16
- import json
17
-
18
- # Set up MongoDB client
19
- # client = MongoClient('mongodb://localhost:27017/')
20
  client = MongoClient('mongodb://db:27017/')
21
-
22
- db = client['invoice'] # Database name
23
-
24
- def save_to_mongodb(request):
25
- if request.method == 'POST':
26
- invoice_number = f"invno_{request.POST.get('invoice_number')}"
27
- print(invoice_number)
28
- tables_data = json.loads(request.POST.get('tables'))
29
- print(tables_data)
30
-
31
- # Use the invoice number as the collection name
32
- collection = db[invoice_number]
33
-
34
- # Insert each table as a document in MongoDB
35
- for table_data in tables_data:
36
- headers = table_data['headers']
37
- rows = table_data['rows']
38
-
39
- # Create a list of dictionaries for each row
40
- table_rows = [dict(zip(headers, row)) for row in rows]
41
-
42
- # Insert into the MongoDB collection
43
- collection.insert_many(table_rows)
44
- print(collection)
45
-
46
- return JsonResponse({'message': 'Table saved to MongoDB successfully.'})
47
-
48
- return JsonResponse({'error': 'Invalid request'}, status=400)
49
 
50
  def upload_document(request):
51
  context = {'kv_df': [], 'tables': []}
@@ -67,6 +39,7 @@ def upload_document(request):
67
  result = poller.result()
68
 
69
  # Process the result
 
70
  kv_pairs = []
71
  for kv_pair in result.key_value_pairs:
72
  if kv_pair.key and kv_pair.value:
@@ -75,7 +48,8 @@ def upload_document(request):
75
 
76
  # Convert list of key-value pairs into a format for Django template
77
  kv_df = [{'key': kv[0], 'value': kv[1]} for kv in kv_pairs]
78
-
 
79
  tables = []
80
  if result.tables:
81
  for table in result.tables:
@@ -97,6 +71,7 @@ def upload_document(request):
97
  'file_name': file.name,
98
  'file_size': file.size,
99
  'file_type': file.content_type,
 
100
  }
101
 
102
  return render(request, 'upload.html', context)
@@ -105,3 +80,70 @@ def remove_file(request):
105
  if request.method == 'POST':
106
  return JsonResponse({'message': 'File removed successfully.'})
107
  return JsonResponse({'message': 'Invalid request'}, status=400)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import os
7
  from dotenv import load_dotenv
8
  from django.http import JsonResponse
9
+ from pymongo import MongoClient
10
+ import json
11
  load_dotenv()
12
 
13
  endpoint = os.environ["endpoint"]
14
  key = os.environ["key"]
15
 
16
+ # client = MongoClient("mongodb://localhost:27017/")
 
 
 
 
17
  client = MongoClient('mongodb://db:27017/')
18
+ db = client["invoices"]
19
+ collection = db["invoice"]
20
+ keyvalue_pair = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def upload_document(request):
23
  context = {'kv_df': [], 'tables': []}
 
39
  result = poller.result()
40
 
41
  # Process the result
42
+ global kv_pairs, tables, keyvalue_pair
43
  kv_pairs = []
44
  for kv_pair in result.key_value_pairs:
45
  if kv_pair.key and kv_pair.value:
 
48
 
49
  # Convert list of key-value pairs into a format for Django template
50
  kv_df = [{'key': kv[0], 'value': kv[1]} for kv in kv_pairs]
51
+ keyvalue_pair = {kv[0]:kv[1] for kv in kv_pairs}
52
+
53
  tables = []
54
  if result.tables:
55
  for table in result.tables:
 
71
  'file_name': file.name,
72
  'file_size': file.size,
73
  'file_type': file.content_type,
74
+
75
  }
76
 
77
  return render(request, 'upload.html', context)
 
80
  if request.method == 'POST':
81
  return JsonResponse({'message': 'File removed successfully.'})
82
  return JsonResponse({'message': 'Invalid request'}, status=400)
83
+
84
+
85
+ # Function to get the field names from MongoDB
86
+ def get_invoice_fields(request):
87
+
88
+ response_data = {
89
+ 'fields': ["Invoice No", "Customer Name", "Invoice Date","Invoice Item", "Quantity", "Base Unit", "Rate", "GST"], # MongoDB document fields for "Target"
90
+ 'kvPairs': [{'key': key, 'value': value} for key, value in keyvalue_pair.items()] # Key-value pairs
91
+ }
92
+ return JsonResponse(response_data, status=200)
93
+
94
+
95
+ def insert_to_db(request):
96
+ """
97
+ This view handles the insertion of mapping data to MongoDB.
98
+ It expects the data in JSON format where source columns are mapped to target columns.
99
+ """
100
+ if request.method == 'POST':
101
+ try:
102
+ # Parse the JSON data received from the AJAX request
103
+ data = json.loads(request.body)
104
+ mappings = data.get('mappings', [])
105
+
106
+ # Dictionary to store all data that will be inserted as new rows (documents) in MongoDB
107
+ mongo_documents = []
108
+
109
+ max_rows = 0
110
+
111
+ # First, determine the maximum number of rows based on the selected table columns
112
+ for mapping in mappings:
113
+ if 'values' in mapping:
114
+ max_rows = max(max_rows, len(mapping['values']))
115
+
116
+ # Prepare the documents for MongoDB
117
+ for i in range(max_rows):
118
+ # Create a new document for each row
119
+ document = {}
120
+
121
+ # For each mapping, add the column values or key-value pairs to the document
122
+ for mapping in mappings:
123
+ target_field = mapping.get('targetField')
124
+
125
+ if 'values' in mapping:
126
+ values = mapping['values']
127
+ # Get the value for the current row, or use an empty string if no value exists for this row
128
+ value = values[i] if i < len(values) else ''
129
+ document[target_field] = value
130
+ elif 'value' in mapping:
131
+ # Replicate the same key-value pair across all rows
132
+ document[target_field] = mapping['value']
133
+
134
+ # Add the constructed document to the list
135
+ mongo_documents.append(document)
136
+
137
+ # Insert the documents into MongoDB
138
+ if mongo_documents:
139
+ collection.insert_many(mongo_documents)
140
+
141
+ return JsonResponse({'status': 'success', 'message': 'Data mapped and saved successfully to MongoDB!'})
142
+
143
+ except Exception as e:
144
+ # Return an error response if something goes wrong
145
+ return JsonResponse({'status': 'error', 'message': str(e)}, status=500)
146
+
147
+ else:
148
+ # Return an error if the request method is not POST
149
+ return JsonResponse({'status': 'error', 'message': 'Invalid request method.'}, status=400)