def handle(self, *args, **options): template_file_path = os.path.abspath(options['template_file_path']) csv_file_path = os.path.abspath(options['csv_file_path']) with open(template_file_path, 'rb') as fh: template = get_or_create_template_from_html_file( fh, template_file_path ) with open(csv_file_path, 'rb') as fh: sys.stderr.write('Creating HITs: ') header, data_rows = parse_csv_file(fh) num_created_hits = 0 for row in data_rows: if not row: continue hit = Hit( template=template, input_csv_fields=dict(zip(header, row)), ) hit.save() num_created_hits += 1 sys.stderr.write('%d HITs created.\n' % num_created_hits)
def handle(self, *args, **options): if len(args) != 2: sys.stderr.write('received args: %s\n' % args) raise CommandError(self.usage('publish_hits')) template_file_path, csv_file_path = map(os.path.abspath, args) with open(template_file_path, 'rb') as fh: template = get_or_create_template_from_html_file( fh, template_file_path ) with open(csv_file_path, 'rb') as fh: sys.stderr.write('Creating HITs: ') header, data_rows = parse_csv_file(fh) num_created_hits = 0 for row in data_rows: if not row: continue hit = Hit( template=template, input_csv_fields=dict(zip(header, row)), ) hit.save() num_created_hits += 1 sys.stderr.write('%d HITs created.\n' % num_created_hits)
def setUp(self): with open('hits/tests/resources/form_0.html') as f: form = f.read().decode('utf-8') self.hit_template = HitTemplate(name="filepath", form=form) self.hit_template.save() field_names = u"tweet0_id,tweet0_entity,tweet0_before_entity,tweet0_after_entity,tweet0_word0,tweet0_word1,tweet0_word2,tweet1_id,tweet1_entity,tweet1_before_entity,tweet1_after_entity,tweet1_word0,tweet1_word1,tweet1_word2,tweet2_id,tweet2_entity,tweet2_before_entity,tweet2_after_entity,tweet2_word0,tweet2_word1,tweet2_word2,tweet3_id,tweet3_entity,tweet3_before_entity,tweet3_after_entity,tweet3_word0,tweet3_word1,tweet3_word2,tweet4_id,tweet4_entity,tweet4_before_entity,tweet4_after_entity,tweet4_word0,tweet4_word1,tweet4_word2,tweet5_id,tweet5_entity,tweet5_before_entity,tweet5_after_entity,tweet5_word0,tweet5_word1,tweet5_word2", values = u"268,SANTOS, Muy bien America ......... y lo siento mucho , un muy buen rival,mucho, , ,2472,GREGORY, Ah bueno , tampoco andes pidiendo ese tipo de milagros . @jcabrerac @CarlosCabreraR,bueno, , ,478,ALEJANDRO, @aguillen19 , un super abrazo mi querido , , mis mejores deseos para este 2012 ... muakkk !,querido, , ,906_control, PF, Acusan camioneros extorsiones de, : Transportistas acusaron que deben pagar entre 13 y 15 mil pesos a agentes que .. http://t.co/d8LUVvhP,acusaron, , ,2793_control, CHICARO, Me gusta cuando chicharo hace su oracion es lo que lo hace especial ., ,gusta, , ,357,OSCAR WILDE", " @ ifilosofia : Las pequeñas acciones de cada día son las que hacen o deshacen el carácter." , bueno !!!! Es así,bueno, , ", self.hit = Hit( template=self.hit_template, input_csv_fields=dict(zip(field_names, values)) ) self.hit.save()
def setUp(self): """ Sets up HitTemplate, Hit objects, and saves them to the DB. The HitTemplate is bare, the Hit has inputs and answers and refers to the HitTemplate form. """ form = HitTemplate(name='test', form="<p></p>") form.save() hit = Hit( template=form, input_csv_fields={u'foo': u'bar'}, answers={ u"comment": u"\u221e", u"userDisplayLanguage": u"", u"sentence_textbox_3_verb1": u"", u"city": u"", u"sentence_textbox_1_verb6": u"", u"sentence_textbox_1_verb7": u"", u"sentence_textbox_1_verb4": u"", u"sentence_textbox_1_verb5": u"", u"sentence_textbox_1_verb2": u"", u"sentence_textbox_1_verb3": u"", u"sentence_textbox_1_verb1": u"", u"sentence_textbox_2_verb4": u"", u"csrfmiddlewaretoken": u"7zxQ9Yyug6Nsnm4nLky9p8ObJwNipdu8", u"sentence_drop_2_verb3": u"foo", u"sentence_drop_2_verb2": u"foo", u"sentence_drop_2_verb1": u"foo", u"sentence_textbox_2_verb1": u"", u"sentence_textbox_2_verb3": u"", u"sentence_drop_2_verb4": u"foo", u"sentence_textbox_2_verb2": u"", u"submitit": u"Submit", u"browserInfo": u"", u"sentence_drop_1_verb1": u"foo", u"sentence_drop_1_verb2": u"foo", u"sentence_drop_1_verb3": u"foo", u"sentence_drop_1_verb4": u"foo", u"sentence_drop_1_verb5": u"foo", u"sentence_drop_1_verb6": u"foo", u"sentence_drop_1_verb7": u"foo", u"country": u"", u"sentence_drop_3_verb1": u"foo", u"ipAddress": u"", u"region": u"" }, completed=True, ) hit.save() self.hit = hit
class TestSubmission(django.test.TestCase): def setUp(self): template = HitTemplate(name='foo', form='<p></p>') template.save() self.hit = Hit(template=template, input_csv_fields='{}') self.hit.save() def test_0(self): post_request = RequestFactory().post(u'/hits/1/submission', {u'foo': u'bar'}) post_request.csrf_processing_done = True submission(post_request, 1) h = Hit.objects.get(id=1) expect = {u'foo': u'bar'} actual = h.answers self.assertEqual(expect, actual)
def create(self, request, *args, **kwargs): create_hit_serializer = CreateHitSerializer(data=request.data) if not create_hit_serializer.is_valid(): return Response({"details": create_hit_serializer.errors}, status.HTTP_400_BAD_REQUEST) cleaned_data = create_hit_serializer.data hit = Hit( assigned_to=CUser.objects.get(email=cleaned_data["assigned_to"]), target_name=cleaned_data["target_name"], description=cleaned_data["description"], requester=request.user, ) hit.save() return Response( self.serializer_class(hit).data, status.HTTP_201_CREATED)
def test_map_fields_csv_row(self): hit_template = HitTemplate( name='test', form=u"""</select> con relación a <span style="color: rgb(0, 0, 255);">${tweet0_entity}</span> en este mensaje.</p>""" ) hit_template.save() hit = Hit( template=hit_template, input_csv_fields=dict( zip( [u"tweet0_id", u"tweet0_entity"], [u"268", u"SANTOS"], ) ), ) hit.save() expect = """<div style=" width:100%; border:2px solid black; margin-top:10px"><div style="width:100%; margin:10px"></select> con relación a <span style="color: rgb(0, 0, 255);">SANTOS</span> en este mensaje.</p></div></div>""" actual = hit.generate_form() self.assertEqual(expect, actual)
class TestGenerateForm(django.test.TestCase): def setUp(self): with open('hits/tests/resources/form_0.html') as f: form = f.read().decode('utf-8') self.hit_template = HitTemplate(name="filepath", form=form) self.hit_template.save() field_names = u"tweet0_id,tweet0_entity,tweet0_before_entity,tweet0_after_entity,tweet0_word0,tweet0_word1,tweet0_word2,tweet1_id,tweet1_entity,tweet1_before_entity,tweet1_after_entity,tweet1_word0,tweet1_word1,tweet1_word2,tweet2_id,tweet2_entity,tweet2_before_entity,tweet2_after_entity,tweet2_word0,tweet2_word1,tweet2_word2,tweet3_id,tweet3_entity,tweet3_before_entity,tweet3_after_entity,tweet3_word0,tweet3_word1,tweet3_word2,tweet4_id,tweet4_entity,tweet4_before_entity,tweet4_after_entity,tweet4_word0,tweet4_word1,tweet4_word2,tweet5_id,tweet5_entity,tweet5_before_entity,tweet5_after_entity,tweet5_word0,tweet5_word1,tweet5_word2", values = u"268,SANTOS, Muy bien America ......... y lo siento mucho , un muy buen rival,mucho, , ,2472,GREGORY, Ah bueno , tampoco andes pidiendo ese tipo de milagros . @jcabrerac @CarlosCabreraR,bueno, , ,478,ALEJANDRO, @aguillen19 , un super abrazo mi querido , , mis mejores deseos para este 2012 ... muakkk !,querido, , ,906_control, PF, Acusan camioneros extorsiones de, : Transportistas acusaron que deben pagar entre 13 y 15 mil pesos a agentes que .. http://t.co/d8LUVvhP,acusaron, , ,2793_control, CHICARO, Me gusta cuando chicharo hace su oracion es lo que lo hace especial ., ,gusta, , ,357,OSCAR WILDE", " @ ifilosofia : Las pequeñas acciones de cada día son las que hacen o deshacen el carácter." , bueno !!!! Es así,bueno, , ", self.hit = Hit( template=self.hit_template, input_csv_fields=dict(zip(field_names, values)) ) self.hit.save() def test_generate_form(self): with open('hits/tests/resources/form_0_filled.html') as f: form = f.read().decode('utf-8') expect = form actual = self.hit.generate_form() self.assertNotEqual(expect, actual) def test_map_fields_csv_row(self): hit_template = HitTemplate( name='test', form=u"""</select> con relación a <span style="color: rgb(0, 0, 255);">${tweet0_entity}</span> en este mensaje.</p>""" ) hit_template.save() hit = Hit( template=hit_template, input_csv_fields=dict( zip( [u"tweet0_id", u"tweet0_entity"], [u"268", u"SANTOS"], ) ), ) hit.save() expect = """<div style=" width:100%; border:2px solid black; margin-top:10px"><div style="width:100%; margin:10px"></select> con relación a <span style="color: rgb(0, 0, 255);">SANTOS</span> en este mensaje.</p></div></div>""" actual = hit.generate_form() self.assertEqual(expect, actual)
class TestSubmission(django.test.TestCase): def setUp(self): template = HitTemplate(name='foo', form='<p></p>') template.save() self.hit = Hit(template=template, input_csv_fields='{}') self.hit.save() def test_0(self): post_request = RequestFactory().post( u'/hits/1/submission', {u'foo': u'bar'} ) post_request.csrf_processing_done = True submission(post_request, 1) h = Hit.objects.get(id=1) expect = {u'foo': u'bar'} actual = h.answers self.assertEqual(expect, actual)
def handle(self, *args, **options): template_file_path = os.path.abspath(options['template_file_path']) csv_file_path = os.path.abspath(options['csv_file_path']) with open(template_file_path, 'rb') as fh: template = get_or_create_template_from_html_file( fh, template_file_path) with open(csv_file_path, 'rb') as fh: sys.stderr.write('Creating HITs: ') header, data_rows = parse_csv_file(fh) num_created_hits = 0 for row in data_rows: if not row: continue hit = Hit( template=template, input_csv_fields=dict(zip(header, row)), ) hit.save() num_created_hits += 1 sys.stderr.write('%d HITs created.\n' % num_created_hits)
def setUp(self): template = HitTemplate(name='foo', form='<p></p>') template.save() self.hit = Hit(template=template, input_csv_fields='{}') self.hit.save()