mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
wip
This commit is contained in:
parent
0e2e66d0e6
commit
0a2c5526bc
7 changed files with 55 additions and 41 deletions
|
|
@ -49,9 +49,9 @@ class CourseRead(CourseBase):
|
||||||
|
|
||||||
class FullCourseRead(CourseBase):
|
class FullCourseRead(CourseBase):
|
||||||
id: int
|
id: int
|
||||||
course_uuid: str
|
course_uuid: Optional[str]
|
||||||
creation_date: str
|
creation_date: Optional[str]
|
||||||
update_date: str
|
update_date: Optional[str]
|
||||||
# Chapters, Activities
|
# Chapters, Activities
|
||||||
chapters: List[ChapterRead]
|
chapters: List[ChapterRead]
|
||||||
authors: List[UserRead]
|
authors: List[UserRead]
|
||||||
|
|
@ -60,9 +60,9 @@ class FullCourseRead(CourseBase):
|
||||||
|
|
||||||
class FullCourseReadWithTrail(CourseBase):
|
class FullCourseReadWithTrail(CourseBase):
|
||||||
id: int
|
id: int
|
||||||
course_uuid: str
|
course_uuid: Optional[str]
|
||||||
creation_date: str
|
creation_date: Optional[str]
|
||||||
update_date: str
|
update_date: Optional[str]
|
||||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||||
authors: List[UserRead]
|
authors: List[UserRead]
|
||||||
# Chapters, Activities
|
# Chapters, Activities
|
||||||
|
|
|
||||||
|
|
@ -47,10 +47,10 @@ class TrailRunRead(BaseModel):
|
||||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||||
user_id: int = Field(default=None, foreign_key="user.id")
|
user_id: int = Field(default=None, foreign_key="user.id")
|
||||||
# course object
|
# course object
|
||||||
course: dict
|
course: Optional[dict]
|
||||||
# timestamps
|
# timestamps
|
||||||
creation_date: str
|
creation_date: Optional[str]
|
||||||
update_date: str
|
update_date: Optional[str]
|
||||||
# number of activities in course
|
# number of activities in course
|
||||||
course_total_steps: int
|
course_total_steps: int
|
||||||
steps: list[TrailStep]
|
steps: list[TrailStep]
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,11 @@ class TrailCreate(TrailBase):
|
||||||
# trick because Lists are not supported in SQLModel (runs: list[TrailRun] )
|
# trick because Lists are not supported in SQLModel (runs: list[TrailRun] )
|
||||||
class TrailRead(BaseModel):
|
class TrailRead(BaseModel):
|
||||||
id: Optional[int] = Field(default=None, primary_key=True)
|
id: Optional[int] = Field(default=None, primary_key=True)
|
||||||
trail_uuid: str
|
trail_uuid: Optional[str]
|
||||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||||
user_id: int = Field(default=None, foreign_key="user.id")
|
user_id: int = Field(default=None, foreign_key="user.id")
|
||||||
creation_date: str
|
creation_date: Optional[str]
|
||||||
update_date: str
|
update_date: Optional[str]
|
||||||
runs: list[TrailRunRead]
|
runs: list[TrailRunRead]
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
|
|
||||||
|
|
@ -207,6 +207,10 @@ async def get_course_chapters(
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
limit: int = 10,
|
limit: int = 10,
|
||||||
) -> List[ChapterRead]:
|
) -> List[ChapterRead]:
|
||||||
|
|
||||||
|
statement = select(Course).where(Course.id == course_id)
|
||||||
|
course = db_session.exec(statement).first()
|
||||||
|
|
||||||
statement = (
|
statement = (
|
||||||
select(Chapter)
|
select(Chapter)
|
||||||
.join(CourseChapter, Chapter.id == CourseChapter.chapter_id)
|
.join(CourseChapter, Chapter.id == CourseChapter.chapter_id)
|
||||||
|
|
@ -220,7 +224,7 @@ async def get_course_chapters(
|
||||||
chapters = [ChapterRead(**chapter.dict(), activities=[]) for chapter in chapters]
|
chapters = [ChapterRead(**chapter.dict(), activities=[]) for chapter in chapters]
|
||||||
|
|
||||||
# RBAC check
|
# RBAC check
|
||||||
await rbac_check(request, "chapter_x", current_user, "read", db_session)
|
await rbac_check(request, course.course_uuid, current_user, "read", db_session)
|
||||||
|
|
||||||
# Get activities for each chapter
|
# Get activities for each chapter
|
||||||
for chapter in chapters:
|
for chapter in chapters:
|
||||||
|
|
@ -532,7 +536,7 @@ async def reorder_chapters_and_activities(
|
||||||
|
|
||||||
async def rbac_check(
|
async def rbac_check(
|
||||||
request: Request,
|
request: Request,
|
||||||
course_id: str,
|
course_uuid: str,
|
||||||
current_user: PublicUser | AnonymousUser,
|
current_user: PublicUser | AnonymousUser,
|
||||||
action: Literal["create", "read", "update", "delete"],
|
action: Literal["create", "read", "update", "delete"],
|
||||||
db_session: Session,
|
db_session: Session,
|
||||||
|
|
@ -543,7 +547,7 @@ async def rbac_check(
|
||||||
request,
|
request,
|
||||||
current_user.id,
|
current_user.id,
|
||||||
action,
|
action,
|
||||||
course_id,
|
course_uuid,
|
||||||
db_session,
|
db_session,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,12 +96,17 @@ async def get_course_meta(
|
||||||
chapters = await get_course_chapters(request, course.id, db_session, current_user)
|
chapters = await get_course_chapters(request, course.id, db_session, current_user)
|
||||||
|
|
||||||
# Trail
|
# Trail
|
||||||
|
trail = None
|
||||||
|
|
||||||
|
if isinstance(current_user, AnonymousUser):
|
||||||
|
trail = None
|
||||||
|
else:
|
||||||
trail = await get_user_trail_with_orgid(
|
trail = await get_user_trail_with_orgid(
|
||||||
request, current_user, course.org_id, db_session
|
request, current_user, course.org_id, db_session
|
||||||
)
|
)
|
||||||
|
|
||||||
trail = TrailRead.from_orm(trail)
|
trail = TrailRead.from_orm(trail)
|
||||||
|
|
||||||
|
|
||||||
return FullCourseReadWithTrail(
|
return FullCourseReadWithTrail(
|
||||||
**course.dict(),
|
**course.dict(),
|
||||||
chapters=chapters,
|
chapters=chapters,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ from src.db.courses import Course
|
||||||
from src.db.trail_runs import TrailRun, TrailRunRead
|
from src.db.trail_runs import TrailRun, TrailRunRead
|
||||||
from src.db.trail_steps import TrailStep
|
from src.db.trail_steps import TrailStep
|
||||||
from src.db.trails import Trail, TrailCreate, TrailRead
|
from src.db.trails import Trail, TrailCreate, TrailRead
|
||||||
from src.db.users import PublicUser
|
from src.db.users import AnonymousUser, PublicUser
|
||||||
|
|
||||||
|
|
||||||
async def create_user_trail(
|
async def create_user_trail(
|
||||||
|
|
@ -122,9 +122,15 @@ async def check_trail_presence(
|
||||||
|
|
||||||
|
|
||||||
async def get_user_trail_with_orgid(
|
async def get_user_trail_with_orgid(
|
||||||
request: Request, user: PublicUser, org_id: int, db_session: Session
|
request: Request, user: PublicUser | AnonymousUser, org_id: int, db_session: Session
|
||||||
) -> TrailRead:
|
) -> TrailRead:
|
||||||
|
|
||||||
|
if isinstance(user, AnonymousUser):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
detail="Anonymous users cannot access this endpoint",
|
||||||
|
)
|
||||||
|
|
||||||
trail = await check_trail_presence(
|
trail = await check_trail_presence(
|
||||||
org_id=org_id,
|
org_id=org_id,
|
||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import FormLayout, { ButtonBlack, FormField, FormLabel, FormLabelAndMessage, For
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import * as Form from '@radix-ui/react-form';
|
import * as Form from '@radix-ui/react-form';
|
||||||
import { getOrgLogoMediaDirectory } from '@services/media/media';
|
import { getOrgLogoMediaDirectory } from '@services/media/media';
|
||||||
import { AlertTriangle } from 'lucide-react';
|
import { AlertTriangle, Check, User } from 'lucide-react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { signup } from '@services/auth/auth';
|
import { signup } from '@services/auth/auth';
|
||||||
import { getUriWithOrg } from '@services/config/config';
|
import { getUriWithOrg } from '@services/config/config';
|
||||||
|
|
@ -44,10 +44,6 @@ const validate = (values: any) => {
|
||||||
errors.username = 'Username must be at least 4 characters';
|
errors.username = 'Username must be at least 4 characters';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!values.full_name) {
|
|
||||||
errors.full_name = 'Required';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!values.bio) {
|
if (!values.bio) {
|
||||||
errors.bio = 'Required';
|
errors.bio = 'Required';
|
||||||
}
|
}
|
||||||
|
|
@ -61,6 +57,7 @@ function SignUpClient(props: SignUpClientProps) {
|
||||||
const [isSubmitting, setIsSubmitting] = React.useState(false);
|
const [isSubmitting, setIsSubmitting] = React.useState(false);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [error, setError] = React.useState('');
|
const [error, setError] = React.useState('');
|
||||||
|
const [message, setMessage] = React.useState('');
|
||||||
const formik = useFormik({
|
const formik = useFormik({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
org_slug: props.org?.slug,
|
org_slug: props.org?.slug,
|
||||||
|
|
@ -68,7 +65,8 @@ function SignUpClient(props: SignUpClientProps) {
|
||||||
password: '',
|
password: '',
|
||||||
username: '',
|
username: '',
|
||||||
bio: '',
|
bio: '',
|
||||||
full_name: '',
|
first_name: '',
|
||||||
|
last_name: '',
|
||||||
},
|
},
|
||||||
validate,
|
validate,
|
||||||
onSubmit: async values => {
|
onSubmit: async values => {
|
||||||
|
|
@ -76,7 +74,8 @@ function SignUpClient(props: SignUpClientProps) {
|
||||||
let res = await signup(values);
|
let res = await signup(values);
|
||||||
let message = await res.json();
|
let message = await res.json();
|
||||||
if (res.status == 200) {
|
if (res.status == 200) {
|
||||||
router.push(`/`);
|
//router.push(`/login`);
|
||||||
|
setMessage('Your account was successfully created')
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
}
|
}
|
||||||
else if (res.status == 401 || res.status == 400 || res.status == 404 || res.status == 409) {
|
else if (res.status == 401 || res.status == 400 || res.status == 404 || res.status == 409) {
|
||||||
|
|
@ -126,6 +125,16 @@ function SignUpClient(props: SignUpClientProps) {
|
||||||
<div className="font-bold text-sm">{error}</div>
|
<div className="font-bold text-sm">{error}</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{message && (
|
||||||
|
<div className="flex flex-col space-y-4 justify-center bg-green-200 rounded-md text-green-950 space-x-2 items-center p-4 transition-all shadow-sm">
|
||||||
|
<div className='flex space-x-2'>
|
||||||
|
<Check size={18} />
|
||||||
|
<div className="font-bold text-sm">{message}</div>
|
||||||
|
</div>
|
||||||
|
<hr className='border-green-900/20 800 w-40 border' />
|
||||||
|
<Link className='flex space-x-2 items-center' href={'/login'}><User size={14} /> <div>Login </div></Link>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<FormLayout onSubmit={formik.handleSubmit}>
|
<FormLayout onSubmit={formik.handleSubmit}>
|
||||||
<FormField name="email">
|
<FormField name="email">
|
||||||
<FormLabelAndMessage label='Email' message={formik.errors.email} />
|
<FormLabelAndMessage label='Email' message={formik.errors.email} />
|
||||||
|
|
@ -150,16 +159,6 @@ function SignUpClient(props: SignUpClientProps) {
|
||||||
</Form.Control>
|
</Form.Control>
|
||||||
</FormField>
|
</FormField>
|
||||||
|
|
||||||
{/* for full name */}
|
|
||||||
<FormField name="full_name">
|
|
||||||
<FormLabelAndMessage label='Full Name' message={formik.errors.full_name} />
|
|
||||||
|
|
||||||
<Form.Control asChild>
|
|
||||||
<Input onChange={formik.handleChange} value={formik.values.full_name} type="text" required />
|
|
||||||
</Form.Control>
|
|
||||||
|
|
||||||
</FormField>
|
|
||||||
|
|
||||||
{/* for bio */}
|
{/* for bio */}
|
||||||
<FormField name="bio">
|
<FormField name="bio">
|
||||||
<FormLabelAndMessage label='Bio' message={formik.errors.bio} />
|
<FormLabelAndMessage label='Bio' message={formik.errors.bio} />
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue