Ejemplo n.º 1
0
def login():
    '''
    send json post request example:
    {
      "display_name": "admin",
      "password": "******"
    }
    fetch current user data
    '''
    #     request.headers.get('Authorization').split()[1]
    req_json = request.get_json()
    displayName = req_json['display_name']
    password = req_json['password']
    # fetching from the database
    session = Session()
    user_objects = session.query(Users.user_id, Users.display_name, Users.callback_url, Users.country_phone_code, Users.create_date, Users.phone_number ).\
    filter(Users.display_name.like(displayName) & Users.password.like(password)).all()
    schema = UserSchema(many=True)
    userList = schema.dump(user_objects)
    responseObject = userList.data

    if (len(user_objects) > 0 and user_objects[0].display_name != ""):
        display_name_db = user_objects[0].display_name
        jwt_token = jwtUtil.encode_auth_token(display_name_db, SECRETKEY)
        responseObject = {'user': responseObject, 'message': 'SUCCESS'}
        responseObject.update(jwt_token)
        print(responseObject)

    # serializing as JSON
    session.close()
    return json.dumps(responseObject)
Ejemplo n.º 2
0
def getCurrentUser():
    '''
    send json post request example:
    {
      "displayName": "Manmohan",
      "phoneNum": "9023051078"
    }
    fetch current user data
    '''
    #     displayName = request.args.get('displayName')
    #     phoneNum = request.args.get('phoneNum')

    req_json = request.get_json()
    displayName = req_json['displayName']
    phoneNum = req_json['phoneNum']
    # fetching from the database
    session = Session()
    user_objects = session.query(Users.user_id, Users.display_name, Users.callback_url, Users.country_phone_code, Users.create_date, Users.phone_number ).\
    filter(Users.display_name.like(displayName) & Users.phone_number.like(phoneNum)).all()

    # transforming into JSON-serializable objects
    schema = UserSchema(many=True)
    userList = schema.dump(user_objects)

    # serializing as JSON
    session.close()
    return json.dumps(userList.data)
Ejemplo n.º 3
0
def getContacts(displayName='Krishna', phoneNum='8427434777'):
    '''
    Fetch list of contacts for a user
    {
      "displayName": "Manmohan",
      "phoneNum": "9023051078",
      "user_id": "4"
    }
    '''

    req_json = request.get_json()
    #displayName = req_json['displayName']
    #phoneNum = req_json['phoneNum']
    sender_id = req_json['sender_id']

    auth_header = request.headers.get('Authorization')
    sub = jwtUtil.parse_decode_auth_token(auth_header, SECRETKEY)
    if ('INVALID' == sub):
        return json.dumps({'status': 'Logged Out'})

    session = Session()
    u = aliased(Users)

    contact_objects = session.query(u.user_id, u.display_name, u.callback_url, u.country_phone_code, u.create_date, u.phone_number).\
    filter(u.contact_id == sender_id).all()
    schema = UserSchema(many=True)
    contactList = schema.dump(contact_objects)

    session.close()
    return json.dumps(contactList.data)
Ejemplo n.º 4
0
def fetchUsers():
    '''
    fetch all users data
    '''
    # fetching from the database
    session = Session()
    user_objects = session.query(Users).all()

    # transforming into JSON-serializable objects
    schema = UserSchema(many=True)
    userList = schema.dump(user_objects)

    # serializing as JSON
    session.close()
    return json.dumps(userList.data)
Ejemplo n.º 5
0
def getCountries():
    '''
    fetch all country data
    '''
    # fetching from the database
    session = Session()
    c = aliased(Country)
    country_objects = session.query(c.iso, c.name, c.phonecode).all()

    # transforming into JSON-serializable objects
    schema = CountrySchema(many=True)
    countryList = schema.dump(country_objects)

    # serializing as JSON
    session.close()
    return json.dumps(countryList.data)
