def delete(id): book = get_model().read(id) crspath=book["Path"] if (book["createdById"]==str(session['profile']['id'])) : path = current_app.config['HW_UPLOAD_FOLDER'] UPLOAD_FOLDER = os.path.join(path, crspath) for root,dirs, files in os.walk(UPLOAD_FOLDER): for file in files: os.remove(UPLOAD_FOLDER+"/"+file) get_model().delete(id) return redirect(url_for('.list'))
def view(id): book = get_model().read(id) crspath=book["Path"] crsclassno=book["Classno"] seat=session['profile']['Seat'] classno=session['profile']['Classno'] lecturesfile=[] filenames=[] if (book["createdById"]==str(session['profile']['id'])) or (crsclassno==classno) : path = current_app.config['HW_UPLOAD_FOLDER'] UPLOAD_FOLDER = os.path.join(path, crspath) if not os.path.isdir(UPLOAD_FOLDER): os.mkdir(UPLOAD_FOLDER) for root,dirs, files in os.walk(UPLOAD_FOLDER): for file in files: basename, extension = file.rsplit('.', 1) _file=basename.split('-_')[0]+"."+extension if "-_None" in file: lecturesfile.append({"f":quote(str(file)),"n":_file}) if f"-_{seat}" in file: filenames.append(_file) elif session['profile']['Role']<"8": filenames.append(_file) return render_template("view.html", book=book,lecturesfile=lecturesfile,filenames=filenames) return redirect(url_for('index'))
def edit(id): book = get_model().read(id) if request.method == 'POST': data = request.form.to_dict(flat=True) path = current_app.config['HW_UPLOAD_FOLDER'] image_url = upload_image_file(request.files.get('image'),path) if image_url: data['imageUrl'] = image_url book = get_model().update(data, id) return redirect(url_for('.view', id=book['id'])) return render_template("form.html", action="Edit", book=book)
def list(): token = request.args.get('page_token', None) if token: token = token.encode('utf-8') books, next_page_token = get_model().list(cursor=token) return render_template( "list.html", books=books, next_page_token=next_page_token)
def list_mine(): token = request.args.get('page_token', None) if token: token = token.encode('utf-8') books, next_page_token = get_model().list_by_user( user_id=session['profile']['id'], cursor=token) return render_template( "list.html", books=books, next_page_token=next_page_token)
def download_file(id,filename): book = get_model().read(id) crspath=book["Path"] seat=session['profile']['Seat'] #return render_template("view.html", book=book) # Get current path os.getcwd() path = current_app.config['HW_UPLOAD_FOLDER'] # file Upload UPLOAD_FOLDER = os.path.join(path, crspath) FilePath=UPLOAD_FOLDER+"/"+filename basename, extension = filename.rsplit('.', 1) _file=basename.split('-_')[0]+"."+extension return send_file(FilePath, mimetype = 'zip', attachment_filename= _file, as_attachment = True)
def uploadfiles(id): book = get_model().read(id) crspath=book["Path"] seat=session['profile']['Seat'] # Get current path os.getcwd() path = current_app.config['HW_UPLOAD_FOLDER'] UPLOAD_FOLDER = os.path.join(path, crspath) if not os.path.isdir(UPLOAD_FOLDER): os.mkdir(UPLOAD_FOLDER) if request.method == 'POST': if 'files[]' not in request.files: flash('No file part') return render_template("view.html", book=book) files = request.files.getlist('files[]') for file in files: upload_hw_file(file,UPLOAD_FOLDER,seat) flash('File(s) successfully uploaded') return redirect(f"/lessons/{id}")
def add(): if request.method == 'POST': data = request.form.to_dict(flat=True) # If an image was uploaded, update the data to point to the new image. path = current_app.config['HW_UPLOAD_FOLDER'] image_url = upload_image_file(request.files.get('image'),path) if image_url: data['imageUrl'] = image_url # If the user is logged in, associate their profile with the new book. if 'profile' in session: data['createdById'] = session['profile']['id'] book = get_model().create(data) return redirect(url_for('.view', id=book['id'])) return render_template("form.html", action="Add", book={})
def download_all(id): book = get_model().read(id) crspath=book["Path"] seat=session['profile']['Seat'] path = current_app.config['HW_UPLOAD_FOLDER'] UPLOAD_FOLDER = os.path.join(path, crspath) ZIP_PATH = os.path.join(path, "ZIPFILE") if not os.path.isdir(ZIP_PATH): os.mkdir(ZIP_PATH) # Zip file Initialization and you can change the compression type ZipFileName=f"HW{crspath}.zip" ZipFilePath=ZIP_PATH+"/"+ZipFileName zipfolder = zipfile.ZipFile(ZipFilePath,'w', compression = zipfile.ZIP_STORED) # zip all the files which are inside in the folder for root,dirs, files in os.walk(UPLOAD_FOLDER): for file in files: zipfolder.write(UPLOAD_FOLDER+'/'+file) zipfolder.close() return send_file(ZipFilePath, mimetype = 'zip', attachment_filename= ZipFileName, as_attachment = True) os.remove(ZipFilePath)