Exemplo n.º 1
0
def reset_latest_import_timestamp():
    form = FlaskForm()
    if form.validate_on_submit():
        try:
            listens_importer.update_latest_listened_at(
                current_user.id, ExternalServiceType.LASTFM, 0)
            flash.info(
                "Latest import time reset, we'll now import all your data instead of stopping at your last imported listen."
            )
        except DatabaseException:
            flash.error(
                "Something went wrong! Unable to reset latest import timestamp right now."
            )
        return redirect(url_for("profile.info"))

    if form.csrf_token.errors:
        flash.error(
            'Cannot reset import time due to error during authentication, please try again later.'
        )
        return redirect(url_for('profile.info'))

    return render_template(
        "profile/resetlatestimportts.html",
        form=form,
    )
Exemplo n.º 2
0
def delete_listens():
    """ Delete all the listens for the currently logged-in user from ListenBrainz.

    If POST request, this view checks for the correct authorization token and
    deletes the listens. If deletion is successful, redirects to user's profile page,
    else flashes an error and redirects to user's info page.

    If GET request, this view renders a page asking the user to confirm that they
    wish to delete their listens.
    """
    if request.method == 'POST':
        if request.form.get('token') and (request.form.get('token') == current_user.auth_token):
            try:
                delete_listens_history(current_user.musicbrainz_id)
            except Exception as e:
                current_app.logger.error('Error while deleting listens for %s: %s', current_user.musicbrainz_id, str(e))
                flash.error('Error while deleting listens for %s, please try again later.' % current_user.musicbrainz_id)
                return redirect(url_for('profile.info'))
            flash.info('Successfully deleted listens for %s.' % current_user.musicbrainz_id)
            return redirect(url_for('user.profile', user_name=current_user.musicbrainz_id))
        else:
            raise Unauthorized("Auth token invalid or missing.")
    else:
        return render_template(
            'profile/delete_listens.html',
            user=current_user,
        )
Exemplo n.º 3
0
def delete_listens():
    """ Delete all the listens for the currently logged-in user from ListenBrainz.

    If POST request, this view checks for the correct authorization token and
    deletes the listens. If deletion is successful, redirects to user's profile page,
    else flashes an error and redirects to user's info page.

    If GET request, this view renders a page asking the user to confirm that they
    wish to delete their listens.
    """
    form = FlaskForm()
    if form.validate_on_submit():
        try:
            delete_listens_history(current_user.musicbrainz_id)
            flash.info('Successfully deleted listens for %s.' %
                       current_user.musicbrainz_id)
            return redirect(
                url_for('user.profile', user_name=current_user.musicbrainz_id))
        except Exception as e:
            current_app.logger.error('Error while deleting listens for %s: %s',
                                     current_user.musicbrainz_id, str(e))
            flash.error(
                'Error while deleting listens for %s, please try again later.'
                % current_user.musicbrainz_id)
            return redirect(url_for('profile.info'))

    if form.csrf_token.errors:
        flash.error(
            'Cannot delete listens due to error during authentication, please try again later.'
        )
        return redirect(url_for('profile.info'))

    return render_template('profile/delete_listens.html',
                           user=current_user,
                           form=form)
Exemplo n.º 4
0
def delete():
    """ Delete currently logged-in user from ListenBrainz.

    If POST request, this view checks for the correct authorization token and
    deletes the user. If deletion is successful, redirects to home page, else
    flashes an error and redirects to user's info page.

    If GET request, this view renders a page asking the user to confirm
    that they wish to delete their ListenBrainz account.
    """
    form = FlaskForm()
    if form.validate_on_submit():
        try:
            delete_user(current_user.musicbrainz_id)
            flash.success("Successfully deleted account for %s." %
                          current_user.musicbrainz_id)
            return redirect(url_for('index.index'))
        except Exception:
            current_app.logger.error('Error while deleting user: %s',
                                     current_user.musicbrainz_id,
                                     exc_info=True)
            flash.error(
                'Error while deleting user %s, please try again later.' %
                current_user.musicbrainz_id)
            return redirect(url_for('profile.info'))

    if form.csrf_token.errors:
        flash.error(
            'Cannot delete user due to error during authentication, please try again later.'
        )
        return redirect(url_for('profile.info'))

    return render_template('profile/delete.html', user=current_user, form=form)
