Ejemplo n.º 1
0
def app_create():
    if request.method == 'GET':
        user_apps = UserApp.query.filter_by(email=current_user.email).all()
        return serialize(user_apps)
    else:
        name = request.json['name']
        new_app = Application(name)

        user_app = UserApp(current_user.email, new_app.app_key, "w")
        db.session.add(new_app)
        db.session.add(user_app)
        db.session.commit()
        return serialize(user_app)
Ejemplo n.º 2
0
def update_build_notes(app_key, build_id):
    build = Build.query.filter_by(app_key=app_key, id=build_id).first()
    if build:
        build.release_notes = request.json['release_notes']
        db.session.commit()
        return serialize(build)
    return make_response('{"error":"build_not_found"}', 404)
Ejemplo n.º 3
0
def revoke_team_membership():
    app_key = request.json['app_key']
    email = request.json['email']
    user_app = UserApp.query.filter_by(app_key=app_key, email=email).first()
    db.session.delete(user_app)
    db.session.commit()
    return serialize(user_app, PermissionSerializer)
Ejemplo n.º 4
0
def update_build_notes(app_key, build_id):
    build = Build.query.filter_by(app_key=app_key, id=build_id).first()
    if build:
        build.release_notes = request.json['release_notes']
        db.session.commit()
        return serialize(build)
    return make_response('{"error":"build_not_found"}', 404)
Ejemplo n.º 5
0
def upload(app_key):
    release_notes = 'empty'
    if 'releaseNotes' in request.form:
        release_notes = request.form['releaseNotes']
    apk_file = request.files['file']
    if apk_file:
        apk_filename = secure_filename(apk_file.filename)
        apk_file_path = os.path.join(app.config["TMP_DIR"], apk_filename)
        apk_file.save(apk_file_path)

        result = parse_apk(apk_file_path, app_key)

        build = result["build"]
        icon_path = result["icon_path"]
        build.release_notes = release_notes
        db.session.add(build)
        db.session.commit()

        storage_worker.put(build, apk_file_path, icon_path)

        application = Application.query.filter_by(app_key=app_key).first()
        application.icon_url = storage_worker.get_icon_link(app_key)
        db.session.commit()
        return serialize(build)
    return make_response('{"error":"upload_error"}', 400)
Ejemplo n.º 6
0
def add_user_to_team():
    app_key = request.json["app_key"]
    email = None
    if "email" in request.json:
        email = request.json["email"].lower()
    else:
        return make_response('{"error":"invalid_email"}', 403)
    permission = request.json["permission"]

    email_match = re.match(r"\w[\w\.-]*@\w[\w\.-]+\.\w+", email)
    if len(email) > 25:
        return make_response('{"error":"invalid_email_length"}', 403)
    elif not email_match:
        return make_response('{"error":"invalid_email_format"}', 403)

    user = User.query.filter_by(email=email).first()
    if user:
        user_app = UserApp.query.filter_by(app_key=app_key, user_id=user.id).first()
        if user_app:
            return make_response('{"error":"user_with_current_email_already_exist"}', 409)
    else:
        user = User(email)
        db.session.add(user)
        db.session.commit()
    user_app = UserApp(user.id, app_key, permission)
    user_app.user = user
    db.session.add(user_app)
    db.session.commit()
    return serialize(user_app, PermissionSerializer)
Ejemplo n.º 7
0
def add_user_to_team():
    app_key = request.json['app_key']
    email = None
    if 'email' in request.json:
        email = request.json['email'].lower()
    else:
        return make_response('{"error":"invalid_email"}', 403)
    permission = request.json['permission']

    email_match = re.match(r'\w[\w\.-]*@\w[\w\.-]+\.\w+', email)
    if len(email) > 25:
        return make_response('{"error":"invalid_email_length"}', 403)
    elif not email_match:
        return make_response('{"error":"invalid_email_format"}', 403)

    user = User.query.filter_by(email=email).first()
    if user:
        user_app = UserApp.query.filter_by(app_key=app_key, user_id=user.id).first()
        if user_app:
            return make_response('{"error":"user_with_current_email_already_exist"}', 409)
    else:
        user = User(email)
        db.session.add(user)
        db.session.commit()
    user_app = UserApp(user.id, app_key, permission)
    user_app.user = user
    db.session.add(user_app)
    db.session.commit()
    return serialize(user_app, PermissionSerializer)
