def clone(cls, oldInstance, newDescription, username): newInstance = cls(description=newDescription, experiment_id=oldInstance.dataset.experiment.id) for param in oldInstance.parameters: if param.name.name not in cls.doNotCopyParams: if param.name.isNumeric(): value = param.numerical_value else: value = param.string_value newInstance.new_param(param.name.name, value) import shutil import os for filename in oldInstance.get_params("uploaded_file", value=True): if filename[-8:] != ".jobfile": thisfile = Dataset_File.objects.get( dataset=oldInstance.dataset, filename=filename) shutil.copy(thisfile.get_absolute_filepath(), get_full_staging_path(username)) newfileurl = os.path.join(get_full_staging_path(username), filename) newDatafile = Dataset_File( dataset=newInstance.dataset, url=newfileurl, protocol="staging", mimetype=thisfile.mimetype, ) newDatafile.save() return newInstance
def render_response_index(request, *args, **kwargs): is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None if ('context_instance' in kwargs): kwargs['context_instance'] = RequestContext(request, kwargs['context_instance']) else: kwargs['context_instance'] = RequestContext(request) kwargs['context_instance']['is_authenticated'] = is_authenticated kwargs['context_instance']['is_superuser'] = is_superuser kwargs['context_instance']['username'] = username staging = get_full_staging_path(username) if staging: kwargs['context_instance']['has_staging_access'] = True else: kwargs['context_instance']['has_staging_access'] = False return render(request, *args, **kwargs)
def setUp(self): # Create test owner without enough details username, email, password = ("testuser", "*****@*****.**", "password") user = User.objects.create_user(username, email, password) # Need UserAuthentication UserAuthentication(userProfile=user.userprofile, username=username, authenticationMethod="localdb").save() # Create staging dir from os import path, makedirs staging_dir = path.join(settings.STAGING_PATH, username) if not path.exists(staging_dir): makedirs(staging_dir) # Ensure that staging dir is set up properly expect(get_full_staging_path(username)).to_be_truthy() # Create test experiment and make user the owner of it experiment = Experiment(title="Text Experiment", institution_name="Test Uni", created_by=user) experiment.save() acl = ObjectACL( pluginId=django_user, entityId=str(user.id), content_object=experiment, canRead=True, isOwner=True, aclOwnershipType=ObjectACL.OWNER_OWNED, ) acl.save() self.dataset = Dataset(description="dataset description...") self.dataset.save() self.dataset.experiments.add(experiment) self.dataset.save() self.username, self.password = (username, password)
def testStageFile(self): client = self._get_authenticated_client() staging_dir = get_full_staging_path(self.username) from os.path import basename from tempfile import NamedTemporaryFile with NamedTemporaryFile('w', dir=staging_dir) as f: # Write some content f.write('This is just some content') f.flush() data = [ f.name ] content_type = 'application/json; charset=utf-8' response = client.post(self._get_staging_url(), data=json.dumps(data), content_type=content_type) # Expect 201 Created expect(response.status_code).to_equal(201) # Expect to get a list of URLs back urls = json.loads(response.content) expect(len(urls)).to_equal(1) # Should have single staging file dataset = Dataset.objects.get(id=self.dataset.id) expect(dataset.dataset_file_set.count()).to_equal(1) datafile = dataset.dataset_file_set.all()[0] expect(datafile.filename).to_equal(basename(f.name)) expect(urlparse(datafile.url).scheme).to_equal('')
def list_staging_files(request, dataset_id): """ Creates an jstree view of the staging area of the user, and provides a selection mechanism importing files. """ staging = get_full_staging_path(request.user.username) if not staging: return HttpResponseNotFound() from_path = staging root = False try: path_var = request.GET.get('path', '') if not path_var: root = True from_path = path.join(staging, urllib2.unquote(path_var)) except ValueError: from_path = staging c = { 'dataset_id': dataset_id, 'directory_listing': staging_list(from_path, staging, root=root), } return HttpResponse(render( request, 'tardis_portal/ajax/list_staging_files.html', c))
def testStageFile(self): client = self._get_authenticated_client() staging_dir = get_full_staging_path(self.username) from os.path import basename from tempfile import NamedTemporaryFile with NamedTemporaryFile('w', dir=staging_dir) as f: # Write some content f.write('This is just some content') f.flush() data = [ f.name ] content_type = 'application/json; charset=utf-8' response = client.post(self._get_staging_url(), data=json.dumps(data), content_type=content_type) # Expect 201 Created expect(response.status_code).to_equal(201) # Expect to get the email address of # staging user back # Can't test for async file staging emails = json.loads(response.content) expect(len(emails)).to_equal(1)
def testStageFile(self): client = self._get_authenticated_client() staging_dir = get_full_staging_path(self.username) from os.path import basename from tempfile import NamedTemporaryFile with NamedTemporaryFile('w', dir=staging_dir) as f: # Write some content f.write('This is just some content') f.flush() data = [f.name] content_type = 'application/json; charset=utf-8' response = client.post(self._get_staging_url(), data=json.dumps(data), content_type=content_type) # Expect 201 Created expect(response.status_code).to_equal(201) # Expect to get the email address of # staging user back # Can't test for async file staging emails = json.loads(response.content) expect(len(emails)).to_equal(1)
def add_staged_file_to_dataset(rel_filepath, dataset_id, username, mimetype="application/octet-stream"): """ add file in user's staging path to a dataset may be replaced by main code functions. quick and dirty hack to get it working """ originfilepath = os.path.join(get_full_staging_path(username), rel_filepath) dataset = Dataset.objects.get(pk=dataset_id) newDatafile = Dataset_File() newDatafile.dataset = dataset newDatafile.size = os.path.getsize(originfilepath) newDatafile.protocol = "tardis" newDatafile.mimetype = mimetype file_dir = "/" + str(dataset.experiment.id) + "/" + str(dataset.id) + "/" file_path = file_dir + rel_filepath prelim_full_file_path = settings.FILE_STORE_PATH + file_path full_file_path = duplicate_file_check_rename(prelim_full_file_path) newDatafile.filename = os.path.basename(full_file_path) newDatafile.url = "%s://%s" % (newDatafile.protocol, full_file_path[ len(settings.FILE_STORE_PATH) + len(file_dir):]) if not os.path.exists(os.path.dirname(full_file_path)): os.makedirs(os.path.dirname(full_file_path)) shutil.move(originfilepath, full_file_path) newDatafile.save()
def testStageFile(self): client = self._get_authenticated_client() staging_dir = get_full_staging_path(self.username) from os.path import basename from tempfile import NamedTemporaryFile with NamedTemporaryFile('w', dir=staging_dir) as f: # Write some content f.write('This is just some content') f.flush() data = [f.name] content_type = 'application/json; charset=utf-8' response = client.post(self._get_staging_url(), data=json.dumps(data), content_type=content_type) # Expect 201 Created expect(response.status_code).to_equal(201) # Expect to get a list of URLs back urls = json.loads(response.content) expect(len(urls)).to_equal(1) # Should have single staging file dataset = Dataset.objects.get(id=self.dataset.id) expect(dataset.dataset_file_set.count()).to_equal(1) datafile = dataset.dataset_file_set.all()[0] expect(datafile.filename).to_equal(basename(f.name)) expect(urlparse(datafile.url).scheme).to_equal('')
def render_response_index(request, *args, **kwargs): is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None kwargs['context_instance'] = RequestContext(request) kwargs['context_instance']['is_authenticated'] = is_authenticated kwargs['context_instance']['is_superuser'] = is_superuser kwargs['context_instance']['username'] = username staging = get_full_staging_path( username) if staging: kwargs['context_instance']['has_staging_access'] = True else: kwargs['context_instance']['has_staging_access'] = False #if request.mobile: # template_path = args[0] # split = template_path.partition('/') # args = (split[0] + '/mobile/' + split[2], ) + args[1:] return render_to_response(*args, **kwargs)
def create_staging_datafiles(files, user_id, dataset_id, is_secure): init_filters() from tardis.tardis_portal.staging import get_full_staging_path def f7(seq): # removes any duplicate files that resulted from traversal seen = set() seen_add = seen.add return [x for x in seq if x not in seen and not seen_add(x)] def list_dir(dir): # returns a list from a recursive directory search file_list = [] for dirname, dirnames, filenames in os.walk(dir): for filename in filenames: file_list.append(os.path.join(dirname, filename)) return file_list user = User.objects.get(id=user_id) staging = get_full_staging_path(user.username) stage_files = [] for f in files: abs_path = '' if f == 'phtml_1': abs_path = staging else: abs_path = path.join(staging, f) if path.isdir(abs_path): stage_files = stage_files + list_dir(abs_path) else: stage_files.append(abs_path) full_file_list = f7(stage_files) protocol = "" if is_secure: protocol = "s" current_site_complete = "http%s://%s" % (protocol, Site.objects.get_current().domain) context = Context({ 'username': user.username, 'current_site': current_site_complete, 'dataset_id': dataset_id, }) subject = '[MyTardis] Import Successful' # traverse directory paths (if any to build file list) job = group( create_staging_datafile.s(f, user.username, dataset_id) for f in full_file_list) if user.email: job = chain(job, email_user_task.s( subject, 'import_staging_success', context, user)) job().delay()
def render_response_index(request, *args, **kwargs): is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None if "context_instance" in kwargs: kwargs["context_instance"] = RequestContext(request, kwargs["context_instance"]) else: kwargs["context_instance"] = RequestContext(request) kwargs["context_instance"]["is_authenticated"] = is_authenticated kwargs["context_instance"]["is_superuser"] = is_superuser kwargs["context_instance"]["username"] = username staging = get_full_staging_path(username) if staging: kwargs["context_instance"]["has_staging_access"] = True else: kwargs["context_instance"]["has_staging_access"] = False return render(request, *args, **kwargs)
def list_staging_files(request, dataset_id): """ Creates an jstree view of the staging area of the user, and provides a selection mechanism importing files. """ staging = get_full_staging_path(request.user.username) if not staging: return HttpResponseNotFound() from_path = staging root = False try: path_var = request.GET.get('path', '') if not path_var: root = True from_path = path.join(staging, urllib2.unquote(path_var)) except ValueError: from_path = staging c = { 'dataset_id': dataset_id, 'directory_listing': staging_list(from_path, staging, root=root), } return HttpResponse( render(request, 'tardis_portal/ajax/list_staging_files.html', c))
def render_response_index(request, *args, **kwargs): is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None kwargs['context_instance'] = RequestContext(request) kwargs['context_instance']['is_authenticated'] = is_authenticated kwargs['context_instance']['is_superuser'] = is_superuser kwargs['context_instance']['username'] = username staging = get_full_staging_path(username) if staging: kwargs['context_instance']['has_staging_access'] = True else: kwargs['context_instance']['has_staging_access'] = False #if request.mobile: # template_path = args[0] # split = template_path.partition('/') # args = (split[0] + '/mobile/' + split[2], ) + args[1:] return render_to_response(*args, **kwargs)
def edit_experiment(request, experiment_id, template="tardis_portal/create_experiment_with_samples.html"): """Edit an existing experiment. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param experiment_id: the ID of the experiment to be edited :type experiment_id: string :param template_name: the path of the template to render :type template_name: string :rtype: :class:`django.http.HttpResponse` """ experiment = Experiment.objects.get(id=experiment_id) c = Context({'subtitle': 'Edit Project', 'user_id': request.user.id, 'experiment_id': experiment_id, }) staging = get_full_staging_path( request.user.username) if staging: c['directory_listing'] = staging_traverse(staging) c['staging_mount_prefix'] = settings.STAGING_MOUNT_PREFIX from .experiments import ExperimentFormHandler if request.method == 'POST': form = ProjectForm(request.POST, request.FILES, instance=experiment, extra=0) if form.is_valid(): full_experiment = form.save(commit=False) experiment = full_experiment['experiment'] experiment.created_by = request.user full_experiment.save_m2m() # Get Wrapper information ExperimentFormHandler(experiment_id).edit_experiment(form.cleaned_data, experiment_id) request.POST = {'status': "Experiment Saved."} return HttpResponseRedirect(reverse( 'tardis.tardis_portal.views.view_experiment', args=[str(experiment.id)]) + "#saved") c['status'] = "Errors exist in form." c["error"] = 'true' else: experiment_handler = ExperimentFormHandler(experiment_id) form = ProjectForm(initial=experiment_handler.form_data(experiment_id), instance=experiment, extra=0) c['form'] = form c['status'] = form.errors return HttpResponse(render_response_index(request, template, c))
def makeJobScripts(self, request): """ create PBS/OGE job submission files, one for each rmsd, pdb file and spacegroup """ time = "12:0:0" pbs_prefix = "#$ " pbs_head = "#!/bin/sh\n" pbs_head += "%s-m abe\n" % pbs_prefix pbs_head += "%s-S /bin/bash\n" % pbs_prefix pbs_head += "%s-cwd\n" % pbs_prefix pbs_head += "%s-l h_rt=%s\n" % (pbs_prefix, time) pbs_commands = "\n. /etc/profile\n" pbs_commands += "module load phenix\n" pbs_commands += ". $PHENIX/build/$PHENIX_MTYPE/setpaths.sh\n" pingurl = request.build_absolute_uri( reverse('tardis.apps.mrtardis.views.jobfinished', args=[self.dataset.id])) wget_command = "wget -O - %s?jobid=$JOB_ID" % pingurl ## ping server ten times with more and more delay pbs_footer = "touch jobid-$JOB_ID.finished\n" pbs_footer += "I=0; while [[ \"true\" != `%s`" % wget_command pbs_footer += "&& $I -lt 10 ]];" pbs_footer += "do echo yes; sleep $(($I*2)); I=$(($I+1)); done" #pbs_footer = wget_command phaser_command = "phenix.phaser" spacegroups = [utils.sgNumNameTrans(number=sgnum) for sgnum in self.get_params("space_group", value=True)] if self.get_param("sg_all", value=True) == "True": spacegroups.append("ALL") rmsds = self.get_params("rmsd", value=True) for pdbfile in self.get_params("PDBfile", value=True): for sg in spacegroups: for rmsd in rmsds: parameters = self.getPhaserCommands(sg, rmsd, pdbfile) output = pbs_head + pbs_commands output += "echo -e \"" + parameters + "\"|" +\ phaser_command + " \n" output += pbs_footer jobfilename = pdbfile + "_" + sg + "_" + \ str(rmsd) + ".jobfile" ofile = open(os.path.join( get_full_staging_path(request.user.username), jobfilename), 'w') ofile.write(output) ofile.close() utils.add_staged_file_to_dataset( jobfilename, self.dataset.id, request.user.username, mimetype="application/x-shellscript") self.new_param("jobscript", jobfilename)
def addFile(request, dataset_id): import shutil from tardis.apps.mrtardis.utils import add_staged_file_to_dataset if "file_id" not in request.POST: return HttpResponseNotFound() file_id = request.POST["file_id"] file = Dataset_File.objects.get(pk=file_id) shutil.copy(file.get_absolute_filepath(), get_full_staging_path(request.user.username)) add_staged_file_to_dataset(file.filename, dataset_id, request.user.username, file.mimetype) return parseMTZfile(request, dataset_id=dataset_id)
def user_details_processor(request): is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None staging = True if get_full_staging_path(username) else False return {'username': username, 'is_authenticated': is_authenticated, 'is_superuser': is_superuser, 'has_staging_access': staging}
def user_details_processor(request): is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None staging = True if get_full_staging_path(username) else False return { 'username': username, 'is_authenticated': is_authenticated, 'is_superuser': is_superuser, 'has_staging_access': staging }
def retrieveFromHPC(self, location="msg"): if self.get_status(value=True) != "readyToRetrieve": return False hpc_username = self.get_param("hpc_username", value=True) user = HPCUser.objects.get(hpc_username=hpc_username) excludefiles = self.get_params("uploaded_file", value=True) hpclink = self.connectToHPC(location, hpc_username) newfiles = hpclink.download(self.get_hpc_dir(), get_full_staging_path(user.user.username), excludefiles=excludefiles) for newfile in newfiles: add_staged_file_to_dataset(newfile, self.dataset.id, user.user.username) hpclink.rmtree(self.get_hpc_dir()) self.set_status("finished") return True
def setUp(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() # Need UserAuthentication UserAuthentication(userProfile=profile, username=username, authenticationMethod='localdb').save() # Create staging dir from os import path, makedirs staging_dir = path.join(settings.STAGING_PATH, username) if not path.exists(staging_dir): makedirs(staging_dir) # Ensure that staging dir is set up properly expect(get_full_staging_path(username)).to_be_truthy() Location.force_initialize() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId=django_user, entityId=str(user.id), experiment=experiment,\ canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() self.dataset = \ Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(experiment) self.dataset.save() self.username, self.password = (username, password)
def import_staging_files(request, dataset_id): """ Creates an jstree view of the staging area of the user, and provides a selection mechanism importing files. """ staging = get_full_staging_path(request.user.username) if not staging: return HttpResponseNotFound() c = { 'dataset_id': dataset_id, 'staging_mount_prefix': settings.STAGING_MOUNT_PREFIX, 'staging_mount_user_suffix_enable': settings.STAGING_MOUNT_USER_SUFFIX_ENABLE, } return HttpResponse( render(request, 'tardis_portal/ajax/import_staging_files.html', c))
def aadd_staged_file_to_dataset(rel_filepath, dataset_id, username, mimetype="application/octet-stream"): """ add file in user's staging path to a dataset may be replaced by main code functions. quick and dirty hack to get it working """ originfilepath = os.path.join(get_full_staging_path(username), rel_filepath) dataset = Dataset.objects.get(pk=dataset_id) newDatafile = Dataset_File( dataset=dataset, url=originfilepath, protocol="staging", mimetype=mimetype, ) newDatafile.save()
def render_response_search(request, *args, **kwargs): from tardis.tardis_portal.views import getNewSearchDatafileSelectionForm is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None links = {} for app in settings.INSTALLED_APPS: if app.startswith('tardis.apps.'): view = '%s.views.search' % app try: links[app.split('.')[2]] = reverse(view) except: pass kwargs['context_instance'] = RequestContext(request) kwargs['context_instance']['is_authenticated'] = is_authenticated kwargs['context_instance']['is_superuser'] = is_superuser kwargs['context_instance']['username'] = username kwargs['context_instance']['searchDatafileSelectionForm'] = \ getNewSearchDatafileSelectionForm(request.GET.get('type', None)) kwargs['context_instance']['links'] = links staging = get_full_staging_path( username) if staging: kwargs['context_instance']['has_staging_access'] = True else: kwargs['context_instance']['has_staging_access'] = False #if request.mobile: # template_path = args[0] # split = template_path.partition('/') # args = (split[0] + '/mobile/' + split[2], ) + args[1:] return render_to_response(*args, **kwargs)
def setUp(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() # Need UserAuthentication UserAuthentication(userProfile=profile, username=username, authenticationMethod='localdb').save() # Create staging dir from os import path, makedirs staging_dir = path.join(settings.STAGING_PATH, username) if not path.exists(staging_dir): makedirs(staging_dir) # Ensure that staging dir is set up properly expect(get_full_staging_path(username)).to_be_truthy() Location.force_initialize() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ObjectACL( pluginId=django_user, entityId=str(user.id), content_object=experiment, canRead=True, isOwner=True, aclOwnershipType=ObjectACL.OWNER_OWNED, ) acl.save() self.dataset = \ Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(experiment) self.dataset.save() self.username, self.password = (username, password)
def render_response_search(request, *args, **kwargs): from tardis.tardis_portal.views import getNewSearchDatafileSelectionForm is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None links = {} for app in settings.INSTALLED_APPS: if app.startswith('tardis.apps.'): view = '%s.views.search' % app try: links[app.split('.')[2]] = reverse(view) except: pass kwargs['context_instance'] = RequestContext(request) kwargs['context_instance']['is_authenticated'] = is_authenticated kwargs['context_instance']['is_superuser'] = is_superuser kwargs['context_instance']['username'] = username kwargs['context_instance']['searchDatafileSelectionForm'] = \ getNewSearchDatafileSelectionForm(request.GET.get('type', None)) kwargs['context_instance']['links'] = links staging = get_full_staging_path(username) if staging: kwargs['context_instance']['has_staging_access'] = True else: kwargs['context_instance']['has_staging_access'] = False #if request.mobile: # template_path = args[0] # split = template_path.partition('/') # args = (split[0] + '/mobile/' + split[2], ) + args[1:] return render_to_response(*args, **kwargs)
def extractPDBzips(self, username): """ Extracts pdb files out of zips, adds them to the dataset and removes the zip. """ zipquery = Dataset_File.objects.filter(dataset=self.dataset, filename__iendswith=".zip") for zipfileobj in zipquery: zippath = zipfileobj.get_absolute_filepath() thiszip = zipfile.ZipFile(zippath, 'r') extractlist = [] for filename in thiszip.namelist(): if filename.endswith((".pdb", ".PDB")) and \ not filename.startswith("__MACOSX"): extractlist.append(filename) thiszip.extractall(get_full_staging_path(username), extractlist) thiszip.close() for pdbfile in extractlist: #print pdbfile utils.add_staged_file_to_dataset( pdbfile, self.dataset.id, username, mimetype="chemical/x-pdb") zipfileobj.deleteCompletely()
def render_response_search(request, *args, **kwargs): from tardis.tardis_portal.views import getNewSearchDatafileSelectionForm is_authenticated = request.user.is_authenticated() if is_authenticated: is_superuser = request.user.is_superuser username = request.user.username else: is_superuser = False username = None links = {} for app in settings.INSTALLED_APPS: if app.startswith("tardis.apps."): view = "%s.views.search" % app try: links[app.split(".")[2]] = reverse(view) except: pass kwargs["context_instance"] = RequestContext(request) kwargs["context_instance"]["is_authenticated"] = is_authenticated kwargs["context_instance"]["is_superuser"] = is_superuser kwargs["context_instance"]["username"] = username kwargs["context_instance"]["searchDatafileSelectionForm"] = getNewSearchDatafileSelectionForm( request.GET.get("type", None) ) kwargs["context_instance"]["links"] = links staging = get_full_staging_path(username) if staging: kwargs["context_instance"]["has_staging_access"] = True else: kwargs["context_instance"]["has_staging_access"] = False return render(request, *args, **kwargs)
def getUser(self, user_dict): """Return a user model based on the user dict. This function is responsible for creating the user within the Django DB and returning the resulting user model. """ from django.contrib.auth.models import User from tardis.tardis_portal.models import UserProfile, UserAuthentication if not self._initialised: self._manual_init() plugin = user_dict['pluginname'] username = '' if not 'id' in user_dict: email = user_dict['email'] username =\ self._authentication_backends[plugin].getUsernameByEmail(email) else: username = user_dict['id'] try: user = UserAuthentication.objects.get(username=username, authenticationMethod=plugin).userProfile.user return user except UserAuthentication.DoesNotExist: pass # length of the maximum username max_length = 30 # the username to be used on the User table if username.find('@') > 0: unique_username = username.partition('@')[0][:max_length] else: unique_username = username[:max_length] # Generate a unique username i = 0 try: while (User.objects.get(username=unique_username)): i += 1 unique_username = username[:max_length - len(str(i))] + str(i) except User.DoesNotExist: pass password = User.objects.make_random_password() user = User.objects.create_user(username=unique_username, password=password, email=user_dict.get("email", "")) user.save() userProfile = UserProfile(user=user, isDjangoAccount=False) userProfile.save() userAuth = UserAuthentication(userProfile=userProfile, username=username, authenticationMethod=plugin) userAuth.save() if settings.STAGING_PROTOCOL == plugin: # to be put in its own function staging_path = get_full_staging_path(username) import os if not os.path.exists(staging_path): os.makedirs(staging_path) os.system('chmod g+w ' + staging_path) os.system('chown ' + username + ' ' + staging_path) return user
def create_experiment(request, template_name='tardis_portal/create_experiment_with_samples.html'): """Create a new experiment view. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param template_name: the path of the template to render :type template_name: string :rtype: :class:`django.http.HttpResponse` """ c = Context({ 'subtitle': 'Create Project', 'user_id': request.user.id, }) staging = get_full_staging_path(request.user.username) if staging: c['directory_listing'] = staging_traverse(staging) c['staging_mount_prefix'] = settings.STAGING_MOUNT_PREFIX if request.method == 'POST': form = ProjectForm(request.POST) if form.is_valid(): full_experiment = form.save(commit=False) # group/owner assignment stuff, soon to be replaced experiment = full_experiment['experiment'] experiment.created_by = request.user full_experiment.save_m2m() # add defaul ACL acl = ExperimentACL(experiment=experiment, pluginId=django_user, entityId=str(request.user.id), canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() request.POST = {'status': "Experiment Created."} # Add wrapper information from .experiments import ExperimentFormHandler ExperimentFormHandler(experiment.id).add_experiment(form.cleaned_data) return HttpResponseRedirect(reverse( 'tardis.tardis_portal.views.view_experiment', args=[str(experiment.id)]) + "#created") c['status'] = "Errors exist in form." c["error"] = 'true' else: form = ProjectForm(extra=1) c['form'] = form c['default_institution'] = settings.DEFAULT_INSTITUTION c['status'] = form.errors return HttpResponse(render_response_index(request, template_name, c))