Exemplo n.º 5
0
def reset_latest_import_timestamp():
    if request.method == "POST":
        token = request.form.get("token")
        if token != current_user.auth_token:
            raise BadRequest(
                "Can only reset latest import timestamp of currently logged in user"
            )
        reset = request.form.get("reset")
        if reset == "yes":
            try:
                db_user.reset_latest_import(current_user.musicbrainz_id)
                flash.info(
                    "Latest import time reset, we'll now import all your data instead of stopping at your last imported listen."
                )
            except DatabaseException:
                flash.error(
                    "Something went wrong! Unable to reset latest import timestamp right now."
                )
        return redirect(url_for("profile.info"))
    else:
        token = current_user.auth_token
        return render_template(
            "profile/resetlatestimportts.html",
            token=token,
        )
Exemplo n.º 6
0
def delete():
    """ Delete currently logged-in user from ListenBrainz.

    If POST request, this view checks for the correct authorization token and
    deletes the user. If deletion is successful, redirects to home page, else
    flashes an error and redirects to user's info page.

    If GET request, this view renders a page asking the user to confirm
    that they wish to delete their ListenBrainz account.
    """
    if request.method == 'POST':
        if request.form.get('token') == current_user.auth_token:
            try:
                delete_user(current_user.musicbrainz_id)
            except Exception as e:
                current_app.logger.error('Error while deleting %s: %s', current_user.musicbrainz_id, str(e))
                flash.error('Error while deleting user %s, please try again later.' % current_user.musicbrainz_id)
                return redirect(url_for('profile.info'))
            return redirect(url_for('index.index'))
        else:
            flash.error('Cannot delete user due to error during authentication, please try again later.')
            return redirect('profile.info')
    else:
        return render_template(
            'profile/delete.html',
            user=current_user,
        )
Exemplo n.º 7
0
def delete():
    """ Delete currently logged-in user from ListenBrainz.

    If POST request, this view checks for the correct authorization token and
    deletes the user. If deletion is successful, redirects to home page, else
    flashes an error and redirects to user's info page.

    If GET request, this view renders a page asking the user to confirm
    that they wish to delete their ListenBrainz account.
    """
    if request.method == 'POST':
        if request.form.get('token') == current_user.auth_token:
            try:
                delete_user(current_user.musicbrainz_id)
            except Exception as e:
                current_app.logger.error('Error while deleting %s: %s',
                                         current_user.musicbrainz_id, str(e))
                flash.error(
                    'Error while deleting user %s, please try again later.' %
                    current_user.musicbrainz_id)
                return redirect(url_for('profile.info'))
            return redirect(url_for('index.index'))
        else:
            flash.error(
                'Cannot delete user due to error during authentication, please try again later.'
            )
            return redirect('profile.info')
    else:
        return render_template(
            'profile/delete.html',
            user=current_user,
        )
def musicbrainz_post():
    """Callback endpoint."""
    if provider.validate_post_login():
        user = provider.get_user()
        db_user.update_last_login(user.musicbrainz_id)
        login_user(user, remember=True, duration=datetime.timedelta(current_app.config['SESSION_REMEMBER_ME_DURATION']))
        next = session.get('next')
        if next:
            return redirect(next)
    else:
        flash.error("Login failed.")
    return redirect(url_for('index.index'))
Exemplo n.º 9
0
def musicbrainz_post():
    """Callback endpoint."""
    if provider.validate_post_login():
        user = provider.get_user()
        db_user.update_last_login(user.musicbrainz_id)
        login_user(user)
        next = session.get('next')
        if next:
            return redirect(next)
    else:
        flash.error("Login failed.")
    return redirect(url_for('index.index'))
Exemplo n.º 10
0
def music_services_callback(service_name: str):
    service = _get_service_or_raise_404(service_name)
    code = request.args.get('code')
    if not code:
        raise BadRequest('missing code')
    token = service.fetch_access_token(code)
    if service.add_new_user(current_user.id, token):
        flash.success('Successfully authenticated with %s!' %
                      service_name.capitalize())
    else:
        flash.error('Unable to connect to %s! Please try again.' %
                    service_name.capitalize())
    return redirect(url_for('profile.music_services_details'))
Exemplo n.º 11
0
def gdpr_notice():
    if request.method == 'GET':
        return render_template('index/gdpr.html', next=request.args.get('next'))
    elif request.method == 'POST':
        if request.form.get('gdpr-options') == 'agree':
            try:
                db_user.agree_to_gdpr(current_user.musicbrainz_id)
            except DatabaseException as e:
                flash.error('Could not store agreement to GDPR terms')
            next = request.form.get('next')
            if next:
                return redirect(next)
            return redirect(url_for('index.index'))
        elif request.form.get('gdpr-options') == 'disagree':
            return redirect(url_for('profile.delete'))
        else:
            flash.error('You must agree to or decline our terms')
            return render_template('index/gdpr.html', next=request.args.get('next'))
