def copy_file(frompath, fromversion, topath, updated_metadata=None): rec = getRecord(NAMES, frompath, columns=[fromversion, 'type']) version_dict = json.loads(rec[fromversion]) file_type = rec['type'] if updated_metadata: for key, val in updated_metadata.iteritems(): version_dict[key] = val for type in version_dict['types']: prev_subfiles = version_dict['types'][type]['subfiles'] new_subfiles = [] for subfile in prev_subfiles: prev_sub_split = subfile.split('/') prev_sub_vers = prev_sub_split[-1] prev_sub_path = '/'.join(prev_sub_split[:-1]) subrec = getRecord(NAMES, prev_sub_path, columns=['type', prev_sub_vers]) prev_sub_path = prev_sub_path[len(frompath):] prev_type = subrec['type'] new_sub_path = topath + prev_sub_path new_subfile_version = get_new_version_from_path(new_sub_path, file_type=prev_type) insertRecord(NAMES, new_sub_path, columns={new_subfile_version: subrec[prev_sub_vers]}) new_sub_path = new_sub_path + '/' + new_subfile_version new_subfiles.append(new_sub_path) version_dict['types'][type]['subfiles'] = new_subfiles new_file_version = get_new_version_from_path(topath, file_type=file_type) insertRecord(NAMES, topath, columns={new_file_version: json.dumps(version_dict)}) save_file_upload(topath.split('/')[1], topath) return topath + '/' + new_file_version
def upload_processing(request, task_id='', action=False): username = request.GET.get('username') user_authed = False if request.user['is_authenticated']: user_authed = True if username is None: username = request.session['username'] try: upload_rec = get_pending_upload(username, task_id) except: return HttpResponseForbidden() res = AsyncResult(task_id) xhr = request.GET.has_key('xhr') if xhr: json_result = {'state':res.state} if res.state == 'SUCCESS': json_result['path'] = res.result return HttpResponse(simplejson.dumps(json_result, default=json_handler), mimetype='application/json') api = request.GET.has_key('api') if api: json_result = {'state':res.state} if res.state == 'FAILURE': try: remove_pending_upload(username, task_id) except: return HttpResponseServerError("There was an error removing your upload record.") elif res.state == 'SUCCESS': path = res.result json_result['path'] = path if not upload_rec['ephemeral']: try: save_file_upload(username, path) except: return HttpResponseServerError("There was an error saving your upload.") try: remove_pending_upload(username, task_id) except: return HttpResponseServerError("There was an error removing your upload record.") return HttpResponse(simplejson.dumps(json_result, default=json_handler), mimetype='application/json') if user_authed and username == request.session['username'] and action == 'confirm': if upload_rec['task_name'] == 'import_upload': try: remove_pending_upload(username, task_id) except: return HttpResponseServerError("There was an error removing your upload record.") messages.info(request, 'Upload removed') return redirect('users.views.uploads') elif res.state == "FAILURE": try: remove_pending_upload(username, task_id) except: return HttpResponseServerError("There was an error removing your upload record.") messages.info(request, 'Upload removed') return redirect('users.views.uploads') else: path = res.result try: save_file_upload(username, path) except: return HttpResponseServerError("There was an error saving your upload.") try: remove_pending_upload(username, task_id) except: return HttpResponseServerError("There was an error removing your upload record.") messages.info(request, 'Upload saved') return redirect('users.views.uploads') erase = False edit_link = False error_message = None success_message = None if res.state == "FAILURE": if type(res.result).__name__ == "DatabaseError": error_message = "An error occurred when accessing the database." erase = True elif type(res.result).__name__ == "NoDaeFound": error_message = "No DAE file was found in the uploaded archive." erase = True elif type(res.result).__name__ == "ColladaError": error_message = "There was an error parsing the collada file: %s" % str(res.result.orig) erase = True elif type(res.result).__name__ == "ImageError": error_message = "There was an error loading the included texture image: %s" % str(res.result.filename) erase = True elif type(res.result).__name__ == "TooManyDaeFound": error_message = "There is more than one DAE file in the uploaded archive. Click 'Continue' to choose one." edit_link = reverse('content.views.upload_choice', args=[task_id]) elif type(res.result).__name__ == "SubFilesNotFound": error_message = "There were dependencies of the collada file not found in the file uploaded. Click 'Continue' to upload them." edit_link = reverse('content.views.upload', args=[task_id]) else: error_message = "An unknown error has occurred." erase = True elif res.state == "SUCCESS": if upload_rec['task_name'] == 'import_upload': success_message = "File was processed successfully. Click 'Continue' to add information to the file." edit_link = reverse('content.views.upload_import', args=[task_id]) else: success_message = "File was imported successfully. Click 'Continue' to save." edit_link = reverse('content.views.upload_processing', args=['confirm', task_id]) view_params = {'task_id': task_id, 'task_state': res.state, 'error_message': error_message, 'erase': erase, 'edit_link': edit_link, 'success_message': success_message} return render_to_response('content/processing.html', view_params, context_instance = RequestContext(request))