Example #1
0
def test_make_call(url, post_params=None, method=None):
    post_params_string = ''
    if post_params:
        post_params_string = ''.join(post_params.keys())
    path = 'scripts/%s%s.json' % (url.replace('/', '_'), post_params_string)

    try:
        test_script = open(path)
        json_object = json.load(test_script)
        test_script.close()
    except IOError as ex:
        api = PyfooAPI(account, api_key)
        json_object = api.make_call(url, post_params, method)
        print ex
        print '*** Using the regular API ***'
    return json_object
Example #2
0
def test_make_call(url, post_params=None, method=None):
    post_params_string = ''
    if post_params:
        post_params_string = ''.join(post_params.keys())
    path = 'scripts/%s%s.json' % (url.replace('/', '_'), post_params_string)    
    
    try:
        test_script = open(path)
        json_object = json.load(test_script)
        test_script.close()
    except IOError as ex:
        api = PyfooAPI(account, api_key)
        json_object = api.make_call(url, post_params, method)
        print ex
        print '*** Using the regular API ***'
    return json_object
Example #3
0
 def test_login(self):
     api = PyfooAPI(email="email",
                    password="******",
                    integration_key="integration_key")
     self.assertTrue(api.api_key)
     self.assertTrue(api.account)
     forms = api.forms
     self.assertEqual(2, len(forms))
Example #4
0
def get_test_api():
    test_api = PyfooAPI(account, api_key)
    test_api.make_call = test_make_call
    return test_api
Example #5
0
    def handle(self,
               target='.',
               filters=(),
               entries_completed=True,
               write_to_db=True,
               apply_suffix=True,
               suffix=None,
               append=False,
               entity_id_field='EntryId',
               apply_pk=True,
               recreate=False,
               stage=None,
               verbosity=1,
               **_options):
        self.entity_id_field = entity_id_field
        self.verbosity = verbosity

        if stage == 'application':
            filters.append(f'^{settings.REVIEW_PROGRAM_YEAR} ')
            if target == '.':  # i.e. override default
                target = '-'
        elif stage == 'review':
            filters.extend((
                f'^{settings.REVIEW_PROGRAM_YEAR} ',
                f'{RECOMMENDATION_FORM}|{REVIEWER_FORM}',
            ))
            if target == '.':
                target = '-'
        elif stage is not None:
            raise CommandError('unexpected stage argument', stage)

        if target == '-' and not write_to_db:
            raise CommandError(
                "Can only write to standard I/O without database – nothing to do"
            )

        client = PyfooAPI(*Credentials)

        for (form_count, (year, name,
                          form)) in enumerate(stream_forms(client, filters)):
            if form_count != 0:
                self.report()

            self.report('=' * (len(name) + 4))
            self.report('=', name, '=')
            self.report('=' * (len(name) + 4))

            entries = stream_entries(form, completed=entries_completed)
            fields = list(stream_fields(form))

            # Peak ahead for entry column names
            try:
                head = next(entries)
            except StopIteration:
                head = None
            else:
                entries = itertools.chain((head, ), entries)

            # Prepare streams or eagerly write to disk
            if target == '-':
                data_paths = (None, None)
                streams = (
                    functools.partial(self.write_entries_csv, head, entries),
                    functools.partial(self.write_fields_csv, head, fields),
                )
            else:
                data_paths = self.write_disk(target, name, head, entries,
                                             fields)
                streams = (None, None)

            # Write to database
            if not write_to_db or not head:
                continue

            table_names = [f'survey_{name}', f'survey_{name}_fields']
            if apply_suffix:
                table_suffix = suffix or year
                table_names = [
                    table_name +
                    f'_{table_suffix}'.lower()  # avoid psql name ambiguity
                    for table_name in table_names
                ]

            table_col_defns = [
                # entries table columns
                ', '.join(
                    self.get_field_sql(field_name) for field_name in head),

                # fields table columns
                '"field_id" varchar, "field_title" varchar',
            ]

            # maybe apply primary key to entries table, never to fields table
            tables_apply_pk = (apply_pk, False)

            for (table_count, table_name, table_col_defn, data_path, stream,
                 table_apply_pk) in zip(itertools.count(), table_names,
                                        table_col_defns, data_paths, streams,
                                        tables_apply_pk):
                if table_count != 0:
                    self.report()

                table_name_tmp = f'{table_name}_tmp'

                with connection.cursor() as cursor:
                    cursor.execute(f"select to_regclass('{table_name}')")
                    (result, ) = cursor.fetchone()
                    table_exists = bool(result)

                direct_write = append or not table_exists

                if direct_write:
                    target_table_name = table_name

                    if recreate and table_exists:
                        # tear down old table
                        self.op_drop_table(table_name)
                        table_exists = False
                else:
                    target_table_name = table_name_tmp

                    self.execute_sql(
                        f'create temp table {table_name_tmp} '
                        f'({table_col_defn})', "creating temporary table:",
                        table_name_tmp)

                if not table_exists:
                    self.op_create_destination_table(table_name,
                                                     table_col_defn,
                                                     table_apply_pk)

                if data_path:
                    self.report("copying file", data_path.name,
                                "to database table:", target_table_name)
                    # avoid weird carriage returns inside quoted strings
                    # https://docs.python.org/3/library/csv.html#examples
                    open_file = functools.partial(open, data_path, newline='')
                else:
                    self.report("streaming to database table:",
                                target_table_name)
                    open_file = functools.partial(ohio.PipeTextIO, stream)

                with open_file() as infile, \
                        connection.cursor() as cursor:
                    cursor.copy_expert(
                        f"copy {target_table_name} from stdin with csv header",
                        infile,
                    )

                if not direct_write:
                    try:
                        self.execute_sql('begin')

                        if table_exists:
                            if recreate:
                                # tear down old table
                                self.op_drop_table(table_name)

                                # set up new table
                                self.op_create_destination_table(
                                    table_name, table_col_defn, table_apply_pk)
                            else:
                                self.execute_sql(
                                    f'truncate table only {table_name}',
                                    "truncating destination table:",
                                    table_name)

                        self.report("(re)-populating destination table",
                                    "from temporary table:", table_name_tmp,
                                    '→', table_name)
                        self.execute_sql(f'insert into {table_name} '
                                         f'select * from {table_name_tmp}')
                    except BaseException:
                        try:
                            self.execute_sql('rollback',
                                             'rolling back transaction')
                        except BaseException:
                            pass

                        raise
                    else:
                        self.execute_sql('commit')
