Compare commits

...

3 Commits

3 changed files with 43 additions and 41 deletions

View File

@@ -1,3 +1,4 @@
from functools import wraps
from flask_login import login_required, login_user, logout_user from flask_login import login_required, login_user, logout_user
from app.bp import bp from app.bp import bp
from flask import jsonify, request from flask import jsonify, request
@@ -20,6 +21,17 @@ def check_data(data, required_fields):
return None return None
def instructor_required(func):
@wraps(func)
def dec(*args, **kwargs):
print(current_user)
if current_user.role != "instructor":
return error_response(400, "User is not instructor!")
return func(*args, **kwargs)
return dec
@bp.route("/login", methods=["POST"]) @bp.route("/login", methods=["POST"])
def login_route(): def login_route():
data = request.get_json() data = request.get_json()
@@ -88,7 +100,9 @@ def create_course():
c = Course.query.filter_by(course_code=data["course_code"]).first() c = Course.query.filter_by(course_code=data["course_code"]).first()
if c: if c:
return error_response(400, f"Course with course code {data['course_code']} already exists") return error_response(
400, f"Course with course code {data['course_code']} already exists"
)
if u.role != "instructor": if u.role != "instructor":
return error_response(400, "User is not instructor") return error_response(400, "User is not instructor")
@@ -114,6 +128,7 @@ def get_courses(id):
@bp.route("/user/<int:uid>/enroll/<int:cid>", methods=["POST", "DELETE"]) @bp.route("/user/<int:uid>/enroll/<int:cid>", methods=["POST", "DELETE"])
@login_required @login_required
@instructor_required
def enroll_student(uid, cid): def enroll_student(uid, cid):
u = User.query.get(uid) u = User.query.get(uid)
if not u: if not u:
@@ -125,7 +140,9 @@ def enroll_student(uid, cid):
if request.method == "POST": if request.method == "POST":
if not u.enroll(c): if not u.enroll(c):
return error_response(400, f"User {uid} is already enrolled in course {cid}") return error_response(
400, f"User {uid} is already enrolled in course {cid}"
)
elif request.method == "DELETE": elif request.method == "DELETE":
if not u.unenroll(c): if not u.unenroll(c):

View File

@@ -1,43 +1,25 @@
import { useContext, useEffect, useState } from "react";
import { Card, Container } from "react-bootstrap"; import { Card, Container } from "react-bootstrap";
import { Link } from "wouter"; import { Link } from "wouter";
import UserContext from "../contexts/UserContext";
import { makeRequest } from "../utils.ts";
const CoursesWidget = ({ className = "" }) => { const CoursesWidget = ({ className = "" }) => {
const dummyData = [ const [courseData, setCourseData] = useState([]);
{ const { currentUser } = useContext(UserContext);
course_id: 1,
course_title: "Advanced Website Design", useEffect(() => {
couse_code: "COMP 2707", makeRequest({ url: `http://localhost:5000/user/${currentUser.id}/courses` })
instructor: "Saja Al Mamoori", .then((resp) => resp.json())
}, .then((data) => {
{ setCourseData(data.courses);
course_id: 2, });
course_title: "Introduction to Roman Civilization", }, [setCourseData, currentUser]);
couse_code: "GRST 1200",
instructor: "Max Nelson",
},
{
course_id: 3,
course_title: "Software Verification and Testing",
couse_code: "COMP 4110",
instructor: "Serif Saad",
},
{
course_id: 4,
course_title: "Selected Topics in Software Engineering",
couse_code: "COMP 4800",
instructor: "Jessica Chen",
},
{
course_id: 5,
course_title: "Project Management: Techniques and Tools",
couse_code: "COMP 4990",
instructor: "Arunita Jaekel",
},
];
return ( return (
<Container className={`${className} py-3 grid`}> <Container className={`${className} py-3 grid`}>
<div className="row justify-content-center"> <div className="row justify-content-center">
{dummyData.map((course, i) => { {courseData.map((course, i) => {
return ( return (
<Link <Link
is="a" is="a"
@@ -46,9 +28,9 @@ const CoursesWidget = ({ className = "" }) => {
className="col col-lg-2" className="col col-lg-2"
> >
<Card role="button" className="m-2" style={{ width: "300px" }}> <Card role="button" className="m-2" style={{ width: "300px" }}>
<h2 className="text-center py-5 border">{course.couse_code}</h2> <h2 className="text-center py-5 border">{course.course_code}</h2>
<Card.Body> <Card.Body>
<Card.Title>{course.course_title}</Card.Title> <Card.Title>{course.name}</Card.Title>
<Card.Text>{course.instructor}</Card.Text> <Card.Text>{course.instructor}</Card.Text>
</Card.Body> </Card.Body>
</Card> </Card>

View File

@@ -1,11 +1,14 @@
const makeRequest = ({ url, method, body = {} }): Promise<Response> => { const makeRequest = ({ url, method, body = null }): Promise<Response> => {
return fetch(url, { const req: RequestInit = {
method: method, method: method,
credentials: "include", credentials: "include",
body: JSON.stringify(body),
headers: { "content-type": "application/json" }, headers: { "content-type": "application/json" },
mode: "cors", mode: "cors",
}); };
if (body) {
req["body"] = JSON.stringify(body);
}
return fetch(url, req);
}; };
const sendLoginRequest = async ( const sendLoginRequest = async (