def form(request): """amaaazing functoin that creates a schema using SQL alchemy model which then outputs data in to a rendered form input: specific model you want to create form from output: form including error catching see comments to understand how it works """ setup_schema(None, atr) schema = atr.__colanderalchemy__ atrschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, chemicals) schema = chemicals.__colanderalchemy__ chemicalsschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, data_aquisition) schema = data_aquisition.__colanderalchemy__ data_aquisitionschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, depositor) schema = depositor.__colanderalchemy__ depositorschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, dried_film) schema = dried_film.__colanderalchemy__ dried_filmschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, experiment) schema = experiment.__colanderalchemy__ experimentschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, experimental_conditions) schema = experimental_conditions.__colanderalchemy__ experimental_conditionsschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, fourier_transform_processing) schema = fourier_transform_processing.__colanderalchemy__ fourier_transform_processingschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, gas) schema = gas.__colanderalchemy__ gasschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, liquid) schema = liquid.__colanderalchemy__ liquidschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, molecular_composition) schema = molecular_composition.__colanderalchemy__ molecular_compositionschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, molecule) schema = molecule.__colanderalchemy__ moleculeschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, not_atr) schema = not_atr.__colanderalchemy__ not_atrschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, post_processing_and_deposited_spectra) schema = post_processing_and_deposited_spectra.__colanderalchemy__ post_processing_and_deposited_spectraschema = deform.Form( schema, buttons=('submit', )) setup_schema(None, project) schema = project.__colanderalchemy__ projectschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, protein) schema = protein.__colanderalchemy__ proteinschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, publication) schema = publication.__colanderalchemy__ publicationschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, sample) schema = sample.__colanderalchemy__ sampleschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, solid) schema = solid.__colanderalchemy__ solidschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, spectrometer) schema = spectrometer.__colanderalchemy__ spectrometerschema = deform.Form(schema, buttons=('submit', )) setup_schema(None, state_of_sample) schema = state_of_sample.__colanderalchemy__ state_of_sampleschema = deform.Form(schema, buttons=('submit', )) tmpstore = FileUploadTempStore() class Schema(colander.Schema): upload = colander.SchemaNode( deform.FileData(), widget=deform.widget.FileUploadWidget(tmpstore)) schema = Schema() form = deform.Form(schema, buttons=('submit', )) if request.POST: #name = request.params['name'] #data = request.params['data'] #magic = request.params['magic']# detect that the submit button was clicked controls = request.POST.items() # get the form controls #newfile = request.params['widget'] thinking = request.POST print(thinking) folder = 'C:/ftirdb/ftirdb/views' if not os.path.exists(folder): os.makedir(mypath) print "Path is created" fname = folder + "/" + "test.txt" with open(fname, "w") as x: x.write("thisis a boy") #print(form) #with open(folder, 'w+') as f: #f.write(captured['upload']['fp'].read()) #page = FTIRModel(name=name,data=data,magic=magic) #request.dbsession.add(page) for i in controls: print(i) #session.commit() #record = merge_session_with_post(record,request.POST.items()) #page = Depositor(record) #request.dbsession.add(page) #print(controls) #page.creator = request.user #request.dbsession.add(page) try: appstruct = form.validate(controls) next_url = request.route_url('view_page', pagename='name') return HTTPFound(location=next_url) # call validate except deform.ValidationFailure as e: # catch the exception return {'form': e.render()} # re-render the form with an exception return {'form': appstruct} else: form = form.render() atrform = atrschema.render() chemicalsform = chemicalsschema.render() data_aquisitionform = data_aquisitionschema.render() depositorform = depositorschema.render() dried_filmform = dried_filmschema.render() experimentform = experimentschema.render() experimental_conditionsform = experimental_conditionsschema.render() fourier_transform_processingform = fourier_transform_processingschema.render( ) gasform = gasschema.render() liquidform = liquidschema.render() molecular_compositionform = molecular_compositionschema.render() moleculeform = moleculeschema.render() not_atrform = not_atrschema.render() post_processing_and_deposited_spectraform = post_processing_and_deposited_spectraschema.render( ) projectform = projectschema.render() proteinform = proteinschema.render() publicationform = publicationschema.render() sampleform = sampleschema.render() solidform = solidschema.render() spectrometerform = spectrometerschema.render() state_of_sampleform = state_of_sampleschema.render() return { 'form': form, 'liquidform': liquidform, 'dried_filmform': dried_filmform, 'experimental_conditionsform': experimental_conditionsform, 'proteinform': proteinform, 'atrform': atrform, 'depositorform': depositorform, 'not_atrform': not_atrform, 'fourier_transform_processingform': fourier_transform_processingform, 'post_processing_and_deposited_spectraform': post_processing_and_deposited_spectraform, 'spectrometerform': spectrometerform, 'projectform': projectform, 'solidform': solidform, 'publicationform': publicationform, 'molecular_compositionform': molecular_compositionform, 'moleculeform': moleculeform, 'experimentform': experimentform, 'sampleform': sampleform, 'gasform': gasform, 'state_of_sampleform': state_of_sampleform, 'data_aquisitionform': data_aquisitionform, 'chemicalsform': chemicalsform }
from deform.interfaces import FileUploadTempStore import colander import peppercorn import validators from pymongo.errors import DuplicateKeyError from pymongo.objectid import ObjectId from security import _check_pw, _hash_pw, USERS from gridfs import GridFS #temporary hardcoded variables, move these to a better place looking_fors =(('love', "Love"), ('friends', 'Friends'), ('fun','Fun')) interests = (('men', 'Men'), ('women', 'Women')) username_regex = r'^[A-Za-z](?=[A-Za-z0-9_.]{3,31}$)[a-zA-Z0-9_]*\.?[a-zA-Z0-9_]*$' msg = "Username must be 4 to 32 characters and start with a letter. You may use letters, numbers, underscores, and one dot (.)" ages = [[x,x] for x in range(1,130)] tmpstore = FileUploadTempStore() """Schemas used to create and validate forms""" #TODO: move these to a separate module class SignupSchema(colander.MappingSchema): """ Signup form schema for creation and validation. """ username = colander.SchemaNode(colander.String(), validator=colander.Regex(usernameregex, msg)) firstname = colander.SchemaNode(colander.String()) lastname = colander.SchemaNode(colander.String()) password = colander.SchemaNode(colander.String(), validator=colander.Length(min=5, max=100), widget=widget.CheckedPasswordWidget(size=20),
def spectrometerForm(request): """ project form page """ tmpstore = FileUploadTempStore() exp = request.matchdict['experiment_ID'] class Sample(colander.MappingSchema): setup_schema(None, spectrometer) spectrometerSchema = spectrometer.__colanderalchemy__ light_source = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=light_source_choice)) beamsplitter = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=beam_splitter_choice)) detector = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=detector_choice)) optics = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=optics_choice)) type_of_recording = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=recording_choice)) mode_of_recording = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=mode_choice)) setup_schema(None, atr) atrSchema = atr.__colanderalchemy__ prism = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=prism_choice)) setup_schema(None, not_atr) not_atrSchema = not_atr.__colanderalchemy__ window = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=window_choice)) setup_schema(None, transflectance_diffuse) trans_diff_Schema = transflectance_diffuse.__colanderalchemy__ form = Sample() form = deform.Form(form, buttons=('submit', )) if 'submit' in request.POST: # controls = request.POST.items() try: #upload file functionality - sample_power_spectrum as initial example print(controls) appstruct = form.validate(controls) pstruct = peppercorn.parse(controls) print(pstruct) optics = request.params['optics'] beamsplitter = request.params['beamsplitter'] type_of_recording = request.params['type_of_recording'] mode_of_recording = request.params['mode_of_recording'] detector = request.params['detector'] light_source = request.params['light_source'] prism = request.params['prism'] window = request.params['window'] #break through adding schema to db without having to manually enter each one """ok = pstruct['Spectrometerschema'] page = spectrometer(experiment_ID=exp,optics=optics,beamsplitter=beamsplitter,type_of_recording=type_of_recording, detector__type=detector,light_source=light_source,mode_of_recording=mode_of_recording,**ok) request.dbsession.add(page) #try the same for upload and file name to add to db pok = pstruct['atrSchema'] page = atr(**pok,prism_material=prism) request.dbsession.add(page) naok = pstruct['not_atrSchema'] page = not_atr(**naok,sample_window_material=window) request.dbsession.add(page) tran = pstruct['trans_diff_Schema'] page = transflectance_diffuse(**tran) request.dbsession.add(page) appstruct = form.validate(controls) #call validate #need to fix this print(appstruct) experiment_id = request.dbsession.query(spectrometer).filter_by(optics=optics).first() spec_id = experiment_id.spectrometer_ID""" next_url = request.route_url('spectrometerPage', spectrometer_ID=1) return HTTPFound(location=next_url) except deform.ValidationFailure as e: # catch the exception return {'spectrometerForm': e.render()} else: spectrometerForm = form.render() return {'spectrometerForm': spectrometerForm}
def _makeOne(self): # Deform from deform.interfaces import FileUploadTempStore return FileUploadTempStore()
def spectraForm(request): """ project form page """ tmpstore = FileUploadTempStore() class Sample(colander.MappingSchema): setup_schema(None, spectra) spectraSchema = spectra.__colanderalchemy__ type = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=type_choices)) format = colander.SchemaNode( colander.String(), default='', widget=deform.widget.SelectWidget(values=format_choices)) sample_power_spectrum = colander.SchemaNode( deform.FileData(), widget=deform.widget.FileUploadWidget(tmpstore)) background_power_spectrum = colander.SchemaNode( deform.FileData(), widget=deform.widget.FileUploadWidget(tmpstore)) initial_result_spectrum = colander.SchemaNode( deform.FileData(), widget=deform.widget.FileUploadWidget(tmpstore)) final_spectrum = colander.SchemaNode( deform.FileData(), widget=deform.widget.FileUploadWidget(tmpstore)) setup_schema(None, post_processing_and_deposited_spectra) ppSchema = post_processing_and_deposited_spectra.__colanderalchemy__ upload = colander.SchemaNode( deform.FileData(), widget=deform.widget.FileUploadWidget(tmpstore)) form = Sample() form = deform.Form(form, buttons=('submit', )) if 'submit' in request.POST: #add more of these and then work on jcamp graph overlays with javascript try: #appstruct = form.validate(controls) #call validate #upload file functionality - sample_power_spectrum as initial example print(request.POST) controls = request.POST.items() pstruct = peppercorn.parse(controls) print(pstruct) """ this doesnt work for now dirName = request.params['experiment_ID'] dirName = 'C:/ftirdb/ftirdb/data/' + dirName os.mkdir(dirName)""" myfile = pstruct['sample_power_spectrum']['upload'] background = pstruct['background_power_spectrum']['upload'] init = pstruct['initial_result_spectrum']['upload'] final = pstruct['final_spectrum']['upload'] #using pure path as coding on windows and putting on to a linux server permanent_store = pathlib.PureWindowsPath( 'C:/ftirdb/ftirdb/static/data') permanent_file = open( os.path.join(permanent_store, myfile.filename.lstrip(os.sep)), 'wb') shutil.copyfileobj(myfile.file, permanent_file) myfile.file.close() permanent_file.close() permanent_file = open( os.path.join(permanent_store, background.filename.lstrip(os.sep)), 'wb') shutil.copyfileobj(background.file, permanent_file) background.file.close() permanent_file.close() permanent_file = open( os.path.join(permanent_store, init.filename.lstrip(os.sep)), 'wb') shutil.copyfileobj(init.file, permanent_file) init.file.close() permanent_file.close() permanent_file = open( os.path.join(permanent_store, final.filename.lstrip(os.sep)), 'wb') shutil.copyfileobj(final.file, permanent_file) final.file.close() permanent_file.close() print(myfile.filename) #break through adding schema to db without having to manually enter each one ok = pstruct['spectraSchema'] type = request.params['type'] format = request.params['format'] page = spectra(**ok, spectra_type=type, format=format) request.dbsession.add(page) #try the same for upload and file name to add to db pok = pstruct['ppSchema'] sample_power_spectrum = myfile.filename background_power_spectrum = background.filename initial = init.filename final = final.filename searchdb = request.dbsession.query(spectra).order_by( spectra.spectra_ID.desc()).first() spectra_ID = searchdb.spectra_ID print(spectra_ID) page = post_processing_and_deposited_spectra( spectra_ID=spectra_ID, final_published_spectrum=final, sample_power_spectrum=sample_power_spectrum, background_power_spectrum=background_power_spectrum, initial_result_spectrum=initial, **pok) request.dbsession.add(page) #in future change this so it just querys spectra and takes the first option next_url = request.route_url('spectraPage', spectra_ID=spectra_ID) return HTTPFound(location=next_url) except deform.ValidationFailure as e: # catch the exception return {'spectraForm': e.render()} else: spectraForm = form.render() return {'spectraForm': spectraForm}
def save(self, fs): input_file = fs.file ext = get_ext(fs.filename) fullpath = self.create_fullpath(ext) print ext, fullpath output_file = open(fullpath, 'wb') input_file.seek(0) while True: data = input_file.read(2<<16) if not data: break output_file.write(data) output_file.close() return fullpath tmpstore = FileUploadTempStore() class AddSchema(colander.Schema): upload = colander.SchemaNode( FileData(), widget=widget.FileUploadWidget(tmpstore), title='Unggah') def get_form(schema_cls): schema = schema_cls() return Form(schema, buttons=('simpan', 'batalkan')) @view_config(route_name='upload-logo', renderer='templates/upload.pt',