def handle(self, *args, **options):
        task = Task.objects.select_related().get(pk=args[0])
        cp = make_querydict(decode_checkpoint(task.checkpoint))
        total_forms = int(cp['form-TOTAL_FORMS'])
        print 'Total Forms:', total_forms
        print
        print 'OLD CHECKPOINT:'
        print json.dumps(cp, indent=4)
        print
        # TRY TO FIX:
        newcp = cp.copy()
        changes = 0

        for i in range(0, total_forms):
            for word in ('source', 'word', 'language', 'entry'):
                token = self._get(i, word)
                if word == 'language':
                    assert task.language, 'Task does not have a set language - failing'
                    value = cp.get(token, None)
                    expected = u'%d' % task.language.id
                    if value != expected:
                        newcp[token] = expected
                        print "Fixing language: %s - %r -> %r " % (
                            token, value, expected)
                        changes += 1
                elif word == 'source':
                    assert task.source, 'Task does not have a set source - failing'
                    value = cp.get(token, None)
                    expected = u'%d' % task.source.id
                    if value != expected:
                        newcp[token] = expected
                        print "Fixing source: %s - %r -> %r " % (token, value,
                                                                 expected)
                        changes += 1

                elif token not in cp and word in ('entry', 'word'):
                    print "Error - missing: %s - I can't fix this" % token

        if changes == 0:
            quit("No changes to make")
        else:
            print
            print 'NEW CHECKPOINT:'
            print json.dumps(newcp, indent=4)

            # save as file
            filename = 'task_%d_checkpoint.json' % task.id
            print 'Writing old checkpoint to %s' % filename
            with open(filename, 'w') as handle:
                handle.write(encode_checkpoint(cp))

            print 'Writing old checkpoint to task.checkpoint'
            task.checkpoint = encode_checkpoint(make_querydict(newcp))
            task.save()
 def handle(self, *args, **options):
     task = Task.objects.select_related().get(pk=args[0])
     cp = make_querydict(decode_checkpoint(task.checkpoint))
     total_forms = int(cp['form-TOTAL_FORMS'])
     print 'Total Forms:', total_forms
     print 
     print 'OLD CHECKPOINT:'
     print json.dumps(cp, indent=4)
     print
     # TRY TO FIX:
     newcp = cp.copy()
     changes = 0
     
     for i in range(0, total_forms):
         for word in ('source', 'word', 'language', 'entry'):
             token = self._get(i, word)
             if word == 'language':
                 assert task.language, 'Task does not have a set language - failing'
                 value = cp.get(token, None)
                 expected = u'%d' % task.language.id
                 if value != expected:
                     newcp[token] = expected
                     print "Fixing language: %s - %r -> %r " % (token, value, expected)
                     changes += 1
             elif word == 'source':
                 assert task.source, 'Task does not have a set source - failing'
                 value = cp.get(token, None)
                 expected = u'%d' % task.source.id
                 if value != expected:
                     newcp[token] = expected
                     print "Fixing source: %s - %r -> %r " % (token, value, expected)
                     changes += 1
                 
             elif token not in cp and word in ('entry', 'word'):
                 print "Error - missing: %s - I can't fix this" % token
     
     if changes == 0:
         quit("No changes to make")
     else:
         print
         print 'NEW CHECKPOINT:'
         print json.dumps(newcp, indent=4)
         
         # save as file
         filename = 'task_%d_checkpoint.json' % task.id
         print 'Writing old checkpoint to %s' % filename
         with open(filename, 'w') as handle:
             handle.write(encode_checkpoint(cp))
         
         print 'Writing old checkpoint to task.checkpoint'
         task.checkpoint = encode_checkpoint(make_querydict(newcp))
         task.save()
 def test_checkpoint(self):
     self.task.checkpoint = encode_checkpoint(self.form_data)
     self.task.save()
     # get from db.
     t = Task.objects.get(pk=self.task.id)
     assert t.checkpoint is not None
     restored = decode_checkpoint(t.checkpoint)
     for k, v in self.form_data.items():
         assert k in restored, "Missing %s from checkpoint" % k
         assert restored[k] == v, "Expected %s to be %s" % (k, v)
 def test_checkpoint_with_unicode_via_database(self):
     unicode_sample = u'àáâãäåạɐæʌèéêëɛəìííîïɨịñŋòóôõöøðɔþùúûüụųʔřɬɤƀꝑšʷᵘ·'
     task = Task.objects.create(
         editor=self.editor,
         name="Test Task",
         description="A Test of Data Entry",
         source=self.source,
         image=self.file_testimage,
         language=self.lang,
         done=False,
         checkpoint=encode_checkpoint(unicode_sample),
         view="GenericView",
         records=
         1,  # needed so we don't have too many empty forms to validate
     )
     assert unicode_sample == decode_checkpoint(
         Task.objects.get(pk=task.id).checkpoint)
 def test_checkpoint(self):
     task = Task.objects.create(
         editor=self.editor,
         name="Test Task",
         description="A Test of Data Entry",
         source=self.source,
         image=self.file_testimage,
         language=self.lang,
         done=False,
         checkpoint=encode_checkpoint(self.form_data),
         view="GenericView",
         records=
         1,  # needed so we don't have too many empty forms to validate
     )
     # get from db.
     t = Task.objects.get(pk=task.id)
     assert t.checkpoint is not None
     restored = decode_checkpoint(t.checkpoint)
     for k, v in self.form_data.items():
         assert k in restored, "Missing %s from checkpoint" % k
         assert restored[k] == v, "Expected %s to be %s" % (k, v)
 def test_checkpoint_with_unicode_via_database(self):
     unicode_sample = u'àáâãäåạɐæʌèéêëɛəìííîïɨịñŋòóôõöøðɔþùúûüụųʔřɬɤƀꝑšʷᵘ·'
     self.task.checkpoint = encode_checkpoint(unicode_sample)
     self.task.save()
     assert unicode_sample == decode_checkpoint(Task.objects.get(pk=self.task.id).checkpoint)
 def test_checkpoint_with_unicode(self):
     unicode_sample = u'àáâãäåạɐæʌèéêëɛəìííîïɨịñŋòóôõöøðɔþùúûüụųʔřɬɤƀꝑšʷᵘ·'
     retrieved = decode_checkpoint(encode_checkpoint(unicode_sample))
     assert unicode_sample == retrieved
 def test_checkpoint_with_unicode(self):
     unicode_sample = u'àáâãäåạɐæʌèéêëɛəìííîïɨịñŋòóôõöøðɔþùúûüụųʔřɬɤƀꝑšʷᵘ·'
     retrieved = decode_checkpoint(encode_checkpoint(unicode_sample))
     assert unicode_sample == retrieved