Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #############################################################views.py
- def index(request):
- # Handle file upload
- if request.method == 'POST':
- form = DocumentForm(request.POST, request.FILES)
- if form.is_valid():
- try:
- validate_email(request.POST['email_addr'])
- except ValidationError as e:
- print('Not a valid email address')
- newdoc = CompanyInfo(
- company_name = request.POST['company_name'],
- docfile = request.FILES['docfile'],
- email_addr = request.POST['email_addr']
- )
- newdoc.save()
- sizing_data = processSizing(newdoc.docfile)
- extracted_data_dir = sizing_data.unpack_data(newdoc.docfile)
- os.chdir(extracted_data_dir)
- sizing_report = glob.glob('*.xlsx')
- rpt_location = extracted_data_dir + '/' + sizing_report[0]
- sizing_data.sendReport(newdoc.email_addr, rpt_location)
- shutil.rmtree(extracted_data_dir)
- # Redirect to the document list after POST
- return HttpResponseRedirect(reverse('sizeEstimate.views.index'))
- else:
- print('form not valid!:', (request.FILES.keys()))
- else:
- form = DocumentForm()
- #############################################################forms.py
- from django import forms
- class DocumentForm(forms.Form):
- docfile = forms.FileField(
- label='Select IO Collector Output File'
- )
- company_name = forms.CharField(
- label='Company Name'
- )
- email_addr = forms.EmailField(
- label='Email Address'
- )
- #############################################################models.py
- class CompanyInfo(models.Model):
- company_name = models.CharField(max_length=75)
- email_addr = models.EmailField()
- docfile = models.FileField(upload_to='documents/%y/%m/%d')
- ############################################################# index.html
- <form action="{% url "index" %}" id="myDropzone" method="post" enctype="multipart/form-data">
- {% csrf_token %}
- <input type="hidden" name="_token" value="tokenized value">
- <input name="company_name" placeholder="Customer Name" class="i-myclass form-control">
- <input name="email_addr" placeholder="Email" style="margin-top:10px;" class="i-myclass form-control">
- <div id="myDropzone" class="dropzone dz-mycompany">
- <div class="fallback">
- <input type="file" name="file">
- </div>
- </div>
- <div style="margin-top:30px;" class="text-center">
- <button type="submit" class="btn btn-danger text-slim">SUBMIT</button>
- </div>
- </form>
- </div>
- </div>
- <div class="navbar"> </div>
- </div>
- <script src="/static/js/jquery.min.js"></script>
- <script src="/static/js/dropzone.js"></script>
- <script docfile = '{{ docfile }}';></script>
- <script type="text/javascript">
- docfile = '{{ docfile }}';
- Dropzone.options.myDropzone =
- {
- paramName: '{{ docfile }}',
- url: "index.html",
- dictDefaultMessage: "Drop the sizing files here or click to upload.",
- dictRemoveFile: "Remove",
- // Prevents Dropzone from uploading dropped files immediately
- autoProcessQueue : false,
- init : function()
- {
- var submitButton = document.querySelector("#submit-all")
- myDropzone = this;
- submitButton.addEventListener("click", function()
- {
- myDropzone.processQueue();
- myDropzone.preventDefault();
- myDropzonee.stopPropagation();
- // Tell Dropzone to process all queued files.
- });
- // You might want to show the submit button only when
- // files are dropped here:
- this.on("addedfile", function()
- {
- // Show submit button here and/or inform user to click it.
- });
- }
- };
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement