def register(): if current_user.is_authenticated and 'username' in session: return redirect(url_for('upload_file')) else: user_logout() post = False data = {'username': '', 'email': ''} form = RegistrationForm() if request.method == 'POST': post = True if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() flash('You are now a registered user!') return redirect(url_for('login')) if form.username.data is not None: data['username'] = form.username.data if form.email.data is not None: data['email'] = form.email.data return render_template('register.html', title='Register', form=form, post=post, data=data)
def login(): if current_user.is_authenticated and 'username' in session: return redirect(url_for('upload_file')) else: user_logout() post = False email = '' form = LoginForm() if request.method == 'POST': post = True if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user is None or not user.check_password(form.password.data): flash('Error: Invalid username or password') return redirect(url_for('login')) login_user(user, remember=form.remember_me.data) next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': add_session(user) user.last_access = session['current_access'] db.session.commit() session['id'] = user.id next_page = url_for('upload_file') return redirect(next_page) if form.email.data is not None: email = form.email.data return render_template('login.html', title='Sign In', form=form, post=post, email=email)
def downloads(): if 'username' not in session: user_logout() if request.method == "POST": return redirect(url_for("download_file")) return render_template('download.html')
def download_file(fileindex): if 'username' not in session: user_logout() try: if fileindex >= 0: # Download the selected file file_folder = session['converted_files'][fileindex]['folder'] file_name = session['converted_files'][fileindex]['file'] # Accounting of files downloaded by the user statistic_query = Statistic.query.filter_by( username_id=session['id'], id=date.today()).first() if statistic_query.file_downloads is None: statistic_query.file_downloads = 1 db.session.add(statistic_query) else: statistic_query.file_downloads = statistic_query.file_downloads + 1 db.session.add(statistic_query) db.session.commit() return send_from_directory(file_folder, file_name, as_attachment=True) else: # Compress all in zip to return them i = 1 with ZipFile( os.path.join(session['user_folder'], 'converted_files.zip'), 'w') as zip_directory: for file_to_zip in session['converted_files']: zip_directory.write(os.path.join(file_to_zip['folder'], file_to_zip['file']), os.path.join(str(i), file_to_zip['file']), compress_type=ZIP_DEFLATED) i += 1 zip_directory.close() # Accounting of files downloaded by the user statistic_query = Statistic.query.filter_by( username_id=session['id'], id=date.today()).first() if statistic_query.file_downloads is None: statistic_query.file_downloads = i - 1 db.session.add(statistic_query) else: statistic_query.file_downloads = statistic_query.file_downloads + i - 1 db.session.add(statistic_query) db.session.commit() return send_from_directory(session['user_folder'], 'converted_files.zip', as_attachment=True) except Exception as e: return str(e)
def upload_file(): if 'username' not in session: user_logout() # Reset variables to prevent page rollback session['topographical_file'] = '' session['config_file'] = '' session['symbols_file'] = '' session['files_folder'] = '' session['dxf_filename'] = '' f_config = "" f_symbols = "" f_topo = "" post = False form = UploadForm() if request.method == 'POST': post = True if form.validate_on_submit(): if "config_file" in request.files: f_config = request.files["config_file"] if "symbols_file" in request.files: f_symbols = request.files["symbols_file"] f_topo = request.files["topographical_file"] session['topographical_file'] = secure_filename(f_topo.filename) # Create customized directory for each user create_user_folder() # Save topography files, configuration and symbols in the user folder save_upload_files(f_topo, f_config, f_symbols) # Analyze topography file upload_topographical_file( os.path.join(session['files_folder'], session['topographical_file'])) # Analyze the configuration file (if it exists) upload_config_file( os.path.join(session['files_folder'], session['config_file'])) # Analyze and extract the symbols from the file (if it exists) upload_symbols_file( os.path.join(session['files_folder'], session['symbols_file'])) return redirect(url_for("convert_file_dxf")) return render_template('upload.html', title='Upload Files', form=form, post=post)
def unauthorized_callback(): user_logout() flash('You must be logged in to access this page.') return redirect(url_for('login'))
def convert_file_dxf(): if 'username' not in session: user_logout() post = False form = request.form.to_dict() layers = [] cad_version = '' # Load possible file errors errors, duplicate_color_errors, \ cad_color_errors = check_files_errors(get_config_file(), post) if not errors: layers = get_layers_table() elif (get_errors_upload_topographical_file() or file_empty( os.path.join(session['files_folder'], session['topographical_file'])) or errors_square() or errors_rectangle()) \ or (session['config_file'] and ( get_errors_config_file() or file_empty(os.path.join(session['files_folder'], session['config_file'])) or get_errors_config_file_duplicate_elements())) \ or (session['symbols_file'] and error_symbols()): shutil.rmtree(session['files_folder']) if request.method == "POST": post = True cad_version = form['cadversion'] del form['cadversion'] session['dxf_filename'] = form['dxf_filename'] del form['dxf_filename'] # Check if the file extension is correct (dxf) check_DXF_ext() # Updating the layers layers.clear() layers = update_layers(form) # Load possible file errors errors, duplicate_color_errors, \ cad_color_errors = check_files_errors(get_dxf_configuration(layers), post) if duplicate_color_errors or cad_color_errors: return render_template( 'convert.html', title='Conversion DXF', form=form, layers=layers, dxf_filename=session['dxf_filename'], cad_version=cad_version, symbols=get_symbols(), cad_versions=app.config["CAD_VERSIONS"], errors=errors, duplicate_color_errors=duplicate_color_errors, cad_color_errors=cad_color_errors) # Generate the DXF and add it to the list of user generated in the session converted = generate_dxf(session['files_folder'], session['dxf_filename'], layers, cad_version) if converted: session['converted_files'].append({ 'folder': session['files_folder'], 'file': session['dxf_filename'] }) # Accounting of files converted by the user statistic_query = Statistic.query.filter_by( username_id=session['id'], id=date.today()).first() if statistic_query is None: st = Statistic(id=date.today(), username_id=session['id']) db.session.add(st) st.file_converts = 1 else: statistic_query.file_converts = statistic_query.file_converts + 1 db.session.add(statistic_query) db.session.commit() flash('File successfully converted!!!') else: flash('Error: File could not be generated!!!') return redirect(url_for("downloads")) return render_template('convert.html', title='Conversion DXF', form=form, layers=layers, dxf_filename=session['dxf_filename'], cad_version=cad_version, symbols=get_symbols(), cad_versions=app.config["CAD_VERSIONS"], errors=errors, duplicate_color_errors=duplicate_color_errors, cad_color_errors=cad_color_errors)