Parent: [c9d94b] (diff)

Child: [c22082] (diff)

Download this file

models.py    593 lines (530 with data), 25.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import importlib
from django.db import models
from datetime import datetime
from random import randrange
from pygal import Radar, Bar
from pygal.style import NeonStyle, DarkSolarizedStyle, LightSolarizedStyle, LightStyle, CleanStyle, RedBlueStyle, DarkColorizedStyle, LightColorizedStyle, TurquoiseStyle, LightGreenStyle, DarkGreenStyle, DarkGreenBlueStyle, BlueStyle
from entity.models import Entity
from OSSEval.utils import xmlMinidom
from xml.dom.minidom import Node
class Methodology(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=2000,null=True,blank=True)
documentation = models.TextField(null=True,blank=True)
active = models.BooleanField(default=True)
# The entity type the methodology helps you assess
entity = models.ForeignKey(Entity)
def __str__(self):
return self.name
def from_xml(self, xmldoc, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
self.description = xmlMinidom.getString(xmldoc, 'Description')
self.documentation = xmlMinidom.getString(xmldoc, 'Documentation')
self.active = xmlMinidom.getStringAttribute(xmldoc, 'Active')
e = Entity()
xml_entity = xmldoc.getElementsByTagName('Entity')[0]
e.from_xml(xml_entity, insert)
self.entity = e
# I save so I get the ID (in case insert == True)
self.save()
def to_xml(self):
str_xml = "<Description><![CDATA[" + self.description + "]]></Description>"
str_xml += "<Documentation><![CDATA[" + self.documentation + "]]></Documentation>"
str_xml += self.entity.to_xml()
return '<Methodology Id="' + str(self.id) + '" Name="' + self.name + '" Active="' + str(self.active) + '">' + str_xml + "</Methodology>"
class HasPages(models.Model):
bar_chart = models.TextField(blank=True)
radar_chart = models.TextField(blank=True)
def create_graphs(self, instances, include_max = True):
# I draw the graphs for the report
#styles = [NeonStyle, DarkSolarizedStyle, LightSolarizedStyle, LightStyle, CleanStyle, RedBlueStyle, DarkColorizedStyle, LightColorizedStyle, TurquoiseStyle, LightGreenStyle, DarkGreenStyle, DarkGreenBlueStyle, BlueStyle]
#style=styles[randrange(len(styles))]
style=LightColorizedStyle
bar_chart = Bar(width=300, height=400, explicit_size=True, rounded_bars=5, disable_xml_declaration=True, style=style)
radar_chart = Radar(width=400, height=400, explicit_size=True, legend_at_bottom=True, disable_xml_declaration=True, style=style)
if self.__class__.__name__ == "Page":
radar_chart.title = self.name
else:
radar_chart.title = 'Summary graph'
radar_chart.x_labels = []
instance_scores = {}
for instance in instances:
instance_scores[instance.id] = []
if include_max:
# I use 0 as an id for the max score
instance_scores[0] = []
for page in self.page_set.all():
radar_chart.x_labels.append(page.name)
for instance in instances:
try:
ps = PageScore.objects.get(page=page, instance=instance)
instance_scores[instance.id].append(ps.score)
except:
instance_scores[instance.id].append(0)
if include_max:
instance_scores[0].append(5)
print str(page.id) + " - " + str(page.max_score)
for instance in instances:
radar_chart.add(instance.name, instance_scores[instance.id])
bar_chart.add(instance.name, sum(instance_scores[instance.id]))
if include_max:
radar_chart.add("MAX", instance_scores[0])
bar_chart.add("MAX", sum(instance_scores[0]))
self.radar_chart = radar_chart.render()
self.bar_chart = bar_chart.render()
self.save()
class Meta:
abstract = True
class MethodologyVersion(HasPages):
number = models.IntegerField()
created = models.DateField()
current = models.BooleanField(default=False)
methodology = models.ForeignKey(Methodology)
def __str__(self):
return self.methodology.name + " - " + str(self.number) + (" (Active version)" if self.current else "")
def from_xml(self, xmldoc, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.number = xmldoc.attributes["Number"].firstChild.data
self.created = xmlMinidom.getStringAttribute(xmldoc, 'Created')
self.current = xmlMinidom.getStringAttribute(xmldoc, 'Current')
m = Methodology()
xml_methodology = xmldoc.getElementsByTagName('Methodology')[0]
m.from_xml(xml_methodology, insert)
self.methodology = m
# I save so I get the ID (if insert == True)
self.save()
# Pages
for xml_child in xmldoc.childNodes:
# Some nodes are text nodes (e.g. u'\n ') I need to look just at ELEMENT_NODE
if xml_child.nodeType == Node.ELEMENT_NODE and xml_child.tagName == 'Pages':
xml_pages = xml_child
break
for xml_page in xml_pages.childNodes:
if xml_page.nodeType == Node.ELEMENT_NODE and xml_page.tagName == 'Page':
p = Page()
p.from_xml(xml_page, self, None, insert)
#WeightScenarios
xml_weight_scenarios = xmldoc.getElementsByTagName('WeightScenarios')
for xml_weight_scenario in xml_weight_scenarios:
ws = WeightScenario()
ws.from_xml(xml_weight_scenario, self, insert)
def to_xml(self):
str_xml = self.methodology.to_xml()
str_xml += "<Pages>"
for page in self.page_set.all():
str_xml += page.to_xml()
str_xml += "</Pages>"
str_xml += "<WeightScenarios>"
for weight_scenario in self.weightscenario_set.all():
str_xml += weight_scenario.to_xml()
str_xml += "</WeightScenarios>"
return '<MethodologyVersion Id="' + str(self.id) + '" Number="' + str(self.number) + '" Created="' + str(self.created) + '" Current="' + str(self.current) + '">' + str_xml + "</MethodologyVersion>"
class Page(HasPages):
name = models.CharField(max_length=200)
order = models.IntegerField(null=False,blank=False)
parent = models.ForeignKey('self',null=True,blank=True)
methodology_version = models.ForeignKey(MethodologyVersion,null=True,blank=True)
max_score = 0
def questions(self):
q = list(self.question_set.all())
for p in self.page_set.all():
q += p.questions()
return q
def __str__(self):
return self.name
def from_xml(self, xmldoc, methodology_version, parent_page, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
self.order = xmldoc.attributes["Order"].firstChild.data
if methodology_version is not None:
self.methodology_version = methodology_version
if parent_page is not None:
self.parent = parent_page
self.save()
# Pages
for xml_child in xmldoc.childNodes:
# Some nodes are text nodes (e.g. u'\n ') I need to look just at ELEMENT_NODE
if xml_child.nodeType == Node.ELEMENT_NODE and xml_child.tagName == 'Pages':
xml_pages = xml_child
break
for xml_page in xml_pages.childNodes:
if xml_page.nodeType == Node.ELEMENT_NODE and xml_page.tagName == 'Page':
p = Page()
p.from_xml(xml_page, None, self, insert)
# Questions
for xml_child in xmldoc.childNodes:
# Some nodes are text nodes (e.g. u'\n ') I need to look just at ELEMENT_NODE
if xml_child.nodeType == Node.ELEMENT_NODE and xml_child.tagName == 'Questions':
xml_questions = xml_child
break
for xml_question in xml_questions.childNodes:
if xml_question.nodeType == Node.ELEMENT_NODE and xml_question.tagName == 'Question':
q = Question()
q.from_xml(xml_question, self, insert)
def to_xml(self):
str_xml = "<Pages>"
for page in self.page_set.all():
str_xml += page.to_xml()
str_xml += "</Pages>"
str_xml += "<Questions>"
for question in self.question_set.all():
str_xml += question.to_xml()
str_xml += "</Questions>"
return '<Page Id="' + str(self.id) + '" Name="' + self.name + '" Order="' + str(self.order) + '">' + str_xml + "</Page>"
def calculate_scores(self, instance, weight_scenario, instances):
self.max_score = 5
# Let's reset the score to 0
try:
ps = PageScore.objects.get(page=self, instance=instance)
except:
ps = PageScore(page=self, instance=instance, score=0)
ps.score = 0
# and calculate the score for child pages and add it to current page's score
for page in self.page_set.all():
ps.score += page.calculate_scores(instance, weight_scenario, instances)
# Loop on questions
for question in self.questions():
weight = weight_scenario.question_weight(question.id)
try:
answer = Answer.objects.get(question=question, instance=instance)
answer.score = weight * (answer.value_integer-1)
answer.save()
ps.score += answer.score
except Exception as ex:
#I haven't found an answer, nothing to add
print ex.message
pass
ps.save()
self.create_graphs(instances)
return ps.score
class Meta:
ordering = ['order']
class QuestionType(models.Model):
'''
For future use; at the moment a question is just a multiple choice
'''
name = models.CharField(max_length=200)
def from_xml(self, xmldoc, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
def to_xml(self):
return '<QuestionType Id="' + str(self.id) + '" Name="' + self.name + '"/>'
class Question(models.Model):
page = models.ForeignKey(Page)
text = models.CharField(max_length=200)
order = models.IntegerField()
eval_description = models.TextField(null=True,blank=True)
eval_value = models.TextField(null=True,blank=True)
question_type = models.ForeignKey(QuestionType)
def __str__(self):
return self.page.name + " - " + self.text
def from_xml(self, xmldoc, page, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.text = xmlMinidom.getStringAttribute(xmldoc, 'Text')
self.order = xmlMinidom.getNaturalAttribute(xmldoc, 'Order')
#Do so that QuestionType default is 1
qt = QuestionType.objects.get(pk=1)
self.page = page
self.question_type = qt
self.eval_description = xmlMinidom.getString(xmldoc, 'EvalDescription')
self.eval_value = xmlMinidom.getString(xmldoc, 'EvalValue')
self.save()
# Queries
xml_queries = xmldoc.getElementsByTagName('Query')
for xml_query in xml_queries:
q = Query()
q.from_xml(xml_query, self, insert)
# Choices
xml_choices = xmldoc.getElementsByTagName('Choice')
for xml_choice in xml_choices:
c = Choice()
c.from_xml(xml_choice, self, insert)
def to_xml(self):
str_xml = "<EvalDescription><![CDATA[" + ("" if self.eval_description is None else self.eval_description) + "]]></EvalDescription>"
str_xml += "<EvalValue><![CDATA[" + ("" if self.eval_value is None else self.eval_value) + "]]></EvalValue>"
# We do not use question_type at the moment as we have just one type
# str_xml += self.question_type.to_xml()
str_xml += "<Queries>"
for query in self.query_set.all():
str_xml += query.to_xml()
str_xml += "</Queries>"
str_xml += "<Choices>"
for choice in self.choice_set.all():
str_xml += choice.to_xml()
str_xml += "</Choices>"
return '<Question Id="' + str(self.id) + '" Text="' + self.text + '" Order="' + str(self.order) + '">' + str_xml + '</Question>'
class Meta:
ordering = ['order']
class Query(models.Model):
'''
Queries against web search engines used to automate the answer to a question and/or as a hint to the reviewer
'''
question = models.ForeignKey(Question)
eval_text = models.CharField(max_length=2000)
eval_site = models.CharField(max_length=2000)
eval_site_exclude = models.CharField(max_length=2000)
def from_xml(self, xmldoc, question, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.question = question
self.eval_text = xmlMinidom.getString(xmldoc, 'EvalText')
self.eval_site = xmlMinidom.getString(xmldoc, 'EvalSite')
self.eval_site_exclude = ""
self.save()
def to_xml(self):
str_xml = "<EvalText><![CDATA[" + self.eval_text + "]]></EvalText>"
str_xml += "<EvalSite><![CDATA[" + self.eval_site + "]]></EvalSite>"
return '<Query Id="' + str(self.id) + '">' + str_xml + '</Query>'
class Choice(models.Model):
'''
'''
question = models.ForeignKey(Question)
text = models.CharField(max_length=200)
order = models.IntegerField()
todo = models.CharField(max_length=2000)
def from_xml(self, xmldoc, question, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.question = question
self.text = xmlMinidom.getStringAttribute(xmldoc, 'Text')
self.order = xmlMinidom.getNaturalAttribute(xmldoc, 'Order')
self.todo = xmlMinidom.getString(xmldoc, 'Todo')
self.save()
def to_xml(self):
str_xml = "<Todo>" + self.todo + "</Todo>"
return '<Choice Id="' + str(self.id) + '" Text="' + self.text + '" Order="' + str(self.order) + '">' + str_xml + '</Choice>'
class Meta:
ordering = ['order']
class Weight():
'''
used by WeightScenario so I need to define it first
'''
pass
class WeightScenario(models.Model):
name = models.CharField(max_length=200)
methodology_version = models.ForeignKey(MethodologyVersion)
active = models.BooleanField(blank=False)
def question_weight(self, question_id):
'''
returns the weight; default is 1
'''
weight = 1
for w in self.weight_set.all():
if w.question.id == question_id:
weight = w.weight
return weight
def from_xml(self, xmldoc, methodology_version, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.methodology_version = methodology_version
self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
self.active = xmlMinidom.getStringAttribute(xmldoc, 'Active')
self.save()
#Weights
xml_weights = xmldoc.getElementsByTagName('Weight')
for xml_weight in xml_weights:
w = Weight()
w.from_xml(xml_weight, self, insert)
def to_xml(self):
str_xml = "<Weights>"
for weight in self.weight_set.all():
str_xml += weight.to_xml()
str_xml += "</Weights>"
return '<WeightScenario Id="' + str(self.id) + '" Name="' + self.name + '" Active="' + str(self.active) + '">' + str_xml + '</WeightScenario>'
def __str__(self):
return self.name
class Weight(models.Model):
question = models.ForeignKey(Question)
weight = models.FloatField()
scenario = models.ForeignKey(WeightScenario)
def from_xml(self, xmldoc, scenario, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.scenario = scenario
self.weight = xmlMinidom.getStringAttribute(xmldoc, 'weight')
self.question = Question.objects.get(pk=xmlMinidom.getNaturalAttribute(xmldoc, 'question_id'))
# If I am inserting I must check that there's no other Weight for the same
# couple ('question', 'scenario') as I have a unique_together constraint
if not self.id:
# I look for a Weight for the same question and scenario
try:
w = Weight.objects.get(scenario_id=self.scenario.id, question_id=self.question.id)
w.delete()
except:
# I didn't find one; nothing to do
pass
self.save()
def to_xml(self):
return '<Weight Id="' + str(self.id) + '" question_id="' + str(self.question.id) + '" weight="' + str(self.weight) + '"/>'
class Meta:
unique_together = ('question', 'scenario')
class Analysis(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=2000)
comment = models.TextField(null=True,blank=True)
created = models.DateField(default=datetime.now, blank=True)
methodology_version = models.ForeignKey(MethodologyVersion)
user_login = models.CharField(max_length=50)
visible = models.BooleanField(default=True)
weight_scenario = models.ForeignKey(WeightScenario)
protected = models.BooleanField(default=False)
def __str__(self):
return self.name
def calculate_scores(self, weight_scenario_id):
'''
Calculates scores for each couple (instance, page) and (instance, question)
Stores them on the db
'''
#let's get the weight from the scenario if any; the default is 1
try:
if weight_scenario_id > 0:
weight_scenario = WeightScenario.objects.get(pk=weight_scenario_id)
else:
weight_scenario = self.weight_scenario
except:
# an empty one will set all weights to 1
weight_scenario = WeightScenario()
#loop through the pages to reset scores to 0; when I calculate a question
#then I add its score to the corresponding page
#loop on top level pages and recursive calculation on child pages
for page in self.methodology_version.page_set.all():
for instance in self.instance_set.all():
page.calculate_scores(instance, weight_scenario, self.instance_set.all())
self.methodology_version.create_graphs(self.instance_set.all())
return weight_scenario
# @staticmethod
# def create_graphs(object, pages, instances):
'''
a static method because I need to create the same
'''
def from_xml(self, xmldoc, insert = True):
'''
All but the MethodologyVersion so that we can independently import from xml
MethodologyVersion and Analysis
'''
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
self.comment = xmlMinidom.getStringAttribute(xmldoc, 'Comment')
self.created = xmlMinidom.getStringAttribute(xmldoc, 'Created')
self.methodology_version = MethodologyVersion.objects.get(pk=xmlMinidom.getNaturalAttribute(xmldoc, 'MethodologyVersionId'))
self.user_login = xmlMinidom.getStringAttribute(xmldoc, 'UserLogin')
self.visible = xmlMinidom.getStringAttribute(xmldoc, 'Visible')
self.protected = xmlMinidom.getStringAttribute(xmldoc, 'Protected')
#self.weight_scenario
xml_weight_scenario = xmldoc.getElementsByTagName('WeightScenario')[0]
ws = WeightScenario()
ws.from_xml(xml_weight_scenario, self.methodology_version, insert)
self.weight_scenario = ws
self.save()
#Instances
xml_instances = xmldoc.getElementsByTagName('Instance')
for xml_instance in xml_instances:
i = Instance()
i.from_xml(xml_instance, self, insert)
def to_xml(self):
str_xml = "<Description>" + self.description + "</Description>"
str_xml += "<Comment>" + self.comment + "</Comment>"
str_xml += self.weight_scenario.to_xml()
str_xml += "<Instances>"
for instance in self.instance_set.all():
str_xml += instance.to_xml()
str_xml += "</Instances>"
return '<Analysis Id="' + str(self.id) + '" MethodologyVersionId="' + str(self.methodology_version.id) + '" Name="' + self.name + '" Created="' + str(self.created) + '" Visible="' + str(self.visible) + '" Protected="' + str(self.protected) + '" UserLogin="' + self.user_login + '">' + str_xml + "</Analysis>"
class Instance(models.Model):
"""
It represents one of the entities we evaluate in the assessment
"""
name = models.CharField(max_length=200)
# name_for_search is not used yet; it is intended to provide a name to be used to search the web
name_for_search = models.CharField(max_length=200, default="")
analysis = models.ForeignKey(Analysis)
def __str__(self):
return self.name + " - " + self.name_for_search
def from_xml(self, xmldoc, analysis, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
#ActualInstance
self.analysis = analysis
xml_actual_instance = xmldoc.getElementsByTagName(self.analysis.methodology_version.methodology.entity.actual_entity_class)[0]
module = importlib.import_module(self.analysis.methodology_version.methodology.entity.actual_entity_app + ".models")
actual_entity_class = getattr(module, self.analysis.methodology_version.methodology.entity.actual_entity_class)
self.actual_instance = actual_entity_class()
self.actual_instance.from_xml(xml_actual_instance, self, insert)
self.save()
#Answers
xml_answers = xmldoc.getElementsByTagName('Answer')
for xml_answer in xml_answers:
a = Answer()
a.from_xml(xml_answer, self, insert)
def to_xml(self):
str_xml = "<Answers>"
for answer in self.answer_set.all():
str_xml += answer.to_xml()
str_xml += "</Answers>"
str_xml += self.actual_instance.to_xml()
return '<Instance Id="' + str(self.id) + '" Name="' + self.name + '">' + str_xml + "</Instance>"
class PageScore(models.Model):
page = models.ForeignKey(Page)
instance = models.ForeignKey(Instance)
score = models.FloatField()
class Meta:
unique_together = ('page', 'instance',)
class Answer(models.Model):
"""
value_integer is the value of the answer
score is value_integer * weight
where weight is associated to the question in a WeightScenario
"""
instance = models.ForeignKey(Instance)
question = models.ForeignKey(Question)
value_integer = models.IntegerField()
score = models.FloatField(default=0)
notes = models.CharField(max_length=2000)
def from_xml(self, xmldoc, instance, insert = True):
if not insert:
self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
self.instance = instance
xml_question = xmldoc.getElementsByTagName('Question')[0]
question = Question.objects.get(pk = xmlMinidom.getNaturalAttribute(xml_question, 'Id'))
self.question = question
self.value_integer = xmlMinidom.getNaturalAttribute(xmldoc, 'ValueInteger')
xml_notes = xmldoc.getElementsByTagName('Notes')[0]
try:
self.notes = xml_notes.firstChild.data
except:
self.notes = ""
# If I am inserting I must check that there's no other answer for the same
# couple ('question', 'instance') as I have a unique_together constraint
if not self.id:
# I look for an answer for the same question and instance
try:
a = Answer.objects.get(instance_id=self.instance.id, question_id=self.question.id)
a.delete()
except:
# I didn't find one; nothing to do
pass
self.save()
def to_xml(self):
str_xml = "<Notes>" + self.notes + "</Notes>"
str_xml += '<Question Id="' + str(self.question.id) + '"></Question>'
return '<Answer Id="' + str(self.id) + '" ValueInteger="' + str(self.value_integer) + '">' + str_xml + "</Answer>"
class Meta:
unique_together = ('question', 'instance',)
class Configuration(models.Model):
default_methodology_version = models.ForeignKey(MethodologyVersion, blank=True, null=True)
class UploadedFile(models.Model):
'''
Used to save uploaded xml file so that it can be later retrieved and imported
'''
docfile = models.FileField(upload_to='documents/%Y/%m/%d')
class MyModel(models.Model):
field1 = models.CharField(max_length=40, blank=False, null=False)
field2 = models.CharField(max_length=60, blank=True, null=True)