Example #1
0
def add_lesson():
    form = request.form
    lesson = Lesson(name=form['name'])
    lesson.save()
    company = Company.objects(id=form['company_id']).first()
    company.insert_lesson(lesson)
    company.save()
    return redirect(url_for('.write_point', lesson_id=lesson.id))
Example #2
0
def create():
    # Check if user exists and signed in
    jwt_user = get_jwt_identity()
    user = User.get_or_none(User.name == jwt_user)
    if not user:
        return error_401("Unauthorized action")

    # Retrieve data from json
    title = request.form.get("title")
    description = request.form.get("description")
    if (str(request.form.get("teach")) == "true"):
        teach = True
    elif (str(request.form.get("teach")) == "false"):
        teach = False
    else:
        return error_401("Invalid teach input")
    skill = request.form.get("skill")

    # Retrieve image from json 
    image_valid = False
    if "image" in request.files:
        image_file = request.files['image']
        if image_file and allowed_file(image_file.filename):
            image_file.filename = secure_filename(image_file.filename)
            output   	  = upload_file_to_s3(image_file)
            image_valid = True

    # Check title and description and then create new lesson
    if title and description:
        if image_valid:
            lesson = Lesson(title=title, description=description, teach=teach, owner=user, skill=skill, image=image_file.filename)
        else:
            lesson = Lesson(title=title, description=description, teach=teach, owner=user, skill=skill)
        if lesson.save():
            lesson = {
                'id': lesson.id,
                'title': lesson.title,
                'description': lesson.description,
                'rating': lesson.rating,
                'teach': lesson.teach,
                'owner_id': lesson.owner_id,
                'owner_name': lesson.owner.name,
                'skill_id': lesson.skill_id,
                'skill_name': lesson.skill.name,
                'image_url': lesson.image_url
            }
            return success_201("New lesson created successfully", lesson) 
        else:
            return error_401("Create lesson failed")    
    else:
        return error_401("Invalid title or description")