Exemple #1
0
def create_student():
    body = json.loads(request.data)
    email = body["email"]
    club_ids = body["club_ids"]

    student = Student(
        email=email
    )

    existing_student = Student.query.filter_by(email=email).first()
    if existing_student is not None:
        db.session.delete(existing_student)

    club_list = []
    for id in club_ids:
        club = Club.query.filter_by(id=id).first()
        if club is not None:
            student.clubs.append(club)
            club_list.append(club.name)
        else:
            return json.dumps({"Success": False, "data": "Club does not exist!"}), 201

    db.session.add(student)
    db.session.commit()

    EMAIL_ADDRESS = '*****@*****.**'
    EMAIL_PASSWORD = '******'

    smtplibObj = smtplib.SMTP('smtp.gmail.com', 587)
    smtplibObj.ehlo()
    smtplibObj.starttls()
    smtplibObj.login(EMAIL_ADDRESS, EMAIL_PASSWORD)

    club_message = ''
    for club in club_list:
        club_message += club + '\n    '

    subject = 'Clubby confirmation email'
    body = '    You have successfully registered in our Clubby notification system! Here is a list of ' + (
    'project teams you subscribed: \n\n    ') + club_message + '\n    You will receive email notification when ' + (
    'these teams update their application info.')

    msg = f'Subject: {subject}\n\n{body}'

    try:
        smtplibObj.sendmail(EMAIL_ADDRESS, email, msg)
    except:
        print("Internal error")
        return json.dumps({"Success": False, "data": "Email is not valid!"}), 400

    return json.dumps({"Success": True, "data": student.serialize()}), 201
Exemple #2
0
def add_student_to_user(user_id):
    body = json.loads(request.data)
    if body.get("description") is None:
        return failure_response(
            missing_parameter_response(body, ["description"]))
    user = User.query.filter_by(id=user_id).first()
    if user is None:
        return failure_response("Given user id is not associated with a user!")
    desc = body.get("description")
    new_student = Student(user_id=user_id, description=desc)
    user.student.append(new_student)
    DB.session.add(new_student)
    DB.session.commit()
    return success_response(new_student.serialize(), 201)