Exemplo n.º 1
0
def get(username_or_id, albumslug):
    """
    Get album details.
    ---
    tags:
        - Albums
    parameters:
        - name: user_id
          in: path
          type: integer
          required: true
          description: User ID
        - name: albumslug
          in: path
          type: string
          required: true
          description: Album slug
    responses:
        200:
            description: Returns album object.
    """
    # Get logged in user from bearer token, or None if not logged in
    if current_token:
        current_user = current_token.user
    else:
        current_user = None

    # Get the associated User from url fetch
    if username_or_id.isdigit():
        album_user = User.query.filter(User.id == username_or_id).first()
    else:
        album_user = User.query.filter(User.name == username_or_id, User.local.is_(True)).first()
    if not album_user:
        return jsonify({"error": "User not found"}), 404

    if current_user and album_user.id == current_user.id:
        # we have a valid token, and album user is token user, can fetch private
        album = Album.query.filter(Album.slug == albumslug, Album.user_id == album_user.id).first()
    else:
        # only fetch public ones
        album = Album.query.filter(
            Album.slug == albumslug, Album.user_id == album_user.id, Album.private.is_(False)
        ).first()

    if not album:
        return jsonify({"error": "not found"}), 404

    if album.private:
        if current_user:
            if album.user_id != current_user.id:
                return jsonify({"error": "forbidden"}), 403
        else:
            return jsonify({"error": "forbidden"}), 403

    relationship = to_json_relationship(current_user, album.user)
    account = to_json_account(album.user, relationship)
    resp = to_json_album(album, account)

    return jsonify(resp)
Exemplo n.º 2
0
def albums():
    """
    User albums timeline.
    ---
    tags:
        - Timelines
    parameters:
        - name: count
          in: query
          type: integer
          required: true
          description: count
        - name: page
          in: query
          type: integer
          description: page number
        - name: user
          in: query
          type: string
          description: the user flake id to get albums list
    responses:
        200:
            description: Returns array of Status
    """
    count = int(request.args.get("count", 20))
    page = int(request.args.get("page", 1))
    user = request.args.get("user", None)
    if not user:
        abort(400)

    user = User.query.filter(User.flake_id == user).first()
    if not user:
        return jsonify({"error": "User does not exist"}), 404

    q = Album.query.order_by(Album.created.desc())

    only_public = True
    if current_token and current_token.user:
        if user.id == current_token.user.id:
            only_public = False

    if only_public:
        q = q.filter(Album.user_id == user.id, Album.private.is_(False))
    else:
        q = q.filter(Album.user_id == current_token.user.id)

    q = q.paginate(page=page, per_page=count)

    albums = []
    for t in q.items:
        relationship = False
        if current_token and current_token.user:
            relationship = to_json_relationship(current_token.user, t.user)
        account = to_json_account(t.user, relationship)
        albums.append(to_json_album(t, account))
    resp = {"page": page, "page_size": count, "totalItems": q.total, "items": albums, "totalPages": q.pages}
    return jsonify(resp)
Exemplo n.º 3
0
def reorder(username, albumslug):
    """
    Edit album tracks order.
    ---
    tags:
        - Albums
    security:
        - OAuth2:
            - write
    responses:
        200:
            description: Returns a Status with extra reel2bits params.
    """
    current_user = current_token.user
    if not current_user:
        return jsonify({"error": "Unauthorized"}), 403

    # Get the album
    album = Album.query.filter(Album.user_id == current_user.id,
                               Album.slug == albumslug).first()
    if not album:
        return jsonify({"error": "Not found"}), 404

    pos = 0
    for track in request.json:
        dbt = Sound.query.filter(Sound.flake_id == track["id"],
                                 Sound.album_id == album.id).first()
        if not dbt:
            return jsonify({"error": "Not found"}), 404
        dbt.album_order = pos
        pos += 1
    db.session.commit()

    relationship = to_json_relationship(current_user, album.user)
    account = to_json_account(album.user, relationship)
    resp = to_json_album(album, account)

    return jsonify(resp)
Exemplo n.º 4
0
def edit(username, albumslug):
    """
    Edit album.
    ---
    tags:
        - Albums
    security:
        - OAuth2:
            - write
    parameters:
        - name: username
          in: path
          type: string
          required: true
          description: User username
        - name: albumslug
          in: path
          type: string
          required: true
          description: Album slug
    responses:
        200:
            description: Returns a Status with extra reel2bits params.
    """
    current_user = current_token.user
    if not current_user:
        return jsonify({"error": "Unauthorized"}), 403

    # Get the album
    album = Album.query.filter(Album.user_id == current_user.id,
                               Album.slug == albumslug).first()
    if not album:
        return jsonify({"error": "Not found"}), 404

    description = request.json.get("description")
    private = request.json.get("private")
    title = request.json.get("title")
    genre = request.json.get("genre")
    tags = request.json.get("tags")

    if album.private and not private:
        return jsonify(
            {"error": "Cannot change to private: album already federated"})

    if not title:
        return jsonify({"error": "Album title is required"}), 400

    album.title = title
    album.description = description
    album.genre = genre

    # First remove tags which have been removed
    for tag in album.tags:
        if tag.name not in tags:
            album.tags.remove(tag)

    # Then add the new ones if new
    for tag in tags:
        if tag not in [a.name for a in album.tags]:
            dbt = SoundTag.query.filter(SoundTag.name == tag).first()
            if not dbt:
                dbt = SoundTag(name=tag)
                db.session.add(dbt)
            album.tags.append(dbt)

    # Purge orphaned tags
    for otag in SoundTag.query.filter(
            and_(~SoundTag.sounds.any(), ~SoundTag.albums.any())).all():
        db.session.delete(otag)

    db.session.commit()

    relationship = False
    if current_token and current_token.user:
        relationship = to_json_relationship(current_token.user, album.user)
    account = to_json_account(album.user, relationship)
    return jsonify(to_json_album(album, account))