Ejemplo n.º 6
0
def getChatHistory(senderId=3, receiverId=4):
    '''
    Fetch list of contacts for a user
    '''

    #     req_json = request.get_json()
    senderId = request.args.get('sender_id')
    receiverId = request.args.get('receiver_id')

    session = Session()

    chat_objects = session.query(Chat).\
    filter(Chat.sender_id_fk.in_((senderId, receiverId)) & Chat.receiver_id_fk.in_((senderId,receiverId))).order_by(Chat.create_date.asc()).all()
    schema = ChatSchema(many=True)
    chatList = schema.dump(chat_objects)

    session.close()
    return json.dumps(chatList.data)
Ejemplo n.º 7
0
def sendMessage():
    '''
    example post request:
        {
          "currentUser_displayName": "Manmohan",
          "receiver_displayName": "Bala",
          "sender_phoneNum": "9023051078",
          "receiver_phoneNum": "7009600580",
          "receiver_countryPhoneCode":"91",
          "message":"Be grateful to the great Thanos."  
        }
    '''
    req_json = request.get_json()
    ''' 1    
    sender_displayName = req_json['currentUser_displayName']
    receiver_displayName = req_json['receiver_displayName']
    sender_phoneNum = req_json['sender_phoneNum']
    '''
    sender_id = req_json['sender_id']
    receiver_id = req_json['receiver_id']
    receiver_countryPhoneCode = req_json['receiver_countryPhoneCode']
    receiver_phoneNum = req_json['receiver_phoneNum']
    message = req_json['message']
    '''
    1. Hit the user table get customerId API key
    2. send the sms
    3. save message in chat table with sender and receiver
    4. optional: do a getrequest to check whether user has received the message    
    '''
    # fetching from the database
    session = Session()
    ''' 1
        user_objects = session.query( Users.telesign_customer_id, Users.telesign_api_key, Users.user_id ).\
        filter(Users.display_name.like(sender_displayName) & Users.phone_number.like(sender_phoneNum)).all()
    '''
    user_objects = session.query( Users.telesign_customer_id, Users.telesign_api_key, Users.user_id ).\
    filter(Users.user_id.like(sender_id)).all()

    APIkey = user_objects[0].telesign_api_key
    customerID = user_objects[0].telesign_customer_id
    ''' 2
     sender_id = user_objects[0].user_id
    '''
    '''
    3
    fetch receiver_id_fk by querying user table 
    Save message data 
    
    receiver_objects = session.query( Users.user_id ).\
    filter(Users.display_name.like(receiver_displayName) & Users.phone_number.like(receiver_phoneNum)).all()
    receiver_id = receiver_objects[0].user_id
    '''

    iMessage = Chat(message=message,
                    sender_id_fk=sender_id,
                    receiver_id_fk=receiver_id)

    session.add(iMessage)
    session.commit()

    # serializing as JSON
    session.close()
    #customerID = '0E09D1B5-86CB-4E89-828E-CA61C850BA06'
    #APIkey = 'M4hajYKMhoXrFnXsg84yMm+FeeSU8Jjoi3JMZVOiMysXXpPvnBv+pxNTss++zrobMxGSc8lcN8ib4elemPPBHw=='
    url = 'https://rest-ww.telesign.com/v1/messaging'
    #phoneNumber = '919023051078'
    #message = 'Hi I want to order pepperoni pizza'
    messagetype = 'ARN'

    data = {
        'phone_number': receiver_countryPhoneCode + receiver_phoneNum,
        'message': message,
        'message_type': messagetype
    }

    encode = (customerID + ':' + APIkey).encode('utf-8')
    b64encoded = base64.urlsafe_b64encode(encode).decode('ascii')
    header = {'Authorization': 'Basic ' + b64encoded}

    response = post(url, data=data, headers=header)
    jsonResponse = json.loads(response.content)
    print(jsonify(jsonResponse))
    '''
    json to be returned having
    Message-sent: yes
    Timestamp : dd-mm-yy hh:mm
    Error: only if there is some error otherwise empty
    
    In DB save:
    message
    from phone
    To phone
    Timestamp    
    '''
    return jsonify(jsonResponse)