The requested changes are too big and content was truncated. Show full diff
@@ -1,160 +1,159 | |||||
1 |
|
1 | |||
2 | from django.contrib.auth.decorators import login_required |
|
2 | from django.contrib.auth.decorators import login_required | |
3 | from django.shortcuts import render |
|
3 | from django.shortcuts import render | |
4 | from apps.updata.forms import UpdataForm, ExpForm |
|
4 | from apps.updata.forms import UpdataForm, ExpForm | |
5 | from django.core.files.storage import FileSystemStorage |
|
5 | from django.core.files.storage import FileSystemStorage | |
6 | from django.contrib import messages |
|
6 | from django.contrib import messages | |
7 |
|
7 | |||
8 | import os |
|
8 | import os | |
9 | import glob |
|
9 | import glob | |
10 | import datetime |
|
10 | import datetime | |
11 |
|
11 | |||
12 | # madrigal imports |
|
12 | # madrigal imports | |
13 | import madrigal.metadata |
|
13 | import madrigal.metadata | |
14 | import madrigal.ui.web |
|
14 | import madrigal.ui.web | |
15 | import madrigal.admin |
|
15 | import madrigal.admin | |
16 |
|
16 | |||
17 | @login_required(login_url='/madrigal/accounts/login/') |
|
17 | @login_required(login_url='/madrigal/accounts/login/') | |
18 | def index(request): |
|
18 | def index(request): | |
19 | ''' |
|
19 | ''' | |
20 | Uploading experiments data view. Allows user to upload experiment files |
|
20 | Uploading experiments data view. Allows user to upload experiment files | |
21 |
|
21 | |||
22 | ''' |
|
22 | ''' | |
23 | dbAdminObj = madrigal.admin.MadrigalDBAdmin() |
|
23 | dbAdminObj = madrigal.admin.MadrigalDBAdmin() | |
24 | madDB = madrigal.metadata.MadrigalDB() |
|
24 | madDB = madrigal.metadata.MadrigalDB() | |
25 | madWebObj = madrigal.ui.web.MadrigalWeb(madDB) |
|
25 | madWebObj = madrigal.ui.web.MadrigalWeb(madDB) | |
26 | siteName, siteList = madWebObj.getSiteInfo() |
|
26 | siteName, siteList = madWebObj.getSiteInfo() | |
27 | err = False |
|
27 | err = False | |
28 | if request.method == 'POST': |
|
28 | if request.method == 'POST': | |
29 | for root, folders, tmp_files in os.walk('/tmp/'): |
|
29 | for root, folders, tmp_files in os.walk('/tmp/'): | |
30 | for f in tmp_files: |
|
30 | for f in tmp_files: | |
31 | os.remove(os.path.join(root, f)) |
|
31 | os.remove(os.path.join(root, f)) | |
32 |
|
32 | |||
33 | form = UpdataForm(request.POST, request.FILES) |
|
33 | form = UpdataForm(request.POST, request.FILES) | |
34 | files = list(request.FILES.getlist('file')) |
|
34 | files = list(request.FILES.getlist('file')) | |
35 | files.sort() |
|
35 | files.sort() | |
36 | filenames = [] |
|
36 | filenames = [] | |
37 |
|
37 | |||
38 | choose = request.POST.get('choose') |
|
38 | choose = request.POST.get('choose') | |
39 | if choose == 'new': |
|
39 | if choose == 'new': | |
40 | instCode = int(request.POST.get('inst')) |
|
40 | instCode = int(request.POST.get('inst')) | |
41 | expTitle = request.POST.get('exp') |
|
41 | expTitle = request.POST.get('exp') | |
42 | else: |
|
42 | else: | |
43 | instCode = int(request.POST.get('instruments')) |
|
43 | instCode = int(request.POST.get('instruments')) | |
44 | expId = request.POST.get('experiments') |
|
44 | expId = request.POST.get('experiments') | |
45 | madExp = madrigal.metadata.MadrigalExperiment() |
|
45 | madExp = madrigal.metadata.MadrigalExperiment() | |
46 | expTitle = madExp.getExpNameByExpId(expId) |
|
46 | expTitle = madExp.getExpNameByExpId(expId) | |
47 |
|
47 | |||
48 | description = request.POST.get('description') |
|
48 | description = request.POST.get('description') | |
49 | perm = int(request.POST.get('permission')) |
|
49 | perm = int(request.POST.get('permission')) | |
50 | optchar = request.POST.get('optchar').strip() |
|
50 | optchar = request.POST.get('optchar').strip() | |
51 | first = True |
|
51 | first = True | |
52 |
|
52 | |||
53 | if len(files) == 1: |
|
53 | if len(files) == 1: | |
54 | fs = FileSystemStorage(location='/tmp') |
|
54 | fs = FileSystemStorage(location='/tmp') | |
55 | fs.save(files[0].name, files[0]) |
|
55 | fs.save(files[0].name, files[0]) | |
56 | filename = os.path.join('/tmp', files[0].name) |
|
56 | filename = os.path.join('/tmp', files[0].name) | |
57 | fileInfo = madrigal.data.MadrigalFile(filename, madDB) |
|
57 | fileInfo = madrigal.data.MadrigalFile(filename, madDB) | |
58 | sTime = fileInfo.getEarliestTime() |
|
58 | sTime = fileInfo.getEarliestTime() | |
59 | startTime = datetime.datetime(sTime[0],sTime[1],sTime[2],sTime[3],sTime[4],sTime[5]) |
|
59 | startTime = datetime.datetime(sTime[0],sTime[1],sTime[2],sTime[3],sTime[4],sTime[5]) | |
60 | exp_list = madWebObj.getExpsOnDate(instCode, startTime.year, startTime.month, startTime.day) |
|
60 | exp_list = madWebObj.getExpsOnDate(instCode, startTime.year, startTime.month, startTime.day) | |
61 | if exp_list and os.path.exists(os.path.join(exp_list[0][2], files[0].name)): |
|
61 | if exp_list and os.path.exists(os.path.join(exp_list[0][2], files[0].name)): | |
62 | expDir = exp_list[0][2] |
|
62 | expDir = exp_list[0][2] | |
63 | try: |
|
63 | try: | |
64 | dbAdminObj.overwriteMadrigalFile(expDir, filename, notify=True) |
|
64 | dbAdminObj.overwriteMadrigalFile(expDir, filename, notify=True) | |
65 | messages.warning(request, |
|
65 | messages.warning(request, | |
66 | 'Filename {} has been overwritten'.format(files[0].name) |
|
66 | 'Filename {} has been overwritten'.format(files[0].name) | |
67 | ) |
|
67 | ) | |
68 | except Exception as e: |
|
68 | except Exception as e: | |
69 | err = True |
|
69 | err = True | |
70 | messages.error(request, |
|
70 | messages.error(request, | |
71 | 'An error occur adding file {}: {}'.format(filename.split('/')[-1], e) |
|
71 | 'An error occur adding file {}: {}'.format(filename.split('/')[-1], e) | |
72 | ) |
|
72 | ) | |
73 | else: |
|
73 | else: | |
74 | fs = FileSystemStorage(location='/tmp') |
|
74 | if os.path.exists(os.path.join('/tmp', 'overview', files[0].name + '.summary')): | |
75 | fs.save(files[0].name, files[0]) |
|
75 | os.remove(os.path.join('/tmp', 'overview', files[0].name + '.summary')) | |
76 | filename = os.path.join('/tmp', files[0].name) |
|
|||
77 | ext = filename.split('.')[-1] |
|
76 | ext = filename.split('.')[-1] | |
78 | if ext not in ('hdf5', 'h5'): |
|
77 | if ext not in ('hdf5', 'h5'): | |
79 | convert = True |
|
78 | convert = True | |
80 | else: |
|
79 | else: | |
81 | convert = False |
|
80 | convert = False | |
82 | try: |
|
81 | try: | |
83 | expDir = dbAdminObj.createMadrigalExperiment(filename, expTitle, perm, description, |
|
82 | expDir = dbAdminObj.createMadrigalExperiment(filename, expTitle, perm, description, | |
84 | instCode, optChar=optchar, updateToMad3=convert) |
|
83 | instCode, optChar=optchar, updateToMad3=convert) | |
85 | except Exception as e: |
|
84 | except Exception as e: | |
86 | err = True |
|
85 | err = True | |
87 | messages.error(request, |
|
86 | messages.error(request, | |
88 | 'An error occur creating the experiment {}: {}'.format(expTitle, e) |
|
87 | 'An error occur creating the experiment {}: {}'.format(expTitle, e) | |
89 | ) |
|
88 | ) | |
90 | filenames.append(filename.split('/')[-1]) |
|
89 | filenames.append(filename.split('/')[-1]) | |
91 | else: |
|
90 | else: | |
92 | for f in files: |
|
91 | for f in files: | |
93 | fs = FileSystemStorage(location='/tmp') |
|
92 | fs = FileSystemStorage(location='/tmp') | |
94 | fs.save(f.name, f) |
|
93 | fs.save(f.name, f) | |
95 | filename = os.path.join('/tmp', f.name) |
|
94 | filename = os.path.join('/tmp', f.name) | |
96 | ext = filename.split('.')[-1] |
|
95 | ext = filename.split('.')[-1] | |
97 | if ext not in ('hdf5', 'h5'): |
|
96 | if ext not in ('hdf5', 'h5'): | |
98 | convert = True |
|
97 | convert = True | |
99 | else: |
|
98 | else: | |
100 | convert = False |
|
99 | convert = False | |
101 |
|
100 | |||
102 | if first: |
|
101 | if first: | |
103 | first = False |
|
102 | first = False | |
104 | try: |
|
103 | try: | |
105 | expDir = dbAdminObj.createMadrigalExperiment(filename, expTitle, perm, description, |
|
104 | expDir = dbAdminObj.createMadrigalExperiment(filename, expTitle, perm, description, | |
106 | instCode, optChar=optchar, updateToMad3=convert) |
|
105 | instCode, optChar=optchar, updateToMad3=convert) | |
107 | except Exception as e: |
|
106 | except Exception as e: | |
108 | err = True |
|
107 | err = True | |
109 | messages.error(request, |
|
108 | messages.error(request, | |
110 | 'An error occur creating the experiment {}: {}'.format(expTitle, e) |
|
109 | 'An error occur creating the experiment {}: {}'.format(expTitle, e) | |
111 | ) |
|
110 | ) | |
112 | break |
|
111 | break | |
113 | else: |
|
112 | else: | |
114 | try: |
|
113 | try: | |
115 | dbAdminObj.addMadrigalFile(expDir, filename, perm, description, updateToMad3=convert) |
|
114 | dbAdminObj.addMadrigalFile(expDir, filename, perm, description, updateToMad3=convert) | |
116 | except Exception as e: |
|
115 | except Exception as e: | |
117 | err = True |
|
116 | err = True | |
118 | messages.error(request, |
|
117 | messages.error(request, | |
119 | 'An error occur adding file {}: {}'.format(filename.split('/')[-1], e) |
|
118 | 'An error occur adding file {}: {}'.format(filename.split('/')[-1], e) | |
120 | ) |
|
119 | ) | |
121 | filenames.append(filename.split('/')[-1]) |
|
120 | filenames.append(filename.split('/')[-1]) | |
122 | os.remove(filename) |
|
121 | os.remove(filename) | |
123 |
|
122 | |||
124 | if not err: |
|
123 | if not err: | |
125 | messages.success(request, |
|
124 | messages.success(request, | |
126 | 'Experiment {} created succesfully with files: {}'.format(expTitle, filenames) |
|
125 | 'Experiment {} created succesfully with files: {}'.format(expTitle, filenames) | |
127 | ) |
|
126 | ) | |
128 |
|
127 | |||
129 | madInstParams = madrigal.metadata.MadrigalInstrumentParameters() |
|
128 | madInstParams = madrigal.metadata.MadrigalInstrumentParameters() | |
130 | madInstKindats = madrigal.metadata.MadrigalInstrumentKindats() |
|
129 | madInstKindats = madrigal.metadata.MadrigalInstrumentKindats() | |
131 |
|
130 | |||
132 | print('*** Updating local metadata ***') |
|
131 | print('*** Updating local metadata ***') | |
133 | dbAdminObj.__updateLocalMetadata__() |
|
132 | dbAdminObj.__updateLocalMetadata__() | |
134 | print('*** Rebuilding instParmTab.txt ***') |
|
133 | print('*** Rebuilding instParmTab.txt ***') | |
135 | madInstParams.rebuildInstParmTable() |
|
134 | madInstParams.rebuildInstParmTable() | |
136 | print('*** Rebuilding instKindatTab.txt ***') |
|
135 | print('*** Rebuilding instKindatTab.txt ***') | |
137 | madInstKindats.rebuildInstKindatTable() |
|
136 | madInstKindats.rebuildInstKindatTable() | |
138 |
|
137 | |||
139 | form = UpdataForm() |
|
138 | form = UpdataForm() | |
140 |
|
139 | |||
141 | else: |
|
140 | else: | |
142 | form = UpdataForm() |
|
141 | form = UpdataForm() | |
143 |
|
142 | |||
144 | return render(request, 'updata/index.html', { |
|
143 | return render(request, 'updata/index.html', { | |
145 | 'form': form, |
|
144 | 'form': form, | |
146 | 'site_name': siteName, |
|
145 | 'site_name': siteName, | |
147 | 'site_list': siteList, |
|
146 | 'site_list': siteList, | |
148 | }) |
|
147 | }) | |
149 |
|
148 | |||
150 |
|
149 | |||
151 | def get_experiments(request): |
|
150 | def get_experiments(request): | |
152 | """get_experiments is a Ajax call that returns the experiments select html to support the |
|
151 | """get_experiments is a Ajax call that returns the experiments select html to support the | |
153 | updata UI. Called when a user modifies the intruments select field. |
|
152 | updata UI. Called when a user modifies the intruments select field. | |
154 |
|
153 | |||
155 | Inputs: |
|
154 | Inputs: | |
156 | request |
|
155 | request | |
157 | """ |
|
156 | """ | |
158 | form = ExpForm(request.GET) |
|
157 | form = ExpForm(request.GET) | |
159 |
|
158 | |||
160 | return render(request, 'updata/experiments.html', {'form': form}) |
|
159 | return render(request, 'updata/experiments.html', {'form': form}) |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now