diff --git a/docker-compose.yml b/docker-compose.yml index db69412..3b537fe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,14 +8,14 @@ services: image: madrigal working_dir: /madrigal/source/madpy/djangoMad command: gunicorn djangoMad.wsgi --bind 0.0.0.0:8000 - # command: python manage.py runserver 0.0.0.0:8000 + # command: python manage.py runserver 0.0.0.0:8888 env_file: .env # ports: - # - 8080:8080 + # - 8888:8888 volumes: - '${EXP_DIR}:/madrigal/experiments' - - '/usr/local/madrigal/metadata:/madrigal/metadata' - - '/usr/local/madrigal/source/madpy/djangoMad:/madrigal/source/madpy/djangoMad' + # - '/usr/local/madrigal/metadata:/madrigal/metadata' + - '/home/jespinoza/workspace/madrigal/source/madpy/djangoMad:/madrigal/source/madpy/djangoMad' nginx: container_name: 'madrigal-nginx' diff --git a/nginx/app.conf b/nginx/app.conf index 5c466f8..7eb6919 100644 --- a/nginx/app.conf +++ b/nginx/app.conf @@ -6,6 +6,7 @@ upstream djangomad { server { listen 8080; + client_max_body_size 50M; location /madrigal { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/source/madpy/djangoMad/apps/login/views.py b/source/madpy/djangoMad/apps/login/views.py index 4a585fa..560d012 100755 --- a/source/madpy/djangoMad/apps/login/views.py +++ b/source/madpy/djangoMad/apps/login/views.py @@ -23,7 +23,7 @@ def login_view(request): if user.is_active: login(request, user) - return redirect('/updata') + return redirect(request.GET.get('next')) else: return render(request, "login/login.html" ) else: @@ -35,4 +35,4 @@ def login_view(request): def logout_view(request): logout(request) - return redirect('/') \ No newline at end of file + return redirect('index') \ No newline at end of file diff --git a/source/madpy/djangoMad/apps/updata/forms.py b/source/madpy/djangoMad/apps/updata/forms.py index 3d019e6..281c9e5 100755 --- a/source/madpy/djangoMad/apps/updata/forms.py +++ b/source/madpy/djangoMad/apps/updata/forms.py @@ -20,42 +20,24 @@ import madweb.forms import datetime, time -def getSelection(keyword, args, kwargs): - """getSelection returns '0' if keyword not a key in either args[0] or kwargs, - otherwise the value - - args, kwargs - arguments as passed into SingleExpDefaultForm __init__ - """ - if len(args) == 0 and len(list(kwargs.keys())) == 0: - return('0') # default case when no data passed in - elif len(args) > 0: - # args[0] is data dict argument to bind data - if keyword in args[0]: - return(args[0][keyword]) - else: - return('0') - elif keyword in kwargs: - return(kwargs[keyword]) - elif 'data' in kwargs: - if keyword in kwargs['data']: - return(kwargs['data'][keyword]) - else: - return('0') - else: - return('0') -def getExperimentList(args, kwargs, madWeb, header='Select experiment: '): +def getExperimentList(instrumentId=None): - instrumentsId= int(getSelection('instruments', args, kwargs)) + madDB = madrigal.metadata.MadrigalDB() + madWeb = madrigal.ui.web.MadrigalWeb(madDB) + + if instrumentId: + kinstList = [int(instrumentId)] + else: + kinstList = [0] - kinstList = [int(instrumentsId)] startDate = datetime.datetime(1950,1,1) startDT = datetime.datetime(startDate.year, startDate.month, startDate.day, 0, 0, 0) now = datetime.datetime.now() endDate = datetime.datetime(now.year, 12, 31, 23, 59, 59) endDT = datetime.datetime(endDate.year, endDate.month, endDate.day, 23, 59, 59) experiments = madWeb.getExperimentList(kinstList,startDT, endDT, True) - expListin = [('0', header),] + expListin = [('0', 'Select experiment: '),] for exp in experiments: expListin.append((exp[0], exp[2])) @@ -68,31 +50,49 @@ def getExperimentList(args, kwargs, madWeb, header='Select experiment: '): return(expList) +def getInstrumentList(): + + madDB = madrigal.metadata.MadrigalDB() + madInstData = madrigal.metadata.MadrigalInstrumentData(madDB) + instruments = madInstData.getInstruments(0, True) + instList = [('0', "Select Instrument"), ] + for kinst, instDesc, siteID in instruments: + instList.append((str(kinst), instDesc)) + + return(instList) + class UpdataForm(forms.Form): - def __init__(self, *args, **kwargs): - super(UpdataForm, self).__init__(*args, **kwargs) - madDB = madrigal.metadata.MadrigalDB() - madInstData = madrigal.metadata.MadrigalInstrumentData(madDB) - instruments = madInstData.getInstruments(0, True) - instList = [('0', "Select Instrument"), ] - for kinst, instDesc, siteID in instruments: - instList.append((str(kinst), instDesc)) - instrumentSelection = getSelection('instruments', args, kwargs) - self.fields['instruments'] = django.forms.ChoiceField(widget = django.forms.Select(attrs={"onChange":'populateExp(this)'}), - choices=instList, - initial=instrumentSelection, - label='Instrument:') - - madWebObj = madrigal.ui.web.MadrigalWeb(madDB) - experimentSelection = getSelection('experiments', args, kwargs) - self.fields['experiments'] = django.forms.ChoiceField(choices=getExperimentList(args, kwargs, madWebObj), - initial=experimentSelection, - required=False, label='Experiment:') - - description = forms.CharField(widget=forms.Textarea(attrs={'cols': 40,'rows': 3, 'style': 'resize:none'}), label='Description') - type = forms.ChoiceField(choices=[('0', 'Public'),('1', 'Private')], initial=0,widget=forms.RadioSelect(attrs={'class': 'custom-radio'})) - file = forms.FileField(label='Select Files', widget=forms.ClearableFileInput(attrs={'multiple': True})) + CHOICES=[('new','New Instrument/Experiment'), + ('existing','Existing')] + choose = forms.ChoiceField( + choices=CHOICES, + widget=forms.RadioSelect(attrs={'class': 'custom-radio', 'onClick': 'updateRadio(this);'}), + initial='existing') + inst = forms.IntegerField(min_value=1, label='Instrument ID:', disabled=True, required=False) + exp = forms.CharField(max_length=40, label='Experiment Title:', disabled=True, required=False) + instruments = forms.ChoiceField( + widget = django.forms.Select(attrs={"onChange":'populateExp(this);'}), + choices=getInstrumentList(), + required=False, + label='Instruments:') + + experiments = forms.ChoiceField( + choices=[], + required=False, + label='Experiments:') + permission = forms.ChoiceField( + choices=[('0', 'Public'),('1', 'Private')], + initial=0, + widget=forms.RadioSelect(attrs={'class': 'custom-radio'})) + description = forms.CharField( + widget=forms.Textarea(attrs={'cols': 40,'rows': 3, 'style': 'resize:none'}), + required=False, + label='Description') + optchar = forms.CharField(max_length=1, label='Optional Char:', required=False) + file = forms.FileField( + label='Select Files', + widget=forms.ClearableFileInput(attrs={'multiple': True})) class ExpForm(forms.Form): """SingleExpInstForm is a Form class for the instrument select field in the Single Experiment interface. @@ -100,9 +100,7 @@ class ExpForm(forms.Form): """ def __init__(self, *args, **kwargs): super(ExpForm, self).__init__(*args, **kwargs) - madDB = madrigal.metadata.MadrigalDB() - madWebObj = madrigal.ui.web.MadrigalWeb(madDB) - experimentSelection = getSelection('experiments', args, kwargs) - self.fields['experiments'] = django.forms.ChoiceField(choices=getExperimentList(args, kwargs, madWebObj), - initial=experimentSelection, - required=False, label='Experiment') + self.fields['experiments'] = forms.ChoiceField( + choices=getExperimentList(args[0]['instrument']), + # initial=kwargs['exp'], + required=False, label='Experiments') diff --git a/source/madpy/djangoMad/apps/updata/templates/updata/experiments.html b/source/madpy/djangoMad/apps/updata/templates/updata/experiments.html index e5b0b41..8a5489d 100755 --- a/source/madpy/djangoMad/apps/updata/templates/updata/experiments.html +++ b/source/madpy/djangoMad/apps/updata/templates/updata/experiments.html @@ -1,6 +1,4 @@ -
- {{ form.experiments.label }} -
+
diff --git a/source/madpy/djangoMad/apps/updata/templates/updata/index.html b/source/madpy/djangoMad/apps/updata/templates/updata/index.html index 71e817a..6310ca6 100755 --- a/source/madpy/djangoMad/apps/updata/templates/updata/index.html +++ b/source/madpy/djangoMad/apps/updata/templates/updata/index.html @@ -6,14 +6,31 @@ function populateExp(select) { var kinst = select.options[select.selectedIndex].value; - var url = "{% url 'updata:get_experiments' %}" + '?instruments=' + kinst; + var url = "{% url 'updata:get_experiments' %}" + '?instrument=' + kinst; // first delete all forms that are now out of date - divIndex = $(".single_form").index($("#experiments")) - $(".single_form").slice(divIndex).empty() + //divIndex = $(".single_form").index($("#id_experiments")) + //$(".single_form").slice(divIndex).empty() // second populate the categories html - $(".single_form").slice(divIndex, divIndex + 1).load(url); + //$(".single_form").slice(divIndex, divIndex + 1).load(url); + $("#id_experiments").load(url); } + function updateRadio(myRadio) { + if (myRadio.value == 'new') { + $( "#id_exp" ).prop( "disabled", false ); + $( "#id_inst" ).prop( "disabled", false ); + $( "#id_experiments" ).prop( "disabled", true ); + $( "#id_instruments" ).prop( "disabled", true ); + } else { + $( "#id_exp" ).prop( "disabled", true ); + $( "#id_inst" ).prop( "disabled", true ); + $( "#id_experiments" ).prop( "disabled", false ); + $( "#id_instruments" ).prop( "disabled", false ); + } + + } + + {% endblock %} @@ -32,70 +53,7 @@ {% csrf_token %} -{% block description %} - -
-
- {{ form.description.label }} -
- -
-
- {{ form.description }} -
-
-
- -{% endblock %} - -{% block file %} -
-
- {{ form.file.label }} -
- -
-
- {{ form.file }} -
-
-
- -{% endblock %} - -{% block type %} -
- -
-
- {{ form.type }} -
-
-
- -{% endblock %} - -{% block instruments %} - -
- - {% if form.instruments %} - {% include "madweb/instruments.html" %} - {% endif %} - -
-{% endblock %} - -{% block experiments %} - -
- - {% if form.experiments %} - {% include "updata/experiments.html" %} - {% endif %} - -
-{% endblock %} +{{ form.as_p }} diff --git a/source/madpy/djangoMad/apps/updata/views.py b/source/madpy/djangoMad/apps/updata/views.py index a0a5686..ebf2b90 100755 --- a/source/madpy/djangoMad/apps/updata/views.py +++ b/source/madpy/djangoMad/apps/updata/views.py @@ -1,7 +1,7 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import render -from .forms import UpdataForm, ExpForm +from apps.updata.forms import UpdataForm, ExpForm from django.core.files.storage import FileSystemStorage from django.contrib import messages @@ -22,45 +22,77 @@ def index(request): madDB = madrigal.metadata.MadrigalDB() madWebObj = madrigal.ui.web.MadrigalWeb(madDB) siteName, siteList = madWebObj.getSiteInfo() - + err = False if request.method == 'POST': 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) - if form.is_valid(): - try: - description = form.cleaned_data['description'] - instCode = int(form.cleaned_data['instruments']) - expId = form.cleaned_data['experiments'] - perm = int(form.cleaned_data['type']) - - #saving file - for f in files: - fs = FileSystemStorage(location='/tmp') - fs.save(f.name, f) - madExp = madrigal.metadata.MadrigalExperiment() - filepath = os.path.join('/tmp', f.name) - expTitle = madExp.getExpNameByExpId(expId) - - dbAdminObj.createMadrigalExperiment(filepath,expTitle, perm, description, instCode) - + description = request.POST.get('description') + perm = int(request.POST.get('permission')) + optchar = request.POST.get('optchar').strip() + first = True + for f in files: + fs = FileSystemStorage(location='/tmp') + fs.save(f.name, f) + filename = os.path.join('/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, 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, 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() - 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() - messages.success( - request, 'Experimento(s) creado(s) exitosamente') - form = UpdataForm() - - except Exception as e: - messages.error( - request, str(e)) else: form = UpdataForm()