Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make export constants consistent with FormPack - fix #2037

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions jsapp/js/components/formEditors.es6
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,33 @@ export class ProjectDownloads extends React.Component {
dataInterface.getAssetExports(this.props.asset.uid).done((data)=>{
if (data.count > 0) {
data.results.reverse();
this.setState({exports: data.results});
let results = data.results.map(result => {
if (result.data) {
let lang = result.data.lang,
langDescription = lang;

// Backend now changes `_default` to `FormPack.constant.UNTRANSLATED` in `ExportTaskViewSet`.
// `FormPack.constant.UNTRANSLATED` constant equals `None`.
// Old exports can still have `_default` as value for `lang` though.
if (lang === '_default' || lang === null) {
langDescription = t('Default');
}
// Because `xml` can be a code for an existing language (https://en.wikipedia.org/wiki/Malaysian_Sign_Language),
// `_xml` is now sent to backend instead of `xml`.
// Backend also changes `_xml` to `FormPack.constant.UNSPECIFIED_TRANSLATION`
// `FormPack.constant.UNSPECIFIED_TRANSLATION` constant equals `False`.
else if (lang === '_xml' || lang === false) {
langDescription = t('XML values and headers');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admirable, but this wraps onto a second line:
image

Also, for every language it's the values and headers, but we probably wouldn't want to say English (en) values and headers either.

Copy link
Contributor Author

@noliveleger noliveleger Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the suggestion? Only XML in caps?
I've seen your change. Sounds good.

}
result.data['lang_desc'] = langDescription;
}
return result;
});

this.setState({exports: results});

// Start a polling Interval if there is at least one export is not yet complete
data.results.every((item) => {
results.every((item) => {
if(item.status === 'created' || item.status === 'processing'){
this.pollingInterval = setInterval(this.refreshExport, 4000, item.url);
return false;
Expand Down Expand Up @@ -232,7 +255,7 @@ export class ProjectDownloads extends React.Component {
<label htmlFor='lang'>{t('Value and header format')}</label>
<select name='lang' value={this.state.lang}
onChange={this.langChange}>
<option value='xml'>{t('XML values and headers')}</option>
<option value='_xml'>{t('XML values and headers')}</option>
{ translations.length < 2 &&
<option value='_default'>{t('Labels')}</option>
}
Expand Down Expand Up @@ -320,7 +343,7 @@ export class ProjectDownloads extends React.Component {
{formatTime(item.date_created)}
</bem.FormView__label>
<bem.FormView__label m='lang'>
{item.data.lang === '_default' ? t('Default') : item.data.lang}
{item.data.lang_desc}
</bem.FormView__label>
<bem.FormView__label m='include-groups'>
{item.data.hierarchy_in_labels === 'false' ? t('No') : t('Yes')}
Expand Down
19 changes: 13 additions & 6 deletions kpi/models/import_export_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.utils.six.moves.urllib import parse as urlparse

import formpack.constants
from formpack.schema.fields import ValidationStatusCopyField
from pyxform import xls2json_backends
from formpack.utils.string import ellipsize
from kobo.apps.reports.report_data import build_formpack
Expand Down Expand Up @@ -342,9 +343,11 @@ class ExportTask(ImportExportTask):
`data` attribute to a dictionary with the following keys:
* `type`: required; `xls` or `csv`
* `source`: required; URL of a deployed `Asset`
* `lang`: optional; `xml` for XML names or the name of the language to be
used for labels. Leave unset, or use `_default` or `None`, for
labels in the default language
* `lang`: optional; It can be:
- unset (`None`) or `formpack.constants.UNTRANSLATED` for labels in the default language (default: `None`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to keep lines under 80 characters for readability. It can certainly be a pain sometimes.

Is there a docstring parser you like to use that doesn't handle line breaks within these parameter descriptions well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jnm , I kept the default of my IDE which is 120 characters instead of 80. Nowadays, screen resolutions are wider than before. 80 is really narrow, but if you want me to go with 80. I will.

- `formpack.constants.UNSPECIFIED_TRANSLATION` for XML names.
- language code for labels in one of the translated language (e.g. `en` for English).

* `hierarchy_in_labels`: optional; when `true`, include the labels for all
ancestor groups in each field label, separated by
`group_sep`. Defaults to `False`
Expand Down Expand Up @@ -375,7 +378,13 @@ class ExportTask(ImportExportTask):
last_submission_time = models.DateTimeField(null=True)
result = PrivateFileField(upload_to=export_upload_to, max_length=380)

COPY_FIELDS = ('_id', '_uuid', '_submission_time')
COPY_FIELDS = (
'_id',
'_uuid',
'_submission_time',
ValidationStatusCopyField
)

TIMESTAMP_KEY = '_submission_time'
# Above 244 seems to cause 'Download error' in Chrome 64/Linux
MAXIMUM_FILENAME_LENGTH = 240
Expand Down Expand Up @@ -445,8 +454,6 @@ def _build_export_options(self, pack):
group_sep = self.data.get('group_sep', '/')
translations = pack.available_translations
lang = self.data.get('lang', None) or next(iter(translations), None)
if lang == '_default':
lang = formpack.constants.UNTRANSLATED
tag_cols_for_header = self.data.get('tag_cols_for_header', ['hxl'])

return {
Expand Down
11 changes: 11 additions & 0 deletions kpi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
ASSET_TYPE_TEMPLATE, ASSET_TYPE_SURVEY, ASSET_TYPES
from deployment_backends.backends import DEPLOYMENT_BACKENDS
from deployment_backends.mixin import KobocatDataProxyViewSetMixin
from formpack.constants import UNTRANSLATED, UNSPECIFIED_TRANSLATION
from kobo.apps.hook.utils import HookUtils
from kpi.exceptions import BadAssetTypeException
from kpi.utils.log import logging
Expand Down Expand Up @@ -560,6 +561,16 @@ def create(self, request, *args, **kwargs):
opt_val = request.POST.get(opt, None)
if opt_val is not None:
task_data[opt] = opt_val

# To be compliant with `ExportTask` docstring,
# override `lang` if needed.
lang = task_data.get("lang")
if lang == "_default":
lang = UNTRANSLATED
elif lang == "_xml":
lang = UNSPECIFIED_TRANSLATION
task_data["lang"] = lang

# Complain if no source was specified
if not task_data.get('source', False):
raise exceptions.ValidationError(
Expand Down