Skip to content

Commit

Permalink
add code for sample form enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobfilik committed Jul 11, 2024
1 parent 5cca780 commit f2c2633
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 11 deletions.
4 changes: 3 additions & 1 deletion database/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ CREATE TABLE xas_standard_data (
COMMENT ON TABLE xas_standard_data IS 'Data file storing the standard data';

CREATE TYPE review_status_enum AS ENUM('pending', 'approved', 'rejected');
CREATE TYPE licence_enum AS ENUM('cc_by', 'cc_0', 'logged_in_only');
CREATE TYPE licence_enum AS ENUM('cc_by', 'cc_0');
CREATE TYPE sample_form_enum AS ENUM('other', 'foil', 'pellet');

CREATE Table xas_standard (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
Expand All @@ -241,6 +242,7 @@ CREATE Table xas_standard (
sample_name TEXT,
sample_prep TEXT,
sample_comp TEXT,
sample_form sample_form_enum,
beamline_id INTEGER,
mono_name TEXT,
mono_dspacing TEXT,
Expand Down
12 changes: 12 additions & 0 deletions xas-standards-api/src/xas_standards_api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Person,
PersonInput,
ReviewStatus,
SampleForm,
XASStandard,
XASStandardAdminReviewInput,
XASStandardData,
Expand Down Expand Up @@ -82,6 +83,7 @@ def get_metadata(session):
output["edges"] = select_all(session, Edge)
output["beamlines"] = select_all(session, Beamline)
output["licences"] = list(LicenceType)
output["sample_forms"] = list(SampleForm)

return output

Expand Down Expand Up @@ -260,3 +262,13 @@ def is_admin_user(session: Session, user_id: str):
raise HTTPException(status_code=401, detail=f"User {user_id} not admin")

return True


def get_registered_elements(session: Session):
statement = (
select(Element)
.where(XASStandard.element_z == Element.z)
.where(XASStandard.review_status == ReviewStatus.approved)
)
results = session.exec(statement)
return results.unique().all()
8 changes: 7 additions & 1 deletion xas-standards-api/src/xas_standards_api/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ class ReviewStatus(enum.Enum):
class LicenceType(enum.Enum):
cc_by = "cc_by"
cc_0 = "cc_0"
logged_in_only = "logged_in_only"


class SampleForm(enum.Enum):
other = "other"
foil = "foil"
pellet = "pellet"


class PersonInput(SQLModel):
Expand Down Expand Up @@ -102,6 +107,7 @@ class XASStandardInput(SQLModel):
sample_name: str
sample_prep: Optional[str]
sample_comp: Optional[str]
sample_form: SampleForm = Field(sa_column=Column(Enum(SampleForm)))
beamline_id: int = Field(foreign_key="beamline.id")
licence: LicenceType = Field(sa_column=Column(Enum(LicenceType)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ class MetadataResponse(SQLModel):
elements: List[Element]
edges: List[Edge]
licences: List[str]
sample_forms: List[str]
14 changes: 13 additions & 1 deletion xas-standards-api/src/xas_standards_api/routers/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from fastapi_pagination.cursor import CursorPage
from sqlmodel import Session

from ..crud import get_data, get_file, get_metadata, get_standard, read_standards_page
from ..crud import (
get_data,
get_file,
get_metadata,
get_registered_elements,
get_standard,
read_standards_page,
)
from ..database import get_session
from ..models.models import ReviewStatus
from ..models.response_models import (
Expand Down Expand Up @@ -48,3 +55,8 @@ async def read_data(
return get_file(session, id)

return get_data(session, id)


@router.get("/api/elements/metrics")
async def read_elements(session: Session = Depends(get_session)):
return get_registered_elements(session)
2 changes: 2 additions & 0 deletions xas-standards-api/src/xas_standards_api/routers/protected.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def add_standard_file(
beamline_id: Annotated[int, Form()],
sample_name: Annotated[str, Form()],
sample_prep: Annotated[str, Form()],
sample_form: Annotated[str, Form()],
doi: Annotated[str, Form()],
citation: Annotated[str, Form()],
comments: Annotated[str, Form()],
Expand All @@ -62,6 +63,7 @@ def add_standard_file(
edge_id=edge_id,
sample_name=sample_name,
sample_prep=sample_prep,
sample_form=sample_form,
submitter_comments=comments,
citation=citation,
licence=licence,
Expand Down
36 changes: 29 additions & 7 deletions xas-standards-client/src/components/submission/SampleForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Grid, TextField } from "@mui/material";
import {
Grid,
TextField,
FormControl,
InputLabel,
Select,
MenuItem,
} from "@mui/material";

function SampleForm(props: {
sampleName: string;
Expand All @@ -7,6 +14,9 @@ function SampleForm(props: {
setSampleComp: (composition: string) => void;
samplePrep: string;
setSamplePrep: (preparation: string) => void;
sampleForm: string;
setSampleForm: (preparation: string) => void;
sampleFormOptions: string[];
}) {
const sampleName = props.sampleName;
const setSampleName = props.setSampleName;
Expand Down Expand Up @@ -49,12 +59,24 @@ function SampleForm(props: {
/>
</Grid>
<Grid item xs={6}>
<TextField
margin="dense"
id="sampleform"
label="Sample Form"
variant="outlined"
/>
<FormControl fullWidth>
<InputLabel margin="dense" id="sampleform">
Sample Form
</InputLabel>
<Select
name="sampleform"
id="sampleform"
label="Sample Form"
value={props.sampleForm}
onChange={(e) => props.setSampleForm(e.target.value)}
>
{props.sampleFormOptions.map((x, y) => (
<MenuItem key={y} value={x}>
{x}
</MenuItem>
))}
</Select>
</FormControl>
</Grid>
</Grid>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
const standards_url = "/api/standards";

function StandardSubmission() {
const { elements, edges, beamlines, licences } = useContext(MetadataContext);
const { elements, edges, beamlines, licences, sample_forms } =
useContext(MetadataContext);

const [file, setFile] = useState<File>();
// const [file2, setFile2] = useState<FileList>();
Expand All @@ -42,6 +43,7 @@ function StandardSubmission() {
const [sampleName, setSampleName] = useState("");
const [sampleComp, setSampleComp] = useState("");
const [samplePrep, setSamplePrep] = useState("");
const [sampleForm, setSampleForm] = useState(sample_forms[0]);
const [beamlineId, setBeamlineId] = useState(1);
const [beamlineHeader, setBeamlineHeader] = useState("");
const [doi, setDOI] = useState("");
Expand Down Expand Up @@ -72,6 +74,7 @@ function StandardSubmission() {
form.append("sample_name", sampleName);
form.append("sample_comp", sampleComp);
form.append("sample_prep", samplePrep);
form.append("sample_form", sampleForm);
form.append("doi", doi);
form.append("citation", citation);
form.append("comments", comments);
Expand Down Expand Up @@ -203,6 +206,9 @@ function StandardSubmission() {
setSampleComp={setSampleComp}
samplePrep={samplePrep}
setSamplePrep={setSamplePrep}
sampleForm={sampleForm}
setSampleForm={setSampleForm}
sampleFormOptions={sample_forms}
/>
</Grid>
<Grid item xs={6}>
Expand Down
1 change: 1 addition & 0 deletions xas-standards-client/src/hooks/useMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function useMetadata(): AppMetadata {
elements: [],
edges: [],
licences: [],
sample_forms: []
});

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions xas-standards-client/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface AppMetadata {
elements: Element[];
edges: Edge[];
licences: string[];
sample_forms: string[];
}

export interface User {
Expand Down

0 comments on commit f2c2633

Please sign in to comment.