def projinfo (request, webargs): """Return project and dataset configuration information""" try: return django.http.HttpResponse(ocpcarest.projInfo(webargs), mimetype="product/hdf5" ) except OCPCAError, e: return django.http.HttpResponseNotFound(e.value)
def projinfo(request, webargs): """Return project and dataset configuration information""" try: return django.http.HttpResponse(ocpcarest.projInfo(webargs), content_type="product/hdf5") except OCPCAError, e: return django.http.HttpResponseNotFound(e.value)
def getProjects(request): try: if request.method == 'POST': if 'filter' in request.POST: #FILTER PROJECTS BASED ON INPUT VALUE userid = request.user.id filteroption = request.POST.get('filteroption') filtervalue = (request.POST.get('filtervalue')).strip() pd = ocpcaproj.OCPCAProjectsDB() # get the visible data sets if request.user.is_superuser: visible_datasets=Dataset.objects.all() else: visible_datasets=Dataset.objects.filter(user_id=userid) | Dataset.objects.filter(public=1) visible_datasets.sort() projs = defaultdict(list) dbs = defaultdict(list) for db in visible_datasets: proj = Project.objects.filter(dataset_id=db.dataset_name, user_id=userid) | Project.objects.filter(dataset_id=db.dataset_name, public=1) if proj: dbs[db.dataset_name].append(proj) else: dbs[db.dataset_name].append(None) if request.user.is_superuser: visible_projects = Project.objects.all() else: visible_projects = Project.objects.filter(user_id=userid) | Project.objects.filter(public=1) return render_to_response('projects.html', { 'databases': dbs.iteritems() ,'projects': visible_projects.values_list(flat=True) },context_instance=RequestContext(request)) elif 'delete_data' in request.POST: pd = ocpcaproj.OCPCAProjectsDB() username = request.user.username project_to_delete = (request.POST.get('project_name')).strip() reftokens = Token.objects.filter(project_id=project_to_delete) if reftokens: messages.error(request, 'Project cannot be deleted. Please delete all tokens for this project first.') return HttpResponseRedirect(get_script_prefix()+'ocpuser/projects') else: proj = Project.objects.get(project_name=project_to_delete) if proj: if proj.user_id == request.user.id or request.user.is_superuser: #Delete project from the table followed by the database. # deleting a project is super dangerous b/c you may delete it out from other tokens. # on other servers. So, only delete when it's on the same server for now pd.deleteOCPCADB(project_to_delete) proj.delete() messages.success(request,"Project deleted") else: messages.error(request,"Cannot delete. You are not owner of this project or not superuser.") else: messages.error( request,"Project not found.") return HttpResponseRedirect(get_script_prefix()+'ocpuser/projects') elif 'delete' in request.POST: pd = ocpcaproj.OCPCAProjectsDB() username = request.user.username project_to_delete = (request.POST.get('project_name')).strip() reftokens = Token.objects.filter(project_id=project_to_delete) if reftokens: messages.error(request, 'Project cannot be deleted. Please delete all tokens for this project first.') return HttpResponseRedirect(get_script_prefix()+'ocpuser/projects') else: proj = Project.objects.get(project_name=project_to_delete) if proj: if proj.user_id == request.user.id or request.user.is_superuser: #Delete project from the table followed by the database. # RBTODO deleting a project is super dangerous b/c you may delete it out from other tokens. # on other servers. So, only delete when it's on the same server for now #if proj.getKVServer()==proj.getDBHost(): # pd.deleteOCPCADB(project_to_delete) proj.delete() messages.success(request,"Project deleted") else: messages.error(request,"Cannot delete. You are not owner of this project or not superuser.") else: messages.error( request,"Project not found.") return HttpResponseRedirect(get_script_prefix()+'ocpuser/projects') elif 'info' in request.POST: #GET PROJECT INFO -----------TODO token = (request.POST.get('roptions')).strip() return HttpResponse(ocpcarest.projInfo(token), content_type="product/hdf5" ) elif 'update' in request.POST: project_to_update =(request.POST.get('project_name')).strip() request.session["project_name"] = project_to_update return redirect(updateProject) elif 'tokens' in request.POST: projname=(request.POST.get('project_name')).strip() request.session["project"] = projname return redirect(getTokens) elif 'channels' in request.POST: projname=(request.POST.get('project_name')).strip() request.session["project"] = projname return redirect(getChannels) elif 'backup' in request.POST: projname=(request.POST.get('project_name')).strip() request.session["project"] = projname return redirect(backupProject) # RB old backup code # # path = settings.BACKUP_PATH + '/' + request.user.username # if not os.path.exists(path): # os.mkdir( path, 0755 ) # # Get the database information # pd = ocpcaproj.OCPCAProjectsDB() # db = (request.POST.get('project_name')).strip() # ofile = path +'/'+ db +'.sql' # outputfile = open(ofile, 'w') # dbuser = settings.DATABASES['default']['USER'] # passwd = settings.DATABASES['default']['PASSWORD'] # # p = subprocess.Popen(['mysqldump', '-u'+ dbuser, '-p'+ passwd, '--single-transaction', '--opt', db], stdout=outputfile).communicate(None) # messages.success(request, 'Sucessfully backed up database '+ db) # return HttpResponseRedirect(get_script_prefix()+'ocpuser/projects') else: # Invalid POST messages.error(request,"Unrecognized POST") return HttpResponseRedirect(get_script_prefix()+'ocpuser/projects') else: # GET Projects userid = request.user.id # get the visible data sets if request.user.is_superuser: visible_datasets=Dataset.objects.all() else: visible_datasets=Dataset.objects.filter(user_id=userid) | Dataset.objects.filter(public=1) dbs = defaultdict(list) for db in visible_datasets: proj = Project.objects.filter(dataset_id=db.dataset_name, user_id=userid) | Project.objects.filter(dataset_id=db.dataset_name, public=1) if proj: dbs[db.dataset_name].append(proj) else: dbs[db.dataset_name].append(None) if request.user.is_superuser: visible_projects = Project.objects.all() else: visible_projects = Project.objects.filter(user_id=userid) | Project.objects.filter(public=1) return render_to_response('projects.html', { 'databases': sorted(dbs.iteritems()) ,'projects':visible_projects },context_instance=RequestContext(request)) except OCPCAError, e: messages.error(request,"Unknown exception in administrative interface = {}".format(e)) # GET Projects username = request.user.username all_datasets= Dataset.objects.all() dbs = defaultdict(list) for db in all_datasets: proj = Project.objects.filter(dataset_id=db.dataset_name, user_id = request.user) if proj: dbs[db.dataset_name].append(proj) else: dbs[db.dataset_name].append(None) all_projects = Project.objects.values_list('project_name',flat= True) return render_to_response('projects.html', { 'databases': dbs.iteritems() ,'projects':all_projects },context_instance=RequestContext(request))
def profile(request): try: if request.method == 'POST': if 'filter' in request.POST: #FILTER PROJECTS BASED ON INPUT VALUE openid = request.user.username filteroption = request.POST.get('filteroption') filtervalue = (request.POST.get('filtervalue')).strip() pd = ocpcaproj.OCPCAProjectsDB() projects = pd.getFilteredProjects ( openid,filteroption,filtervalue ) databases = pd.getDatabases ( openid) dbs = defaultdict(list) for db in databases: proj = pd.getFilteredProjs(openid,filteroption,filtervalue,db[0]); dbs[db].append(proj) return render_to_response('profile.html', { 'projs': projects,'databases': dbs.iteritems() }, context_instance=RequestContext(request)) elif 'delete' in request.POST: #DELETE PROJECT WITH SPECIFIED TOKEN pd = ocpcaproj.OCPCAProjectsDB() openid = request.user.username project = (request.POST.get('projname')).strip() print " Ready to delete " ,project #token = (request.POST.get('token')).strip() pd.deleteOCPCADatabase(project) # pd.deleteTokenDescription(token) #return redirect(profile) elif 'downloadtoken' in request.POST: #DOWNLOAD TOKEN FILE # token = (request.POST.get('token')).strip() token = (request.POST.get('roptions')).strip() print token response = HttpResponse(token,content_type='text/html') response['Content-Disposition'] = 'attachment; filename="ocpca.token"' return response elif 'info' in request.POST: #GET PROJECT INFO -----------TODO token = (request.POST.get('roptions')).strip() #token = (request.POST.get('token')).strip()+"/projinfo/test" return HttpResponse(ocpcarest.projInfo(token), mimetype="product/hdf5" ) elif 'update' in request.POST: #UPDATE PROJECT TOKEN -- FOLOWUP #token = (request.POST.get('token')).strip() token = (request.POST.get('roptions')).strip() print token request.session["token"] = token return redirect(updateproject) elif 'tokens' in request.POST: #View token for the project #import pdb;pdb.set_trace(); print "in view tokens" projname = (request.POST.get('projname')).strip() print projname request.session["project"] = projname #return render_to_response('token.html', # projname, # context_instance=RequestContext(request)) return redirect(tokens) elif 'backup' in request.POST: #BACKUP DATABASE path = '/data/scratch/ocpbackup/'+ request.user.username if not os.path.exists(path): os.mkdir( path, 0755 ) #subprocess.call('whoami') # Get the database information pd = ocpcaproj.OCPCAProjectsDB() db = (request.POST.get('projname')).strip() #Open backupfile ofile = path +'/'+ db +'.sql' outputfile = open(ofile, 'w') p = subprocess.Popen(['mysqldump', '-ubrain', '-p88brain88', '--single-transaction', '--opt', db], stdout=outputfile).communicate(None) # return redirect(profile) messages.success(request, 'Sucessfully backed up database '+ db) return redirect(profile) else: # Invalid POST # pd = ocpcaproj.OCPCAProjectsDB() openid = request.user.username # projects = pd.getProjects ( openid ) projects = pd.getFilteredProjects ( openid ,"","") databases = pd.getDatabases ( openid) dbs = defaultdict(list) for db in databases: proj = pd.getFilteredProjs(openid,"","",db[0]); dbs[db].append(proj) return render_to_response('profile.html', { 'projs': projects, 'databases': dbs.iteritems() },context_instance=RequestContext(request)) else: # GET Option #import pdb;pdb.set_trace() pd = ocpcaproj.OCPCAProjectsDB() openid = request.user.username databases = pd.getDatabases ( openid) dbs = defaultdict(list) for db in databases: proj = pd.getFilteredProjs(openid,"","",db[0]); dbs[db].append(proj) return render_to_response('profile.html', { 'databases': dbs.iteritems() },context_instance=RequestContext(request)) except OCPCAError, e: #import pdb;pdb.set_trace(); messages.error(request, e.value) pd = ocpcaproj.OCPCAProjectsDB() openid = request.user.username projects = pd.getFilteredProjects ( openid ,"","") databases = pd.getDatabases ( openid) dbs = defaultdict(list) for db in databases: proj = pd.getFilteredProjs(openid,"","",db[0]); dbs[db].append(proj) return render_to_response('profile.html', { 'projs': projects, 'databases': dbs.iteritems() },context_instance=RequestContext(request))