Ejemplo n.º 8
0
def upload(app_key):
    #TODO: !!! method should be totally refactored
    application = Application.query.filter_by(app_key=app_key).first()

    if application:
        build_file = request.files['file']
        if build_file:
            filename = secure_filename(build_file.filename)
            file_path = os.path.join(app.config["TMP_DIR"], filename)
            build_file.save(file_path)
        else:
            return make_response('{"error":"build_file_absent"}', 406)

        try:
            app_type = get_app_type(filename)
        except TypeError:
            return make_response('{"error":"wrong_file_extension"}', 406)

        if not application.app_type:
            application.app_type = app_type
        elif application.app_type != app_type:
            return make_response('{"error":"wrong_app_type"}', 406)

        try:
            if app_type == ANDR:
                result = parse_apk(file_path)
            elif app_type == IOS:
                result = parse_ipa(file_path)
        except Exception as e:
            # TODO: make correct exception handling and logging
            print e
            return make_response('{"error":"invalid_file_format"}', 406)

        if 'releaseNotes' in request.form:
            release_notes = request.form['releaseNotes']
        else:
            release_notes = 'empty notes'

        package = result["package"]
        version_code = result['version_code']
        version_name = result['version_name']
        tmp_icon = result['tmp_icon']

        build = Build(app_key, version_code, version_name, release_notes)

        if not application.package:
            application.package = package
        elif application.package != package:
            return make_response('{"error":"wrong_package"}', 406)

        db.session.add(build)
        db.session.commit()

        storage_worker.put(build, file_path, tmp_icon)

        return serialize(build)
    else:
        return make_response('{"error":"app_not_found"}', 406)

    return make_response('{"error":"upload_error"}', 400)
Ejemplo n.º 9
0
def login():
    email = request.json['email'].lower()
    password = request.json['password']
    u = User.query.filter(User.email == email).first()
    if u is not None:
        if u.verify_password(password):
            return serialize(u.token)

    return Response('{"error":"wrong_credentials"}', 403)
Ejemplo n.º 10
0
def update_user():
    app_key = request.json['app_key']
    email = request.json['email']
    permission = request.json['permission']
    user_app = UserApp.query.filter_by(app_key=app_key, email=email).first()
    if user_app is None:
        return make_response('{"error":"user_app_not_found}', 404)
    user_app.permission = permission
    db.session.commit()
    return serialize(user_app, PermissionSerializer)
Ejemplo n.º 11
0
def login():
    """
    User login
    ---
    tags:
        - auth
    parameters:
        - name: email
          in: formData
          type: string
          required: true
        - name: password
          in: formData
          type: string
          required: true
    responses:
        200:
            description: Login successful
            schema:
                type: object
                required:
                - email
                - password
                properties:
                    email:
                        type: string
                    password:
                        type: string
            examples:
                application/json:
                    authToken: 5addfaee6d90df1a979119dd34332597
        401:
            description: Wrong credentials
            schema:
                type: object
                required:
                - error
                properties:
                    error:
                        type: string
            examples:
                application/json:
                    error: user_not_authorized
    """
    request_params = request.json
    if request_params is None:
        raise BadRequest()

    email = request_params.get('email')
    password = request_params.get('password')
    try:
        token = UserService(db).authenticate(email, password)
        return serialize(token)
    except UserNotAuthorized as e:
        raise AnarchoApiException(e, 401)
Ejemplo n.º 12
0
def upload(app_key):
    application = Application.query.filter_by(app_key=app_key).first()

    if not application:
        return make_response('{"error":"app_not_found"}', 406)
    else:
        build_file = request.files['file']
        if build_file:
            filename = secure_filename(build_file.filename)
            file_path = os.path.join(app.config["TMP_DIR"], filename)
            build_file.save(file_path)
        else:
            return make_response('{"error":"build_file_absent"}', 406)

        try:
            app_type = get_app_type(filename)
        except TypeError:
            return make_response('{"error":"wrong_file_extension"}', 406)

        if not application.app_type:
            application.app_type = app_type
        elif application.app_type != app_type:
            return make_response('{"error":"wrong_app_type"}', 406)

        try:
            if app_type == ANDR:
                result = parse_apk(file_path, app_key)
            elif app_type == IOS:
                result = parse_ipa(file_path, app_key)
        except Exception:
            return make_response('{"error":"invalid_file_format"}', 406)

        package = result["package"]
        icon_path = result["icon_path"]
        build = result["build"]

        if not application.package:
            application.package = package
        elif application.package != package:
            return make_response('{"error":"wrong_package"}', 406)

        if 'releaseNotes' in request.form:
            release_notes = request.form['releaseNotes']
        else:
            release_notes = 'empty notes'
        build.release_notes = release_notes

        db.session.add(build)
        db.session.commit()

        storage_worker.put(build, file_path, icon_path)

        return serialize(build)

    return make_response('{"error":"upload_error"}', 400)
Ejemplo n.º 13
0
def remove_permission(user_app):
    email = request.json["email"]
    if g.user.email == email:
        return make_response('{"error":"user_can_not_delete_himself"}', 403)
    elif user_app:
        result = serialize(user_app, PermissionSerializer)
        db.session.delete(user_app)
        db.session.commit()
        return result
    else:
        return make_response('{"error":"user_app_not_found}', 404)
