Example #1
0
        def test_get_connection(self):
            # As a full test would require a working AWS secret key,
            # only rudimentary tests are performed on this function
            with self.settings(DJURK=None, DJURK_CONFIG_FILE=None):
                self.assertRaises(InvalidDjurkSettings, get_connection)

            with self.settings(DJURK={'aws_access_key_id': '123'}):
                self.assertRaises(KeyError, get_connection)

            with self.settings(DJURK={'aws_secret_access_key': '123'}):
                self.assertRaises(KeyError, get_connection)

            with self.settings(DJURK={'aws_access_key_id': '123',
                                      'aws_secret_access_key': '456'}):
                mtc = get_connection()
                self.assertTrue(isinstance(get_connection(),
                                boto.mturk.connection.MTurkConnection))

            with self.settings(DJURK=None,
                    DJURK_CONFIG_FILE=self.djurk_config_filename):
                f = open(self.djurk_config_filename, 'w')
                f.write("[Connection]\naws_access_key_id: 123\n"
                        "aws_secret_access_key: 456\n")
                f.close()
                self.assertTrue(isinstance(get_connection(),
                                boto.mturk.connection.MTurkConnection))
Example #2
0
    def handle(self, *args, **options):
        self.mtc = get_connection()

        if options['account_balance']:
            self.check_account_balance()

        if options['sample_hit']:
            demo_create_favorite_color_hit()
Example #3
0
    def handle(self, *args, **options):
        self.mtc = get_connection()

        if options['account_balance']:
            self.check_account_balance()

        if options['sample_hit']:
            demo_create_favorite_color_hit()
Example #4
0
def init_connection_callback(sender, **signal_args):
    """Mechanical Turk connection signal callback

    By using Django pre-init signals, class level connections can be
    made available to Django models that configure this pre-init
    signal.
    """
    sender.args = sender
    object_args = signal_args['kwargs']
    sender.connection = get_connection()
Example #5
0
def init_connection_callback(sender, **signal_args):
    """Mechanical Turk connection signal callback

    By using Django pre-init signals, class level connections can be
    made available to Django models that configure this pre-init
    signal.
    """
    sender.args = sender
    object_args = signal_args['kwargs']
    sender.connection = get_connection()
Example #6
0
    def handle(self, *args, **options):
        mtc = get_connection()
        do_update_assignments = options['do_update_assignments']

        while True:
            if options['reviewable']:
                logging.info(("Updating Reviewable HITs with "
                              "Assignments: %s") % do_update_assignments)
                update_reviewable_hits(
                    do_update_assignments=do_update_assignments)
            else:
                logging.info(("Updating All HITs with "
                              "Assignments: %s") % do_update_assignments)
                update_all_hits(do_update_assignments=do_update_assignments)
            logging.info("Sleeping")
            if not options['loop']:
                break
            time.sleep(SLEEP_TIME)
Example #7
0
    def handle(self, *args, **options):
        mtc = get_connection()
        do_update_assignments = options['do_update_assignments']

        while True:
            if options['reviewable']:
                logging.info(("Updating Reviewable HITs with "
                              "Assignments: %s") % do_update_assignments)
                update_reviewable_hits(
                        do_update_assignments=do_update_assignments)
            else:
                logging.info(("Updating All HITs with "
                              "Assignments: %s") % do_update_assignments)
                update_all_hits(
                        do_update_assignments=do_update_assignments)
            logging.info("Sleeping")
            if not options['loop']:
                break
            time.sleep(SLEEP_TIME)
