Switch to unified view

a/OSSEval/analysis/views.py b/OSSEval/analysis/views.py
1
from django.conf import settings
1
from django.conf import settings
2
from django.core.urlresolvers import reverse
2
from django.core.urlresolvers import reverse
3
from django.http import HttpResponseRedirect
3
from django.http import HttpResponseRedirect, HttpResponse
4
from django.shortcuts import render, get_object_or_404, render_to_response
4
from django.shortcuts import render, get_object_or_404, render_to_response, redirect
5
from django.template import RequestContext
5
from django.template import RequestContext
6
from django.utils import simplejson, timezone
6
from django.views.generic import ListView
7
from django.views.generic import ListView
7
8
from xml.dom import minidom
8
from xml.dom import minidom
9
9
10
from forms import AnalysisForm, UploadFileForm, ImportChoice
10
from forms import AnalysisForm, UploadFileForm, ImportChoice
11
from analysis.models import Analysis, UploadedFile
11
from analysis.models import Analysis, Configuration, Instance, UploadedFile, Answer
12
from methodology.models import Methodology, MethodologyVersion
12
from methodology.models import Methodology, MethodologyVersion, Question
13
from OSSEval.utils import xmlMinidom, TrivialJSONEncoder
13
14
14
def analysis_new(request, analysis_id=0):
15
def analysis_new(request, analysis_id=0):
16
    if request.method == "POST":
15
    if analysis_id>0:
17
        if analysis_id>0:
16
        analysis = get_object_or_404(Analysis, pk=analysis_id)
18
            analysis = get_object_or_404(Analysis, pk=analysis_id)
17
        form = AnalysisForm(instance = analysis)
19
            form = AnalysisForm(instance = analysis)
20
        else:
21
            form = AnalysisForm(request.POST)
22
            if form.is_valid():
23
                cfg = Configuration.objects.get(pk=1)
24
                model_instance = form.save()
25
                model_instance.created = timezone.now()
26
#                 model_instance.methodology_version = cfg.default_methodology_version
27
#                 print "model_instance.methodology_version:  " + str(model_instance.methodology_version.id)
28
                model_instance.save()
29
                return HttpResponseRedirect(reverse('analysis_list'))
18
    else:
30
    else:
19
        form = AnalysisForm()
31
        form = AnalysisForm()
20
    return render(request, 'analysis/analysis_new.html', {'form': form})
32
    return render(request, 'analysis/analysis_new.html', {'form': form})
21
    
33
    
22
34
...
...
32
    exec("from " + entity.actual_entity_app + ".views import search_html_form, instance_list_html")
44
    exec("from " + entity.actual_entity_app + ".views import search_html_form, instance_list_html")
33
    #"Content-Type: text/html; charset=utf-8" has to be removed as these methods return just a partial
45
    #"Content-Type: text/html; charset=utf-8" has to be removed as these methods return just a partial
