def upsert_user(twitter_handle):
    try:
        twitter_user = twitter_api.get_user(twitter_handle)
        if User.query.get(twitter_user.id):
            db_user = User.query.get(twitter_user.id)
        else:
            db_user = User(id=twitter_user.id, name=twitter_handle)
        DB.session.add(db_user)

        user_tweets = twitter_user.timeline(count=200,
                                            exclude_replies=True,
                                            include_rts=False,
                                            tweet_mode="extended")

        if user_tweets:
            db_user.newest_tweet_id = user_tweets[0].id

        for tweet in user_tweets:
            vectorized_tweet = vectorize_tweet(tweet.full_text)
            db_tweet = Tweet(id=tweet.id,
                             text=tweet.full_text,
                             vect=vectorized_tweet)
            db_user.tweets.append(db_tweet)
            DB.session.add(db_tweet)
    except Exception as e:
        raise e
    else:
        DB.session.commit()
def change_password(user_id, secret):
    if not User.validate_reset_secret(user_id, secret):
        abort(403)
    change_pass_form = ChangePassowrdForm(request.form)
    error = None
    if request.method == 'POST' and change_pass_form.validate():
        User.change_password(user_id, change_pass_form.password.data)
        return redirect(url_for("common_views.index"))
    return render_template("change_pass.html", change_form=change_pass_form, error=error)
Exemple #3
0
def change_password(user_id, secret):
    if not User.validate_reset_secret(user_id, secret):
        abort(403)
    change_pass_form = ChangePassowrdForm(request.form)
    error = None
    if request.method == 'POST' and change_pass_form.validate():
        User.change_password(user_id, change_pass_form.password.data)
        return redirect(url_for("common_views.index"))
    return render_template("change_pass.html",
                           change_form=change_pass_form,
                           error=error)
Exemple #4
0
def user_register():
    form = RegisterForm()
    if (request.method == "POST"):
        if form.validate_on_submit():
            is_success = True
            try:
                user = User(email=form.email.data,
                            create_at=datetime.now(),
                            name=form.user_name.data,
                            password_hash=generate_password_hash(
                                form.password_1.data))

                db.session.add(user)
                db.session.commit()
            except:
                db.session.rollback()
                is_success = False
                pass
            if is_success:
                flash("注册成功, 现在你可以登录了")
                return redirect(url_for("auth.user_login"))
            else:
                flash("注册失败!可能该用户名已被注册")
        else:
            flash("注册失败!输入不合法")
    return render_template('register.html', form=form)
Exemple #5
0
def device_view(device_id):
    device = current_user.get_device(device_id)
    if device is not None:
        device_owner = User.get(user_id=device.device_owner)
        return render_template("admin/admin_device.html", device=device, device_owner=device_owner)
    else:
        abort(403)
Exemple #6
0
def createUser(login_session):
    '''
    Take user from login session and save to db
    returns user.id
    Uniqueness is defined as having unique oauth id number. For hypotheoretical
    case (highly unlikely) when id numbers from different oauth providers can possibly match
    there are prefixes stored ensuring uniqueness of oauth ID
    fb - facebook
    gl - google
    gh - github
    '''

    if login_session['provider'] == 'facebook':
        oauthid = 'fb'
    if login_session['provider'] == 'google':
        oauthid = 'gl'
    if login_session['provider'] == 'github':
        oauthid = 'gh'
    oauthid = oauthid+ str(login_session['oauthid'])
    newUser = User(name=login_session['username'], email=login_session['email'],
                   picture=login_session['picture'],
                   oauthid=oauthid)
    sql_session.add(newUser)
    sql_session.commit()
    user = sql_session.query(User).filter_by(oauthid=oauthid).one()
    return user.id
Exemple #7
0
 def landing():
     DB.drop_all()
     DB.create_all()
     app_user = User(id=1, name='app_user')
     DB.session.add(app_user)
     DB.session.commit()
     with open('templates/landing.json') as f:
         args = json.load(f)
     return render_template('base.html', **args)
