Parent: [314be8] (diff)

Child: [2c9463] (diff)

Download this file

views.py    143 lines (121 with data), 7.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404, render_to_response
from django.template import RequestContext
from django.views.generic import ListView
from xml.dom import minidom
from forms import AnalysisForm, UploadFileForm, ImportChoice
from analysis.models import Analysis, UploadedFile
from methodology.models import Methodology, MethodologyVersion
def analysis_new(request, analysis_id=0):
if analysis_id>0:
analysis = get_object_or_404(Analysis, pk=analysis_id)
form = AnalysisForm(instance = analysis)
else:
form = AnalysisForm()
return render(request, 'analysis/analysis_new.html', {'form': form})
class AnalysisList(ListView):
queryset = Analysis.objects.order_by('-created')
context_object_name = 'analises_list'
def detail(request, analysis_id):
analysis = get_object_or_404(Analysis, pk=analysis_id)
entity = analysis.methodology_version.methodology.entity
exec("from " + entity.actual_entity_app + ".views import search_html_form, instance_list_html")
#"Content-Type: text/html; charset=utf-8" has to be removed as these methods return just a partial
search_html_ui = str(search_html_form(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
analysis_detail = str(instance_list_html(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
# exec("from " + entity.actual_entity_app + ".models import " + entity.actual_entity_class)
# actual_entity_class = locals()[entity.actual_entity_class]
# search_html_ui = actual_entity_class.search_html_ui(request)
#
# analysis_detail = actual_entity_class.instance_list_html(request, analysis_id)
return render(request, 'analysis/analysis_detail.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'search_html_ui': search_html_ui})
def export(request, analysis_id):
a = get_object_or_404(Analysis, pk=analysis_id)
mv = a.methodology_version
exported_xml = "<osseval>" + a.to_xml() + mv.to_xml() + "</osseval>"
return render(request, 'analysis/export.xml', {'xml': exported_xml}, content_type="application/xhtml+xml")
def upload_page(request):
message = ''
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
xml_uploaded = request.FILES['file'].read()
new_uploaded_file = UploadedFile(docfile = request.FILES['file'])
# we save it on disk so that we can process it after the user has told us which part to import and how to import it
new_uploaded_file.save()
# we parse it so that we check what is on the file against what is on the database and we show this info to the user
try:
xmldoc = minidom.parseString(xml_uploaded)
analysis_on_file = Analysis()
analysis_xml = xmldoc.getElementsByTagName('Analysis')
analysis_on_file.id = int(analysis_xml[0].getElementsByTagName('Id')[0].firstChild.data)
analysis_on_file.name = analysis_xml[0].getElementsByTagName('Name')[0].firstChild.data
analysis_on_file.created = analysis_xml[0].getElementsByTagName('Created')[0].firstChild.data
analysis_on_file.user_login = analysis_xml[0].getElementsByTagName('UserLogin')[0].firstChild.data
analysis_on_db = Analysis()
try:
analysis_on_db = Analysis.objects.get(pk=analysis_on_file.id)
except:
pass
methodology_on_file = Methodology()
methodology_version_on_file = MethodologyVersion()
methodology_version_on_file.methodology = methodology_on_file
methodology_version_xml = xmldoc.getElementsByTagName('MethodologyVersion')
methodology_version_on_file.id = methodology_version_xml[0].getElementsByTagName('Id')[0].firstChild.data
methodology_version_on_file.number = methodology_version_xml[0].getElementsByTagName('Number')[0].firstChild.data
methodology_xml = methodology_version_xml[0].getElementsByTagName('Methodology')
methodology_on_file.id = methodology_xml[0].getElementsByTagName('Id')[0].firstChild.data
methodology_on_file.name = methodology_xml[0].getElementsByTagName('Name')[0].firstChild.data
analysis_on_file.methodology_version = methodology_version_on_file
import_choice_form = ImportChoice(initial={'uploaded_file_id': new_uploaded_file.id, 'new_uploaded_file_relpath': new_uploaded_file.docfile.url}) # An unbound form
return render(request, 'analysis/import_file.html', {'prettyxml': xmldoc.toprettyxml(indent=" "),'file': request.FILES['file'], 'analysis_on_file': analysis_on_file, 'analysis_on_db': analysis_on_db, 'new_uploaded_file': new_uploaded_file, 'import_choice_form': import_choice_form})
except Exception as ex:
message = 'Error parsing uploaded file: ' + str(ex)
else:
form = UploadFileForm()
return render_to_response('analysis/upload_page.html', {'form': form, 'message': message}, context_instance=RequestContext(request))
def perform_import(request):
new_uploaded_file_relpath = request.POST["new_uploaded_file_relpath"]
# how_to_import = true ==> always_insert
always_insert = (int(request.POST.get("how_to_import", "")) == 1)
import_methodology = request.POST.get("import_methodology", "")
import_analysis = request.POST.get("import_analysis", "")
with open(settings.BASE_DIR + "/" + new_uploaded_file_relpath, 'r') as content_file:
xml_uploaded = content_file.read()
xmldoc = minidom.parseString(xml_uploaded)
# I assume there's only one
methodology_version_xml = xmldoc.getElementsByTagName('MethodologyVersion')[0]
mv = MethodologyVersion()
if import_methodology:
mv.from_xml(methodology_version_xml, always_insert)
else:
#If I am not importing the methodology I still need to associate it to the analysis
mv.id = methodology_version_xml.getElementsByTagName('Id')[0].firstChild.data
a = Analysis()
a.methodology_version = mv
analysis_xml = xmldoc.getElementsByTagName('Analysis')[0]
if import_analysis:
a.from_xml(analysis_xml, always_insert)
else:
a.id = analysis_xml.getElementsByTagName('Id')[0].firstChild.data
return HttpResponseRedirect(reverse('analysis_detail', args=(a.id,)))
# what_to_import = request.POST.get("what_to_import", "")
#
# uf = UploadedFile.objects.get(pk=uploaded_file_id)
# response = urllib2.urlopen(uf.docfile.url)
# xml = response.read()
# from forms.ImportChoice
# HOW_TO_IMPORT = [['0','Update if ID exists, create if ID is empty or non existent'],['1','Always create new records']]
# WHAT_TO_IMPORT = [['0','Import methodology'],['1','Import analysis']]
# if how_to_import == 0:
#
# else:
# xml_uploaded = request.FILES['file'].read()
# uploaded_file_id = UploadedFile(docfile = request.FILES['file'])
# HOW_TO_IMPORT = [['0','Update if ID exists, create if ID is empty or non existent'],['1','Always create new records']]
# WHAT_TO_IMPORT = [['0','Import methodology'],['1','Import analysis']]
# how_to_import = forms.ChoiceField( widget=RadioSelect(), choices=HOW_TO_IMPORT)
# what_to_import = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=WHAT_TO_IMPORT, label='What to import (select at least one):')
# uploaded_file_id = forms.CharField(widget=forms.HiddenInput())