Switch to unified view

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