def add_featured_links(self, request, zip_file, save_dir): structure = peppercorn.parse(request.POST.items()) if structure.has_key('featuredlinks'): featuredlinks = build_featured_links(structure) if featuredlinks: cnxml = get_cnxml_from_zipfile(zip_file) new_cnxml = add_featuredlinks_to_cnxml(cnxml, featuredlinks) files = get_files_from_zipfile(zip_file) save_cnxml(save_dir, new_cnxml, files) return featuredlinks
def cnxml_view(request): check_login(request) form = Form(request, schema=CnxmlSchema) save_dir = os.path.join(request.registry.settings['transform_dir'], request.session['upload_dir']) cnxml_filename = os.path.join(save_dir, 'index.cnxml') transformerror = request.session.get('transformerror') # Check for successful form completion if 'cnxml' in request.POST and form.validate(): cnxml = form.data['cnxml'] # Keep sure we use the standard python ascii string and encode Unicode to xml character mappings if isinstance(cnxml, unicode): cnxml = cnxml.encode('ascii', 'xmlcharrefreplace') # get the list of files from upload.zip if it exists files = [] zip_filename = os.path.join(save_dir, 'upload.zip') if os.path.exists(zip_filename): zip_archive = zipfile.ZipFile(zip_filename, 'r') for filename in zip_archive.namelist(): if filename == 'index.cnxml': continue fp = zip_archive.open(filename, 'r') files.append((filename, fp.read())) fp.close() try: files = get_files_from_zipfile(os.path.join(save_dir, 'upload.zip')) save_cnxml(save_dir, cnxml, files) validate_cnxml(cnxml) except ConversionError as e: return render_conversionerror(request, e.msg) # Return to preview return HTTPFound(location=request.route_url('preview'), request=request) # Read CNXML try: with open(cnxml_filename, 'rt') as fp: cnxml = fp.read() except IOError: raise HTTPNotFound('index.cnxml not found') # Clean CNXML cnxml = clean_cnxml(cnxml) cnxml = cnxml.decode('utf-8') cnxml = unicode(cnxml) return { 'codemirror': True, 'form': FormRenderer(form), 'cnxml': cnxml, 'transformerror': transformerror, }
def preview_save(request): check_login(request) html = request.POST['html'] if isinstance(html, unicode): html = html.encode('ascii', 'xmlcharrefreplace') save_dir = os.path.join(request.registry.settings['transform_dir'], request.session['upload_dir']) # Save new html file from preview area save_and_backup_file(save_dir, 'index.html', html) conversionerror = '' # Get the title from aloha html. We have to do this using a separate # parse operation, because aloha_to_etree below does not give us a # tree on which path() works. A bug or this developer is just stumped. tree = etree.fromstring(html, etree.HTMLParser()) try: edited_title = tree.xpath('/html/head/title/text()')[0] request.session['title'] = edited_title except IndexError: if not request.session.get('title', None): request.session['title'] = 'Untitled Document' #transform preview html to cnxml cnxml = None try: tree = aloha_to_etree(html) #1 create structured HTML5 tree canonical_html = etree.tostring(tree, pretty_print=True) cnxml = etree_to_valid_cnxml(tree, pretty_print=True) except Exception as e: #return render_conversionerror(request, str(e)) conversionerror = str(e) if cnxml is not None: save_and_backup_file(save_dir, 'index.cnxml', cnxml) save_and_backup_file(save_dir, 'index.structured.html', canonical_html) files = get_files_from_zipfile(os.path.join(save_dir, 'upload.zip')) save_zip(save_dir, cnxml, canonical_html, files) try: validate_cnxml(cnxml) except ConversionError as e: #return render_conversionerror(request, str(e)) conversionerror = str(e) response = Response(json.dumps({'saved': True, 'error': conversionerror})) response.content_type = 'application/json' return response
def preview_save(request): check_login(request) html = request.POST['html'] if isinstance(html, unicode): html = html.encode('ascii', 'xmlcharrefreplace') save_dir = os.path.join(request.registry.settings['transform_dir'], request.session['upload_dir']) # Save new html file from preview area save_and_backup_file(save_dir, 'index.html', html) conversionerror = '' #transform preview html to cnxml cnxml = None try: structured_html = aloha_to_html(html) #1 create structured HTML5 # parse the new title from structured HTML tree = etree.fromstring(structured_html, etree.HTMLParser()) try: edited_title = tree.xpath('/html/head/title/text()')[0] request.session['title'] = edited_title except: request.session['title'] = 'Untitled Document' cnxml = html_to_valid_cnxml(structured_html) #2 create cnxml from structured HTML5 except Exception as e: #return render_conversionerror(request, str(e)) conversionerror = str(e) if cnxml is not None: save_and_backup_file(save_dir, 'index.cnxml', cnxml) files = get_files_from_zipfile(os.path.join(save_dir, 'upload.zip')) save_zip(save_dir, cnxml, html, files) try: validate_cnxml(cnxml) except ConversionError as e: #return render_conversionerror(request, str(e)) conversionerror = str(e) response = Response(json.dumps({'saved': True, 'error': conversionerror})) response.content_type = 'application/json' return response
def do_transition(self, form=None, request=None): errors = {} session = self.session request = self.request workspaces = self.workspaces self.target_module_url = session.get('target_module_url', None) # Check for successful form completion if form.validate(): # Find what the user actually wanted to do. # This is important since we don't want to upload the module to cnx # if the user clicked on the back button. action = self.request.params.get( 'btn-forward') and 'forward' or 'back' if action == 'forward': self.set_selected_workspace(form.data['workspace']) self.update_session(session, self.remember_fields, form) # Reconstruct the path to the saved files save_dir = os.path.join( request.registry.settings['transform_dir'], session['upload_dir']) # Create a connection to the sword service conn = self.get_connection() # Send zip file to Connexions through SWORD interface with open(os.path.join(save_dir, 'upload.zip'), 'rb') as zip_file: # Create the metadata entry metadata_entry = self.get_metadata_entry(form, session) title = form.data['title'] # get the cnxml file from zip and update it updated_cnxml = self.update_cnxml(request, zip_file) # write cnxml et al to server and zip filename files = get_files_from_zipfile(zip_file) save_cnxml(save_dir, updated_cnxml, files, title=title) if self.target_module_url: # this is an update not a create deposit_receipt = self.update_module( save_dir, conn, metadata_entry, self.target_module_url) else: # this is a workaround until I can determine why the # featured links don't upload correcly with a multipart # upload during module creation. See redmine issue 40 # TODO: # Fix me properly! if self.featured_links: deposit_receipt = create_module_in_2_steps( form, conn, metadata_entry, zip_file, save_dir) else: deposit_receipt = self.create_module( form, conn, metadata_entry, zip_file) # Remember to which workspace we submitted session['deposit_workspace'] = workspaces[[ x[0] for x in workspaces ].index(form.data['workspace'])][1] # The deposit receipt cannot be pickled, so we pickle the xml session['deposit_receipt'] = deposit_receipt.to_xml() else: errors.update(form.errors) return errors
def do_transition(self, form=None, request=None): errors = {} session = self.session request = self.request workspaces = self.workspaces self.target_module_url = session.get('target_module_url', None) # Check for successful form completion if form.validate(): # Find what the user actually wanted to do. # This is important since we don't want to upload the module to cnx # if the user clicked on the back button. action = self.request.params.get('btn-forward') and 'forward' or 'back' if action == 'forward': self.set_selected_workspace(form.data['workspace']) self.update_session(session, self.remember_fields, form) # Reconstruct the path to the saved files save_dir = os.path.join( request.registry.settings['transform_dir'], session['upload_dir'] ) # Create a connection to the sword service conn = self.get_connection() # Send zip file to Connexions through SWORD interface with open(os.path.join(save_dir, 'upload.zip'), 'rb') as zip_file: # Create the metadata entry metadata_entry = self.get_metadata_entry(form, session) title = form.data['title'] # get the cnxml file from zip and update it updated_cnxml = self.update_cnxml(request, zip_file) # write cnxml et al to server and zip filename files = get_files_from_zipfile(zip_file) save_cnxml(save_dir, updated_cnxml, files, title=title) if self.target_module_url: # this is an update not a create deposit_receipt = self.update_module( save_dir, conn, metadata_entry, self.target_module_url) else: # this is a workaround until I can determine why the # featured links don't upload correcly with a multipart # upload during module creation. See redmine issue 40 # TODO: # Fix me properly! if self.featured_links: deposit_receipt = create_module_in_2_steps( form, conn, metadata_entry, zip_file, save_dir) else: deposit_receipt = self.create_module( form, conn, metadata_entry, zip_file) # Remember to which workspace we submitted session['deposit_workspace'] = workspaces[[x[0] for x in workspaces].index(form.data['workspace'])][1] # The deposit receipt cannot be pickled, so we pickle the xml session['deposit_receipt'] = deposit_receipt.to_xml() else: errors.update(form.errors) return errors