Skip to content

Commit

Permalink
fix: edit uses underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
xnought committed Mar 15, 2024
1 parent 9b3ea60 commit 22a84b2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
20 changes: 16 additions & 4 deletions backend/src/api/protein.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,25 @@ def upload_protein_entry(body: UploadBody, req: Request):
return UploadError.QUERY_ERROR


# TODO: add more edits, now only does name and content edits
@router.put("/protein/edit", response_model=UploadError | None)
class ProteinEditSuccess(CamelModel):
edited_name: str


@router.put("/protein/edit", response_model=ProteinEditSuccess)
def edit_protein_entry(body: EditBody, req: Request):
"""edit_protein_entry
Returns: On successful edit, will return an object with editedName
If not successful will through an HTTP status 500
"""

# check that the name is not already taken in the DB
# TODO: check if permission so we don't have people overriding other people's names
requiresAuthentication(req)
try:
# replace spaces in the name with underscores
body.old_name = format_protein_name(body.old_name)
body.new_name = format_protein_name(body.new_name)

if body.new_name != body.old_name:
os.rename(
stored_pdb_file_name(body.old_name), stored_pdb_file_name(body.new_name)
Expand Down Expand Up @@ -330,9 +342,9 @@ def edit_protein_entry(body: EditBody, req: Request):
body.old_name if not name_changed else body.new_name,
],
)

return ProteinEditSuccess(edited_name=body.new_name)
except Exception:
return UploadError.WRITE_ERROR
raise HTTPException(500, "Edit failed, git gud")


# /pdb with two attributes returns both PDBs, superimposed and with different colors.
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type { HTTPValidationError } from './models/HTTPValidationError';
export type { LoginBody } from './models/LoginBody';
export type { LoginResponse } from './models/LoginResponse';
export type { MultipleTutorials } from './models/MultipleTutorials';
export type { ProteinEditSuccess } from './models/ProteinEditSuccess';
export type { ProteinEntry } from './models/ProteinEntry';
export type { RangeFilter } from './models/RangeFilter';
export type { SearchProteinsBody } from './models/SearchProteinsBody';
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/lib/openapi/models/ProteinEditSuccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export type ProteinEditSuccess = {
editedName: string;
};

9 changes: 6 additions & 3 deletions frontend/src/lib/openapi/services/DefaultService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import type { EditBody } from '../models/EditBody';
import type { LoginBody } from '../models/LoginBody';
import type { LoginResponse } from '../models/LoginResponse';
import type { MultipleTutorials } from '../models/MultipleTutorials';
import type { ProteinEditSuccess } from '../models/ProteinEditSuccess';
import type { ProteinEntry } from '../models/ProteinEntry';
import type { RangeFilter } from '../models/RangeFilter';
import type { SearchProteinsBody } from '../models/SearchProteinsBody';
import type { SearchProteinsResults } from '../models/SearchProteinsResults';
import type { SimilarProtein } from '../models/SimilarProtein';
import type { Tutorial } from '../models/Tutorial';
import type { UploadBody } from '../models/UploadBody';
import type { UploadError } from '../models/UploadError';
import type { UploadPNGBody } from '../models/UploadPNGBody';
Expand Down Expand Up @@ -232,13 +232,16 @@ export class DefaultService {
}
/**
* Edit Protein Entry
* edit_protein_entry
* Returns: On successful edit, will return an object with editedName
* If not successful will through an HTTP status 500
* @param requestBody
* @returns any Successful Response
* @returns ProteinEditSuccess Successful Response
* @throws ApiError
*/
public static editProteinEntry(
requestBody: EditBody,
): CancelablePromise<(UploadError | null)> {
): CancelablePromise<ProteinEditSuccess> {
return __request(OpenAPI, {
method: 'PUT',
url: '/protein/edit',
Expand Down
64 changes: 35 additions & 29 deletions frontend/src/routes/Edit.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<script lang="ts">
import { Backend, UploadError, setToken, type ProteinEntry } from "../lib/backend";
import {
Backend,
UploadError,
setToken,
type ProteinEntry,
} from "../lib/backend";
import { Button, Input, Label, Helper, Select } from "flowbite-svelte";
import { navigate } from "svelte-routing";
import { onMount } from "svelte";
Expand All @@ -23,7 +28,7 @@
let ogSpecies: string;
let species: string;
let uploadError: UploadError | undefined;
let uploadError = "";
let entry: ProteinEntry | null = null;
let error = false;
let allSpecies: string[] | null;
Expand Down Expand Up @@ -92,7 +97,7 @@
id="protein-name"
placeholder="Name"
/>
{#if uploadError && uploadError === UploadError.NAME_NOT_UNIQUE}
{#if uploadError}
<Helper class="mt-2" color="red"
>This name already exists, please create a unique name
and resubmit</Helper
Expand Down Expand Up @@ -140,47 +145,48 @@
on:click={async () => {
if (entry) {
try {
setToken()
const err = await Backend.editProteinEntry({
newName: name,
oldName: entry.name,
newSpeciesName: species,
oldSpeciesName: ogSpecies,
newContent:
content !== ogContent
? content
: undefined,
newRefs: refs !== ogRefs ? refs : undefined,
newDescription:
description !== ogDescription
? description
: undefined,
});
if (err) {
uploadError = err;
console.log(uploadError);
} else {
// success, so we can go back!
navigate(`/protein/${name}`);
}
setToken();
const editSuccessful =
await Backend.editProteinEntry({
newName: name,
oldName: entry.name,
newSpeciesName: species,
oldSpeciesName: ogSpecies,
newContent:
content !== ogContent
? content
: undefined,
newRefs:
refs !== ogRefs ? refs : undefined,
newDescription:
description !== ogDescription
? description
: undefined,
});
// success, so we can go back, but now with the new name stored in the db.
navigate(
`/protein/${editSuccessful.editedName}`
);
uploadError = ""; // no upload error to report
} catch (e) {
console.log(e);
// in this case, there was some edit error so we should display it
uploadError = String(e);
}
}
}}
disabled={!changed || name.length === 0}
>Edit Protein</Button
>

<Button outline on:click={() => navigate(`/protein/${name}`)}
<Button outline on:click={() => navigate(`/protein/${urlId}`)}
>Cancel</Button
>
</div>
<div>
<Button
color="red"
on:click={async () => {
setToken()
setToken();
await Backend.deleteProteinEntry(urlId);
navigate("/");
}}>Delete Protein Entry</Button
Expand Down

0 comments on commit 22a84b2

Please sign in to comment.