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

Avoid errors when changing default locale of projects #4480

Merged
merged 3 commits into from
May 10, 2021
Merged
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
2 changes: 2 additions & 0 deletions backend/api/projects/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ def patch(self, project_id):
return {"Invalid GeoJson": str(e)}, 400
except NotFound as e:
return {"Error": str(e) or "Project Not Found"}, 404
except ProjectAdminServiceError as e:
return {"Error": str(e)}, 400
except Exception as e:
error_msg = f"ProjectsRestAPI PATCH - unhandled error: {str(e)}"
current_app.logger.critical(error_msg)
Expand Down
7 changes: 5 additions & 2 deletions backend/models/postgis/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ProjectUserStatsDTO,
ProjectSearchDTO,
ProjectTeamDTO,
ProjectInfoDTO,
)
from backend.models.dtos.interests_dto import InterestDTO

Expand Down Expand Up @@ -361,6 +362,10 @@ def update(self, project_dto: ProjectDTO):
""" Updates project from DTO """
self.status = ProjectStatus[project_dto.project_status].value
self.priority = ProjectPriority[project_dto.project_priority].value
if self.default_locale != project_dto.default_locale:
new_locale_dto = ProjectInfoDTO()
new_locale_dto.locale = project_dto.default_locale
project_dto.project_info_locales.append(new_locale_dto)
self.default_locale = project_dto.default_locale
self.enforce_random_task_selection = project_dto.enforce_random_task_selection
self.private = project_dto.private
Expand Down Expand Up @@ -426,9 +431,7 @@ def update(self, project_dto: ProjectDTO):

# Set Project Info for all returned locales
for dto in project_dto.project_info_locales:

project_info = self.project_info.filter_by(locale=dto.locale).one_or_none()

if project_info is None:
new_info = ProjectInfo.create_from_dto(
dto
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/projectEdit/settingsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const SettingsForm = ({ languages, defaultLocale }) => {
</label>
<select name="defaultLocale" onChange={updateDefaultLocale} className="pa2">
{languages.map((l) => (
<option selected={l.code === defaultLocale ? true : false} value={l.code}>
<option key={l.code} selected={l.code === defaultLocale ? true : false} value={l.code}>
{l.language} ({l.code})
</option>
))}
Expand Down
51 changes: 26 additions & 25 deletions frontend/src/views/projectEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,38 @@ export default function ProjectEdit({ id }) {
}, [id, token]);

const saveChanges = (resolve, reject) => {
// Remove locales with less than 3 fields.
const locales = projectInfo.projectInfoLocales;
const [defaultLocaleInfo] = projectInfo.projectInfoLocales.filter(
(l) => l.locale === projectInfo.defaultLocale,
);
// Get data for default locale.
const filtered = locales
.filter((l) => l.locale === projectInfo.defaultLocale)
.map((l) => {
return {
locale: l.locale,
fields: mandatoryFields.filter(
(m) => Object.keys(l).includes(m) === false || l[m] === '',
),
};
})
.filter((l) => l.fields.length > 0);

const nonLocaleMissingFields = { locale: null, fields: [] };

if (projectInfo.mappingTypes.length === 0) {
nonLocaleMissingFields.fields = [...nonLocaleMissingFields.fields, 'mappingTypes'];
let missingFields = [];
if (defaultLocaleInfo === undefined) {
missingFields.push({
locale: projectInfo.defaultLocale,
fields: mandatoryFields,
});
} else {
const mandatoryFieldsMissing = mandatoryFields.filter(
(m) => Object.keys(defaultLocaleInfo).includes(m) === false || defaultLocaleInfo[m] === '',
);
if (mandatoryFieldsMissing.length) {
missingFields.push({
locale: defaultLocaleInfo.locale,
fields: mandatoryFieldsMissing,
});
}
}

if (!projectInfo.organisation) {
nonLocaleMissingFields.fields = [...nonLocaleMissingFields.fields, 'organisation'];
}
const nonLocaleMissingFields = [];
if (projectInfo.mappingTypes.length === 0) nonLocaleMissingFields.push('mappingTypes');
if (!projectInfo.organisation) nonLocaleMissingFields.push('organisation');

if (nonLocaleMissingFields.fields.length > 0) {
filtered.push(nonLocaleMissingFields);
if (nonLocaleMissingFields.length) {
missingFields.push({ locale: null, fields: nonLocaleMissingFields });
}

if (filtered.length > 0) {
setError(filtered);
if (missingFields.length > 0) {
setError(missingFields);
return new Promise((resolve, reject) => reject());
} else {
return pushToLocalJSONAPI(`projects/${id}/`, JSON.stringify(projectInfo), token, 'PATCH')
Expand Down