• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

25
Views
Flask prevent users from making new posts for spam

I am trying to do a flask app where you sign up and you can make new posts. But I want if the user has made over 3 posts in less than 10 minutes then he must wait for 5 minutes. Here is the code:

allpos = []
@app.route('/makepost',methods=['POST','GET'])
def makepost():
    print(allpos)
    sear = 1
    oldtime = time.time()
    if len(allpos) == 3:
        currenttime = time.time()
        if currenttime - oldtime > 300:
            allpos.clear()
            return render_template('posts.html')
        else:
            return render_template('wait.html')
    if not current_user.is_authenticated:
        return redirect('/login') 
    if request.method == 'POST':
        global title
        global content
        title = request.form['title']
        content = request.form['content']
        if len(title) < 5:
            flash("Title must be at least 5")
        if len(title) > 50:
            flash("Title must be 50 characters max")
        if  len(content) < 5:
            flash('Content must be at least 5 letters')
        if len(title) > 5 and len(title) < 50 and len(content) > 5:
            sear = 2
        if sear == 2:
            global post1
            post1 = Post(title=title,content=content,name=current_user.name,author=current_user.id)
            db.session.add(post1)
            db.session.commit()
            allpos.append('1')
    if sear != 2:
        flash('We couldnt make your post')
    return render_template('posts.html')

I want to prevent the user from makings posts along these lines:

oldtime = time.time()
    if len(allpos) == 1:
        currenttime = time.time()
        if currenttime - oldtime > 0:
            print('asdasd')
            allpos.clear()
            return render_template('posts.html')
        else:
            return render_template('wait.html')

But the time passes and it still prevents the user. Also when the user goes to another page the time resets. My main problem is that it prevents the user from making posts. Also I use SQLAlchemy.

Here is the user and post model:

class Users(UserMixin,db.Model):
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(200),unique=True,nullable=False)
    password = db.Column(db.String(200),unique=False,nullable=False)
    role = db.Column(db.String(200),unique=False,nullable=False)
    missions = db.Column(db.String(200),unique=False,nullable=False)
    posts = db.relationship('Post',backref='user')
class Post(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    title = db.Column(db.String(200),unique=False,nullable=False)
    content = db.Column(db.Text,unique=False,nullable=False)
    name = db.Column(db.String(200),unique=False,nullable=False)
    author = db.Column(db.Integer,db.ForeignKey('users.id'),unique=False)

Please give me an answer.

Thanks.

about 1 month ago ·

Juan Pablo Isaza

Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.