def test_save(self):
     eval_id = Evaluation.create('Edit Test Name', self.card_id).id()
     category1_id = EvalCategory.create('Eval Save Test Cat 1', self.card_id).id()
     category2_id = EvalCategory.create('Eval Save Test Cat 2', self.card_id).id()
     item1_id = EvalItem.create('Eval Save Test Item 1', category1_id).id()
     item2_id = EvalItem.create('Eval Save Test Item 2', category1_id).id()
     item3_id = EvalItem.create('Eval Save Test Item 3', category2_id).id()
     text_line1_id = TextLine.create('Eval Save Test Text Line 1', self.card_id).id()
     text_line2_id = TextLine.create('Eval Save Test Text Line 2', self.card_id).id()
     
     response = self.testapp.post('/eval/%d/save' % eval_id, 
         {'comments': 'Comments', 
          'item_%d_score' % item1_id: '1', 
          'item_%d_score' % item2_id: '2', 
          'item_%d_score' % item3_id: '3',
          'text_%d_value' % text_line1_id: 'Text 1',
          'text_%d_value' % text_line2_id: 'Text 2',
         }
     )
     self.assertRedirect('/eval/\d*/fill$', response)
     eval = Evaluation.find_by_id(eval_id)
     data = eval.all_data()
     self.assertEqual('Comments', data['comments'])
     self.assertEqual('1', data['items'][item1_id])
     self.assertEqual('2', data['items'][item2_id])
     self.assertEqual('3', data['items'][item3_id])
     self.assertEqual('Text 1', data['text'][text_line1_id])
     self.assertEqual('Text 2', data['text'][text_line2_id])
 def add(self, card_id):
     card = ReportCard.find_by_id(int(card_id))
     if card.is_authorized():
         text_line_id = TextLine.create(self.request.get('name'), int(card_id)).id()
         text_line = TextLine.find_by_id(text_line_id)
         template = JinjaEnv.get().get_template('templates/text_line/edit_row.html')
         self.response.out.write(template.render({'text_line': text_line}))
 def test_move_up(self):
     text_line1_id = TextLine.create('Move Up Test Name 1', self.card_id).id()
     text_line2_id = TextLine.create('Move Up Test Name 2', self.card_id).id()
     self.assertRaises(Exception, self.testapp.post, '/text_line/%d/move_up' % text_line1_id)
     response = self.testapp.post('/text_line/%d/move_up' % text_line2_id)
     self.assertSuccess(response)
     card = ReportCard.find_by_id(self.card_id)
     text_lines = card.text_lines()
     self.assertEqual(2, len(text_lines))
     self.assertEqual(text_line2_id, text_lines[0].key().id())
     self.assertEqual(text_line1_id, text_lines[1].key().id())
 def test_preview(self):
     e = Evaluation.create('Preview Test Name', self.card_id)
     category1_id = EvalCategory.create('Eval Preview Test Cat 1', self.card_id).id()
     category2_id = EvalCategory.create('Eval Preview Test Cat 2', self.card_id).id()
     item1_id = EvalItem.create('Eval Preview Test Item 1', category1_id).id()
     item2_id = EvalItem.create('Eval Preview Test Item 2', category1_id).id()
     item3_id = EvalItem.create('Eval Preview Test Item 3', category2_id).id()
     text_line1_id = TextLine.create('Eval Preview Test Text Line 1', self.card_id).id()
     text_line2_id = TextLine.create('Eval Preview Test Text Line 2', self.card_id).id()
     response = self.testapp.get('/eval/%d/preview' % e.id())
     self.assertSuccess(response)
 def edit(self, text_line_id):
     text_line = TextLine.find_by_id(int(text_line_id))
     if text_line.card.is_authorized():
         text_line.name = self.request.get('name')
         text_line.save()
         template = JinjaEnv.get().get_template('templates/text_line/edit_row.html')
         self.response.out.write(template.render({'text_line': text_line}))
 def test_edit(self):
     text_line_id = TextLine.create('Edit Test Name', self.card_id).id()
     response = self.testapp.post('/text_line/%d/edit' % text_line_id, {'name': 'Edit Test New Name'})
     self.assertSuccess(response)
     card = ReportCard.find_by_id(self.card_id)
     self.assertEqual(1, len(card.text_lines()))
     self.assertEqual('Edit Test New Name', card.text_lines()[0].name)
 def move_up(self, text_line_id):
     text_line = TextLine.find_by_id(int(text_line_id))
     if text_line.card.is_authorized():
         all_lines = text_line.card.text_lines()
         id_list = map(lambda x: x.key().id(), all_lines)
         index = id_list.index(text_line.key().id())
         if index >= 1:
             temp = text_line.position
             text_line.position = all_lines[index-1].position
             all_lines[index-1].position = temp
             text_line.save()
             all_lines[index-1].save()
         else:
             return webapp2.abort(403)
 def create_or_update(self, text_line_id, evaluation_id, value):
     line = TextLine.find_by_id(text_line_id)
     evaluation = Evaluation.find_by_id(evaluation_id)
     
     data = TextLineData.gql('WHERE text_line = :1 AND evaluation = :2', line, evaluation).fetch(1)
     if len(data) == 0:
         data = TextLineData(parent=self.report_key())
         data.text_line = line
         data.evaluation = evaluation
     else:
         data = data[0]
     
     data.value = value
     return data.put()
         
 def delete(self, text_line_id):
     text_line = TextLine.find_by_id(int(text_line_id))
     if text_line.card.is_authorized():
         card_id = text_line.card.key().id()
         text_line.delete()
 def delete_form(self, text_line_id):
     text_line = TextLine.find_by_id(int(text_line_id))
     if text_line.card.is_authorized():
         template = JinjaEnv.get().get_template('templates/text_line/delete_form.html')
         self.response.out.write(template.render({'text_line_id': text_line_id, 'text_line': text_line}))
 def test_delete(self):
     text_line_id = TextLine.create('Delete Test Name', self.card_id).id()
     response = self.testapp.post('/text_line/%d/delete' % text_line_id)
     self.assertSuccess(response)
     card = ReportCard.find_by_id(self.card_id)
     self.assertEqual(0, len(card.text_lines()))
 def test_delete_form(self):
     text_line_id = TextLine.create('Delete Form Test Name', self.card_id).id()
     response = self.testapp.get('/text_line/%d/delete_form' % text_line_id)
     self.assertSuccess(response)
 def text_lines(self):
     from models.text_line import TextLine
     items = TextLine.gql("WHERE card = :1 ORDER BY position ASC", self).fetch(100)
     return items