Example #6
0
        if field.SubFields:
            output.append(field.Title)
            for subfield in field.SubFields:
                if entry.has_key(subfield.ID):
                    output.append("\t%s: %s" %
                                  (subfield.Label, entry[subfield.ID]))
        else:
            if entry.has_key(field.ID):
                output.append("%s: %s" % (field.Title, entry[field.ID]))
    return output


droid = android.Android()

key, account = get_key_and_account()
api = PyfooAPI(account, key)

droid.dialogCreateSpinnerProgress('Loading Forms')
droid.dialogShow()
forms = api.forms
droid.dialogDismiss()

droid.dialogCreateAlert('Select a Form')
droid.dialogSetItems([form.Name for form in forms])
droid.dialogSetNegativeButtonText('Cancel')
droid.dialogShow()
response = droid.dialogGetResponse()
form = forms[response.result['item']]

droid.dialogCreateSpinnerProgress('Loading Entries')
droid.dialogShow()
 def handle(self, *_, **options):
     auctions = Auction.objects.filter(
         date__gt=datetime.date.today(),
         donation_form__isnull=False,
         donation_form__hash__isnull=False
     )
     api = PyfooAPI('guardsmen', settings.WUFOO_API_KEY)
     localtz = timezone(settings.TIME_ZONE)
     for auction in auctions:
         donation_form = auction.donation_form
         try:
             wufoo_form = next(x for x in api.forms if x.Hash == donation_form.hash)
         except StopIteration:
             continue
         if options['form'] and donation_form.id != options['form']:
             continue
         entries = wufoo_form.get_entries(page_size=wufoo_form.entry_count)
         entries = [x for x in entries if x['CompleteSubmission']]
         processed = Donation.objects.filter(
             source_form__id=donation_form.id
         )
         for entry in entries:
             try:
                 donation = next(x for x in processed if x.form_entry_number == int(entry['EntryId']))
                 if not options['overwrite']:
                     continue
             except StopIteration:
                 donation = Donation(
                     auction=auction,
                     source_form=donation_form,
                     form_entry_number=entry['EntryId'],
                 )
             # get info from form
             donor_organization = entry['Field1']
             donor_name = entry['Field2'] + ' ' + entry['Field3']
             donor_email = entry['Field11']
             donor_address = entry['Field4'] + os.linesep + \
                 ((entry['Field5'] + os.linesep) if entry['Field5'] else '') +\
                 entry['Field6'] + ', ' + entry['Field7'] + ' ' + entry['Field8'] + os.linesep +\
                 entry['Field9']
             guardsmen_contact = (entry['Field13'] + ' ' + entry['Field14']).strip()
             auction_items = entry['Field224']
             auction_value = entry['Field438']
             special_instructions = entry['Field327']
             delivery_method = entry['Field326']
             form_created_time = None
             if entry['DateCreated']:
                 form_created_time = localtz.localize(datetime.datetime.strptime(
                     entry['DateCreated'],
                     '%Y-%m-%d %H:%M:%S'
                 ))
             form_updated_time = None
             if entry['DateUpdated']:
                 form_updated_time = localtz.localize(datetime.datetime.strptime(
                     entry['DateUpdated'],
                     '%Y-%m-%d %H:%M:%S'
                 ))
             donor_phone = None
             if donation_form.phone_number_field:
                 donor_phone = entry['Field{}'.format(donation_form.phone_number_field)]
             auction_description = None
             if donation_form.item_description_field:
                 auction_description = entry['Field{}'.format(donation_form.item_description_field)]
             auction_contact_point = None
             if donation_form.auction_provide_contact_field:
                 auction_contact_type = entry['Field{}'.format(donation_form.auction_provide_contact_field)]
                 if 'above' in auction_contact_type:
                     auction_contact_point = (donor_name or donor_organization) + ' (' + donor_email + ')'
                 elif donation_form.auction_contact_point_field and entry['Field{}'.format(donation_form.auction_contact_point_field)]:
                     auction_contact_point = entry['Field{}'.format(donation_form.auction_contact_point_field)]
                             # logo_field = None
             logo_url = None
             if donation_form.logo_field:
                 logo_str = entry['Field{}'.format(donation_form.logo_field)]
                 match = re.search(r'(.+) \((.+)\)', logo_str)
                 if match:
                     logo_name = match.group(1)
                     logo_url = match.group(2)
                     r = requests.get(logo_url)
                     donation.image.save(logo_name, BytesIO(r.content), save=False)
             donation.donor_organization = donor_organization
             donation.donor_name = donor_name
             donation.donor_email = donor_email
             donation.donor_address = donor_address
             donation.guardsmen_contact = guardsmen_contact
             donation.auction_items = auction_items
             donation.auction_value = auction_value
             donation.special_instructions = special_instructions
             donation.delivery_method = delivery_method
             donation.donor_phone = donor_phone
             donation.auction_description = auction_description
             donation.auction_contact_point = auction_contact_point
             donation.form_created_time = form_created_time
             donation.form_updated_time = form_updated_time
             donation.image_upload_link = logo_url
             donation.save()
     print('Donations processed succesfully')
Example #8
0
def get_test_api():
    test_api = PyfooAPI(account, api_key, test_json_dir='test_scripts')
    test_api.make_call = test_make_call
    return test_api
Example #9
0
File: tests.py Project: wufoo/pyfoo
def get_test_api():
    test_api = PyfooAPI(account, api_key, test_json_dir='test_scripts')
    test_api.make_call = test_make_call
    return test_api
Example #10
0
def get_test_api():
    test_api = PyfooAPI(account, api_key)
    test_api.make_call = test_make_call
    return test_api