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

Initial review workflow #9

Merged
merged 3 commits into from
Jun 14, 2024
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
17 changes: 17 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
postgres:
image: postgres:12
restart: always
user: root
environment:
POSTGRES_USER: xasadmin
POSTGRES_PASSWORD: password
POSTGRES_DB: xasstandarddb
ports:
- '5432:5432'
volumes:
- postgres-data:/var/lib/postgresql/data
- ./database/tables.sql:/docker-entrypoint-initdb.d/create_tables.sql

volumes:
postgres-data:
20 changes: 10 additions & 10 deletions database/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ CREATE TABLE beamline (
xray_source TEXT,
facility_id INTEGER,
PRIMARY KEY (id),
UNIQUE (name),
UNIQUE (facility_id, name),
FOREIGN KEY(facility_id) REFERENCES facility (id)
);

Expand All @@ -203,22 +203,22 @@ INSERT INTO "beamline" VALUES(12,'I18','The Microfocus Beamline','I18 Undulator'
INSERT INTO "beamline" VALUES(13,'I20-scanning','Versatile X-ray Spectroscopy','I20 Wiggler', 8);

CREATE TABLE person (
id SERIAL,
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
identifier TEXT NOT NULL,
PRIMARY KEY (id),
admin BOOLEAN NOT NULL DEFAULT FALSE,
UNIQUE (identifier)
);

COMMENT ON TABLE person IS 'Table to store unique identifier of user';

CREATE TABLE xas_standard_data (
id SERIAL,
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
original_filename TEXT NOT NULL,
transmission BOOLEAN NOT NULL,
fluorescence BOOLEAN NOT NULL,
emission BOOLEAN NOT NULL,
reference BOOLEAN NOT NULL,
location TEXT NOT NULL,
PRIMARY KEY (id)
location TEXT NOT NULL
);

COMMENT ON TABLE xas_standard_data IS 'Data file storing the standard data';
Expand All @@ -227,7 +227,7 @@ CREATE TYPE review_status_enum AS ENUM('pending', 'approved', 'rejected');
CREATE TYPE licence_enum AS ENUM('cc_by', 'cc_0', 'logged_in_only');

CREATE Table xas_standard (
id SERIAL,
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
submitter_id INTEGER NOT NULL,
reviewer_id INTEGER,
submission_date TIMESTAMP,
Expand All @@ -240,12 +240,13 @@ CREATE Table xas_standard (
edge_id INTEGER,
sample_name TEXT,
sample_prep TEXT,
sample_comp TEXT,
beamline_id INTEGER,
mono_name TEXT,
mono_dspacing TEXT,
additional_metadata TEXT,
citation TEXT,
licence licence_enum NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(submitter_id) REFERENCES person (id),
FOREIGN KEY(reviewer_id) REFERENCES person (id),
FOREIGN KEY(data_id) REFERENCES xas_standard_data (id),
Expand All @@ -257,11 +258,10 @@ CREATE Table xas_standard (
COMMENT ON TABLE xas_standard IS 'Metadata relating to an xas standard measurement';

CREATE Table xas_standard_attachment (
id SERIAL,
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
xas_standard_id INTEGER NOT NULL,
location TEXT NOT NULL,
description TEXT,
PRIMARY KEY (id),
FOREIGN KEY(xas_standard_id) REFERENCES xas_standard (id)
);

Expand Down
74 changes: 55 additions & 19 deletions xas-standards-api/src/xas_standards_api/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import os
from typing import Annotated, List, Optional, Union
from typing import Annotated, List, Optional

import requests
from fastapi import (
Expand Down Expand Up @@ -33,15 +33,17 @@
update_review,
)
from .schemas import (
AdminXASStandardResponse,
Beamline,
BeamlineResponse,
Edge,
Element,
LicenceType,
MetadataResponse,
Review,
Person,
ReviewStatus,
XASStandard,
XASStandardAdminResponse,
XASStandardAdminReviewInput,
XASStandardInput,
XASStandardResponse,
)
Expand Down Expand Up @@ -121,8 +123,16 @@ async def get_current_user(


@app.get("/api/user")
async def check(user_id: str = Depends(get_current_user)):
return {"user": user_id}
async def check(
session: Session = Depends(get_session), user_id: str = Depends(get_current_user)
):

statement = select(Person).where(Person.identifier == user_id)
person = session.exec(statement).first()

admin = person is not None and person.admin

return {"user": user_id, "admin": admin}


@app.get("/api/metadata")
Expand Down Expand Up @@ -157,31 +167,44 @@ def read_edges(session: Session = Depends(get_session)) -> List[Edge]:
def read_standards(
session: Session = Depends(get_session),
element: str | None = None,
admin: bool = False,
response_model=Union[XASStandardResponse, XASStandardAdminResponse],
) -> CursorPage[XASStandardAdminResponse | XASStandardResponse]:
) -> CursorPage[XASStandardResponse]:

statement = select(XASStandard)
statement = select(XASStandard).where(
XASStandard.review_status == ReviewStatus.approved
)

if element:
statement = statement.join(Element, XASStandard.element_z == Element.z).where(
Element.symbol == element
)

if admin:
return paginate(
session,
statement.order_by(XASStandard.id),
)

def transformer(x):
return [XASStandardAdminResponse.model_validate(i) for i in x]

else:
@app.get("/api/admin/standards")
def read_standards_admin(
session: Session = Depends(get_session),
user_id: str = Depends(get_current_user),
) -> CursorPage[AdminXASStandardResponse]:

def transformer(x):
return [XASStandardResponse.model_validate(i) for i in x]
statement = select(Person).where(Person.identifier == user_id)
person = session.exec(statement).first()

return paginate(
session, statement.order_by(XASStandard.id), transformer=transformer
if person is None or not person.admin:
raise HTTPException(status_code=401, detail=f"No standard with id={user_id}")

if not person.admin:
raise HTTPException(status_code=401, detail=f"User {user_id} not admin")

statement = select(XASStandard).where(
XASStandard.review_status == ReviewStatus.pending
)

return paginate(session, statement.order_by(XASStandard.id))


@app.get("/api/standards/{id}")
async def read_standard(
Expand Down Expand Up @@ -234,8 +257,21 @@ def add_standard_file(


@app.patch("/api/standards")
def submit_review(review: Review, session: Session = Depends(get_session)):
return update_review(session, review)
def submit_review(
review: XASStandardAdminReviewInput,
session: Session = Depends(get_session),
user_id: str = Depends(get_current_user),
):

statement = select(Person).where(Person.identifier == user_id)
person = session.exec(statement).first()

if person is None or not person.admin:
raise HTTPException(status_code=401, detail=f"No standard with id={user_id}")

if not person.admin:
raise HTTPException(status_code=401, detail=f"User {user_id} not admin")
return update_review(session, review, person.id)


@app.get("/api/data/{id}")
Expand Down
8 changes: 6 additions & 2 deletions xas-standards-api/src/xas_standards_api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ def get_standard(session, id) -> XASStandard:
raise HTTPException(status_code=404, detail=f"No standard with id={id}")


def update_review(session, review):
standard = session.get(XASStandard, review.id)
def update_review(session, review, reviewer_id):
standard = session.get(XASStandard, review.standard_id)
standard.review_status = review.review_status
standard.reviewer_comments = review.reviewer_comments
standard.reviewer_id = reviewer_id
session.add(standard)
session.commit()
session.refresh(standard)
Expand Down Expand Up @@ -89,6 +91,7 @@ def add_new_standard(session, file1, xs_input: XASStandardInput, additional_file

fluorescence = "mufluor" in set_labels
transmission = "mutrans" in set_labels
reference = "murefer" in set_labels
emission = "mutey" in set_labels

xsd = XASStandardDataInput(
Expand All @@ -97,6 +100,7 @@ def add_new_standard(session, file1, xs_input: XASStandardInput, additional_file
original_filename=file1.filename,
emission=emission,
transmission=transmission,
reference=reference,
)

new_standard = XASStandard.model_validate(xs_input)
Expand Down
26 changes: 17 additions & 9 deletions xas-standards-api/src/xas_standards_api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
from sqlmodel import Column, Enum, Field, Relationship, SQLModel


class Review(BaseModel):
id: int
review_status: str


class Mono(BaseModel):
name: Optional[str] = None
d_spacing: Optional[str] = None
Expand All @@ -27,6 +22,7 @@ class PersonInput(SQLModel):

class Person(PersonInput, table=True):
id: int | None = Field(primary_key=True, default=None)
admin: bool = False


class ElementInput(SQLModel):
Expand Down Expand Up @@ -107,6 +103,7 @@ class XASStandardDataInput(SQLModel):
original_filename: str
transmission: bool
fluorescence: bool
reference: bool
emission: bool
location: str

Expand Down Expand Up @@ -153,14 +150,20 @@ class XASStandard(XASStandardInput, table=True):
data_id: int | None = Field(foreign_key="xas_standard_data.id", default=None)
reviewer_id: Optional[int] = Field(foreign_key="person.id", default=None)
reviewer_comments: Optional[str] = None
review_status: Optional[ReviewStatus] = Field(
review_status: ReviewStatus = Field(
sa_column=Column(Enum(ReviewStatus)), default=ReviewStatus.pending
)

xas_standard_data: XASStandardData = Relationship(back_populates="xas_standard")
element: Element = Relationship(sa_relationship_kwargs={"lazy": "joined"})
edge: Edge = Relationship(sa_relationship_kwargs={"lazy": "joined"})
beamline: Beamline = Relationship(sa_relationship_kwargs={"lazy": "selectin"})
submitter: Person = Relationship(
sa_relationship_kwargs={
"lazy": "selectin",
"foreign_keys": "[XASStandard.submitter_id]",
}
)


class XASStandardResponse(XASStandardInput):
Expand All @@ -171,7 +174,12 @@ class XASStandardResponse(XASStandardInput):
submitter_id: int


class XASStandardAdminResponse(XASStandardResponse):
reviewer_id: Optional[int] = None
class AdminXASStandardResponse(XASStandardResponse):
submitter: Person


class XASStandardAdminReviewInput(SQLModel):
reviewer_comments: Optional[str] = None
review_status: Optional[ReviewStatus] = None
review_status: ReviewStatus
standard_id: int
# get fedid from person table
6 changes: 3 additions & 3 deletions xas-standards-client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Header from "./components/Header.tsx";
import { Routes, Route } from "react-router-dom";
import StandardViewerMui from "./components/StandardViewer.tsx";
import StandardSubmission from "./components/StandardSubmission.tsx";
import StandardSubmission from "./components/submission/StandardSubmission.tsx";
import WelcomePage from "./components/WelcomePage.tsx";
import BasicSelect from "./components/SelectTest.tsx";

import { MetadataProvider } from "./contexts/MetadataContext.tsx";
import { UserProvider } from "./contexts/UserContext.tsx";
Expand All @@ -18,6 +17,7 @@ import { Stack } from "@mui/material";
import { createTheme, ThemeProvider } from "@mui/material/styles";

import ColorModeContext from "./contexts/ColorModeContext.tsx";
import ReviewPage from "./components/ReviewPage.tsx";

function App() {
const [mode, setMode] = useState<"light" | "dark">("light");
Expand Down Expand Up @@ -50,7 +50,6 @@ function App() {
<MetadataProvider>
<Routes>
<Route path="/" element={<WelcomePage />} />
<Route path="/test" element={<BasicSelect />} />
<Route path="/view" element={<StandardViewerMui />} />
<Route
path="/submit"
Expand All @@ -60,6 +59,7 @@ function App() {
</RequireAuth>
}
/>
<Route path="/review" element={<ReviewPage />} />
<Route path="/login" element={<LogInPage />} />
{/* <Route path="/review" element={<ReviewPage />} /> */}
</Routes>
Expand Down
4 changes: 4 additions & 0 deletions xas-standards-client/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function Header() {
const user = useContext(UserContext);
console.log(user);
const loggedIn = user != null;
const admin = user != null && user.admin
console.log(loggedIn);

const navitems = {
Expand Down Expand Up @@ -82,6 +83,9 @@ export default function Header() {
{loggedIn && (
<NavListItem to="/submit" label="Submit" />
) }
{admin && (
<NavListItem to="/review" label="Review" />
) }
</List>
</Stack>
<Stack direction="row" alignItems={"center"}>
Expand Down
Loading
Loading