Ejemplo n.º 1
0
def create():
    auth_header = request.headers.get('Authorization')

    if auth_header:
        token = auth_header.split(" ")[1]
    else:
        return jsonify([{
            'status': 'failed',
            'message': 'Not authorization header.'
        }])

    decoded = decode_auth_token(token)
    user = User.get(User.id == decoded)
    if user:

        if request.method == "GET":
            contacts = PersonalContact.select().where(
                PersonalContact.user_id == user.id)
            return jsonify({
                'personalContacts': [{
                    'id': contact.id,
                    'name': contact.name,
                    'relationship': contact.relationship,
                    'location': contact.location,
                    'priority': contact.priority,
                    'phone_number': contact.phone_number,
                    'email': contact.email
                } for contact in contacts]
            })

        elif request.method == "POST":
            req_data = request.get_json()
            req_data['name']
            req_data['email']
            contact = PersonalContact(name=req_data['name'],
                                      email=req_data['email'],
                                      user=user)

            if contact.save():
                return jsonify([{
                    'status': 'success',
                    'message': 'Successfully created a contact.',
                    'contact': {
                        'id': contact.id,
                        'name': contact.name,
                        'email': contact.email
                    }
                }])
            else:
                return jsonify([{
                    'status': 'failed',
                    'message': 'Unable to save contact.'
                }])
    else:
        return jsonify([{
            'status': 'failed',
            'message': 'Authentication failed.'
        }])
Ejemplo n.º 2
0
def create():
    req_data = request.get_json()
    first_name = req_data['firstName']
    last_name = req_data['lastName']
    email = req_data['email']
    date_of_birth = req_data['dateOfBirth']
    nationality = req_data['nationality']
    contact_name = req_data['contactName']
    contact_phone_number = req_data['contactPhone']
    hashed_password = generate_password_hash(req_data['password'])

    user = User(first_name=first_name,
                last_name=last_name,
                email=email,
                password=hashed_password,
                dob=date_of_birth,
                nationality=nationality)

    if user.save():
        token = encode_auth_token(user)
        contact = PersonalContact(user=user,
                                  name=contact_name,
                                  phone_number=contact_phone_number)
        if contact.save():
            return jsonify({
                'auth_token': token,
                'message': 'Successfully created the account. Please log in.',
                'status': 'success',
                'user': {
                    'id': user.id,
                    'first_name': user.first_name,
                    'last_name': user.last_name,
                    'email': user.email
                }
            })
        else:
            errors = contact.errors
            return jsonify({'status': 'failed', 'message': errors})
    elif user.errors:
        errors = user.errors
        return jsonify({'status': 'failed', 'message': errors})
Ejemplo n.º 3
0
def update(contact_id):
    auth_header = request.headers.get('Authorization')

    if auth_header:
        token = auth_header.split(" ")[1]
    else:
        return jsonify([{
            'status': 'failed',
            'message': 'Not authorization header.'
        }])

    decoded = decode_auth_token(token)
    user = User.get(User.id == decoded)
    if user:
        contact = PersonalContact.get(PersonalContact.id == contact_id,
                                      PersonalContact.user_id == user.id)

        req_data = request.get_json()
        contact.name = req_data['name'] or contact.name
        contact.relationship = req_data['relationship'] or contact.relationship
        # contact.location = req_data['location'] or contact.location
        # contact.priority = req_data['priority'] or contact.priority
        contact.email = req_data['email'] or contact.email
        contact.phone_number = req_data['phoneNumber'] or contact.phone_number

        if contact.save():
            return jsonify([{
                'status':
                'success',
                'message':
                'Successfully updated the contact details.'
            }])
        else:
            return jsonify([{
                'status': 'failed',
                'message': 'Unable to update contact.'
            }])
    else:
        return jsonify([{
            'status': 'failed',
            'message': 'Authentication failed.'
        }])
Ejemplo n.º 4
0
def message():
    auth_header = request.headers.get('Authorization')

    if auth_header:
        token = auth_header.split(" ")[1]
    else:
        return jsonify([{
            'status': 'failed',
            'message': 'Not authorization header.'
        }])

    decoded = decode_auth_token(token)
    user = User.get(User.id == decoded)

    contacts = PersonalContact.get(PersonalContact.user_id == user.id)
    phone_number = contacts.phone_number

    req_data = request.get_json()
    latitude = req_data['latitude']
    longitude = req_data['longitude']

    account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
    auth_token = os.environ.get('TWILIO_AUTH_TOKEN')

    client = Client(account_sid, auth_token)

    message = client.messages.create(
        to=phone_number,
        from_=os.environ.get('TWILIO_PHONE_NUMBER'),
        body=
        f"{contacts.name}, {user.first_name} {user.last_name} is in danger! Aunty very worried! Can you please help? Current latitude: {latitude}, longitude: {longitude}"
    )

    if message.sid:
        return jsonify({'status': 'success', 'id': message.sid})
    else:
        return jsonify({'status': 'failed', 'message': 'Will return ASAP.'})