from django.contrib.auth.decorators import login_required from django.shortcuts import render from apps.updata.forms import UpdataForm, ExpForm from django.core.files.storage import FileSystemStorage from django.contrib import messages from django.conf import settings import os import glob import datetime # madrigal imports import madrigal.metadata import madrigal.ui.web import madrigal.admin @login_required(login_url='/{}/accounts/login/'.format(settings.BASE_URL)) def index(request): ''' Uploading experiments data view. Allows user to upload experiment files ''' dbAdminObj = madrigal.admin.MadrigalDBAdmin() madDB = madrigal.metadata.MadrigalDB() madWebObj = madrigal.ui.web.MadrigalWeb(madDB) madMeta = madrigal.metadata.MadrigalInstrument(madDB) siteName, siteList = madWebObj.getSiteInfo() err = False if request.method == 'POST': for root, folders, tmp_files in os.walk('/madrigal/experiments/tmp/'): for f in tmp_files: os.remove(os.path.join(root, f)) form = UpdataForm(request.POST, request.FILES) files = request.FILES.getlist('file') # files.sort() filenames = [] choose = request.POST.get('choose') if choose == 'new': instCode = int(request.POST.get('inst')) expTitle = request.POST.get('exp') else: instCode = int(request.POST.get('instruments')) expId = request.POST.get('experiments') madExp = madrigal.metadata.MadrigalExperiment() expTitle = madExp.getExpNameByExpId(expId) description = request.POST.get('description') perm = int(request.POST.get('permission')) optchar = request.POST.get('optchar').strip() category = int(request.POST.get('category')) first = True if len(files) == 1: fs = FileSystemStorage(location='/madrigal/experiments/tmp') fs.save(files[0].name, files[0]) filename = os.path.join('/madrigal/experiments/tmp', files[0].name) fileInfo = madrigal.data.MadrigalFile(filename, madDB) sTime = fileInfo.getEarliestTime() startTime = datetime.datetime(sTime[0],sTime[1],sTime[2],sTime[3],sTime[4],sTime[5]) exp_list = madWebObj.getExpsOnDate(instCode, startTime.year, startTime.month, startTime.day) ext = filename.split('.')[-1] if ext not in ('hdf5', 'h5'): convert = True else: convert = False expDir = os.path.join('/madrigal/experiments/', startTime.strftime('%Y'), madMeta.getInstrumentMnemonic(instCode), startTime.strftime('%d%b%y').lower()) if os.path.exists(expDir): #expDir = exp_list[0][2] try: if category==1 and os.path.exists(os.path.join(expDir, files[0].name)): dbAdminObj.overwriteMadrigalFile(expDir, filename, notify=True) messages.warning(request, 'Filename {} has been overwritten'.format(files[0].name) ) else: dbAdminObj.addMadrigalFile(expDir, filename, perm, description, category=category, updateToMad3=convert) messages.warning(request, 'Filename {} has been added with category={}'.format(files[0].name, category) ) except Exception as e: err = True messages.error(request, 'An error occur adding file {}: {}'.format(filename.split('/')[-1], e) ) else: if os.path.exists(os.path.join('/madrigal/experiments/tmp', 'overview', files[0].name + '.summary')): os.remove(os.path.join('/madrigal/experiments/tmp', 'overview', files[0].name + '.summary')) try: expDir = dbAdminObj.createMadrigalExperiment(filename, expTitle, perm, description, instCode, category=category, optChar=optchar, updateToMad3=convert) except Exception as e: err = True messages.error(request, 'An error occur creating the experiment {}: {}'.format(expTitle, e) ) filenames.append(filename.split('/')[-1]) else: for f in files: fs = FileSystemStorage(location='/madrigal/experiments/tmp') fs.save(f.name, f) filename = os.path.join('/madrigal/experiments/tmp', f.name) ext = filename.split('.')[-1] if ext not in ('hdf5', 'h5'): convert = True else: convert = False if first: first = False try: expDir = dbAdminObj.createMadrigalExperiment(filename, expTitle, perm, description, instCode, category=category, optChar=optchar, updateToMad3=convert) except Exception as e: err = True messages.error(request, 'An error occur creating the experiment {}: {}'.format(expTitle, e) ) break else: try: dbAdminObj.addMadrigalFile(expDir, filename, perm, description, category=category, updateToMad3=convert) except Exception as e: err = True messages.error(request, 'An error occur adding file {}: {}'.format(filename.split('/')[-1], e) ) filenames.append(filename.split('/')[-1]) os.remove(filename) if not err: messages.success(request, 'Experiment {} created succesfully with files: {}'.format(expTitle, filenames) ) madInstParams = madrigal.metadata.MadrigalInstrumentParameters() madInstKindats = madrigal.metadata.MadrigalInstrumentKindats() print('*** Updating local metadata ***') dbAdminObj.__updateLocalMetadata__() print('*** Rebuilding instParmTab.txt ***') madInstParams.rebuildInstParmTable() print('*** Rebuilding instKindatTab.txt ***') madInstKindats.rebuildInstKindatTable() form = UpdataForm() else: form = UpdataForm() return render(request, 'updata/index.html', { 'form': form, 'site_name': siteName, 'site_list': siteList, }) def get_experiments(request): """get_experiments is a Ajax call that returns the experiments select html to support the updata UI. Called when a user modifies the intruments select field. Inputs: request """ form = ExpForm(request.GET) return render(request, 'updata/experiments.html', {'form': form})