Ejemplo n.º 14
0
def remove_permission(user_app):
    email = request.json['email']
    if g.user.email == email:
        return make_response('{"error":"user_can_not_delete_himself"}', 403)
    elif user_app:
        result = serialize(user_app, PermissionSerializer)
        db.session.delete(user_app)
        db.session.commit()
        return result
    else:
        return make_response('{"error":"user_app_not_found}', 404)
Ejemplo n.º 15
0
def register():
    if 'name' in request.json:
        name = request.json['name']
    else:
        return make_response('{"error":"invalid_user_name"}', 403)

    if name.isspace() or len(name) < 1:
        return make_response('{"error":"user_name_is_empty"}', 403)
    elif len(name) > 20:
        return make_response('{"error":"invalid_user_name_length"}', 403)

    if 'email' in request.json:
        email = request.json['email'].lower()
    else:
        return make_response('{"error":"invalid_email"}', 403)

    email_match = re.match(r'\w[\w\.-]*@\w[\w\.-]+\.\w+', email)

    if email.isspace() or len(email) < 1:
        return make_response('{"error":"email_is_empty"}', 403)
    elif not email_match:
        return make_response('{"error":"invalid_email_format"}', 403)
    elif len(email) > 25:
        return make_response('{"error":"invalid_email_length"}', 403)

    if 'password' in request.json:
        password = request.json['password']
    else:
        return make_response('{"error":"invalid_password"}', 403)

    if password.isspace():
        return make_response('{"error":"empty_password"}', 403)
    elif len(password) < 6:
        return make_response('{"error":"invalid_password_length"}', 403)

    u = User.query.filter(User.email == email).first()
    if not u or not u.name:
        user = None
        if not u:
            user = User(email, name, password)
            db.session.add(user)
            db.session.commit()
        else:
            user = u
            user.name = name
            user.hash_password(password)
            db.session.commit()

        token = Token(user)
        db.session.add(token)
        db.session.commit()
        return serialize(token)

    return Response('{"error":"user_already_registered"}', 409)
Ejemplo n.º 16
0
def add_user():
    app_key = request.json['app_key']
    email = request.json['email']
    permission = request.json['permission']

    user = User.query.filter_by(email=email).first()
    if user is None:
        user = User(email)
    user_app = UserApp(email, app_key, permission)
    user_app.user = user
    db.session.add(user_app)
    db.session.commit()
    return serialize(user_app, PermissionSerializer)
Ejemplo n.º 17
0
def update_permission(user_app):
    email = request.json['email'].lower()
    permission = request.json['permission']
    if g.user.email.lower() == email:
        return make_response('{"error":"user_can_not_change_permission"}', 403)
    elif not is_permission_allowed(permission):
        result = make_response('{"error":"wrong_permission}', 400)
    elif user_app:
        user_app.permission = permission
        db.session.commit()
        result = serialize(user_app, PermissionSerializer)
    else:
        result = make_response('{"error":"user_app_not_found}', 404)
    return result
Ejemplo n.º 18
0
def update_permission(user_app):
    email = request.json["email"].lower()
    permission = request.json["permission"]
    if g.user.email.lower() == email:
        return make_response('{"error":"user_can_not_change_permission"}', 403)
    elif not is_permission_allowed(permission):
        result = make_response('{"error":"wrong_permission}', 400)
    elif user_app:
        user_app.permission = permission
        db.session.commit()
        result = serialize(user_app, PermissionSerializer)
    else:
        result = make_response('{"error":"user_app_not_found}', 404)
    return result
Ejemplo n.º 19
0
def app_create():
    name = request.json["name"]
    new_app = Application(name)

    user_app = UserApp(g.user.id, new_app.app_key, "w")
    db.session.add(new_app)
    db.session.add(user_app)
    db.session.commit()

    api_user = User(name="guest_{0}".format(name))
    db.session.add(api_user)
    db.session.commit()

    api_user_token = Token(api_user)
    api_user_app = UserApp(api_user.id, new_app.app_key, "u")
    db.session.add(api_user_app)
    db.session.add(api_user_token)
    db.session.commit()
    return serialize(user_app)
Ejemplo n.º 20
0
def app_create():
    name = request.json['name']
    new_app = Application(name)

    user_app = UserApp(g.user.id, new_app.app_key, "w")
    db.session.add(new_app)
    db.session.add(user_app)
    db.session.commit()

    api_user = User(name='guest_{0}'.format(name))
    db.session.add(api_user)
    db.session.commit()

    api_user_token = Token(api_user)
    api_user_app = UserApp(api_user.id, new_app.app_key, "u")
    db.session.add(api_user_app)
    db.session.add(api_user_token)
    db.session.commit()
    return serialize(user_app)