Exemple #8
0
def device_view(device_id):
    device = current_user.get_device(device_id)
    if device is not None:
        device_owner = User.get(user_id=device.device_owner)
        return render_template("admin/admin_device.html",
                               device=device,
                               device_owner=device_owner)
    else:
        abort(403)
 def landing():
     DB.drop_all()
     DB.create_all()
     app_user = User(id=1, name='app_user')
     DB.session.add(app_user)
     DB.session.commit()
     with open('/Users/rob/G_Drive_sing.parvi/Colab_Notebooks/Unit-3-Sprint-3-Productization-and-Cloud/module2-consuming-data-from-an-api/John/templates/landing.json') as f:
         args = json.load(f)
     return render_template('base.html', **args)
Exemple #10
0
 def landing():
     # Sqlalchemy drop all the tables
     DB.drop_all()
     DB.create_all()
     app_user = User(id=1, name='User1')
     DB.session.add(app_user)
     app_user = User(id=2, name='User2')
     DB.session.add(app_user)
     app_user = User(id=3, name='User3')
     DB.session.add(app_user)
     app_user = User(id=4, name='User4')
     DB.session.add(app_user)
     app_user = User(id=5, name='User55')
     DB.session.add(app_user)
     DB.session.commit()
     with open(
             '/Users/rob/G_Drive_sing.parvi/Colab_Notebooks/Unit-3-Sprint-3-Productization-and-Cloud/module1-web-application-development-with-flask/assignment3.3.1/templates/landing.json'
     ) as f:
         args = json.load(f)
     return render_template('base.html', **args)
def subs_video(update, context):
    game_id = update.callback_query.data.split(" ")[1]
    Session = sessionmaker(bind=engine)
    session = Session()
    game = session.query(Game).filter(Game.game_id == game_id).first()
    text = f"Вы подписались на уведомления по игре {game.team1} vs {game.team2}"
    context.bot.send_message(text=text, chat_id=update.callback_query.message.chat.id)
    subscription = User(int(update.callback_query.message.chat.id), int(game_id))
    session.add(subscription)
    session.commit()
    get_game_start_twitch(context)
 def landing():
     DB.drop_app()
     DB.create_all()  # create table
     app_user = User(id=1, name='app_user')
     DB.session.add(app_user)
     DB.session.commit()
     # args = {'title': 'Landing', 'body': 'landing body'}
     # return render_template('base.html', **args)
     with open(
             'templates/landing.json') as f:  # keep only in memory for the following command, keep it in ram for just a moment
         args = json.load(f)  # loading as a dictionary and then unpack it
     # return render_template('base.html', title='Landing', body = 'landing body')
     return render_template('base.html', **args)
Exemple #13
0
def participate():
    form = LoginForm(request.form)
    error = None
    if request.method == 'POST' and form.validate():
        user = User.get(email=form.email.data)
        if user is not None:
            if user.authenticate(form.password.data):
                if login_user(user, remember=True):
                    return redirect(request.args.get("next") or url_for("user_views.devices_view"))
            else:
                error = "Email and password do not match!"
        else:
            error = "A user with this email does not exist!"
    return render_template("participate.html", form=form, error=error)
Exemple #14
0
def participate():
    form = LoginForm(request.form)
    error = None
    if request.method == 'POST' and form.validate():
        user = User.get(email=form.email.data)
        if user is not None:
            if user.authenticate(form.password.data):
                if login_user(user, remember=True):
                    return redirect(
                        request.args.get("next")
                        or url_for("user_views.devices_view"))
            else:
                error = "Email and password do not match!"
        else:
            error = "A user with this email does not exist!"
    return render_template("participate.html", form=form, error=error)
