|
|
@@ -11,7 +11,10 @@ from pprint import pprint
|
|
11
|
11
|
from .gen_report import gen_xlsx
|
|
12
|
12
|
from django.core.files.base import File
|
|
13
|
13
|
from pathlib import Path
|
|
14
|
|
-
|
|
|
14
|
+from django.views.decorators.csrf import csrf_exempt
|
|
|
15
|
+from django.http import JsonResponse, HttpResponseBadRequest
|
|
|
16
|
+import json
|
|
|
17
|
+from django.contrib.auth.decorators import login_required
|
|
15
|
18
|
|
|
16
|
19
|
def index(request):
|
|
17
|
20
|
reports = Report.objects.all()
|
|
|
@@ -62,6 +65,52 @@ class ReportCRUDView(ConfigurableCRUDView):
|
|
62
|
65
|
# config_edit_fields = ["lot_no", "code"]
|
|
63
|
66
|
ordering = ["-created_at", "-id",]
|
|
64
|
67
|
|
|
|
68
|
+def create_coi_file(lot_no, user):
|
|
|
69
|
+
|
|
|
70
|
+ data = {
|
|
|
71
|
+ "customer": "Tum Coder",
|
|
|
72
|
+ "inspect_date": "2025-01-15",
|
|
|
73
|
+ "lot_no": "12345",
|
|
|
74
|
+ "staff_name": "Tum 8888",
|
|
|
75
|
+ "man_name": "Tum 999",
|
|
|
76
|
+ "size": "Large",
|
|
|
77
|
+ "pcs": "10 pcs",
|
|
|
78
|
+ "spec": "Spec-A",
|
|
|
79
|
+ "hardness_out.d1_act": "10",
|
|
|
80
|
+ "hardness_out.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
|
|
|
81
|
+ "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
|
|
|
82
|
+ "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
|
|
|
83
|
+ "hardness_out.qa1": "Hello world",
|
|
|
84
|
+ "hardness_out.qa2": "Unknow person",
|
|
|
85
|
+ "hardness_out.v11": 5,
|
|
|
86
|
+ "hardness_out.v12": 15,
|
|
|
87
|
+ "hardness_out.v13": 10,
|
|
|
88
|
+ "hardness_out.v21": 9,
|
|
|
89
|
+ "hardness_out.v22": 8,
|
|
|
90
|
+ "hardness_out.v23": 7,
|
|
|
91
|
+ "dimension_app.d1_act": "33",
|
|
|
92
|
+ "dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
|
|
|
93
|
+ "dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
|
|
|
94
|
+ "dimension_app.spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
|
|
|
95
|
+ }
|
|
|
96
|
+ output_file = gen_xlsx(
|
|
|
97
|
+ template_file="/app/report/coi_templates.xlsx",
|
|
|
98
|
+ selected_sheets=["hardness_out", "dimension_app"], # Replace with your actual sheet names
|
|
|
99
|
+ prefix_filename="/app/media/coi",
|
|
|
100
|
+ data=data
|
|
|
101
|
+ )
|
|
|
102
|
+ report = Report.objects.create(
|
|
|
103
|
+ name=lot_no,
|
|
|
104
|
+ created_by=user,
|
|
|
105
|
+ file=None # Leave this as None or assign a file if required
|
|
|
106
|
+ )
|
|
|
107
|
+ output_file_path = Path(output_file) # Convert to a Path object for convenience
|
|
|
108
|
+ with open(output_file_path, "rb") as f:
|
|
|
109
|
+ report.file.save(output_file_path.name, File(f), save=True)
|
|
|
110
|
+
|
|
|
111
|
+ pprint(f"outputfile = {output_file}")
|
|
|
112
|
+ return report
|
|
|
113
|
+
|
|
65
|
114
|
def coi_view(request):
|
|
66
|
115
|
pprint(f"xxxx method = xxx {request.method}")
|
|
67
|
116
|
if request.method == "POST":
|
|
|
@@ -75,12 +124,12 @@ def coi_view(request):
|
|
75
|
124
|
"staff_name": "Tum 8888",
|
|
76
|
125
|
"man_name": "Tum 999",
|
|
77
|
126
|
"size": "Large",
|
|
78
|
|
- "pcs": "10 pcs",
|
|
|
127
|
+ "lot_size": "10 pcs",
|
|
79
|
128
|
"spec": "Spec-A",
|
|
80
|
|
- "hardness.d1_act": "10",
|
|
81
|
|
- "hardness.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
|
|
82
|
|
- "hardness.acc": True, # Hide rows 24 to 28 if the prefix is "0"
|
|
83
|
|
- "hardness.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
|
|
|
129
|
+ "hardness_out.d1_act": "10",
|
|
|
130
|
+ "hardness_out.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
|
|
|
131
|
+ "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
|
|
|
132
|
+ "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
|
|
84
|
133
|
"dimension_app.d1_act": "33",
|
|
85
|
134
|
"dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
|
|
86
|
135
|
"dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
|
|
|
@@ -88,7 +137,7 @@ def coi_view(request):
|
|
88
|
137
|
}
|
|
89
|
138
|
output_file = gen_xlsx(
|
|
90
|
139
|
template_file="/app/report/coi_templates.xlsx",
|
|
91
|
|
- selected_sheets=["hardness", "dimension_app"], # Replace with your actual sheet names
|
|
|
140
|
+ selected_sheets=["hardness_out", "dimension_app"], # Replace with your actual sheet names
|
|
92
|
141
|
prefix_filename="/app/media/coi",
|
|
93
|
142
|
data=data
|
|
94
|
143
|
)
|
|
|
@@ -124,3 +173,36 @@ def coi_view(request):
|
|
124
|
173
|
messages.success(request, "Request Sent")
|
|
125
|
174
|
return redirect(request.path_info)
|
|
126
|
175
|
return render(request, 'report/coi.html')
|
|
|
176
|
+
|
|
|
177
|
+@csrf_exempt # Disable CSRF for API requests (ensure this is secure in production)
|
|
|
178
|
+@login_required
|
|
|
179
|
+def gen_report_view(request):
|
|
|
180
|
+ if request.method == "POST":
|
|
|
181
|
+ try:
|
|
|
182
|
+ # Parse JSON data from the request body
|
|
|
183
|
+ data = json.loads(request.body)
|
|
|
184
|
+ lot_no = data.get("lot_no")
|
|
|
185
|
+
|
|
|
186
|
+ if not lot_no:
|
|
|
187
|
+ return HttpResponseBadRequest("Missing 'lot_no' in request data")
|
|
|
188
|
+
|
|
|
189
|
+ # Call the `create_coi_file` function with the provided lot_no
|
|
|
190
|
+ report = create_coi_file(lot_no, request.user)
|
|
|
191
|
+
|
|
|
192
|
+ # Return a success response with the report details
|
|
|
193
|
+ return JsonResponse({
|
|
|
194
|
+ "message": "Report generated successfully",
|
|
|
195
|
+ "report_id": report.id,
|
|
|
196
|
+ "file_url": report.file.url if report.file else None,
|
|
|
197
|
+ })
|
|
|
198
|
+
|
|
|
199
|
+ except json.JSONDecodeError:
|
|
|
200
|
+ return HttpResponseBadRequest("Invalid JSON data")
|
|
|
201
|
+ except Exception as e:
|
|
|
202
|
+ pprint(e)
|
|
|
203
|
+ return JsonResponse({"error": str(e)}, status=500)
|
|
|
204
|
+ else:
|
|
|
205
|
+ return HttpResponseBadRequest("Only POST requests are allowed")
|
|
|
206
|
+
|
|
|
207
|
+
|
|
|
208
|
+
|