Exemplo n.º 12
0
def reset_token():
    if request.method == "POST":
        token = request.form.get("token")
        if token != current_user.auth_token:
            raise BadRequest("Can only reset token of currently logged in user")
        reset = request.form.get("reset")
        if reset == "yes":
            try:
                db_user.update_token(current_user.id)
                flash.info("Access token reset")
            except DatabaseException:
                flash.error("Something went wrong! Unable to reset token right now.")
        return redirect(url_for("profile.info"))
    else:
        token = current_user.auth_token
        return render_template(
            "user/resettoken.html",
            token=token,
        )
Exemplo n.º 13
0
def reset_latest_import_timestamp():
    if request.method == "POST":
        token = request.form.get("token")
        if token != current_user.auth_token:
            raise BadRequest("Can only reset latest import timestamp of currently logged in user")
        reset = request.form.get("reset")
        if reset == "yes":
            try:
                db_user.reset_latest_import(current_user.musicbrainz_id)
                flash.info("Latest import time reset, we'll now import all your data instead of stopping at your last imported listen.")
            except DatabaseException:
                flash.error("Something went wrong! Unable to reset latest import timestamp right now.")
        return redirect(url_for("profile.info"))
    else:
        token = current_user.auth_token
        return render_template(
            "profile/resetlatestimportts.html",
            token=token,
        )
Exemplo n.º 14
0
def reset_token():
    if request.method == "POST":
        token = request.form.get("token")
        if token != current_user.auth_token:
            raise BadRequest("Can only reset token of currently logged in user")
        reset = request.form.get("reset")
        if reset == "yes":
            try:
                db_user.update_token(current_user.id)
                flash.info("Access token reset")
            except DatabaseException:
                flash.error("Something went wrong! Unable to reset token right now.")
        return redirect(url_for("profile.info"))
    else:
        token = current_user.auth_token
        return render_template(
            "user/resettoken.html",
            token=token,
        )
Exemplo n.º 15
0
def reset_token():
    form = FlaskForm()
    if form.validate_on_submit():
        try:
            db_user.update_token(current_user.id)
            flash.info("Access token reset")
        except DatabaseException:
            flash.error(
                "Something went wrong! Unable to reset token right now.")
        return redirect(url_for("profile.info"))

    if form.csrf_token.errors:
        flash.error(
            'Cannot reset token due to error during authentication, please try again later.'
        )
        return redirect(url_for('profile.info'))

    return render_template(
        "user/resettoken.html",
        form=form,
    )
Exemplo n.º 16
0
def musicbrainz_post():
    """Callback endpoint."""

    no_email_warning = Markup(
        'You have not provided an email address. Please provide an '
        '<a href="https://musicbrainz.org/account/edit">email address</a> ')
    blog_link = Markup(
        'Read this <a href="https://blog.metabrainz.org/?p=8915">blog post</a> '
        'to understand why we need your email.')

    if provider.validate_post_login():
        try:
            user = provider.get_user()
            if current_app.config[
                    "REJECT_NEW_USERS_WITHOUT_EMAIL"] and not user["email"]:
                # existing user without email, show a warning
                flash.warning(
                    no_email_warning +
                    'before 1 November 2021, or you will be unable to submit '
                    'listens. ' + blog_link)

            db_user.update_last_login(user["musicbrainz_id"])
            login_user(User.from_dbrow(user),
                       remember=True,
                       duration=datetime.timedelta(
                           current_app.config['SESSION_REMEMBER_ME_DURATION']))
            next = session.get('next')
            if next:
                return redirect(next)
        except MusicBrainzAuthSessionError:
            flash.error("Login failed.")
        except MusicBrainzAuthNoEmailError:
            # new user without email tried to create an account
            flash.error(no_email_warning +
                        'before creating a ListenBrainz account. ' + blog_link)
    else:
        flash.error("Login failed.")
    return redirect(url_for('index.index'))
Exemplo n.º 17
0
 def _handle_view(self, name, **kwargs):
     if not self.is_accessible():
         flash.error('You are not authorized to view the admin page.')
         return redirect(url_for('login.index'))