Example #8
0
def demo_create_favorite_color_hit():
    """A HIT to determine the Worker's favorite color"""

    TITLE = 'Tell me your favorite color'
    DESCRIPTION = ('This is a HIT that is created by a computer program '
                   'to demonstrate how Mechanical Turk works. This should '
                   'be a free HIT for the worker.')
    KEYWORDS = 'data collection, favorite, color'
    DURATION = 15 * 60  # 15 minutes (Time to work on HIT)
    MAX_ASSIGNMENTS = 1  # Number of assignments per HIT
    REWARD_PER_ASSIGNMENT = 0.00  # $0.00 USD (1 cent)

    #--------------- BUILD HIT container -------------------
    overview = Overview()
    overview.append_field('Title', TITLE)
    overview.append(
        FormattedContent(
            "<p>This is an experiment to learn Mechanical Turk</p>"))

    #---------------  BUILD QUESTION 1 -------------------
    question_content = QuestionContent()
    question_content.append(
        FormattedContent(
            "<b>What is your favorite color?</b> There isn't a financial "
            "reward for answering, but you will get an easy approval for your "
            "statistics."))

    free_text_answer = FreeTextAnswer(num_lines=1)

    q1 = Question(identifier='favorite_color',
                  content=question_content,
                  answer_spec=AnswerSpecification(free_text_answer),
                  is_required=True)

    #---------------  BUILD QUESTION 3 -------------------
    question_content = QuestionContent()
    question_content.append(
        FormattedContent("""<p>Give me a fun comment:</p>"""))

    q2 = Question(identifier="comments",
                  content=question_content,
                  answer_spec=AnswerSpecification(FreeTextAnswer()))

    #--------------- BUILD THE QUESTION FORM -------------------
    question_form = QuestionForm()
    question_form.append(overview)
    question_form.append(q1)
    question_form.append(q2)

    #--------------- CREATE THE HIT -------------------
    mtc = get_connection()
    hit = mtc.create_hit(questions=question_form,
                         max_assignments=MAX_ASSIGNMENTS,
                         title=TITLE,
                         description=DESCRIPTION,
                         keywords=KEYWORDS,
                         duration=DURATION,
                         reward=REWARD_PER_ASSIGNMENT)

    #---------- SHOW A LINK TO THE HIT GROUP -----------
    base = get_worker_url()

    print "\nVisit this website to see the HIT that was created:"
    print "%s/mturk/preview?groupId=%s" % (base, hit[0].HITTypeId)

    return hit[0]
Example #9
0
def update_reviewable_hits(do_update_assignments=False):

    """Get only reviewable HITS from Amazon"""
    connection = get_connection()
    _update_hits(connection.get_reviewable_hits(),
                 do_update_assignments=do_update_assignments)
Example #10
0
def update_all_hits(do_update_assignments=False):
    """Get All HITS from Amazon"""
    connection = get_connection()
    _update_hits(connection.get_all_hits(),
                 do_update_assignments=do_update_assignments)
Example #11
0
def demo_create_favorite_color_hit():
    """A HIT to determine the Worker's favorite color"""

    TITLE = 'Tell me your favorite color'
    DESCRIPTION = ('This is a HIT that is created by a computer program '
                   'to demonstrate how Mechanical Turk works. This should '
                   'be a free HIT for the worker.')
    KEYWORDS = 'data collection, favorite, color'
    DURATION = 15 * 60  # 15 minutes (Time to work on HIT)
    MAX_ASSIGNMENTS = 1  # Number of assignments per HIT
    REWARD_PER_ASSIGNMENT = 0.00  # $0.00 USD (1 cent)

    #--------------- BUILD HIT container -------------------
    overview = Overview()
    overview.append_field('Title', TITLE)
    overview.append(FormattedContent(
         "<p>This is an experiment to learn Mechanical Turk</p>"))

    #---------------  BUILD QUESTION 1 -------------------
    question_content = QuestionContent()
    question_content.append(FormattedContent(
         "<b>What is your favorite color?</b> There isn't a financial "
         "reward for answering, but you will get an easy approval for your "
         "statistics."))

    free_text_answer = FreeTextAnswer(num_lines=1)

    q1 = Question(identifier='favorite_color',
                  content=question_content,
                  answer_spec=AnswerSpecification(free_text_answer),
                  is_required=True)

    #---------------  BUILD QUESTION 3 -------------------
    question_content = QuestionContent()
    question_content.append(FormattedContent(
        """<p>Give me a fun comment:</p>"""))

    q2 = Question(identifier="comments",
                  content=question_content,
                  answer_spec=AnswerSpecification(FreeTextAnswer()))

    #--------------- BUILD THE QUESTION FORM -------------------
    question_form = QuestionForm()
    question_form.append(overview)
    question_form.append(q1)
    question_form.append(q2)

    #--------------- CREATE THE HIT -------------------
    mtc = get_connection()
    hit = mtc.create_hit(questions=question_form,
                         max_assignments=MAX_ASSIGNMENTS,
                         title=TITLE,
                         description=DESCRIPTION,
                         keywords=KEYWORDS,
                         duration=DURATION,
                         reward=REWARD_PER_ASSIGNMENT)

    #---------- SHOW A LINK TO THE HIT GROUP -----------
    base = get_worker_url()

    print "\nVisit this website to see the HIT that was created:"
    print "%s/mturk/preview?groupId=%s" % (base, hit[0].HITTypeId)

    return hit[0]