Ejemplo n.º 1
0
def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    # Setup storage
    with app.app_context():
        table_service = get_table_service()
        tables = ['user', 'submission', 'response']
        for table in tables:
            print('Setting up "' + table + '" table.')
            try:
                table_service.create_table('user')
            except Exception as ex:
                print(ex)

        blob_service_client = get_blob_service_client()
        containers = ['photos', 'artuploadphotos']
        for container in containers:
            print('Setting up "' + container + '" container.')
            try:
                container_client = blob_service_client.create_container(
                    container)
            except Exception as ex:
                print(ex)

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    @app.route('/', methods=['GET'])
    def hello():
        return "Hello!"

    from . import db
    db.init_app(app)

    from . import user
    app.register_blueprint(user.bp)

    from . import submission
    app.register_blueprint(submission.bp)

    from . import response
    app.register_blueprint(response.bp)

    CORS(app)
    return app
Ejemplo n.º 2
0
def text_others(userId, templateId):
    table_service = get_table_service()
    users = table_service.query_entities('user',
                                         filter="PartitionKey eq '" +
                                         templateId + "'")
    users = [convert_table_user_to_json(user) for user in users]
    othersUsers = [user for user in users if user['user_id'] != userId]
    user = next(user for user in users if user['user_id'] == userId)
    for otherUser in othersUsers:
        message = "New submission from " + user[
            'name'] + ".  Use your login link to check it out: " + build_login_link(
                otherUser['user_id'])
        send_text(otherUser['phone_number'], message)
Ejemplo n.º 3
0
def create_user():
    validate_request_form(['name', 'phone_number', 'template_id'])
    userToUpload = {
        'PartitionKey': request.form['template_id'],
        'RowKey': build_user_id(4),
        'name': request.form['name'],
        'phone_number': request.form['phone_number'],
    }
    table_service = get_table_service()
    table_service.insert_entity('user', userToUpload)

    message = "Hey, " + userToUpload['name'] + "! Thanks for getting going on Chris's caption competition.  If you need to finish uploading your photo or captioning later, use your personal login link: " + build_login_link(userToUpload["RowKey"])
    print("a.sending text to: " + userToUpload["phone_number"], file=sys.stderr)
    print("a.sending text to: " + userToUpload["phone_number"])
    send_text(userToUpload["phone_number"], message)
    return convert_table_user_to_json(userToUpload)
Ejemplo n.º 4
0
def get_submissions():
    validate_request_args(['template_id', 'user_id'])
    userId = request.args['user_id']
    templateId = request.args['template_id']
    validate_user_in_template(userId, templateId)

    table_service = get_table_service()
    submissions = table_service.query_entities('submission',
                                               filter="PartitionKey eq '" +
                                               templateId + "'")
    submissions = [
        convert_table_submission_to_json(submission)
        for submission in submissions
    ]

    responses = table_service.query_entities('response',
                                             filter="PartitionKey eq '" +
                                             templateId + "'")
    responses = [
        convert_table_response_to_json(response) for response in responses
    ]

    users = table_service.query_entities('user',
                                         filter="PartitionKey eq '" +
                                         templateId + "'")
    users = [convert_table_user_to_short_json(user) for user in users]

    for response in responses:
        # raises StopIteration.  Letting it because I don't expect this to happen
        user = next(user for user in users
                    if user['user_id'] == response['user_id'])
        response['user'] = user

    for submission in submissions:
        submissionResponses = [
            response for response in responses
            if response['submission_id'] == submission['submission_id']
        ]
        submission['responses'] = submissionResponses

        user = next(user for user in users
                    if user['user_id'] == submission['user_id'])
        submission['user'] = user
    return jsonify(submissions)
Ejemplo n.º 5
0
def upsert_response():
    validate_request_form(['template_id', 'user_id', 'submission_id', 'text'])
    userId = request.form['user_id']
    templateId = request.form['template_id']
    validate_user_in_template(userId, templateId)
    validate_submission_in_template()

    response_id = request.form[
        'response_id'] if 'response_id' in request.form else str(uuid.uuid1())

    response_to_upload = {
        'PartitionKey': templateId,
        'RowKey': response_id,
        'user_id': userId,
        'submission_id': request.form['submission_id'],
        'text': request.form['text']
    }
    table_service = get_table_service()
    table_service.insert_or_replace_entity('response', response_to_upload)

    return convert_table_response_to_json(response_to_upload)
Ejemplo n.º 6
0
def upsert_submission():
    validate_request_form(['template_id', 'user_id'])
    validate_request_form_photo()
    validate_user_in_template(request.form['user_id'],
                              request.form['template_id'])

    # get submission id and check if upsert
    upsert = False
    submission_id = request.form[
        'submission_id'] if 'submission_id' in request.form else str(
            uuid.uuid1())
    photo_url = upload_photo()

    submission_to_upload = {
        'PartitionKey': request.form['template_id'],
        'RowKey': submission_id,
        'user_id': request.form['user_id'],
        'photo_url': photo_url
    }
    table_service = get_table_service()
    table_service.insert_or_replace_entity('submission', submission_to_upload)
    text_others(request.form['user_id'], request.form['template_id'])

    return convert_table_submission_to_json(submission_to_upload)
Ejemplo n.º 7
0
def get_user_by_id():
    validate_request_args(['template_id', 'user_id'])
    table_service = get_table_service()
    user = table_service.get_entity('user', request.args['template_id'], request.args['user_id'])
    return convert_table_user_to_json(user)