def notify_error(musicbrainz_row_id, error):
    """ Notifies specified user via email about error during Spotify import.

    Args:
        musicbrainz_row_id (int): the MusicBrainz row ID of the user
        error (str): a description of the error encountered.
    """
    user_email = mb_editor.get_editor_by_id(musicbrainz_row_id)['email']
    spotify_url = current_app.config['SERVER_ROOT_URL'] + '/profile/connect-spotify'
    text = render_template('emails/spotify_import_error.txt', error=error, link=spotify_url)
    send_mail(
        subject='ListenBrainz Spotify Importer Error',
        text=text,
        recipients=[user_email],
        from_name='ListenBrainz',
        from_addr='noreply@'+current_app.config['MAIL_FROM_DOMAIN'],
    )
Пример #2
0
 def test_get_by_id(self, session):
     # Manually adding and deleting data in tests can get tedious. However, we have only two tests for which this is
     # needed. In case in future we need to add more tests where the test database needs to be modified, we should
     # explore other alternatives to ease the process.
     with session as db:
         # The editors table in test database has many empty columns and fields like last_login_date may change with
         # new dump.
         insert_editor_1 = Editor(**TestEditor.editor_1)
         db.add(insert_editor_1)
         db.commit()
         try:
             editor = mb_editor.get_editor_by_id(2323)
             assert editor == TestEditor.expected_editor_1
         finally:
             # regardless whether the assertion fails or passes, delete the inserted editor to prevent side effects
             # on subsequent tests
             db.delete(insert_editor_1)
             db.commit()
Пример #3
0
def get_user():
    """Function should fetch user data from database, or, if necessary, create it, and return it."""
    try:
        s = _musicbrainz.get_auth_session(data={
            'code': _fetch_data('code'),
            'grant_type': 'authorization_code',
            'redirect_uri': url_for('login.musicbrainz_post', _external=True)
        }, decoder=musicbrainz_auth_session_decoder)
        data = s.get('oauth2/userinfo').json()
        musicbrainz_id = data.get('sub')
        musicbrainz_row_id = data.get('metabrainz_user_id')
    except KeyError:
        # get_auth_session raises a KeyError if it was unable to get the required data from `code`
        raise MusicBrainzAuthSessionError()

    user = db_user.get_by_mb_row_id(musicbrainz_row_id, musicbrainz_id)
    user_email = None
    if mb_engine:
        user_email = mb_editor.get_editor_by_id(musicbrainz_row_id)['email']

    if user is None:  # a new user is trying to sign up
        if current_app.config["REJECT_NEW_USERS_WITHOUT_EMAIL"] and user_email is None:
            # if flag is set to True and the user does not have an email do not allow to sign up
            raise MusicBrainzAuthNoEmailError()
        db_user.create(musicbrainz_row_id, musicbrainz_id, email=user_email)
        user = db_user.get_by_mb_id(musicbrainz_id, fetch_email=True)
        ts.set_empty_values_for_user(user["id"])
    else:  # an existing user is trying to log in
        # Other option is to change the return type of get_by_mb_row_id to a dict
        # but its used so widely that we would modifying huge number of tests
        user = dict(user)
        user["email"] = user_email
        # every time a user logs in, update the email in LB.
        db_user.update_user_details(user["id"], musicbrainz_id, user_email)

    return user
Пример #4
0
def get_editor(editor_id: int) -> [dict, None]:
    """Return editor serialized to dict, None if not found."""
    try:
        return get_editor_by_id(editor_id)
    except mb_exceptions.NoDataFoundException:
        return None
Пример #5
0
 def test_get_by_id(self):
     self.editor_query.return_value = [editor_1]
     editor = mb_editor.get_editor_by_id(2323)
     self.assertDictEqual(editor, self.editor_1_dict)