def index(): forms = OrderedDict() forms['sample'] = SameDiffSample(g.current_lang) forms['upload'] = SameDiffUpload() # forms['link'] = SameDiffLink() if request.method == 'POST': btn_value = request.form['btn'] # email = None is_sample_data = False titles = [] sample_id = '' if btn_value == 'upload': files = [forms['upload'].data['upload'], forms['upload'].data['upload2']] file_paths = filehandler.open_docs(files) f1name = files[0].filename f2name = files[1].filename logger.debug("New from upload: %s & %s", f1name, f2name) both = unicode(_('%(f1)s and %(f2)s', f1=f1name, f2=f2name)) titles = [f1name, both, f2name] # email = forms['upload'].data['email'] elif btn_value == 'sample': sample_sources = [ forms['sample'].data['sample'], forms['sample'].data['sample2'] ] f1name = filehandler.get_sample_title(sample_sources[0]) f2name = filehandler.get_sample_title(sample_sources[1]) sample_id = str(f1name) + str(f2name) existing_doc_id = mongo.results_for_sample('samediff',sample_id) if existing_doc_id is not None: logger.debug("Existing from sample: %s", sample_id) return redirect(request.url + 'results/' + existing_doc_id) logger.debug("New from sample: %s", ", ".join(sample_sources)) file_paths = [ filehandler.get_sample_path(sample_source) for sample_source in sample_sources ] logger.debug(" loading from %s", ", ".join(file_paths)) is_sample_data = True both = unicode(_('%(f1)s and %(f2)s', f1=f1name, f2=f2name)) titles = [f1name, both, f2name] # email = forms['sample'].data['email'] elif btn_value == 'link': url1 = forms['link'].data['link'] url2 = forms['link'].data['link2'] if not 'http://' in url1: url1 = 'http://' + url1 if not 'http://' in url2: url2 = 'http://' + url2 file_paths = [ filehandler.write_to_temp_file(filehandler.download_webpage(url1)), filehandler.write_to_temp_file(filehandler.download_webpage(url2)) ] titles = ['1', 'both', '2'] if btn_value is not None and btn_value is not u'': return process_results(file_paths, titles, sample_id, btn_value) return render_template('samediff/samediff.html', forms=forms.items(), tool_name='samediff', max_file_size_in_mb = g.max_file_size_mb)
def test_write_to_temp_file(self): timestr = time.strftime("%Y%m%d-%H%M%S") data_to_write = "this is some data on %s" % timestr file_path = filehandler.write_to_temp_file(data_to_write) logging.info("Wrote to %s" % file_path) with codecs.open(file_path, 'r', filehandler.ENCODING_UTF_8) as f: data_as_written = f.read() self.assertEqual(data_as_written, data_to_write)