Advertisement
mcbrune

Django Dropzone Javascript

Jun 14th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. #############################################################views.py
  2. def index(request):
  3. # Handle file upload
  4. if request.method == 'POST':
  5. form = DocumentForm(request.POST, request.FILES)
  6.  
  7. if form.is_valid():
  8. try:
  9. validate_email(request.POST['email_addr'])
  10. except ValidationError as e:
  11. print('Not a valid email address')
  12.  
  13. newdoc = CompanyInfo(
  14. company_name = request.POST['company_name'],
  15. docfile = request.FILES['docfile'],
  16. email_addr = request.POST['email_addr']
  17. )
  18. newdoc.save()
  19.  
  20. sizing_data = processSizing(newdoc.docfile)
  21. extracted_data_dir = sizing_data.unpack_data(newdoc.docfile)
  22.  
  23. os.chdir(extracted_data_dir)
  24. sizing_report = glob.glob('*.xlsx')
  25. rpt_location = extracted_data_dir + '/' + sizing_report[0]
  26.  
  27. sizing_data.sendReport(newdoc.email_addr, rpt_location)
  28. shutil.rmtree(extracted_data_dir)
  29.  
  30. # Redirect to the document list after POST
  31. return HttpResponseRedirect(reverse('sizeEstimate.views.index'))
  32. else:
  33. print('form not valid!:', (request.FILES.keys()))
  34. else:
  35. form = DocumentForm()
  36.  
  37. #############################################################forms.py
  38.  
  39. from django import forms
  40.  
  41.  
  42. class DocumentForm(forms.Form):
  43. docfile = forms.FileField(
  44. label='Select IO Collector Output File'
  45. )
  46.  
  47. company_name = forms.CharField(
  48. label='Company Name'
  49. )
  50.  
  51. email_addr = forms.EmailField(
  52. label='Email Address'
  53. )
  54.  
  55. #############################################################models.py
  56.  
  57. class CompanyInfo(models.Model):
  58. company_name = models.CharField(max_length=75)
  59. email_addr = models.EmailField()
  60. docfile = models.FileField(upload_to='documents/%y/%m/%d')
  61.  
  62. ############################################################# index.html
  63.  
  64. <form action="{% url "index" %}" id="myDropzone" method="post" enctype="multipart/form-data">
  65. {% csrf_token %}
  66.  
  67. <input type="hidden" name="_token" value="tokenized value">
  68. <input name="company_name" placeholder="Customer Name" class="i-myclass form-control">
  69. <input name="email_addr" placeholder="Email" style="margin-top:10px;" class="i-myclass form-control">
  70.  
  71. <div id="myDropzone" class="dropzone dz-mycompany">
  72. <div class="fallback">
  73. <input type="file" name="file">
  74. </div>
  75. </div>
  76. <div style="margin-top:30px;" class="text-center">
  77. <button type="submit" class="btn btn-danger text-slim">SUBMIT</button>
  78. </div>
  79. </form>
  80. </div>
  81. </div>
  82. <div class="navbar">&nbsp;</div>
  83. </div>
  84.  
  85. <script src="/static/js/jquery.min.js"></script>
  86. <script src="/static/js/dropzone.js"></script>
  87. <script docfile = '{{ docfile }}';></script>
  88. <script type="text/javascript">
  89. docfile = '{{ docfile }}';
  90.  
  91. Dropzone.options.myDropzone =
  92. {
  93. paramName: '{{ docfile }}',
  94. url: "index.html",
  95. dictDefaultMessage: "Drop the sizing files here or click to upload.",
  96. dictRemoveFile: "Remove",
  97.  
  98. // Prevents Dropzone from uploading dropped files immediately
  99.  
  100. autoProcessQueue : false,
  101.  
  102. init : function()
  103. {
  104. var submitButton = document.querySelector("#submit-all")
  105. myDropzone = this;
  106.  
  107. submitButton.addEventListener("click", function()
  108. {
  109. myDropzone.processQueue();
  110. myDropzone.preventDefault();
  111. myDropzonee.stopPropagation();
  112. // Tell Dropzone to process all queued files.
  113. });
  114.  
  115. // You might want to show the submit button only when
  116. // files are dropped here:
  117. this.on("addedfile", function()
  118. {
  119. // Show submit button here and/or inform user to click it.
  120. });
  121.  
  122. }
  123. };
  124. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement