Ejemplo n.º 1
0
def get_custom_settings(form_tree):
    """Extracts custom settings from the given form_tree"""
    language_list = query.get_language_list(
        form_tree.tree, ignore_list=[form_tree.language])
    additional_languages = ' '.join([l.lower() for l in language_list])
    return {
        'language': form_tree.language.lower(),
        'additional_languages': additional_languages,
    }
Ejemplo n.º 2
0
def process(url, name, dry=False):
    survey_dict = query.get_survey_dict(url, with_translations=False)
    available_languages = query.get_language_list(
        survey_dict['form_tree'].tree)
    export_path = get_export_csv_path(survey_dict['survey_path'], name)
    if (not export_path) or (not os.path.exists(export_path)):
        answers_name = export_path if export_path else survey_dict['survey_path']
        logger.error('Missing exported survey answers: `%s`', answers_name)
        raise ValueError('Cannot submit survey.')
    survey_dict['filename_prefix'] = (
        export_path.rsplit('/', 1)[-1].replace('.csv', ''))
    submission_list = list(database.read_csv_file(export_path))
    row_map = translator.update_key_map(submission_list[0])
    sent_submissions = get_sent_submissions(survey_dict)
    submission_queue = get_submission_queue(
        submission_list, sent_submissions.keys(), survey_dict['filename_prefix'])
    for i, row in enumerate(submission_queue):
        submission_id = row[0]
        language = get_row_language(row, available_languages)
        locale_url = query.clean_url(url, {'surveylanguage': language})
        logger.debug('Processing submission: `%s`', submission_id)
        partial_payload = translator.prepare_payload(row, row_map)
        if validator.is_valid_payload(partial_payload, survey_dict):
            payload, pre_submission = complete_payload(locale_url, partial_payload)
            if dry:
                logger.info('Dry run. Valid row: `%s`.', submission_id)
                continue
            else:
                # Send submissions
                success_response = send_submission(
                    locale_url, payload, pre_submission, submission_id, survey_dict)
                if not success_response:
                    # Failed submission:
                    logger.error('Failed to send: `%s`', submission_id)
                    continue
                submission_row = get_submission_row(
                    submission_id, survey_dict['filename_prefix'],
                    success_response)
                sent_submissions[submission_id] = submission_row
                logger.info('Submission sent: `%s`', success_response)
    # Update with database with processed submissions:
    return save_sent_submissions(sent_submissions, survey_dict)