34
    search_html_ui = str(search_html_form(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
46
    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"):]
47
    analysis_detail = str(instance_list_html(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
36
    
48
    
37
#     exec("from " + entity.actual_entity_app + ".models import " + entity.actual_entity_class)
49
    return render(request, 'analysis/analysis_detail.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'search_html_ui': search_html_ui})
38
#     actual_entity_class = locals()[entity.actual_entity_class]
50
39
#     search_html_ui = actual_entity_class.search_html_ui(request)
51
def analysis_questions(request, analysis_id):
40
#     
52
    analysis = get_object_or_404(Analysis, pk=analysis_id)
41
#     analysis_detail = actual_entity_class.instance_list_html(request, analysis_id)
53
54
    entity = analysis.methodology_version.methodology.entity
42
    
55
    
56
    exec("from " + entity.actual_entity_app + ".views import search_html_form, instance_list_html")
57
    #"Content-Type: text/html; charset=utf-8" has to be removed as these methods return just a partial
58
    analysis_detail = str(instance_list_html(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
59
    
43
    return render(request, 'analysis/analysis_detail.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'search_html_ui': search_html_ui})
60
    return render(request, 'analysis/analysis_questions.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'methodology_version': analysis.methodology_version})
61
62
def save_answer(request):
63
    try:
64
        question_id = request.POST.get("question_id", "")
65
        id_selected_instance = request.POST.get("id_selected_instance", "")
66
        value = request.POST.get("value", "")
67
        notes = request.POST.get("notes", "")
68
        # I look for an answer for the same question and instance
69
        try:
70
            a = Answer.objects.get(instance_id=id_selected_instance, question_id=question_id)
71
        except:
72
            # I didn't find one; let's create it 
73
            a = Answer()
74
            a.instance = Instance.objects.get(pk=id_selected_instance)
75
            a.question = Question.objects.get(pk=question_id)
76
        a.value_integer = value
77
        a.notes = notes
78
        a.save()
79
    except Exception as ex:
80
        return HttpResponse(simplejson.dumps({'response': ex.message})) 
81
    return HttpResponse(simplejson.dumps({'response': 'OK', 'question_id': question_id}))
82
83
def get_answers(request):
84
    id_instance = request.GET.get("id_instance", "")
85
    answers = Answer.objects.filter(instance_id=id_instance)
86
    return HttpResponse(TrivialJSONEncoder().encode(list(answers)))
44
87
45
def export(request, analysis_id):
88
def export(request, analysis_id):
46
    a = get_object_or_404(Analysis, pk=analysis_id)
89
    a = get_object_or_404(Analysis, pk=analysis_id)
47
    mv = a.methodology_version
90
    mv = a.methodology_version
48
    exported_xml = "<osseval>" + a.to_xml() + mv.to_xml() + "</osseval>"
91
    exported_xml = "<osseval>" + a.to_xml() + mv.to_xml() + "</osseval>"
...
...
63
            try:
106
            try:
64
                xmldoc = minidom.parseString(xml_uploaded)
107
                xmldoc = minidom.parseString(xml_uploaded)
65
108
66
                analysis_on_file = Analysis()
109
                analysis_on_file = Analysis()
67
                analysis_xml = xmldoc.getElementsByTagName('Analysis')
110
                analysis_xml = xmldoc.getElementsByTagName('Analysis')
68
                analysis_on_file.id = int(analysis_xml[0].getElementsByTagName('Id')[0].firstChild.data)
111
                analysis_on_file.id = int(analysis_xml[0].attributes["Id"].firstChild.data)
69
                analysis_on_file.name = analysis_xml[0].getElementsByTagName('Name')[0].firstChild.data
112
                analysis_on_file.name = analysis_xml[0].attributes["Name"].firstChild.data
70
                analysis_on_file.created = analysis_xml[0].getElementsByTagName('Created')[0].firstChild.data
113
                analysis_on_file.created = analysis_xml[0].attributes["Created"].firstChild.data
71
                analysis_on_file.user_login = analysis_xml[0].getElementsByTagName('UserLogin')[0].firstChild.data
114
                analysis_on_file.user_login = analysis_xml[0].attributes["UserLogin"].firstChild.data
72
                analysis_on_db = Analysis()
115
                analysis_on_db = Analysis()
73
                try: 
116
                try: 
74
                    analysis_on_db = Analysis.objects.get(pk=analysis_on_file.id)
117
                    analysis_on_db = Analysis.objects.get(pk=analysis_on_file.id)
75
                except:
118
                except:
76
                    pass
119
                    pass
77
                
120
                
78
                methodology_on_file = Methodology()
121
                methodology_on_file = Methodology()
79
                methodology_version_on_file = MethodologyVersion()
122
                methodology_version_on_file = MethodologyVersion()
80
                methodology_version_on_file.methodology = methodology_on_file
123
                methodology_version_on_file.methodology = methodology_on_file
81
                methodology_version_xml = xmldoc.getElementsByTagName('MethodologyVersion')
124
                methodology_version_xml = xmldoc.getElementsByTagName('MethodologyVersion')
82
                methodology_version_on_file.id = methodology_version_xml[0].getElementsByTagName('Id')[0].firstChild.data
125
                methodology_version_on_file.id = xmlMinidom.getNaturalAttribute(methodology_version_xml, 'Id')
83
                methodology_version_on_file.number = methodology_version_xml[0].getElementsByTagName('Number')[0].firstChild.data
126
                methodology_version_on_file.number = methodology_version_xml[0].attributes["Number"].firstChild.data
84
                methodology_xml = methodology_version_xml[0].getElementsByTagName('Methodology')
127
                methodology_xml = methodology_version_xml[0].getElementsByTagName('Methodology')
85
                methodology_on_file.id = methodology_xml[0].getElementsByTagName('Id')[0].firstChild.data
128
                methodology_on_file.id = xmlMinidom.getNaturalAttribute(methodology_xml, 'Id')
86
                methodology_on_file.name = methodology_xml[0].getElementsByTagName('Name')[0].firstChild.data
129
                methodology_on_file.name = methodology_xml[0].attributes["Name"].firstChild.data
87
130
88
                analysis_on_file.methodology_version = methodology_version_on_file
131
                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
132
                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})
133
                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:
134
            except Exception as ex:
...
...
108
    mv = MethodologyVersion()
151
    mv = MethodologyVersion()
109
    if import_methodology:
152
    if import_methodology:
110
        mv.from_xml(methodology_version_xml, always_insert)
153
        mv.from_xml(methodology_version_xml, always_insert)
111
    else:
154
    else:
112
        #If I am not importing the methodology I still need to associate it to the analysis
155
        #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
156
        mv.id = methodology_version_xml.attributes["Id"].firstChild.data
114
    a = Analysis()
157
    a = Analysis()
115
    a.methodology_version = mv
158
    a.methodology_version = mv
116
    analysis_xml = xmldoc.getElementsByTagName('Analysis')[0]
159
    analysis_xml = xmldoc.getElementsByTagName('Analysis')[0]
117
    if import_analysis:
160
    if import_analysis:
118
        a.from_xml(analysis_xml, always_insert)
161
        a.from_xml(analysis_xml, always_insert)
119
    else:
162
    else:
120
        a.id = analysis_xml.getElementsByTagName('Id')[0].firstChild.data
163
        a.id = analysis_xml.attributes["Id"].firstChild.data
121
    return HttpResponseRedirect(reverse('analysis_detail', args=(a.id,)))
164
    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