def import_bmarks(self): """Allow users to upload a bookmark export file for processing""" username = self.matchdict.get('username') # if auth fails, it'll raise an HTTPForbidden exception with ReqAuthorize(self.request): data = {} post = self.POST # We can't let them submit multiple times, check if this user has # an import in process. if ImportQueueMgr.get(username=username, status=NEW): # They have an import, get the information about it and shoot # to the template. return { 'existing': True, 'import_stats': ImportQueueMgr.get_details( username=username) } if post: # we have some posted values files = post.get('import_file', None) if files is not None: storage_dir_tpl = self.settings.get('import_files', '/tmp/bookie') storage_dir = storage_dir_tpl.format( here=self.settings.get('app_root')) out_fname = store_import_file(storage_dir, username, files) # Mark the system that there's a pending import that needs # to be completed q = ImportQueue(username, unicode(out_fname)) DBSession.add(q) DBSession.flush() # Schedule a task to start this import job. tasks.importer_process.delay(q.id) return HTTPFound( location=self.request.route_url('user_import', username=username)) else: msg = self.request.session.pop_flash() if msg: data['error'] = msg else: data['error'] = None return data else: # we need to see if they've got # just display the form return { 'existing': False }
def import_bmarks(request): """Allow users to upload a delicious bookmark export""" rdict = request.matchdict username = rdict.get('username') # if auth fails, it'll raise an HTTPForbidden exception with ReqAuthorize(request): data = {} post = request.POST # we can't let them submit multiple times, check if this user has an # import in process if ImportQueueMgr.get(username=username, status=NEW): # they have an import, get the information about it and shoot to # the template return { 'existing': True, 'import_stats': ImportQueueMgr.get_details(username=username) } if post: # we have some posted values files = post.get('import_file', None) if files is not None: # save the file off to the temp storage out_dir = "{storage_dir}/{randdir}".format( storage_dir=request.registry.settings.get( 'import_files', '/tmp/bookie').format( here=request.registry.settings.get('app_root')), randdir=random.choice(string.letters), ) # make sure the directory exists # we create it with parents as well just in case if not os.path.isdir(out_dir): os.makedirs(out_dir) out_fname = "{0}/{1}.{2}".format( out_dir, username, files.filename) out = open(out_fname, 'w') out.write(files.file.read()) out.close() # mark the system that there's a pending import that needs to # be completed q = ImportQueue(username, out_fname) DBSession.add(q) DBSession.flush() # Schedule a task to start this import job. tasks.importer_process.delay(q.id) return HTTPFound(location=request.route_url('user_import', username=username)) else: msg = request.session.pop_flash() if msg: data['error'] = msg else: data['error'] = None return data else: # we need to see if they've got # just display the form return { 'existing': False }