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

Automatically en-/dis-able definitions #2161

Merged
merged 10 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
68 changes: 68 additions & 0 deletions Backend.Tests/Controllers/LiftControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,74 @@ public void TestRoundtrip(RoundTripObj roundTripObj)
}
}

[Test]
public void TestHasNoDefinitions()
{
// This test assumes you have the starting .zip (filename) included in your project files.
var filename = "SingleEntryLiftWithSound.zip";
var pathToStartZip = Path.Combine(Util.AssetsDir, filename);
Assert.IsTrue(File.Exists(pathToStartZip));

// Init the project the .zip info is added to.
var proj = Util.RandomProject();
proj.VernacularWritingSystem.Bcp47 = "qaa";
proj = _projRepo.Create(proj).Result;

// Upload the zip file.
// Generate api parameter with filestream.
using (var stream = File.OpenRead(pathToStartZip))
{
var fileUpload = InitFile(stream, filename);

// Make api call.
var result = _liftController.UploadLiftFile(proj!.Id, fileUpload).Result;
Assert.That(result is OkObjectResult);
}

proj = _projRepo.GetProject(proj.Id).Result;
if (proj is null)
{
Assert.Fail();
return;
}

Assert.That(proj.DefinitionsEnabled, Is.False);
}

[Test]
public void TestHasDefinitions()
{
// This test assumes you have the starting .zip (filename) included in your project files.
var filename = "Natqgu.zip";
var pathToStartZip = Path.Combine(Util.AssetsDir, filename);
Assert.IsTrue(File.Exists(pathToStartZip));

// Init the project the .zip info is added to.
var proj = Util.RandomProject();
proj.VernacularWritingSystem.Bcp47 = "qaa";
proj = _projRepo.Create(proj).Result;

// Upload the zip file.
// Generate api parameter with filestream.
using (var stream = File.OpenRead(pathToStartZip))
{
var fileUpload = InitFile(stream, filename);

// Make api call.
var result = _liftController.UploadLiftFile(proj!.Id, fileUpload).Result;
Assert.That(result is OkObjectResult);
}

proj = _projRepo.GetProject(proj.Id).Result;
if (proj is null)
{
Assert.Fail();
return;
}

Assert.That(proj.DefinitionsEnabled, Is.True);
}

private class MockLogger : ILogger<LiftController>
{
public IDisposable BeginScope<TState>(TState state)
Expand Down
7 changes: 7 additions & 0 deletions Backend/Controllers/LiftController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public async Task<IActionResult> UploadLiftFile(string projectId, [FromForm] Fil
int liftParseResult;
// Sets the projectId of our parser to add words to that project
var liftMerger = _liftService.GetLiftImporterExporter(projectId, _wordRepo);
var doesImportHaveDefinitions = false;
try
{
// Add character set to project from ldml file
Expand All @@ -166,6 +167,11 @@ public async Task<IActionResult> UploadLiftFile(string projectId, [FromForm] Fil

// Import words from .lift file
liftParseResult = parser.ReadLiftFile(extractedLiftFiles.First());

// Check if there are any definitions in the imported words
// before they are deleted by SaveImportEntries.
doesImportHaveDefinitions = liftMerger.DoesImportHaveDefinitions();

await liftMerger.SaveImportEntries();
}
catch (Exception e)
Expand All @@ -183,6 +189,7 @@ public async Task<IActionResult> UploadLiftFile(string projectId, [FromForm] Fil
return NotFound(projectId);
}

project.DefinitionsEnabled = doesImportHaveDefinitions;
project.LiftImported = true;
await _projRepo.Update(projectId, project);

Expand Down
1 change: 1 addition & 0 deletions Backend/Interfaces/ILiftService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface ILiftService

public interface ILiftMerger : ILexiconMerger<LiftObject, LiftEntry, LiftSense, LiftExample>
{
bool DoesImportHaveDefinitions();
Task<List<Word>> SaveImportEntries();
}
}
9 changes: 9 additions & 0 deletions Backend/Services/LiftService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,15 @@ public LiftMerger(string projectId, IWordRepository wordRepo)
_wordRepo = wordRepo;
}

/// <summary>
/// Check for any Definitions in the private field <see cref="_importEntries"/>
/// </summary>
/// <returns> A boolean: true if at least one word has a definition. </returns>
public bool DoesImportHaveDefinitions()
{
return _importEntries.Any(w => w.Senses.Any(s => s.Definitions.Count > 0));
}

/// <summary>
/// <see cref="Word"/>s are added to the private field <see cref="_importEntries"/>
/// during lift import. This saves the contents of _importEntries to the database.
Expand Down
52 changes: 0 additions & 52 deletions src/components/ProjectSettings/ProjectDefinitions.tsx

This file was deleted.

11 changes: 0 additions & 11 deletions src/components/ProjectSettings/ProjectSettingsComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Archive,
Assignment,
CalendarMonth,
CloudUpload,
Edit,
Expand All @@ -24,7 +23,6 @@ import BaseSettingsComponent from "components/BaseSettings/BaseSettingsComponent
import { asyncRefreshCurrentProjectUsers } from "components/Project/ProjectActions";
import ExportButton from "components/ProjectExport/ExportButton";
import ProjectAutocomplete from "components/ProjectSettings/ProjectAutocomplete";
import ProjectDefinitions from "components/ProjectSettings/ProjectDefinitions";
import ProjectImport from "components/ProjectSettings/ProjectImport";
import ProjectLanguages from "components/ProjectSettings/ProjectLanguages";
import ProjectName from "components/ProjectSettings/ProjectName";
Expand Down Expand Up @@ -134,15 +132,6 @@ export default function ProjectSettingsComponent() {
body={<ProjectAutocomplete />}
/>

{/* Definitions toggle */}
{permissions.includes(Permission.DeleteEditSettingsAndUsers) && (
<BaseSettingsComponent
icon={<Assignment />}
title={t("projectSettings.definitions.label")}
body={<ProjectDefinitions />}
/>
)}

{/* See current users in project */}
{permissions.includes(Permission.DeleteEditSettingsAndUsers) && (
<BaseSettingsComponent
Expand Down