Няма описание

views.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. from django.contrib.auth.models import User
  18. from legacy.models import Data
  19. def index(request):
  20. reports = Report.objects.all()
  21. report_filter = ReportFilter(request.GET, queryset=reports)
  22. # Paginate the filtered queryset
  23. paginator = Paginator(report_filter.qs, 10) # Show 10 reports per page
  24. page_number = request.GET.get('page')
  25. page_obj = paginator.get_page(page_number)
  26. context = {
  27. 'filter': report_filter,
  28. 'page_obj': page_obj,
  29. }
  30. return render(request, 'report/index.html', context)
  31. def report_create_view(request):
  32. if request.method == "POST":
  33. form = ReportForm(request.POST)
  34. if form.is_valid():
  35. form.save()
  36. messages.success(request, "Report created successfully!")
  37. return redirect("report:report_index") # Adjust with your report list view name
  38. else:
  39. form = ReportForm()
  40. return render(request, "report/create.html", {"form": form})
  41. class ReportCRUDView(ConfigurableCRUDView):
  42. model = Report
  43. list_template_name = 'legacy/datacrud_list.html'
  44. detail_template_name = 'legacy/datacrud_detail.html'
  45. form_template_name = 'report/report_form.html'
  46. confirm_delete_template_name = 'legacy/datacrud_confirm_delete.html'
  47. filterset_class = ReportFilter
  48. page_title = "Reports"
  49. # URL name mappings
  50. list_url_name = 'report:report-list'
  51. create_url_name = 'report:report-create'
  52. update_url_name = 'report:report-update'
  53. delete_url_name = 'report:report-delete'
  54. config_fields = ["name", "file", "created_by", "created_at"]
  55. config_field_orders = ["id", "name", "created_by"]
  56. # config_readonly_fields = ["lot_no"]
  57. # config_edit_fields = ["lot_no", "code"]
  58. ordering = ["-created_at", "-id",]
  59. def convert_sheet_data(sheet_data):
  60. """
  61. Convert sheet_data to the required form with prefixed keys.
  62. :param sheet_data: Dictionary with sheet names as keys and their data as values.
  63. :return: Dictionary in the required key-value format.
  64. """
  65. converted_data = {}
  66. for sheet_name, data in sheet_data.items():
  67. for key, value in data.items():
  68. # Prefix each key with the sheet name
  69. converted_key = f"{sheet_name}.{key}"
  70. converted_data[converted_key] = value
  71. return converted_data
  72. def generate_hardness_out_values(lot_no):
  73. """
  74. Generate a dictionary of placeholder values for a given lot_no.
  75. :param lot_no: The lot number to query data for.
  76. :return: A dictionary with placeholders (e.g., v1_1, v1_2, ...) as keys and their respective values.
  77. """
  78. # Query the Data model for records matching the given lot_no
  79. records = Data.objects.filter(lot_no=lot_no).order_by('row_no')
  80. print(f"records {lot_no} = {records.values()}")
  81. # Initialize an empty dictionary to store placeholder values
  82. placeholders = {}
  83. # Iterate over the records to populate placeholder values
  84. for record_idx, record in enumerate(records, start=1):
  85. placeholders[f'v{record_idx}_1'] = record.p1 # Checkpoint 1 value
  86. placeholders[f'v{record_idx}_2'] = record.p2 # Checkpoint 2 value
  87. placeholders[f'v{record_idx}_3'] = record.p3 # Checkpoint 3 value
  88. placeholders[f'v{record_idx}_4'] = record.avg # Average value
  89. placeholders[f'v{record_idx}_5'] = record.rgrade # Judgment value
  90. return placeholders
  91. def merge_sheet_data_with_data(sheet_data, data):
  92. """
  93. Merge `sheet_data` with `data`.
  94. :param sheet_data: Dictionary containing the sheet-specific data.
  95. :param data: Dictionary containing general data.
  96. :return: A merged dictionary combining both `sheet_data` and `data`.
  97. """
  98. # Merge dictionaries using unpacking
  99. merged_data = {**data, **sheet_data}
  100. return merged_data
  101. def create_coi_file(lot_no, sheets, user, md):
  102. pprint("---- create_coi_file ---")
  103. pprint(md)
  104. qa1 = User.objects.get(pk=md['qa1'])
  105. qa2 = User.objects.get(pk=md['qa2'])
  106. pprint(qa1)
  107. pprint(qa2)
  108. sheet_data = {}
  109. for sheet_name in sheets:
  110. if sheet_name == 'hardness_out':
  111. sheet_data[sheet_name] = generate_hardness_out_values(lot_no)
  112. converted_data = convert_sheet_data(sheet_data)
  113. print(f"sheet_data \n {sheet_data}")
  114. print(f"converted_data \n {converted_data}")
  115. data = {
  116. "customer": "Tum Coder",
  117. "inspect_date": "2025-01-15",
  118. "lot_no": "12345",
  119. "staff_name": "Tum 8888",
  120. "man_name": "Tum 999",
  121. "size": "Large",
  122. "pcs": "10 pcs",
  123. "spec": "Spec-A",
  124. "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  125. "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
  126. "acc": True, # Hide rows 24 to 28 if the prefix is "0"
  127. "spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
  128. # "hardness_out.qa1": f"{qa1.first_name} {qa1.last_name}",
  129. # "hardness_out.qa2": f"{qa2.first_name} {qa2.last_name}",
  130. "qa1": f"{qa1.first_name} {qa1.last_name}",
  131. "qa2": f"{qa2.first_name} {qa2.last_name}",
  132. "dimension_app.d1_act": "33",
  133. "dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
  134. "dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  135. "dimension_app.spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
  136. "sign1": qa1.profile.signed_picture,
  137. "sign2": qa2.profile.signed_picture,
  138. "pos1": qa1.profile.get_position_display(),
  139. "pos2": qa2.profile.get_position_display()
  140. }
  141. merged_data = merge_sheet_data_with_data(converted_data, data)
  142. pprint(f"---- merged_data ---")
  143. pprint(merged_data)
  144. output_file = gen_xlsx(
  145. template_file="/app/report/coi_templates.xlsx",
  146. selected_sheets=sheets, # Replace with your actual sheet names
  147. prefix_filename="/app/media/coi",
  148. data=merged_data
  149. )
  150. report = Report.objects.create(
  151. name=lot_no,
  152. created_by=user,
  153. file=None # Leave this as None or assign a file if required
  154. )
  155. output_file_path = Path(output_file) # Convert to a Path object for convenience
  156. with open(output_file_path, "rb") as f:
  157. report.file.save(output_file_path.name, File(f), save=True)
  158. pprint(f"outputfile = {output_file}")
  159. return report
  160. SHEET_NAMES = {
  161. 'hardness_out': 'Hardness Out',
  162. 'hardness_out_in': 'Hardness Out/In',
  163. 'hardness_both_size': 'Hardness Both Size',
  164. 'dimension': 'Dimension',
  165. 'dimension_app': 'Dimension Appearance',
  166. 'dimension_bal_weight': 'Dimension Balance/Weight',
  167. 'dim_bal_app_hard': 'Dimension Balance/Appearance/Hardness',
  168. 'dim_bal_app_rot_hard': 'Dimension Balance/Appearance/Rotation/Hardness',
  169. 'thickness_8_point': 'Thickness 8 Points',
  170. 'centering': 'Centering',
  171. }
  172. def coi_view(request):
  173. pprint(f"xxxx method = xxx {request.method}")
  174. users = User.objects.all()
  175. if request.method == "POST":
  176. pprint(request.POST)
  177. exports = request.POST.getlist("exports") # Retrieve the list of selected values
  178. pprint(f"Selected Export Options: {exports}")
  179. if 'export' in request.POST:
  180. data = {
  181. "customer": "Tum Coder",
  182. "inspect_date": "2025-01-15",
  183. "lot_no": "12345",
  184. "staff_name": "Tum 8888",
  185. "man_name": "Tum 999",
  186. "size": "Large",
  187. "lot_size": "10 pcs",
  188. "spec": "Spec-A",
  189. "hardness_out.d1_act": "10",
  190. "hardness_out.d2_act": "0[24:28]", # Hide rows 24 to 28 if the prefix is "0"
  191. "hardness_out.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  192. "hardness_out.spe_acc": False, # Hide rows 24 to 28 if the prefix is "0"
  193. "dimension_app.d1_act": "33",
  194. "dimension_app.d2_act": "0[26:32]", # Hide rows 24 to 28 if the prefix is "0"
  195. "dimension_app.acc": True, # Hide rows 24 to 28 if the prefix is "0"
  196. "dimension_app.spe_acc": True, # Hide rows 24 to 28 if the prefix is "0"
  197. }
  198. output_file = gen_xlsx(
  199. template_file="/app/report/coi_templates.xlsx",
  200. selected_sheets=exports, # Replace with your actual sheet names
  201. prefix_filename="/app/media/coi",
  202. data=data
  203. )
  204. report = Report.objects.create(
  205. name=request.POST.get('lot_no','Untitled'),
  206. created_by=request.user,
  207. file=None # Leave this as None or assign a file if required
  208. )
  209. output_file_path = Path(output_file) # Convert to a Path object for convenience
  210. with open(output_file_path, "rb") as f:
  211. report.file.save(output_file_path.name, File(f), save=True)
  212. pprint(f"outputfile = {output_file}")
  213. if 'search_lot' in request.POST:
  214. lot_no = request.POST.get('lot_no', None)
  215. if lot_no:
  216. results = queryFromMaster(lot_no)
  217. first_result = results[0] if results else None
  218. try:
  219. pcs = int(first_result.PRO5) - int(first_result.PRO27)
  220. except:
  221. pcs = 0
  222. size_str = f"{first_result.PRO10}x{first_result.PRO11}x{first_result.PRO12}";
  223. spec = f"{first_result.PRO13} {first_result.PRO14} {first_result.PRO15} {first_result.PRO16} {first_result.PRO17} {first_result.PRO18}"
  224. return render(request, 'report/coi.html', {'result': first_result,
  225. 'pcs':pcs,
  226. 'size_str': size_str,
  227. 'spec': spec})
  228. messages.success(request, "Request Sent")
  229. return redirect(request.path_info)
  230. return render(request, 'report/coi.html', {'SHEET_NAMES': SHEET_NAMES, 'users': users})
  231. @csrf_exempt # Disable CSRF for API requests (ensure this is secure in production)
  232. @login_required
  233. def gen_report_view(request):
  234. if request.method == "POST":
  235. try:
  236. # Parse JSON data from the request body
  237. data = json.loads(request.body)
  238. lot_no = data.get("lot_no").strip()
  239. exports = data.get("exports")
  240. qa1 = data.get('qa1')
  241. qa2 = data.get('qa2')
  242. print(f"data = {data}")
  243. if not lot_no:
  244. return HttpResponseBadRequest("Missing 'lot_no' in request data")
  245. # Call the `create_coi_file` function with the provided lot_no
  246. report = create_coi_file(lot_no, exports, request.user, {'qa1': qa1, 'qa2': qa2})
  247. # Return a success response with the report details
  248. return JsonResponse({
  249. "message": "Report generated successfully",
  250. "report_id": report.id,
  251. "file_url": report.file.url if report.file else None,
  252. })
  253. except json.JSONDecodeError:
  254. return HttpResponseBadRequest("Invalid JSON data")
  255. except Exception as e:
  256. pprint(e)
  257. return JsonResponse({"error": str(e)}, status=500)
  258. else:
  259. return HttpResponseBadRequest("Only POST requests are allowed")