示例#1
0
def listUsers(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = []

    # Retrieving tutor information
    try:
        if not request.user.is_authenticated():
            # Raise bad request response by user not logged in
            raise Exception(1, 'User not logged in')

        # Retrieve tutor by tutor id
        admin = Admin.getByUsername(request.user.username)
        if not admin:
            # Raise not found response
            raise Exception(2, 'Not an admin')

        students = Student.listAll()
        tutors = Tutor.listAll()
        # Generate data
        for s in students:
            if not any(d['username'] == s.username for d in data):
                s_data = {
                    "username": s.username,
                    "first_name": s.first_name,
                    "last_name": s.last_name,
                    "active": s.user_profile.user.is_active
                }
                data.append(s_data)

        for t in tutors:
            if not any(d['username'] == t.username for d in data):
                t_data = {
                    "username": t.username,
                    "first_name": t.first_name,
                    "last_name": t.last_name,
                    "active": t.user_profile.user.is_active
                }
                data.append(t_data)

        # Raise success response and return data
        errno = 0
        msg = 'Request succeeded!'
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Pack information into JSON
    message = {
        "errno": errno,
        "msg": msg,
        "data": data
    }
    # Render response
    return JsonResponse(message)
示例#2
0
def requestReview(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = []

    # Retrieving request information
    try:
        try:
            # Retrieve request information 
            request_json = json.loads(request.body)
            tutor_id = request_json['tutor_id']
        except:
            # Raise bad request response
            raise Exception(1, 'Bad request')

        # Check if user has logged in
        if not request.user.is_authenticated():
            # Raise bad request response by user not logged in
            raise Exception(2, 'User not logged in')

        # Get student that makes the request
        student = Student.getByUsername(request.user.username)
        if not student:
            # Raise bad request response by request not made by a student
            raise Exception(3, 'User is not student')

        # Get tutor to be reviewed
        tutor = Tutor.getByTutorID(tutor_id)
        if not tutor:
            # Raise bad request response by tutor id not found
            raise Exception(4, 'No such tutor')

        # Check if pass booking exist
        bookingSet = Booking.filterByStudentUsername(student.username)
        bookingSet &= Booking.filterByTutorUsername(tutor.username)
        bookingSet &= Booking.filterByEndTimeBefore(datetime.now())
        if not bookingSet:
            # Raise bad request response by no matching booking
            raise Exception(5, 'User not allowed to review')

        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = []
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)
示例#3
0
def review(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = []

    # Retrieving request information
    try:
        try:
            # Retrieve request information 
            request_json = json.loads(request.body)
            tutor_id = request_json['tutor_id']
            stars = request_json['stars']
            comment = request_json['comment']
            is_anonymous = request_json['is_anonymous']
        except:
            # Raise bad request response
            raise Exception(1, 'Bad request')

        # Check if user has logged in
        if not request.user.is_authenticated():
            # Raise bad request response by user not logged in
            raise Exception(2, 'User not logged in')

        # Get student that makes the request
        student = Student.getByUsername(request.user.username)
        if not student:
            # Raise bad request request not made by a student
            raise Exception(3, 'User is not student')

        # Get tutor to be reviewed
        tutor = Tutor.getByTutorID(tutor_id)
        if not tutor:
            raise Exception(4, 'No such tutor')

        # Create review
        if not Review.create(stars, comment, tutor, student, is_anonymous):
            # Throw exception when a review is not created because of invalid number of stars
            raise Exception(5, 'Invalid number of stars')

        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = []
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)
示例#4
0
def studentAddValue(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = {}
    # Retrieving tutor information
    try:
        # Try retrieving the amount to be added to the wallet
        try:
            request_json = json.loads(request.body)
            amount = request_json['amount']
        except:
            raise Exception(1, 'Bad request')

        if not request.user.is_authenticated():
            # Raise bad request response by user not logged in
            raise Exception(2, 'User not logged in')

        # Try retrieving student user from logged in user
        student = Student.getByUsername(request.user.username)
        if student == None:
        	# Raise bad request response by unknown user type
        	raise Exception(3, 'Not a student')

        # Increment the amount
        new_balance = Wallet.incrementBalance(student.user_profile.user.id, amount)
        # Create Transaction record for value added
        Transaction.createTransaction(student.user_profile.user.id, \
                                        amount, \
                                        'Value added by student ' + \
                                        str(student) + "("+student.username+")")

        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = {
        	"new_balance": new_balance
        }
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)
示例#5
0
def accountProfile(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = {}
    # Retrieving tutor information
    try:
        if not request.user.is_authenticated():
            # Raise bad request response by user not logged in
            raise Exception(1, 'User not logged in')

        # Try retrieving student user from logged in user
        student = Student.getByUsername(request.user.username)
        # Try retrieving tutor user from logged in user
        tutor = Tutor.getByUsername(request.user.username)
        # Try retrieving admin user from logged in user
        admin = Admin.getByUsername(request.user.username)

        # redirect as tutor
        if tutor != None:
            # Redirect to accountTutor
            return accountTutor(request)
        # redirect as student
        if student != None:
            # Redirect to accountStudent
            return accountStudent(request)
        # redirect as admin
        if admin != None:
            return accountAdmin(request)

        # Raise bad request response by unknown user type
        raise Exception(2, 'unknown user type')

        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = {}
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)
示例#6
0
def accountStudent(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = {}

    # Retrieving student information
    try:
        # Try retrieving student user from logged in user
        student = Student.getByUsername(request.user.username)
        # Try retrieving tutor user from logged in user
        tutor = Tutor.getByUsername(request.user.username)

        # Determine if the student is also a tutor
        is_tutor = (tutor != None)
            
        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = {
            "firstName" : student.first_name,
            "lastName" : student.last_name,
            "username" : student.username,
            "email" : student.email,
            "telNum" : student.phone_number,
            "is_student" : True,
            "is_tutor" : is_tutor
        }
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)
示例#7
0
def editProfile(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = {}

    # Retrieving signup information
    try:
        try:
            # Retrieve request information 
            request_json = json.loads(request.body)
            email = request_json['email']
            first_name = request_json['first_name']
            last_name = request_json['last_name']
            phone_number = request_json['phone_number']
            hourly_rate = request_json['hourly_rate']
            searchable = request_json['searchable']
            courses = request_json['courses']
            tags = request_json['tags']

        except:
            # Raise bad request response
            raise Exception(1, 'Bad request')

        if not request.user.is_authenticated():
            # Raise bad request response by user not logged in
            raise Exception(2, 'User not logged in')

        if email == '':
            #Raise bad request response caused by empty email
            raise Exception(3, 'Empty email')

        if first_name == '':
            # Raise bad request response caused by empty first name
            raise Exception(4, 'Empty first name')

        if last_name == '':
            # Raise bad request response caused by empty last name
            raise Exception(5, 'Empty last name')

        if phone_number == ' ':
            # Raise bad request response caused by enpty phone number
            raise Exception(6, 'Empty phone number')

        # Try retrieving student user profile from logged in user
        student = Student.getByUsername(request.user.username)
        if student != None:
            # Redirect to accountStudent
            print(student)
            student.email = email
            student.first_name = first_name
            student.last_name = last_name
            student.phone_number = phone_number
            student.saveAll()

         # Try retrieving tutor user profile from logged in user
        tutor = Tutor.getByUsername(request.user.username)
        if tutor != None:
            # Redirect to accountTutor
            print(tutor.email)
            print(email)
            tutor.email = email
            tutor.first_name = first_name
            tutor.last_name = last_name
            tutor.phone_number = phone_number
            tutor.hourly_rate = hourly_rate
            tutor.searchable = searchable
            tutor.tags.clear()
            for c in courses:
                tutor.tags.add(Tag.getByTagID(c))
            for t in tags:
                tutor.tags.add(Tag.getByTagID(t))
            tutor.saveAll()

        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = {}
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)
示例#8
0
def signup(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = {}

    # Retrieving signup information
    try:
        try:
            # Retrieve request information 
            request_json = json.loads(request.body)
            username = request_json['username']
            email = request_json['email']
            password1 = request_json['password1']
            password2 = request_json['password2']
            first_name = request_json['first_name']
            last_name = request_json['last_name']
            phone_number = request_json['phone_number']
            is_student = request_json['is_student']
            is_tutor = request_json['is_tutor']
            is_contracted = request_json['is_contracted']
            is_private = request_json['is_private']
            tutor_id = request_json['tutor_id']
            university = request_json['university']
            hourly_rate = request_json['hourly_rate']

        except:
            # Raise bad request response
            raise Exception(1, 'Bad request')

        if username == '':
            # Raise bad request response caused by empty username
            raise Exception(2, 'Empty username')

        if email == '':
            #Raise bad request response caused by empty email
            raise Exception(3, 'Empty email')

        if password1 != password2:
            # Raise bad request response caused by mismatch passwords
            raise Exception(4, 'Password mismatch')

        if password1 == '':
            # Raise bad request responce caused by empty password
            raise Exception(5, 'Empty password')

        if first_name == '':
            # Raise bad request response caused by empty first name
            raise Exception(6, 'Empty first name')

        if last_name == '':
            # Raise bad request response caused by empty last name
            raise Exception(7, 'Empty last name')

        if phone_number == '':
            # Raise bad request response caused by enpty phone number
            raise Exception(8, 'Empty phone number')

        if (not is_student and not is_tutor) or (is_contracted and is_private):
            # Raise bad request response created by invaliad user type
            raise Exception(9, 'Invalid user type')

        try:
            # Create user
            user = User.objects.create_user(username, email, password1)
            user.first_name = first_name
            user.last_name = last_name
            user.save()

            # Create user profile
            if is_student:
                # Create user profile as student
                profile = Student.create(user=user, \
                                            phone_number=phone_number)
            if is_tutor:
                # Determine tutor type
                tutor_type = "contracted"
                if is_contracted:
                    tutor_type = "contracted"
                elif is_private:
                    tutor_type = "private"

                # Create user profile as contracted tutor
                profile = Tutor.create(user=user, \
                                        phone_number=phone_number, \
                                        tutor_type=tutor_type, \
                                        tutor_id=tutor_id, \
                                        university=university, \
                                        hourly_rate=float(hourly_rate))

                # Create time slots for the newly created tutor
                # Define start time
                start_time = datetime.now()
                start_time = start_time.replace(second=0, microsecond=0)
                # Define end time
                end_time = start_time + dt.timedelta(days=7)
                # Batch create time slots for all tutor
                TimeSlot.rangeCreate(Tutor.getByUsername(profile.username), start_time, end_time, 'available')

            # Create wallet
            Wallet.create(wallet_balance=0, user_profile=profile)

        except Exception as e:
            # Raise bad request response caused by unable to create and save user
            print(e)
            user.delete()
            raise Exception(11, 'Unable to save or create user')

        # Log in user
        user = authenticate(username=username, password=password1)
        defaultLogin(request, user)

        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = {}
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)
示例#9
0
def accountTutor(request):
    # Initialize message variables
    errno = -1
    msg = ''
    data = {}
    # Retrieving tutor information
    try:
        # Try retrieving student user from logged in user
        student = Student.getByUsername(request.user.username)
        # Try retrieving tutor user from logged in user
        tutor = Tutor.getByUsername(request.user.username)

        # Determine if the tutor is also a student
        is_student = (student != None)

        # Retrieve university information
        university = University()
        try:
            university = tutor.university
        except:
            # Raise not found response caused by university not set
            raise Exception(4, 'Tutor university not set')

        # Retrieve all courses of the tutor
        courses = []
        for c in tutor.listAllCourses():
            item = {}
            item["id"] = c.id
            item["name"] = c.name
            courses.append(item)
        # Retrieve all tags of the tutor
        tags = []
        for t in tutor.listAllTags():
            item = {}
            item["id"] = t.id
            item["name"] = t.name
            tags.append(item)

        # Filling response body
        errno = 0
        msg = "Request succeeded"
        data = {
            "firstName" : tutor.first_name,
            "lastName" : tutor.last_name,
            "username" : tutor.username,
            "email" : tutor.email,
            "telNum" : tutor.phone_number,
            "is_student" : is_student,
            "is_tutor" : True,
            "tutorFee" : tutor.hourly_rate,
            "tutorID" : tutor.tutor_id,
            "tutorType" : tutor.tutor_type,
            "university" : university.name,
            "searchable" : tutor.searchable,
            "courseTag" : courses,
            "subjectTag" : tags
        }
    except Exception as e:
        # Catch error responses raised
        try:
            errno, msg = e.args
        except:
            raise e

    # Generate response message
    message = {
        "errno": errno,
        "msg": msg,
        "data": data 
    }
    return JsonResponse(message)