Exemplo n.º 1
0
    def post(self):

        if 'image' not in request.files:
            return jsonify({'message': 'No image'}), 400

        image = request.files['image']

        image_type = request.args.get('imageType')
        transfer_id = request.args.get('transferId')

        original_filename = image.filename

        new_filename = generate_new_filename(original_filename, image_type)

        url = save_to_s3_from_image_object(image_object=image,
                                           new_filename=new_filename)

        uploaded_image = UploadedImage(filename=new_filename,
                                       image_type=image_type)

        if transfer_id:
            uploaded_image.credit_transfer_id = transfer_id

        db.session.add(uploaded_image)
        db.session.commit()

        response_object = {
            'data': {
                'uploaded_image':
                uploaded_image_schema.dump(uploaded_image).data
            }
        }

        return make_response(jsonify(response_object)), 201
def handle_kyc_documents(data=None,
                         document_country=None,
                         document_type=None,
                         kyc_details=None):
    for (key, value) in data.items():
        if set([key]).intersection(
                set([
                    'document_front_base64', 'document_back_base64',
                    'selfie_base64'
                ])) and value is not None:
            try:
                new_filename = generate_new_filename(
                    original_filename="{}-{}.jpg".format(
                        key, document_country),
                    file_type='jpg')
                save_to_s3_from_image_base64(image_base64=value,
                                             new_filename=new_filename)
                uploaded_document = UploadedResource(filename=new_filename,
                                                     reference=document_type,
                                                     user_filename=key)
                db.session.add(uploaded_document)
                # tie document to kyc application
                uploaded_document.kyc_application_id = kyc_details.id
            except Exception as e:
                print(e)
                sentry_sdk.capture_exception(e)
                pass
Exemplo n.º 3
0
def set_custom_attributes(attribute_dict, user):
    default_attributes = {}
    custom_attributes = user.custom_attributes or {
    }  # loads in any existing custom attributes
    for key in attribute_dict.keys():
        if key[0] != '_':
            if key in DEFAULT_ATTRIBUTES:
                default_attributes[key] = attribute_dict[key]
            elif key not in KOBO_META_ATTRIBUTES:
                custom_attributes[key] = {'value': attribute_dict[key]}

    attachments = attribute_dict.get('_attachments', [])

    for attachment in attachments:
        submitted_filename = attachment['filename'].split('/')[-1]
        for attribute in custom_attributes.keys():
            if submitted_filename == custom_attributes[attribute]['value']:
                type = 'custom_attribute_{}'.format(attribute)

                new_filename = generate_new_filename(submitted_filename, type,
                                                     'KOBO')

                uploaded_image = models.UploadedImage(filename=new_filename,
                                                      image_type=type)

                uploaded_image.user = user

                db.session.add(uploaded_image)

                db.session.flush()

                try:

                    if attribute == 'profile_picture':

                        t = threading.Thread(
                            target=save_photo_and_check_for_duplicate,
                            args=(attachment['download_url'], new_filename,
                                  uploaded_image.id))
                    else:
                        t = threading.Thread(target=save_to_s3_from_url,
                                             args=(attachment['download_url'],
                                                   new_filename))

                    t.daemon = True
                    t.start()

                except LoadFileException:
                    print("File has likely expired")
                    pass

                custom_attributes[attribute]['value'] = new_filename
                custom_attributes[attribute][
                    'uploaded_image_id'] = uploaded_image.id

                continue

    user.custom_attributes = custom_attributes

    return default_attributes, custom_attributes
