Ejemplo n.º 1
0
def like_or_unlike_message(msg_id):
    ''' Handle user liking or unliking a message. Adds user id and msg id to
    liked_messages table if messages is liked. Removes relevant record 
    if message is unliked. 
    
    Redirects to homepage'''

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = TokenForm()

    if form.validate_on_submit():
        message = Message.query.get(msg_id)
        user = g.user
        #user.likes is an array of the all the message this user likes
        if message in user.message_likes:
            user.message_likes.remove(
                message
            )  #remove message id from their user's liked message id [])
            db.session.commit()
        else:
            user.message_likes.append(
                message)  #add liked messages to user's liked message list
            db.session.commit()

        return redirect("/")

    else:
        return render_template('home.html', form=form)
Ejemplo n.º 2
0
def register():
    '''
    Checks to see if the user has verified their number (and redirects them away)
    :return: Forms to enter phone number, then token
    '''
    if current_user.verified:
        return redirect(url_for('home',_external=True))

    elif current_user.phone:
        token_form = TokenForm()
        if token_form.validate_on_submit():
            token = str(token_form.data['token'])
            if current_user.check_token(token):
                current_user.verified = True
                db.session.commit()
                return redirect(url_for('register',_external=True))
            else:
                flash('Please check your token.', "info")

        return render_template('token.html', form = token_form)
    else:
        form = PhoneNumberForm()
        if form.validate_on_submit():
            phoneNum = form.data['phone']
            if User.query.filter_by(phone = phoneNum, verified = True).all():
                flash("Number already in use.", "danger")
                return redirect(url_for('register',_external=True))
            current_user.phone = phoneNum
            update_token(current_user)                  #db committed in this method for us
            return current_user.token + " This is your token. Remember it or smth idc."
        return render_template('register.html', form=form)
Ejemplo n.º 3
0
def add_token():
    form = TokenForm()
    users = User.query.all()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.name.data).first()
        if user is not None:
            user.reset_token(form.token.data)
            flash("Token updated for %s" % form.name.data)
            return redirect(url_for('admin.add_token'))
        else:
            user = User(username=form.name.data, token=form.token.data)
            db.session.add(user)
            db.session.commit()
            flash('Token generated for %s.' % form.name.data)
    return render_template('admin/token.html', form=form, users=users)
Ejemplo n.º 4
0
def delete_user():
    """Delete user."""
    #TODO use deleteform()
    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = TokenForm()

    if form.validate_on_submit():
        do_logout()

        db.session.delete(g.user)
        db.session.commit()

    return redirect("/signup", form=form)
Ejemplo n.º 5
0
def add_token():
    form = TokenForm()
    users = User.query.all()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.name.data).first()
        if user is not None:
            user.reset_token(form.token.data)
            flash("Token updated for %s" % form.name.data)
            return redirect(url_for('admin.add_token'))
        else:
            user = User(
                username=form.name.data,
                token=form.token.data
                )
            db.session.add(user)
            db.session.commit()
            flash('Token generated for %s.' % form.name.data)
    return render_template('admin/token.html', form=form, users=users)
Ejemplo n.º 6
0
    def dispatch_request(self, user_token):
        form = TokenForm()
        if form.validate_on_submit():
            user_token = form.user_token.data.upper()
        if user_token:
            registered_users = UserModel.query(
                UserModel.user_token == user_token)
            updated_users = []
            for registered_user in registered_users:
                if registered_user.user_token and registered_user.user_token == user_token:
                    session['user'] = registered_user.to_dict()
                    session['user']['uid'] = registered_user.key.id()
                    #registered_user.user_token = None
                    #updated_users.append(registered_user)
            #ndb.put_multi(updated_users)

        if session.get('user', None):
            return redirect(url_for('list_users'))
        else:
            flash(u"Invalid token, please request a new one.", 'warning')
            return redirect(url_for('login'))
Ejemplo n.º 7
0
    def dispatch_request(self):
        form = TokenForm()
        if form.validate_on_submit():
            token = uuid4().hex[:6].upper()
            phone = phonenumbers.format_number(
                phonenumbers.parse(form.user_phone.data, region='US'),
                phonenumbers.PhoneNumberFormat.E164)
            registered_users = UserModel.query(UserModel.user_phone == phone)
            updated_users = []
            for registered_user in registered_users:
                registered_user.user_token = token
                updated_users.append(registered_user)

            if updated_users:
                ndb.put_multi(updated_users)

                url = request.url_root.rstrip('/') + url_for('validate',
                                                             user_token=token)

                client = TwilioRestClient(TWILIO_SID, TWILIO_TOKEN)
                message = client.messages.create(body="Jeff's Xmas Tracker\n\n"
                                                 "Token: %s\n"
                                                 "or\n"
                                                 "Click: %s" % (token, url),
                                                 to=phone,
                                                 from_="+15153052239")

                flash(
                    u"Token sent to %s. Follow link or type in token above." %
                    phone, 'success')
            else:
                flash(
                    u"The phone number %s isn't registered to any users." %
                    phone, 'warning')

        return render_template('validate_token.html', form=form)
Ejemplo n.º 8
0
def invitar():
    form = TokenForm()
    if form.validate_on_submit():
		
        
        if request.method == 'POST' and form.validate():
    
            email = str(request.form['email'])


            token= ''.join(random.choice(string.ascii_uppercase) for i in range(6))
#
            if sendmail(email, token) == True:

                t = Token(token=token)

                db.session.add(t)
                db.session.commit()
                return render_template('enviado.html', email=email)
            else:
                return render_template('404.html')
            #return render_template('enviado.html', email=email, token=token)
    
    return render_template('invitar.html', form=form)