Compare commits
No commits in common. 'c0d56218b6951a5d832b21274270fe5d5a4ca1f7' and '577675317494a1739176082ebeecb0d00d2d29cc' have entirely different histories.
c0d56218b6
...
5776753174
@ -1,83 +1,3 @@ |
|||||||
from flask import Blueprint, request, jsonify |
from flask import Blueprint |
||||||
from werkzeug.utils import secure_filename |
|
||||||
from sqlalchemy.sql import func |
|
||||||
import os |
|
||||||
import uuid |
|
||||||
from db.model import db, Course, Category, User |
|
||||||
from utils.utils import random_string_generator |
|
||||||
from constants import * |
|
||||||
from config import * |
|
||||||
|
|
||||||
course = Blueprint('course', __name__) |
course = Blueprint('course', __name__) |
||||||
|
|
||||||
@course.route('/create/course', methods=['POST']) |
|
||||||
def create_course(): |
|
||||||
""" |
|
||||||
Endpoint to create a new course. |
|
||||||
Ensures strict adherence to the Course model, with file handling and default behaviors. |
|
||||||
""" |
|
||||||
try: |
|
||||||
# Retrieve form data |
|
||||||
data = request.form |
|
||||||
name :str= data.get('name', '').strip() |
|
||||||
category_id :str = data.get('category_id', '').strip() |
|
||||||
description :str= data.get('description', '') |
|
||||||
is_active :bool = bool(int(data.get('is_active', 0))) |
|
||||||
published_status :int = data.get('published_status', int(PublishedStatus.DRAFT.value)) # Defaults to DRAFT |
|
||||||
author_id :str = data.get('author_id', '').strip() |
|
||||||
|
|
||||||
# Validate required fields |
|
||||||
if not name or not category_id or not author_id: |
|
||||||
return jsonify({"error": "Missing required fields: 'name', 'category_id', or 'author_id'."}), 400 |
|
||||||
|
|
||||||
# Validate category existence |
|
||||||
category :str= Category.query.get(category_id) |
|
||||||
if not category: |
|
||||||
return jsonify({"error": "Category not found."}), 404 |
|
||||||
|
|
||||||
# Validate author existence |
|
||||||
author :str = User.query.get(author_id) |
|
||||||
if not author: |
|
||||||
return jsonify({"error": "Author not found."}), 404 |
|
||||||
|
|
||||||
# File handling |
|
||||||
uploaded_cover_image = request.files.get('cover') |
|
||||||
uploaded_file = request.files.get('pdf') |
|
||||||
|
|
||||||
cover_filename_image :str= '' |
|
||||||
pdf_file_filename :str= '' |
|
||||||
if uploaded_cover_image is not None: |
|
||||||
cover_filename_image :str = random_string_generator(10) + os.path.splitext(uploaded_cover_image.filename)[-1] |
|
||||||
uploaded_cover_image.save(os.path.join(USER_UPLOADS_DIR, cover_filename_image)) |
|
||||||
|
|
||||||
if uploaded_file is not None: |
|
||||||
pdf_file_filename :str= random_string_generator(10) + os.path.splitext(uploaded_file.filename)[-1] |
|
||||||
uploaded_file.save( os.path.join(USER_UPLOADS_DIR, pdf_file_filename)) |
|
||||||
|
|
||||||
# Default to DRAFT if critical content is missing |
|
||||||
if not uploaded_cover_image or not uploaded_file: |
|
||||||
published_status :int= int(PublishedStatus.DRAFT.value) |
|
||||||
|
|
||||||
# Create new course |
|
||||||
new_course = Course( |
|
||||||
name=name, |
|
||||||
categoryID=uuid.UUID(category_id), |
|
||||||
description=description, |
|
||||||
isActive=is_active, |
|
||||||
publishedStatus=int(published_status), |
|
||||||
coverImage=cover_filename_image, |
|
||||||
serverFilename=pdf_file_filename, |
|
||||||
authorID=uuid.UUID(author_id), |
|
||||||
creationDate=func.now(), |
|
||||||
) |
|
||||||
|
|
||||||
# Save the course to the database |
|
||||||
db.session.add(new_course) |
|
||||||
db.session.commit() |
|
||||||
|
|
||||||
return jsonify({"message": "Course created successfully.", "course_id": str(new_course.id)}), 201 |
|
||||||
|
|
||||||
except Exception as e: |
|
||||||
# Rollback in case of an error |
|
||||||
db.session.rollback() |
|
||||||
return jsonify({"error": f"An error occurred: {str(e)}"}), 500 |
|
Loading…
Reference in new issue