| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- from django.shortcuts import render, redirect, get_object_or_404
- from django.core.paginator import Paginator
- from django.contrib import messages
- from core.models import Report
- from core.forms import ReportForm
- from core.utils import ConfigurableCRUDView, queryFromMaster
- from .filters import ReportFilter
- from .forms import ExportOptionsForm
- from pprint import pprint
- from .gen_report import gen_xlsx
- from django.core.files.base import File
- from pathlib import Path
- from django.views.decorators.csrf import csrf_exempt
- from django.http import JsonResponse, HttpResponseBadRequest
- import json
- from django.contrib.auth.decorators import login_required
- def index(request):
- reports = Report.objects.all()
- report_filter = ReportFilter(request.GET, queryset=reports)
-
- # Paginate the filtered queryset
- paginator = Paginator(report_filter.qs, 10) # Show 10 reports per page
- page_number = request.GET.get('page')
- page_obj = paginator.get_page(page_number)
-
- context = {
- 'filter': report_filter,
- 'page_obj': page_obj,
- }
- return render(request, 'report/index.html', context)
- def report_create_view(request):
- if request.method == "POST":
- form = ReportForm(request.POST)
- if form.is_valid():
- form.save()
- messages.success(request, "Report created successfully!")
- return redirect("report:report_index") # Adjust with your report list view name
- else:
- form = ReportForm()
- return render(request, "report/create.html", {"form": form})
- class ReportCRUDView(ConfigurableCRUDView):
- model = Report
- list_template_name = 'legacy/datacrud_list.html'
- detail_template_name = 'legacy/datacrud_detail.html'
- form_template_name = 'report/report_form.html'
- confirm_delete_template_name = 'legacy/datacrud_confirm_delete.html'
- filterset_class = ReportFilter
- page_title = "Reports"
- # URL name mappings
- list_url_name = 'report:report-list'
- create_url_name = 'report:report-create'
- update_url_name = 'report:report-update'
- delete_url_name = 'report:report-delete'
- config_fields = ["name", "file", "created_by", "created_at"]
- config_field_orders = ["id", "name", "created_by"]
- # config_readonly_fields = ["lot_no"]
- # config_edit_fields = ["lot_no", "code"]
- ordering = ["-created_at", "-id",]
- def create_coi_file(lot_no, user):
- data = {
- "customer": "Tum Coder",
- "inspect_date": "2025-01-15",
- "lot_no": "12345",
- "staff_name": "Tum 8888",
- "man_name": "Tum 999",
- "size": "Large",
- "pcs": "10 pcs",
- "spec": "Spec-A",
- "hardness_out.d1_act": "10",
- "hardness_out.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
- "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
- "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
- "hardness_out.qa1": "Hello world",
- "hardness_out.qa2": "Unknow person",
- "hardness_out.v11": 5,
- "hardness_out.v12": 15,
- "hardness_out.v13": 10,
- "hardness_out.v21": 9,
- "hardness_out.v22": 8,
- "hardness_out.v23": 7,
- "dimension_app.d1_act": "33",
- "dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
- "dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
- "dimension_app.spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
- }
- output_file = gen_xlsx(
- template_file="/app/report/coi_templates.xlsx",
- selected_sheets=["hardness_out", "dimension_app"], # Replace with your actual sheet names
- prefix_filename="/app/media/coi",
- data=data
- )
- report = Report.objects.create(
- name=lot_no,
- created_by=user,
- file=None # Leave this as None or assign a file if required
- )
- output_file_path = Path(output_file) # Convert to a Path object for convenience
- with open(output_file_path, "rb") as f:
- report.file.save(output_file_path.name, File(f), save=True)
- pprint(f"outputfile = {output_file}")
- return report
- def coi_view(request):
- pprint(f"xxxx method = xxx {request.method}")
- if request.method == "POST":
- pprint(request.POST)
- if 'export' in request.POST:
- data = {
- "customer": "Tum Coder",
- "inspect_date": "2025-01-15",
- "lot_no": "12345",
- "staff_name": "Tum 8888",
- "man_name": "Tum 999",
- "size": "Large",
- "lot_size": "10 pcs",
- "spec": "Spec-A",
- "hardness_out.d1_act": "10",
- "hardness_out.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
- "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
- "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
- "dimension_app.d1_act": "33",
- "dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
- "dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
- "dimension_app.spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
- }
- output_file = gen_xlsx(
- template_file="/app/report/coi_templates.xlsx",
- selected_sheets=["hardness_out", "dimension_app"], # Replace with your actual sheet names
- prefix_filename="/app/media/coi",
- data=data
- )
- report = Report.objects.create(
- name=request.POST.get('lot_no','Untitled'),
- created_by=request.user,
- file=None # Leave this as None or assign a file if required
- )
- output_file_path = Path(output_file) # Convert to a Path object for convenience
- with open(output_file_path, "rb") as f:
- report.file.save(output_file_path.name, File(f), save=True)
- pprint(f"outputfile = {output_file}")
- if 'search_lot' in request.POST:
- lot_no = request.POST.get('lot_no', None)
- if lot_no:
- results = queryFromMaster(lot_no)
- first_result = results[0] if results else None
- try:
- pcs = int(first_result.PRO5) - int(first_result.PRO27)
- except:
- pcs = 0
- size_str = f"{first_result.PRO10}x{first_result.PRO11}x{first_result.PRO12}";
- spec = f"{first_result.PRO13} {first_result.PRO14} {first_result.PRO15} {first_result.PRO16} {first_result.PRO17} {first_result.PRO18}"
- return render(request, 'report/coi.html', {'result': first_result,
- 'pcs':pcs,
- 'size_str': size_str,
- 'spec': spec})
- exports = request.POST.getlist("exports") # Retrieve the list of selected values
- pprint(f"Selected Export Options: {exports}")
- messages.success(request, "Request Sent")
- return redirect(request.path_info)
- return render(request, 'report/coi.html')
- @csrf_exempt # Disable CSRF for API requests (ensure this is secure in production)
- @login_required
- def gen_report_view(request):
- if request.method == "POST":
- try:
- # Parse JSON data from the request body
- data = json.loads(request.body)
- lot_no = data.get("lot_no")
- if not lot_no:
- return HttpResponseBadRequest("Missing 'lot_no' in request data")
- # Call the `create_coi_file` function with the provided lot_no
- report = create_coi_file(lot_no, request.user)
- # Return a success response with the report details
- return JsonResponse({
- "message": "Report generated successfully",
- "report_id": report.id,
- "file_url": report.file.url if report.file else None,
- })
- except json.JSONDecodeError:
- return HttpResponseBadRequest("Invalid JSON data")
- except Exception as e:
- pprint(e)
- return JsonResponse({"error": str(e)}, status=500)
- else:
- return HttpResponseBadRequest("Only POST requests are allowed")
|