Bläddra i källkod

Добавил в панель админа возможность бана пользователей

Andrei 2 år sedan
förälder
incheckning
2c1119a7ae
4 ändrade filer med 19 tillägg och 7 borttagningar
  1. 1 0
      data/users.py
  2. 13 6
      main.py
  3. 1 1
      static/css/admin.css
  4. 4 0
      templates/admin.html

+ 1 - 0
data/users.py

@@ -26,6 +26,7 @@ class User(SqlAlchemyBase, UserMixin):
     activity = sqlalchemy.Column(sqlalchemy.DateTime, nullable=True)
     birthday = sqlalchemy.Column(sqlalchemy.Date, nullable=True)
     activated = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False, default=False)
+    banned = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False, default=False)
 
     def check_password(self, password):
         return check_password_hash(self.password, password)

+ 13 - 6
main.py

@@ -74,12 +74,14 @@ def admin():
                 data_form = list(map(lambda x: (x[0], x[1]), data_form.items()))
                 list(
                     map(lambda x: save_admin_data(x, data_session), list(filter(lambda x: 'role_' in x[0], data_form))))
-                list_id = list(map(lambda user: int(user[0].split('_')[-1]), list(filter(lambda x: 'active_' in x[0], data_form))))
+                activ_id = list(
+                    map(lambda user: int(user[0].split('_')[-1]), list(filter(lambda x: 'active_' in x[0], data_form))))
+                banned_id = list(
+                    map(lambda user: int(user[0].split('_')[-1]), list(filter(lambda x: 'banned_' in x[0], data_form))))
                 for user in users:
-                    if user.id not in list_id:
-                        user.activated = 0
-                    else:
-                        user.activated = 1
+                    user.activated = 0 if user.id not in activ_id else 1
+                for user in users:
+                    user.banned = 0 if user.id not in banned_id else 1
                 data_session.commit()
             return render_template('admin.html', title='Панель админа', roles=roles, users=users, form=form)
     abort(404)
@@ -622,10 +624,15 @@ def login():
             if not user:
                 user = data_session.query(User).filter(User.login == form.login.data).first()
             if user and user.check_password(form.password.data):
-                if user.activated:
+                if user.activated and not user.banned:
                     login_user(user, remember=form.remember_me.data)
                     logging.info(f'{user.login} logged in')
                     return redirect('/projects')
+                elif user.banned:
+                    return render_template('login.html',
+                                           message="Ваш аккаунт заблокирован, обратитесь в поддержку: inepted@yandex.ru",
+                                           danger=True,
+                                           form=form)
                 else:
                     return render_template('login.html',
                                            message="Ваша почта не подтверждена",

+ 1 - 1
static/css/admin.css

@@ -90,6 +90,6 @@
     flex-direction: column;
     align-items: center;
 }
-.user_active {
+.user_active, .user_banned {
     margin-left: 1vw;
 }

+ 4 - 0
templates/admin.html

@@ -23,6 +23,10 @@
                     <label class="active_label">Активирован</label>
                     <input class="choose_active" name="active_{{user.id}}" type="checkbox" value="y" {% if user.activated == 1 %}checked="yes"{% endif %}>
                 </div>
+                <div class="user_banned">
+                    <label class="banned_label">Бан</label>
+                    <input class="choose_banned" name="banned_{{user.id}}" type="checkbox" value="y" {% if user.banned == 1 %}checked="yes"{% endif %}>
+                </div>
             </div>
             {% endfor %}
         </div>