views.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # views.py
  2. import os
  3. from django.http import HttpResponse, JsonResponse, Http404
  4. import subprocess, sys, os
  5. from django.views.decorators.csrf import csrf_exempt
  6. from django.conf import settings
  7. import re
  8. from datetime import datetime
  9. from openpyxl import load_workbook
  10. media_dir = os.path.join(os.path.dirname(__file__), 'media')
  11. @csrf_exempt
  12. def get_screenshot(request):
  13. if request.method == 'POST':
  14. import json
  15. data = json.loads(request.body or '{}')
  16. site = data.get('site')
  17. site_urls = {
  18. "walmart": "https://www.walmart.com/",
  19. "amazon": "https://www.amazon.com/",
  20. "target": "https://www.target.com/",
  21. "bestbuy": "https://www.bestbuy.com/"
  22. }
  23. print("site", site)
  24. if site not in site_urls:
  25. return JsonResponse({'error': 'Invalid site'}, status=400)
  26. url = site_urls[site]
  27. print("Url", url)
  28. base_dir = r"C:\Users\Abhyuday.Andugula\Work Space\CA\competitor_analysis\media"
  29. result = subprocess.run(
  30. [sys.executable, "-m", "CA_Analysis.capture", url],
  31. cwd=base_dir
  32. )
  33. print("processed")
  34. print("result:", str(result))
  35. full_img = os.path.join(settings.MEDIA_ROOT, site, 'fullpage.png')
  36. if os.path.exists(full_img):
  37. image_url = settings.MEDIA_URL + f'{site}/fullpage.png'
  38. best_seller_img1 = settings.MEDIA_URL + f'{site}/best_sellers_section_1.png'
  39. best_seller_img2 = settings.MEDIA_URL + f'{site}/best_sellers_section_2.png'
  40. carousel_img1 = settings.MEDIA_URL + f'{site}/carosal_img_1.jpg'
  41. carousel_img2 = settings.MEDIA_URL + f'{site}/carosal_img_2.jpg'
  42. carousel_img3 = settings.MEDIA_URL + f'{site}/carosal_img_3.jpg'
  43. if site == 'amazon':
  44. json_path = os.path.join(settings.MEDIA_ROOT, site, f'{site}_data.json')
  45. if os.path.exists(json_path):
  46. with open(json_path, 'r', encoding='utf-8') as f:
  47. json_data = json.load(f)
  48. best_seller_content_keys = list(json_data['Best Sellers'].keys())
  49. best_seller_content1 = json.dumps(json_data['Best Sellers'][best_seller_content_keys[0]]) # ensure serializable
  50. best_seller_content2 = json.dumps(json_data['Best Sellers'][best_seller_content_keys[1]]) # ensure serializable
  51. carousel_name1 = json_data['Carosal Names'][1]
  52. carousel_name2 = json_data['Carosal Names'][2]
  53. carousel_name3 = json_data['Carosal Names'][3]
  54. carousel_link1 = json_data['carosal Links'][1]
  55. carousel_link2 = json_data['carosal Links'][2]
  56. carousel_link3 = json_data['carosal Links'][3]
  57. return JsonResponse({
  58. 'image_url': image_url,
  59. 'best_seller_img1': best_seller_img1,
  60. 'best_seller_img2': best_seller_img2,
  61. 'best_seller_content1': best_seller_content1,
  62. 'best_seller_content2': best_seller_content2,
  63. 'carousel_img1': carousel_img1,
  64. 'carousel_img2': carousel_img2,
  65. 'carousel_img3': carousel_img3,
  66. 'carousel_name1': carousel_name1,
  67. 'carousel_name2': carousel_name2,
  68. 'carousel_name3': carousel_name3,
  69. 'carousel_link1': carousel_link1,
  70. 'carousel_link2': carousel_link2,
  71. 'carousel_link3': carousel_link3
  72. }, status=200)
  73. elif site == 'bestbuy':
  74. best_seller_section = settings.MEDIA_URL + f'{site}/best_selling_section_3.png'
  75. best_seller_img2 = settings.MEDIA_URL + f'{site}/app_exclusive_section.png'
  76. json_path = os.path.join(settings.MEDIA_ROOT, site, f'{site}_data.json')
  77. first_section = settings.MEDIA_URL + f'{site}/first_section.png'
  78. if os.path.exists(json_path):
  79. with open(json_path, 'r', encoding='utf-8') as f:
  80. json_data = json.load(f)
  81. best_seller_content_keys = list(json_data['Best Sellers'].keys())
  82. app_exclusive_keys = list(json_data['App Exclusive'].keys())
  83. best_seller_content1 = json.dumps(json_data['Best Sellers'][best_seller_content_keys[0]]) # ensure serializable
  84. best_seller_content2 = json.dumps(json_data['App Exclusive'][app_exclusive_keys[0]]) # ensure serializable
  85. first_section_title = list(json_data['First Section'].keys())[0]
  86. first_section_url = json_data['First Section'][first_section_title]
  87. return JsonResponse({
  88. 'image_url': image_url,
  89. 'first_section':first_section,
  90. 'first_section_url': first_section_url,
  91. 'first_section_title': first_section_title,
  92. 'first_section_url': first_section_url,
  93. 'best_seller_img1': best_seller_section,
  94. 'best_seller_img2': best_seller_img2,
  95. 'best_seller_content1': best_seller_content1,
  96. 'best_seller_content2': best_seller_content2
  97. }, status=200)
  98. else:
  99. return JsonResponse({
  100. 'image_url': image_url,
  101. }, status=200)
  102. return JsonResponse({'error': 'Screenshot not found'}, status=404)
  103. @csrf_exempt
  104. def run_script(request):
  105. ps_script = r"D:\open_outlook.ps1"
  106. # Run PowerShell silently and close after execution
  107. subprocess.Popen([
  108. "powershell.exe",
  109. "-ExecutionPolicy", "Bypass",
  110. "-File", ps_script
  111. ], creationflags=subprocess.CREATE_NO_WINDOW)
  112. return JsonResponse({'message': '"✅ Outlook mail opened successfully!"'}, status=200)
  113. @csrf_exempt
  114. def download_excel(request):
  115. if request.method == 'GET':
  116. # Match filenames like csr_30-Oct-2025.xlsx
  117. pattern = re.compile(r'^csr_\d{2}-[A-Za-z]{3}-\d{4}\.xlsx$')
  118. folder_path = settings.MEDIA_ROOT
  119. for filename in os.listdir(folder_path):
  120. if pattern.match(filename):
  121. file_path = os.path.join(folder_path, filename)
  122. # ✅ Load the existing Excel workbook
  123. wb = load_workbook(file_path)
  124. ws = wb.active # or use wb[sheet_name] if specific sheet needed
  125. # ✅ Update cell L1 (column 12, row 1)
  126. ws["L1"] = datetime.now().strftime('%d %B %Y')
  127. # ✅ Save new file with updated name
  128. new_filename = f"csr_{datetime.now().strftime('%d-%b-%Y')}.xlsx"
  129. new_file_path = os.path.join(folder_path, new_filename)
  130. wb.save(new_file_path)
  131. # ✅ Return file for download
  132. with open(new_file_path, 'rb') as f:
  133. response = HttpResponse(
  134. f.read(),
  135. content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  136. )
  137. response['Content-Disposition'] = f'attachment; filename="{new_filename}"'
  138. return response
  139. raise Http404("CSR Excel file not found.")