def save(self): doc = Document() doc.file = self.document_path doc.type = self.document_type.data doc.filename = self.document_name.data db.session.add(doc) db.session.commit()
def save_document(request_file, content_subdir, related_obj, ashash=True): uploadedfile = UploadedFile(request_file) file_content = uploadedfile.read() doc_obj = Document() doc_obj.filehash = md5(file_content).hexdigest() doc_obj.urlencfilename = quote(uploadedfile.name) doc_obj.filename = uploadedfile.name doc_obj.content_type = uploadedfile.file.content_type if ashash: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filehash else: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filename if related_obj.__class__.__name__.lower() == "queryset": if len(related_obj) == 1: setattr(doc_obj, related_obj[0].__class__.__name__.lower(), related_obj[0]) else: print "ERROR: The queryset object had %s elements to it" % str( len(related_obj)) else: setattr(doc_obj, related_obj.__class__.__name__.lower(), related_obj) doc_obj.save() wfile = open(doc_obj.filepath, "w") wfile.write(file_content) wfile.close()
def save(self): doc = Document() unique_filename = str(uuid.uuid4()) extension = self.filename.data.filename.split('.')[-1] doc.type = extension doc.file = os.path.join(app.config['UPLOAD_FOLDER'], 'uploaded', unique_filename + '.' + extension) doc.filename = self.filename.data.filename db.session.add(doc) try: self.filename.data.save(doc.file) db.session.commit() except Exception as e: db.session.rollback()
def submit(): dForm = documentForm(request.form) print current_user.id if dForm.validate_on_submit(): title=dForm.title.data description=dForm.description.data location=dForm.location.data issue_idea=dForm.issue_idea.data category=dForm.category.data submitter=current_user.id document = Document(title=title,description=description,location=location,issue_idea=issue_idea,category=category,submitter=submitter) db.session.add(document) db.session.commit() did = db.session.query(func.max(Document.id)).scalar() document = Document.query.get(did) file = request.files['file'] filename = str(did) + ".png" file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) document.filename = filename doc_vote = Votes(docid=did,userid=current_user.id) db.session.add(doc_vote) db.session.commit() return render_template('submission.html',dForm=dForm)
def save_document(request_file, content_subdir, related_obj, ashash = True): uploadedfile = UploadedFile(request_file) file_content = uploadedfile.read() doc_obj = Document() doc_obj.filehash = md5(file_content).hexdigest() doc_obj.urlencfilename = quote(uploadedfile.name) doc_obj.filename = uploadedfile.name doc_obj.content_type = uploadedfile.file.content_type if ashash: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filehash else: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filename if related_obj.__class__.__name__.lower() == "queryset": if len(related_obj) == 1: setattr(doc_obj, related_obj[0].__class__.__name__.lower(), related_obj[0]) else: print "ERROR: The queryset object had %s elements to it" % str(len(related_obj)) else: setattr(doc_obj, related_obj.__class__.__name__.lower(), related_obj) doc_obj.save() wfile = open(doc_obj.filepath, "w") wfile.write(file_content) wfile.close()
def submit2(): #Checks if form1data has been submitted before accessing this page if not session['form1data']: return redirect(url_for('submit')) #Session variable back is decremented session['back'] = session['back'] - 1 form = SubmitForm2() form1data = json.loads(session['form1data']) url_or_file = form1data['url_question'] #Get number of sections sections = int(form1data['section']) #Error variables url_errors = [] file_errors = [] section_errors = [] pdf_errors = [] status_errors = [] if form.validate_on_submit(): #Loops through inputs of form for input in request.form: #checks if inputs are empty and adds ids to the corresponding error list if request.form[input] == '': check = input.split('_') if check[0] == 'url': url_errors.append(int(check[1])) elif check[0] == 'section': section_errors.append(int(check[1])) elif check[0] == 'file': file_errors.append(int(check[1])) else: if url_or_file == 'Yes': if 'url' in input: #checks if url is pdf link url = request.form[input] try: parsed_url = urlparse(url) #checks if the user input has a schema if not parsed_url.scheme: url = "http://" + url proxies = { "http": "http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script", "https": "http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script" } print "requesting: " + url r = requests.get(url_fix(url), proxies=proxies, timeout=1) if r.status_code == 404: status_errors.append(int(input.split('_')[1])) if r.headers['content-type'] != 'application/pdf': pdf_errors.append(int(input.split('_')[1])) #if requesting the website fails; do regular expression checking except: match = re.search('[\w%+\/-].pdf', request.form[input]) if not match: pdf_errors.append(int(input.split('_')[1])) #loops through the files inputted for file_ in request.files: file_id = file_.split('_')[1] #no files inputted add to files errors list if request.files[file_].filename == '': file_errors.append(int(file_id)) #check is the files one of the allowed files ext if not allowed_file(request.files[file_].filename): pdf_errors.append(int(file_id)) #if errors respond with errors if pdf_errors or section_errors or url_errors or status_errors or file_errors: pass else: #Urls processing if url_or_file == 'Yes': if sections == 1: url = request.form.get('url_1') parsed_url = urlparse(url) #check schema if not parsed_url.scheme: url = url_fix("http://" + url) download_url = URL(url) date_created = datetime.date(int(form1data['year']), int(form1data['month']), int(form1data['day'])) doc = Document(title=form1data['title'], type=form1data['type'], description=form1data['description'], dateCreated=date_created, agency=session['agency'], category=form1data['category'], doc_url=url) db.session.add(doc) #add document meta data to datebase db.session.commit() #get max common id did = db.session.query(func.max(Document.id)).scalar() doc = Document.query.get(did) filename = str(did) + '_' + form1data['title'] doc.filename = filename #download file into upload folder try: f = open( os.path.join(app.config['UPLOAD_FOLDER'], filename + ".pdf"), 'wb') if urlparse(url).scheme == "https://": f.write( download_url.download( cached=False, proxy= ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script", 'https'))) else: f.write( download_url.download( cached=False, proxy= ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script", 'http'))) f.close() #hardcopy set to yes if downloaded doc.hardcopy = "yes" doc.path = os.path.join(app.config['DOC_PATH'], filename) #NOTE: Script must be written for documents that have hardcopy set to no; to download file except: pass sub = Submit(did=did, uid=session['uid']) db.session.add(sub) db.session.commit() #if more that one sections do samething above for each section elif sections > 1: #gets the common id to be inserted by querying the max common id + 1 common_id = db.session.query(func.max( Document.common_id)).scalar() if not common_id: common_id = 1 else: common_id = common_id + 1 for doc in range(1, (sections + 1)): url = 'url_' + str(doc) url = request.form.get(url) parsed_url = urlparse(url) if not parsed_url.scheme: url = url_fix("http://" + url) download_url = URL(url) sectionid = 'section_' + str(doc) section = request.form.get(sectionid) date_created = datetime.date(int(form1data['year']), int(form1data['month']), int(form1data['day'])) doc = Document(title=form1data['title'], type=form1data['type'], description=form1data['description'], dateCreated=date_created, agency=session['agency'], category=form1data['category'], doc_url=url, common_id=common_id, section_id=doc) db.session.add(doc) db.session.commit() did = db.session.query(func.max(Document.id)).scalar() doc = Document.query.get(did) filename = str(did) + '_' + form1data['title'] doc.filename = filename try: f = open( os.path.join(app.config['UPLOAD_FOLDER'], filename + ".pdf"), 'wb') if urlparse(url).scheme == "https://": f.write( download_url.download( cached=False, proxy= ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script", 'https'))) else: f.write( download_url.download( cached=False, proxy= ("http://cscisa.csc.nycnet:8080/array.dll?Get.Routing.Script", 'http'))) f.close() doc.hardcopy = "yes" doc.path = os.path.join(app.config['DOC_PATH'], filename) except: pass sub = Submit(did=did, uid=session['uid']) sec = Section(did=did, section=section) db.session.add(sec) db.session.add(sub) db.session.commit() #if the user clicked files elif url_or_file == 'No': if sections == 1: file = request.files['file_1'] if file: date_created = datetime.date(int(form1data['year']), int(form1data['month']), int(form1data['day'])) doc = Document(title=form1data['title'], type=form1data['type'], description=form1data['description'], dateCreated=date_created, agency=session['agency'], category=form1data['category']) db.session.add(doc) db.session.commit() did = db.session.query(func.max(Document.id)).scalar() doc = Document.query.get(did) filename = str(did) + '_' + doc.title + ".pdf" file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename)) doc.path = os.path.join(app.config['DOC_PATH'], filename) doc.filename = filename doc.hardcopy = 'yes' sub = Submit(did=did, uid=session['uid']) db.session.add(sub) db.session.commit() elif sections > 1: for doc in range(1, (sections + 1)): fileid = "file_" + str(doc) file = request.files[fileid] if file: date_created = datetime.date( int(form1data['year']), int(form1data['month']), int(form1data['day'])) doc = Document( title=form1data['title'], type=form1data['type'], description=form1data['description'], dateCreated=date_created, agency=session['agency'], category=form1data['category']) sectionid = 'section_' + str(doc) section = request.form.get(sectionid) db.session.add(doc) db.session.commit() did = db.session.query(func.max( Document.id)).scalar() doc = Document.query.get(did) filename = str(did) + '_' + doc.title + ".pdf" file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename)) doc.filename = filename doc.hardcopy = 'yes' doc.path = os.path.join(app.config['DOC_PATH'], filename) sub = Submit(did=did, uid=session['uid']) sec = Section(did=did, section=section) db.session.add(sub) db.session.add(sec) db.session.commit() #set session variable confirm to be true so that comfirmation page is allowed through session['confirm'] = True return redirect(url_for('confirmation')) return render_template('submit2.html', back=session['back'], form=form, submit2form=request.form, submit2files=request.files, sections=int(sections), url_or_file=url_or_file, url_errors=url_errors, section_errors=section_errors, status_errors=status_errors, pdf_errors=pdf_errors, file_errors=file_errors)