Geen omschrijving

views.py 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. from django.shortcuts import render, redirect, get_object_or_404
  2. from django.core.paginator import Paginator
  3. from django.contrib import messages
  4. from core.models import Report
  5. from core.forms import ReportForm
  6. from core.utils import ConfigurableCRUDView, queryFromMaster
  7. from .filters import ReportFilter
  8. from .forms import ExportOptionsForm
  9. from pprint import pprint
  10. from .gen_report import gen_xlsx
  11. from django.core.files.base import File
  12. from pathlib import Path
  13. from django.views.decorators.csrf import csrf_exempt
  14. from django.http import JsonResponse, HttpResponseBadRequest
  15. import json
  16. from django.contrib.auth.decorators import login_required
  17. def index(request):
  18. reports = Report.objects.all()
  19. report_filter = ReportFilter(request.GET, queryset=reports)
  20. # Paginate the filtered queryset
  21. paginator = Paginator(report_filter.qs, 10) # Show 10 reports per page
  22. page_number = request.GET.get('page')
  23. page_obj = paginator.get_page(page_number)
  24. context = {
  25. 'filter': report_filter,
  26. 'page_obj': page_obj,
  27. }
  28. return render(request, 'report/index.html', context)
  29. def report_create_view(request):
  30. if request.method == "POST":
  31. form = ReportForm(request.POST)
  32. if form.is_valid():
  33. form.save()
  34. messages.success(request, "Report created successfully!")
  35. return redirect("report:report_index") # Adjust with your report list view name
  36. else:
  37. form = ReportForm()
  38. return render(request, "report/create.html", {"form": form})
  39. class ReportCRUDView(ConfigurableCRUDView):
  40. model = Report
  41. list_template_name = 'legacy/datacrud_list.html'
  42. detail_template_name = 'legacy/datacrud_detail.html'
  43. form_template_name = 'report/report_form.html'
  44. confirm_delete_template_name = 'legacy/datacrud_confirm_delete.html'
  45. filterset_class = ReportFilter
  46. page_title = "Reports"
  47. # URL name mappings
  48. list_url_name = 'report:report-list'
  49. create_url_name = 'report:report-create'
  50. update_url_name = 'report:report-update'
  51. delete_url_name = 'report:report-delete'
  52. config_fields = ["name", "file", "created_by", "created_at"]
  53. config_field_orders = ["id", "name", "created_by"]
  54. # config_readonly_fields = ["lot_no"]
  55. # config_edit_fields = ["lot_no", "code"]
  56. ordering = ["-created_at", "-id",]
  57. def create_coi_file(lot_no, user):
  58. data = {
  59. "customer": "Tum Coder",
  60. "inspect_date": "2025-01-15",
  61. "lot_no": "12345",
  62. "staff_name": "Tum 8888",
  63. "man_name": "Tum 999",
  64. "size": "Large",
  65. "pcs": "10 pcs",
  66. "spec": "Spec-A",
  67. "hardness_out.d1_act": "10",
  68. "hardness_out.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
  69. "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  70. "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
  71. "hardness_out.qa1": "Hello world",
  72. "hardness_out.qa2": "Unknow person",
  73. "hardness_out.v11": 5,
  74. "hardness_out.v12": 15,
  75. "hardness_out.v13": 10,
  76. "hardness_out.v21": 9,
  77. "hardness_out.v22": 8,
  78. "hardness_out.v23": 7,
  79. "dimension_app.d1_act": "33",
  80. "dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
  81. "dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  82. "dimension_app.spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
  83. }
  84. output_file = gen_xlsx(
  85. template_file="/app/report/coi_templates.xlsx",
  86. selected_sheets=["hardness_out", "dimension_app"], # Replace with your actual sheet names
  87. prefix_filename="/app/media/coi",
  88. data=data
  89. )
  90. report = Report.objects.create(
  91. name=lot_no,
  92. created_by=user,
  93. file=None # Leave this as None or assign a file if required
  94. )
  95. output_file_path = Path(output_file) # Convert to a Path object for convenience
  96. with open(output_file_path, "rb") as f:
  97. report.file.save(output_file_path.name, File(f), save=True)
  98. pprint(f"outputfile = {output_file}")
  99. return report
  100. def coi_view(request):
  101. pprint(f"xxxx method = xxx {request.method}")
  102. if request.method == "POST":
  103. pprint(request.POST)
  104. if 'export' in request.POST:
  105. data = {
  106. "customer": "Tum Coder",
  107. "inspect_date": "2025-01-15",
  108. "lot_no": "12345",
  109. "staff_name": "Tum 8888",
  110. "man_name": "Tum 999",
  111. "size": "Large",
  112. "lot_size": "10 pcs",
  113. "spec": "Spec-A",
  114. "hardness_out.d1_act": "10",
  115. "hardness_out.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
  116. "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  117. "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
  118. "dimension_app.d1_act": "33",
  119. "dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
  120. "dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  121. "dimension_app.spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
  122. }
  123. output_file = gen_xlsx(
  124. template_file="/app/report/coi_templates.xlsx",
  125. selected_sheets=["hardness_out", "dimension_app"], # Replace with your actual sheet names
  126. prefix_filename="/app/media/coi",
  127. data=data
  128. )
  129. report = Report.objects.create(
  130. name=request.POST.get('lot_no','Untitled'),
  131. created_by=request.user,
  132. file=None # Leave this as None or assign a file if required
  133. )
  134. output_file_path = Path(output_file) # Convert to a Path object for convenience
  135. with open(output_file_path, "rb") as f:
  136. report.file.save(output_file_path.name, File(f), save=True)
  137. pprint(f"outputfile = {output_file}")
  138. if 'search_lot' in request.POST:
  139. lot_no = request.POST.get('lot_no', None)
  140. if lot_no:
  141. results = queryFromMaster(lot_no)
  142. first_result = results[0] if results else None
  143. try:
  144. pcs = int(first_result.PRO5) - int(first_result.PRO27)
  145. except:
  146. pcs = 0
  147. size_str = f"{first_result.PRO10}x{first_result.PRO11}x{first_result.PRO12}";
  148. spec = f"{first_result.PRO13} {first_result.PRO14} {first_result.PRO15} {first_result.PRO16} {first_result.PRO17} {first_result.PRO18}"
  149. return render(request, 'report/coi.html', {'result': first_result,
  150. 'pcs':pcs,
  151. 'size_str': size_str,
  152. 'spec': spec})
  153. exports = request.POST.getlist("exports") # Retrieve the list of selected values
  154. pprint(f"Selected Export Options: {exports}")
  155. messages.success(request, "Request Sent")
  156. return redirect(request.path_info)
  157. return render(request, 'report/coi.html')
  158. @csrf_exempt # Disable CSRF for API requests (ensure this is secure in production)
  159. @login_required
  160. def gen_report_view(request):
  161. if request.method == "POST":
  162. try:
  163. # Parse JSON data from the request body
  164. data = json.loads(request.body)
  165. lot_no = data.get("lot_no")
  166. if not lot_no:
  167. return HttpResponseBadRequest("Missing 'lot_no' in request data")
  168. # Call the `create_coi_file` function with the provided lot_no
  169. report = create_coi_file(lot_no, request.user)
  170. # Return a success response with the report details
  171. return JsonResponse({
  172. "message": "Report generated successfully",
  173. "report_id": report.id,
  174. "file_url": report.file.url if report.file else None,
  175. })
  176. except json.JSONDecodeError:
  177. return HttpResponseBadRequest("Invalid JSON data")
  178. except Exception as e:
  179. pprint(e)
  180. return JsonResponse({"error": str(e)}, status=500)
  181. else:
  182. return HttpResponseBadRequest("Only POST requests are allowed")