Exemple #1
0
def requestLoginWithUsername(request):
    """
        Authenticate a login request.

        If it is a legal one, a push token(for iOS device) will also be generated
        and stored in the database.
    """

    if request.method == 'POST' :
        username = request.POST['username']
        username = username.lower()
        password = request.POST['password']
        logging.info("login request: %s, %s", username, password)
        #
        # validate the login attempt
        #
        if not user.isValid(username, password):
            to_json = {
                     "requestType": "login",
                     "message": "fail"
                     }
        else:
            #
            # Generate a push token
            pushToken = ''
            try:
                # iOS front-end
                pushToken = request.POST['pushToken']
            except:
                # Web front-end
                pass
            print pushToken

            logging.debug("Generated pushToken: %s", pushToken)
            uid = user.getIdByUsername(username)
            user.setPushToken(uid, pushToken)
            privilege_id = user.getPrivilegeById(uid)

            #
            # Generate the login success mesage
            to_json = {
                    "requestType": "login",         # ca be banned since pushing is not necessary in this method
                    "username": username,
                    "message": "success",
                    "privilege_id": privilege_id,
                    }
        #
        response = HttpResponse(simplejson.dumps(to_json), mimetype='application/json')
        logging.info("response: %s", to_json)
    else:
        response = HttpResponse("Error!")
    #
    # TODO WRAP
    if request.META['HTTP_USER_AGENT'].find("iPhone") == -1:
        response['Access-Control-Allow-Origin']='*'
    return response
Exemple #2
0
def updateStatusWithUsername(request):
    """
        Called to sync with the server.
        TODO: More wrap needed!
    """
    if request.method == 'POST':
        username = request.POST['username']
        uid = user.getIdByUsername(username)
        privilege_id = user.getPrivilegeById(uid)

        if privilege_id == '0':
            #
            # then he is a user
            # check comming call
            session_id = user.checkCallById(uid)
            if session_id != '':
                #
                # generate a corresponding token
                token = opentok_sdk.generate_token(session_id)
                to_json = {'requestType':'answerVideoCall',
                        'message':'accept',
                        'sessionId':session_id,
                        'token':token}
                logging.info('%s get a comming call', username)

            else:
                #
                # There's no comming call
                to_json = {'sessionId':'', 'token':'', 'message':'reject'}
            response = HttpResponse(simplejson.dumps(to_json), mimetype='application/json')

        if privilege_id == '2':
            #
            # he is a translator
            (lang_1, lang_2) = user.getLanguageByID(uid)
            session_id = user.getTransTask()
            if session_id != '':
                #
                # generate a corresponding token
                token = opentok_sdk.generate_token(session_id)
                to_json = {'requestType':'answerVideoCall',
                        'message':'accept',
                        'sessionId':session_id,
                        'token':token}
            else:
                to_json = {'sessionId':'', 'token':'', 'message':'reject'}
            response = HttpResponse(simplejson.dumps(to_json), mimetype='application/json')
    else:
        response = HttpResponse("Error!")

    #logging.info("response: %s", to_json)
    if request.META['HTTP_USER_AGENT'].find("iPhone") == -1:
        response['Access-Control-Allow-Origin']='*'
    return response