Example #1
0
def oauth_callback(provider):
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()

    if not email:
        email = '{}@oauth.com'.format(social_id)

    if not social_id:
        flash('Sorry, the authentication failed.')
        return redirect(url_for('auth.home'))

    user = User.query.filter_by(oauth_id=social_id).first()
    if not user:
        user = User(username=username,
                    oauth_id=social_id,
                    email=email,
                    password="******".format(social_id),
                    registered_on=datetime.utcnow(),
                    activated_on=datetime.utcnow(),
                    active=True)
        db.session.add(user)
        db.session.commit()

        login_user(user, True)
        return redirect(url_for('users.account', user_name=username))

    login_user(user, True)
    return return_user_homepage(user.homepage, user.username)
Example #2
0
def home():
    login_form = LoginForm()
    register_form = RegistrationForm()

    if login_form.validate_on_submit():
        user = User.query.filter_by(
            username=login_form.login_username.data).first()
        if user and not user.active:
            app.logger.info(
                '[INFO] - [{}] Connexion attempt while account not activated'.
                format(user.id))
            flash(
                'Your Account is not activated. Please check your email address to activate your account.',
                'danger')
        elif user and bcrypt.check_password_hash(
                user.password, login_form.login_password.data):
            login_user(user, remember=login_form.login_remember.data)
            app.logger.info('[INFO] - [{}] Logged in.'.format(user.id))
            flash("You're now logged in. Welcome {0}".format(user.username),
                  "success")

            next_page = request.args.get('next')
            if next_page:
                return redirect(next_page)
            return return_user_homepage(user.homepage, user.username)
        else:
            flash('Login Failed. Please check username and password.',
                  'warning')
    elif register_form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            register_form.register_password.data).decode('utf-8')
        user = User(username=register_form.register_username.data,
                    oauth_id='{}'.format(random.randint(0, 16846876000056)),
                    email=register_form.register_email.data,
                    password=hashed_password,
                    registered_on=datetime.utcnow())
        db.session.add(user)
        db.session.commit()
        app.logger.info(
            '[INFO] - [{}] New account registration: Username: {}, email: {}'.
            format(user.id, register_form.register_username.data,
                   register_form.register_email.data))
        try:
            send_register_email(user)
            flash(
                'Your account has been created. Check your e-mail address to activate your account.',
                'info')
        except Exception as e:
            app.logger.error(
                '[ERROR] - Sending register email to account ID [{}]: {}.'.
                format(user.id, e))
            flash(
                "An error occured while sending your register e-mail. Admin were advised. Please try again later."
            )
        return redirect(url_for('auth.home'))

    return render_template('home.html',
                           login_form=login_form,
                           register_form=register_form)
Example #3
0
def autocomplete():
    search = request.args.get('q')
    media_select = request.args.get('media_select')

    if media_select == 'TMDB':
        try:
            Api_data = ApiTMDB()
            Api_data.search(search)
            media_results = Api_data.get_autocomplete_list()
        except Exception as e:
            media_results = []
            app.logger.error(f"[ERROR] - Requesting the TMDB API: {e}")
    elif media_select == 'IGDB':
        try:
            Api_data = ApiGames()
            Api_data.search(search)
            media_results = Api_data.get_autocomplete_list()
        except Exception as e:
            media_results = []
            app.logger.error(f"[ERROR] - Requesting the IGDB API: {e}")
    elif media_select == 'BOOKS':
        try:
            Api_data = ApiBooks()
            Api_data.search(search)
            media_results = Api_data.get_autocomplete_list()
        except Exception as e:
            media_results = []
            app.logger.error(
                f"[SYSTEM] - Error requesting the GoogleBooks API: {e}")

        # query = Books.query.filter(Books.name.like(f"%{search}%")).all()
        # media_results = []
        # for b in query:
        #     try:
        #         date = datetime.strftime(datetime.strptime(b.release_date, '%m/%d/%y'), '%d %b %Y')
        #     except:
        #         date = b.release_date
        #     media_results.append({'api_id': b.id,
        #                           'display_name': b.name,
        #                           'image_cover': '/static/covers/books_covers/' + b.image_cover,
        #                           'date': date,
        #                           'category': 'Books',
        #                           'type': 'Books'})
    elif media_select == 'users':
        media_results = User.get_autocomplete_list(search)
    else:
        return request.referrer or '/'

    if len(media_results) == 0:
        return jsonify(search_results=[{
            'nb_results': 0,
            'category': None
        }]), 200

    media_results = sorted(media_results, key=lambda i: i['category'])

    return jsonify(search_results=media_results), 200
Example #4
0
def create_first_data():
    db.create_all()
    if User.query.filter_by(id='1').first() is None:
        admin1 = User(username='******',
                      email='*****@*****.**',
                      password=bcrypt.generate_password_hash("password").decode('utf-8'),
                      active=True,
                      private=True,
                      registered_on=datetime.utcnow(),
                      activated_on=datetime.utcnow(),
                      role=RoleType.ADMIN,
                      oauth_id="a")
        manager1 = User(username='******',
                        email='*****@*****.**',
                        password=bcrypt.generate_password_hash("password").decode('utf-8'),
                        active=True,
                        registered_on=datetime.utcnow(),
                        activated_on=datetime.utcnow(),
                        role=RoleType.MANAGER,
                        oauth_id="b")
        user1 = User(username='******',
                     email='*****@*****.**',
                     password=bcrypt.generate_password_hash("password").decode('utf-8'),
                     active=True,
                     registered_on=datetime.utcnow(),
                     activated_on=datetime.utcnow(),
                     oauth_id="c")
        db.session.add(admin1)
        db.session.add(manager1)
        db.session.add(user1)
        add_frames_to_db()
        add_badges_to_db()
        add_ranks_to_db()
    refresh_db_frames()
    refresh_db_badges()
    refresh_db_ranks()

    # add_eps_watched()
    compute_media_time_spent()
    # update_Mylists_stats()

    db.session.commit()
Example #5
0
def register_account_token(token):
    user = User.verify_reset_token(token)
    if not user or user.active:
        flash('That is an invalid or an expired token.', 'warning')
        return redirect(url_for('auth.reset_password'))

    user.active = True
    user.activated_on = datetime.utcnow()
    db.session.commit()
    app.logger.info('[INFO] - [{}] Account activated'.format(user.id))
    flash('Your account has been activated.', 'success')

    return redirect(url_for('auth.home'))
Example #6
0
def email_update_token(token):
    user = User.verify_reset_token(token)

    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('auth.home'))

    if user.id != current_user.id:
        return redirect(url_for('auth.home'))

    old_email = user.email
    user.email = user.transition_email
    user.transition_email = None

    db.session.commit()
    app.logger.info('[{}] Email successfully changed from {} to {}'.format(
        user.id, old_email, user.email))
    flash('Email successfully updated!', 'success')

    return redirect(url_for('auth.home'))
Example #7
0
def reset_password_token(token):
    user = User.verify_reset_token(token)
    if not user:
        flash('That is an invalid or an expired token.', 'warning')
        return redirect(url_for('auth.reset_password'))

    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_password
        db.session.commit()
        app.logger.info(
            '[INFO] - [{}] Password reset via reset password email.'.format(
                user.id))
        flash('Your password has been updated! You are now able to log in.',
              'success')
        return redirect(url_for('auth.home'))

    return render_template('reset_password_token.html',
                           title='Reset Password',
                           form=form)