Ejemplo n.º 21
0
def user():
    """
    Get user info
    ---
    tags:
        - auth
    parameters:
        - name: x-auth-token
          in: header
          type: string
          required: true
          default: d8058758acbddce3cfa4308bbfe8a7b9
    responses:
        200:
            description: User info
            schema:
                type: object
                required:
                - id
                - name
                properties:
                    id:
                        type: int
                    name:
                        type: string
            examples:
                application/json:
                    id: 1
                    name: boonya
        401:
            description: Unauthorized user
            schema:
                type: object
                required:
                - error
                properties:
                    error:
                        type: string
            examples:
                application/json:
                    error: user_not_authorized
    """
    return serialize(g.user)
Ejemplo n.º 22
0
def app_info(app_key):
    application = UserApp.query.filter_by(app_key=app_key, email=current_user.email).first()
    if application:
        return serialize(application)
    return make_response('{"error":"app_not_found"}', 404)
Ejemplo n.º 23
0
def app_info(app_key):
    application = UserApp.query.filter_by(app_key=app_key, user_id=g.user.id).first()
    if application:
        application.icon_url = storage_worker.get_icon_link(app_key)
        return serialize(application)
    return make_response('{"error":"app_not_found"}', 404)
Ejemplo n.º 24
0
def apps_list():
    user_apps = UserApp.query.filter_by(user_id=g.user.id).all()
    return serialize(user_apps)
Ejemplo n.º 25
0
def app_info(app_key):
    application = UserApp.query.filter_by(app_key=app_key, user_id=g.user.id).first()
    if application:
        application.icon_url = storage_worker.get_icon_link(app_key)
        return serialize(application)
    return make_response('{"error":"app_not_found"}', 404)
Ejemplo n.º 26
0
def apps_list():
    user_apps = UserApp.query.filter_by(user_id=g.user.id).all()
    return serialize(user_apps)
Ejemplo n.º 27
0
def user():
    return serialize(g.user)
Ejemplo n.º 28
0
def builds_list(app_key):
    builds = Build.query.filter_by(app_key=app_key).all()
    return serialize(builds)
Ejemplo n.º 29
0
def users_list(app_key=None):
    user_apps = UserApp.query.filter(UserApp.app_key == app_key, UserApp.permission != "u").all()
    return serialize(user_apps, serializer=PermissionSerializer)
Ejemplo n.º 30
0
def users_list(app_key=None):
    user_apps = UserApp.query.filter_by(app_key=app_key).all()
    return serialize(user_apps, serializer=PermissionSerializer)
Ejemplo n.º 31
0
def users_list(app_key=None):
    user_apps = UserApp.query.filter(UserApp.app_key == app_key, UserApp.permission != 'u').all()
    return serialize(user_apps, serializer=PermissionSerializer)
Ejemplo n.º 32
0
def builds_list(app_key):
    builds = Build.query.filter_by(app_key=app_key).order_by(desc(Build.created_on)).all()
    return serialize(builds)
Ejemplo n.º 33
0
def register():
    """
    Register user
    ---
    tags:
        - auth
    parameters:
        - name: name
          in: formData
          type: string
          required: true
        - name: email
          in: formData
          type: string
          required: true
        - name: password
          in: formData
          type: string
          required: true
    responses:
        200:
            description: User registered successfully
            schema:
                type: object
                required:
                - authToken
                properties:
                    authToken:
                        type: string
            examples:
                application/json:
                    authToken: d8058758acbddce3cfa4308bbfe8a7b9
        409:
            description: User already registered
            schema:
                type: object
                required:
                - error
                properties:
                    error:
                        type: string
            examples:
                application/json:
                    - error:user_already_exist
        400:
            description: Data validation errors
            schema:
                type: object
                required:
                - error
                properties:
                    error:
                        type: string
            examples:
                application/json:
                    - error:username_length_is_wrong
                    - error:email_format_is_wrong
                    - error:password_is_empty
                    - error:password_is_too_short
    """
    request_params = request.json
    if not request_params:
        raise BadRequest()

    name = request_params.get('name')
    email = request_params.get('email')
    password = request_params.get('password')
    try:
        user_service = UserService(db)
        user_service.create_user(name, email, password)
        token = user_service.authenticate(email, password)
        return serialize(token)
    except UserAlreadyExist as e:
        raise AnarchoApiException(e, 409)
    except UserServiceException as e:
        raise AnarchoApiException(e)
Ejemplo n.º 34
0
def builds_list(app_key):
    builds = Build.query.filter_by(app_key=app_key).order_by(
        desc(Build.created_on)).all()
    return serialize(builds)