Exemplo n.º 4
0
    def post(self):
        reference = None
        kyc_application_id = None

        if 'kyc_application_id' in request.form:
            kyc_application_id = request.form['kyc_application_id']

        if 'document' not in request.files:
            return make_response(jsonify({'message': 'No File'})), 400

        document = request.files['document']
        filename = document.filename

        if kyc_application_id is None:
            return make_response(jsonify({'message': 'You must append documents to a business profile'})), 400

        business_details = KycApplication.query.filter_by(id=kyc_application_id).first()

        if not business_details:
            return make_response(jsonify({'message': 'Cannot find kyc for id {}'.format(kyc_application_id)})), 404

        if business_details.organisation_id and AccessControl.has_suffient_role(g.user.roles, {'ADMIN': 'superadmin'}) is not True:
            return make_response(jsonify({'message': 'Must be a superadmin to edit admin org KYC object'})), 401

        if filename == '':
            return make_response(jsonify({'message': 'No File'})), 400

        if not allowed_file(filename):
            return make_response(jsonify({'message': 'Must be JPG, JPEG, PNG or PDF'})), 400

        file_type = filename.rsplit('.', 1)[1].lower()
        new_filename = generate_new_filename(filename, file_type)

        saved_document = UploadedResource.query.filter_by(filename=new_filename).first()

        if saved_document:
            return make_response(jsonify({'message': 'Document already exists'})), 400

        save_to_s3_from_document(document=document, new_filename=new_filename)

        if 'reference' in request.form:
            reference = request.form['reference']

        uploaded_document = UploadedResource(filename=new_filename, file_type=file_type,
                                             reference=reference, user_filename=filename)
        db.session.add(uploaded_document)

        # tie document to kyc application
        uploaded_document.kyc_application_id = business_details.id

        response_object = {
            'message': 'Document uploaded',
            'data': {'kyc_application': kyc_application_schema.dump(business_details).data}
        }

        return make_response(jsonify(response_object)), 201
Exemplo n.º 5
0
    def post(self):
        reference = None
        kyc_application_id = None

        if 'kyc_application_id' in request.form:
            kyc_application_id = request.form['kyc_application_id']

        if 'document' not in request.files:
            return make_response(jsonify({'message': 'No File'})), 400

        document = request.files['document']
        filename = document.filename

        if kyc_application_id is None:
            return make_response(jsonify({'message': 'You must append documents to a business profile'})), 400

        if filename == '':
            return make_response(jsonify({'message': 'No File'})), 400

        if not allowed_file(filename):
            return make_response(jsonify({'message': 'Must be JPG, JPEG, PNG or PDF'})), 400

        file_type = filename.rsplit('.', 1)[1].lower()
        new_filename = generate_new_filename(filename, file_type)

        saved_document = UploadedDocument.query.filter_by(filename=new_filename).first()

        if saved_document:
            return make_response(jsonify({'message': 'Document already exists'})), 400

        save_to_s3_from_document(document=document, new_filename=new_filename)

        if 'reference' in request.form:
            reference = request.form['reference']

        uploaded_document = UploadedDocument(filename=new_filename, file_type=file_type,
                                             reference=reference, user_filename=filename)
        db.session.add(uploaded_document)

        business_details = KycApplication.query.filter_by(id=kyc_application_id).first()

        # tie document to kyc application
        uploaded_document.kyc_application_id = business_details.id

        db.session.commit()

        response_object = {
            'message': 'Document uploaded',
            'data': {'kyc_application': kyc_application_schema.dump(business_details).data}
        }

        return make_response(jsonify(response_object)), 201
Exemplo n.º 6
0
def set_attachments(attribute_dict, user, custom_attributes):
    attachments = attribute_dict.get('_attachments', [])

    for attachment in attachments:
        submitted_filename = attachment['filename'].split('/')[-1]
        for attribute in custom_attributes:
            if submitted_filename == attribute.value:
                type = 'custom_attribute_{}'.format(attribute)

                new_filename = generate_new_filename(submitted_filename, type,
                                                     'KOBO')

                uploaded_image = UploadedResource(filename=new_filename,
                                                  file_type=type)

                uploaded_image.user = user

                db.session.add(uploaded_image)

                db.session.flush()

                try:

                    if attribute == 'profile_picture':

                        t = threading.Thread(
                            target=save_photo_and_check_for_duplicate,
                            args=(attachment['download_url'], new_filename,
                                  uploaded_image.id))
                    else:
                        t = threading.Thread(target=save_to_s3_from_url,
                                             args=(attachment['download_url'],
                                                   new_filename))

                    t.daemon = True
                    t.start()

                except LoadFileException:
                    print("File has likely expired")
                    pass

                attribute.value = new_filename
                attribute.uploaded_image_id = uploaded_image.id
                continue

    return custom_attributes