Ejemplo n.º 1
0
def api_retrieve_for_notifications():
    """Retrieve a list of students who can receive a given notification.
    
    Endpoint: 
        POST /api/retrievefornotifications
    Headers: 
        Content-Type: application/json
    Success response status: 
        HTTP 200
    Request body example 1:
        {
            "teacher": "*****@*****.**",
            "notification": "Hello students! @[email protected] @[email protected]"
        }
    Success response body 1:
        {
            "recipients":
            [
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**"
            ]
        }
    """
    # Load latest app config into db
    database = db.DB(app)
    
    # Check input format
    content = request.json
    if content==None:
        raise errors.InvalidUsage("Missing JSON, provide teacher mail under 'teacher' and message under 'notification'",status_code = 400)
    try:
        teacher = content['teacher']
        notification = content['notification']
    except KeyError:
        raise errors.InvalidUsage("Invalid JSON format, provide teacher mail under 'teacher' and message under 'notification'",status_code = 400)
    
    if teacher == None or len(teacher)<1 or type(teacher)!=str:
        raise errors.InvalidUsage("Invalid JSON format, provide teacher mail under 'teacher' string",status_code = 400)
    if notification == None or type(notification) != str:
        raise errors.InvalidUsage("Invalid JSON format, provide message under 'notification' as string",status_code = 400)
        
    # Query db
    session = database.DBSession()
    if not database.is_teacher_exist(session,teacher):
        raise errors.InvalidUsage("Provided teacher mail not found in database",status_code=404)
    students = database.get_student_mail_by_notification(session,teacher,notification)
    session.close()
    return jsonify({
        "students":list(students)
    })
Ejemplo n.º 2
0
def api_register():
    """Registers students under a teacher
    
    Endpoint:
        POST /api/register
    Headers: Content-Type:
        application/json
    Success response status:
        HTTP 204
    Request body example:
        {
            "teacher": "*****@*****.**"
            "students":
            [
            "*****@*****.**",
            "*****@*****.**"
        }
    """
    # Load latest app config into db
    database = db.DB(app)
    
    # Check input format
    content = request.json
    if content==None:
        raise errors.InvalidUsage("Missing JSON, provide teacher mail and list of students under 'teacher' and 'students' respectively",status_code = 400)
    try:
        teacher = content['teacher']
        students= content['students']
    except KeyError:
        raise errors.InvalidUsage("Invalid JSON format, provide teacher mail and list of students under 'teacher' and 'students' respectively",status_code = 400)
    
    if teacher==None or students==None or len(teacher)<1 or len(students)<1:
        raise errors.InvalidUsage('Invalid JSON format, provide non-empty teacher mail and list of students',status_code = 400)
    if type(students)!=list:
        raise errors.InvalidUsage("Invalid JSON format, provide list of students under 'students'",status_code = 400)
    
    # Query and update db
    session = database.DBSession()
    if database.register_students(session,teacher,students):
        # Transaction completed successfully
        session.commit()
        session.close()
        return Response(status=204)
    else:
        # Return status code 404 = Not found, when teacher / students is not found in db
        session.rollback()
        session.close()
        raise errors.InvalidUsage('Provided mail(s) not found in database',status_code = 404)
Ejemplo n.º 3
0
def api_get_common_students():
    """Retrieves students commonly registered to a list of teachers
    
    Endpoint:
        GET /api/commonstudents
    Success response status:
        HTTP 200
    Request example 1:
        GET /api/commonstudents?teacher=teacherken%40example.com
    Success response body 1:
        {
            "students" :
            [
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**"
            ]
        }
    Request example 2:
        GET /api/commonstudents?teacher=teacherken%40example.com&teacher=teacherjoe%40example.com
    Success response body 2:
        {
            "students" :
            [
                "*****@*****.**",
                "*****@*****.**"
            ]
        }
    """
    
    # Load latest app config into db
    database = db.DB(app)
    
    # Check input format
    try:
        teachers = dict(request.args)['teacher']
    except KeyError:
        raise errors.InvalidUsage("Invalid parameter format, provide teacher mail(s) under 'teacher'",status_code = 400)
    
    # Query db
    session = database.DBSession()
    students = database.get_student_mail_by_teachers(session,teachers) # set
    session.close()
    return jsonify({
        "students":list(students)
    })
Ejemplo n.º 4
0
def api_suspend():
    """Suspends a specified student
    
    Endpoint: 
        POST /api/suspend
    Headers: 
        Content-Type: application/json
    Success response status: 
        HTTP 204
    Request body example:
        {
            "student" : "*****@*****.**"
        }
    """
    # Load latest app config into db
    database = db.DB(app)
    
    # Check input format
    content = request.json
    if content==None:
        raise errors.InvalidUsage("Missing JSON, provide student mail under 'student'",status_code = 400)
    try:
        student = content['student']
    except KeyError:
        raise errors.InvalidUsage("Invalid JSON format, provide student mail under 'student'",status_code = 400)
    
    if student==None or len(student)<1:
        raise errors.InvalidUsage('Invalid JSON format, provide non-empty student mail',status_code = 400)
    if type(student)!=str:
        raise errors.InvalidUsage("Invalid JSON format, provide student mail under 'students' as a string",status_code = 400)
        
    # Query and update db
    session = database.DBSession()
    if database.suspend_student(session,student):
        session.commit()
        session.close()
        return Response(status=204)
    else:
        # Return status code 404 = Not found, when student is not found in db
        session.rollback()
        session.close()
        raise errors.InvalidUsage('Provided mail(s) not found in database',status_code = 404)
Ejemplo n.º 5
0
import json

sys.path.append("..")

from app import app

# Sets config for app so that db can load the new config
TEST_DB = 'test.db'
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['DEBUG'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
    os.path.join(app.config['BASE_DIR'], TEST_DB)

from app import db
db = db.DB(app)


class BasicTests(unittest.TestCase):

    ############################
    #### setup and teardown ####
    ############################

    # executed prior to each test
    def setUp(self):
        self.app = app.test_client()
        self.session = db.DBSession()
        db.drop_all()
        db.create_all(self.session)
        self.session.commit()