Exemple #15
0
def forgot_password():
    forgot_form = ForgotPasswordForm(request.form)
    error = None
    if request.method == 'POST':
        if forgot_form.validate() and recaptcha_check(request.form["g-recaptcha-response"]):
            user = User.get(email=forgot_form.email.data)
            if user is not None:
                if user.forgot_password():
                    link = url_for("common_views.change_password", user_id=user.user_id, secret=user.forgot_secret, _external=True)
                    current_app.logger.info(link)
                    mail = mail_handler.Mail(user.email, link)
                    mail.send()
                    return redirect(url_for("common_views.index"))
            else:
                error = "A user with this email does not exist!"
        else:
            error = "Please enter email address and prove you are not a robot!"
    return render_template("forgot.html", forgot_form=forgot_form, error=error)
Exemple #16
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()

            form.populate_obj(user)
            # we hash the users password to avoid saving it as plaintext in the db,
            # remove to use plain text:
            # user.password = generate_password_hash(form.password.data)

            db.session.add(user)
            db.session.commit()

            login_user(user)
            return redirect(url_for('.index'))
        link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()
Exemple #17
0
def forgot_password():
    forgot_form = ForgotPasswordForm(request.form)
    error = None
    if request.method == 'POST':
        if forgot_form.validate() and recaptcha_check(
                request.form["g-recaptcha-response"]):
            user = User.get(email=forgot_form.email.data)
            if user is not None:
                if user.forgot_password():
                    link = url_for("common_views.change_password",
                                   user_id=user.user_id,
                                   secret=user.forgot_secret,
                                   _external=True)
                    current_app.logger.info(link)
                    mail = mail_handler.Mail(user.email, link)
                    mail.send()
                    return redirect(url_for("common_views.index"))
            else:
                error = "A user with this email does not exist!"
        else:
            error = "Please enter email address and prove you are not a robot!"
    return render_template("forgot.html", forgot_form=forgot_form, error=error)
Exemple #18
0
def get_user_and_tweets(username):

    heroku_url = 'https://lambda-ds-twit-assist.herokuapp.com/user/'

    user = ast.literal_eval(requests.get(heroku_url + username).text)

    try:
        if User.query.get(user['twitter_handle']['id']):
            db_user = User.query.get(user['twitter_handle']['id'])
        else:
            db_user = User(id=user['twitter_handle']['id'],
                           name=user['twitter_handle']['username'])
        DB.session.add(db_user)

        for tweet in user['tweets']:
            db_tweet = Tweet(id=tweet['id'], text=tweet['full_text'])
            db_user.tweets.append(db_tweet)
            DB.session.add(db_tweet)
    except Exception as e:
        raise e
    else:
        DB.session.commit()
Exemple #19
0
def usersignup():
    # print "I am in the sign up page"
    """ User input details to populate the user profile page"""
    first_name = request.form.get('fname')
    last_name = request.form.get('lname')
    email_address = request.form.get('email')
    password = request.form.get('password')
    user_address = request.form.get('address')
    phone_number = request.form.get('phone')
    role = request.form.get('role')
    email_reminder = request.form.get('email_reminder')
    user_count = User.query.filter_by(email_address=email_address).count()

    # print role
    # print email_reminder
    # print user_address

    if user_count != 0:
        flash("Looks an email_address with your name already exists")
        return render_template("login.html")
    else:
        #flash("Please signup for an account")
        # Inserting a new user record into users table
        new_user = User(password=password,
                        first_name=first_name,
                        last_name=last_name,
                        user_type='User',
                        role=role,
                        reminder=email_reminder,
                        email_address=email_address,
                        phone_number=phone_number,
                        status='Active',
                        user_address=user_address)
        db.session.add(new_user)
        db.session.commit()
        #
        new_user = User.query.filter_by(email_address=email_address).one()
        return render_template("user_profile.html", new_user=new_user)
Exemple #20
0
def load_user(user_id):
    return User.get(user_id)
Exemple #21
0
def load_user(user_id):
    return User.get(user_id)