Compare commits
3 Commits
0ba212e1bb
...
1dfa9c1421
| Author | SHA1 | Date | |
|---|---|---|---|
|
1dfa9c1421
|
|||
|
15d336ca53
|
|||
|
9c6b46f68f
|
@@ -1,3 +1,4 @@
|
||||
from functools import wraps
|
||||
from flask_login import login_required, login_user, logout_user
|
||||
from app.bp import bp
|
||||
from flask import jsonify, request
|
||||
@@ -20,6 +21,17 @@ def check_data(data, required_fields):
|
||||
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"])
|
||||
def login_route():
|
||||
data = request.get_json()
|
||||
@@ -88,7 +100,9 @@ def create_course():
|
||||
|
||||
c = Course.query.filter_by(course_code=data["course_code"]).first()
|
||||
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":
|
||||
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"])
|
||||
@login_required
|
||||
@instructor_required
|
||||
def enroll_student(uid, cid):
|
||||
u = User.query.get(uid)
|
||||
if not u:
|
||||
@@ -125,7 +140,9 @@ def enroll_student(uid, cid):
|
||||
|
||||
if request.method == "POST":
|
||||
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":
|
||||
if not u.unenroll(c):
|
||||
|
||||
@@ -1,43 +1,25 @@
|
||||
import { useContext, useEffect, useState } from "react";
|
||||
import { Card, Container } from "react-bootstrap";
|
||||
import { Link } from "wouter";
|
||||
import UserContext from "../contexts/UserContext";
|
||||
import { makeRequest } from "../utils.ts";
|
||||
|
||||
const CoursesWidget = ({ className = "" }) => {
|
||||
const dummyData = [
|
||||
{
|
||||
course_id: 1,
|
||||
course_title: "Advanced Website Design",
|
||||
couse_code: "COMP 2707",
|
||||
instructor: "Saja Al Mamoori",
|
||||
},
|
||||
{
|
||||
course_id: 2,
|
||||
course_title: "Introduction to Roman Civilization",
|
||||
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",
|
||||
},
|
||||
];
|
||||
const [courseData, setCourseData] = useState([]);
|
||||
const { currentUser } = useContext(UserContext);
|
||||
|
||||
useEffect(() => {
|
||||
makeRequest({ url: `http://localhost:5000/user/${currentUser.id}/courses` })
|
||||
.then((resp) => resp.json())
|
||||
.then((data) => {
|
||||
setCourseData(data.courses);
|
||||
});
|
||||
}, [setCourseData, currentUser]);
|
||||
|
||||
return (
|
||||
<Container className={`${className} py-3 grid`}>
|
||||
<div className="row justify-content-center">
|
||||
{dummyData.map((course, i) => {
|
||||
{courseData.map((course, i) => {
|
||||
return (
|
||||
<Link
|
||||
is="a"
|
||||
@@ -46,9 +28,9 @@ const CoursesWidget = ({ className = "" }) => {
|
||||
className="col col-lg-2"
|
||||
>
|
||||
<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.Title>{course.course_title}</Card.Title>
|
||||
<Card.Title>{course.name}</Card.Title>
|
||||
<Card.Text>{course.instructor}</Card.Text>
|
||||
</Card.Body>
|
||||
</Card>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
const makeRequest = ({ url, method, body = {} }): Promise<Response> => {
|
||||
return fetch(url, {
|
||||
const makeRequest = ({ url, method, body = null }): Promise<Response> => {
|
||||
const req: RequestInit = {
|
||||
method: method,
|
||||
credentials: "include",
|
||||
body: JSON.stringify(body),
|
||||
headers: { "content-type": "application/json" },
|
||||
mode: "cors",
|
||||
});
|
||||
};
|
||||
if (body) {
|
||||
req["body"] = JSON.stringify(body);
|
||||
}
|
||||
return fetch(url, req);
|
||||
};
|
||||
|
||||
const sendLoginRequest = async (
|
||||
|
||||
Reference in New Issue
Block a user