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