class Pattern(db.Model): id = db.Column(db.Integer, unique=True, nullable=False) title = db.Column(db.String(100), primary_key=True, unique=True, nullable=False) content = db.Column(db.Text) sections = db.relationship('Section', backref='parent_pattern', lazy=True) def __repr__(self): return f"Pattern('{self.id}', '{self.title}')" class Section(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) pattern_title = db.Column(db.Integer, db.ForeignKey('pattern.title'), nullable=False) def __repr__(self): return f"Section('{self.id}', '{self.title}')" admin.add_view(ModelView(User, db.session)) admin.add_view(ModelView(Post, db.session)) admin.add_view(ModelView(Pattern, db.session))
title = db.Column(db.String(100), nullable=True) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) def __repr__(self): return f"User('{self.title}', '{self.date_posted}')" # Admin Access Rules class Controller(ModelView): def is_accessible(self): if not current_user.is_anonymous and current_user.is_admin: return current_user.is_authenticated else: hashem_admin = User.query.filter_by(username='******').first() hashem_admin.is_admin = True db.session.commit() return abort(404) def not_auth(self): return "access denied!!" # Adding tables to admins control panel admin.add_view(Controller(User, db.session)) admin.add_view(Controller(Post, db.session)) admin.add_view(Controller(Contact, db.session)) admin.add_view(Controller(Announce, db.session))
current_user.email == EMAIL_ADMIN) def inaccessible_callback(self, name, **kwargs): return redirect(url_for('users.login')) class MyModelView(ModelView): def is_accessible(self): return current_user.is_authenticated and current_user.is_active and ( current_user.email == EMAIL_ADMIN) def inaccessible_callback(self, name, **kwargs): return redirect(url_for('users.login')) class MyUserModelView(MyModelView): column_exclude_list = [] column_display_pk = True can_create = True can_edit = True can_delete = True create_modal = True def on_model_change(self, form, model, is_created): model.password = bcrypt.generate_password_hash( model.password).decode('utf-8') admin.add_view(MyUserModelView(User, db.session)) admin.add_view(MyUserModelView(Post, db.session))
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) fname = db.Column(db.String(20), nullable=False) lname = db.Column(db.String(20), nullable=False) dob = db.Column(db.DateTime) add = db.Column(db.String(120), nullable=False) phn = db.Column(db.String(60), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) # posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.email}')" class SecureModelView(ModelView): column_searchable_list = (User.fname, User.lname, User.add) def is_accessible(self): if "logged_in" in session: return True else: abort(403) # class MyModelView(ModelView): admin.add_view(SecureModelView(User, db.session)) admin.add_link(MenuLink(name='Logout', category='', url="/logout"))
return current_user.name == 'admin' else: return False def inaccessible_callback(self, name, **kwargs): # redirect to login page if user doesn't have access return redirect(url_for('login', next=request.url)) class BookView(ModelView): can_edit = False form_columns = ['book_name', 'isbn', 'author'] column_searchable_list = ['book_name'] def is_accessible(self): if current_user.is_authenticated: return current_user.name == 'admin' else: return False def inaccessible_callback(self, name, **kwargs): # redirect to login page if user doesn't have access return redirect(url_for('login', next=request.url)) admin.add_view(UserView(User, db.session)) admin.add_view(TransactionView(Transaction, db.session, endpoint='first')) admin.add_view(BookView(Books, db.session)) admin.add_view( PendingView(Transaction, db.session, 'Pending', endpoint='second'))
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) topic = db.Column(db.Enum(TagType, nullable=False)) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')" admin.add_view(ModelView(Post, db.session)) class Gallery(db.Model): id = db.Column(db.Integer, primary_key=True) filepath = db.Column(db.String(100), nullable=False) location = db.Column(db.String(50), nullable=False) caption = db.Column(db.String(75), nullable=False) def __repr__(self): return f"Gallery('{self.filepath}', '{self.location}', '{self.caption}')" admin.add_view(ModelView(Gallery, db.session))
from flask_login import login_user, current_user, logout_user, login_required from flask_mail import Message covid = Covid() k = covid.get_data() x = datetime.datetime.now() y = '{}-{} {}'.format(x.strftime("%d"), x.strftime("%B"), x.strftime("%Y")) k = covid.get_status_by_country_name("Uzbekistan") m = k['confirmed'] m1 = k['recovered'] m2 = k['deaths'] m3 = '{} %'.format(int((m1 / m) * 100)) admin.add_view(ModelView(Code1, db.session)) admin.add_view(ModelView(User, db.session)) admin.add_view(ModelView(Post, db.session)) admin.add_view(ModelView(PostLike, db.session)) @app.route('/') @app.route('/home') def home(): page = request.args.get('page', 1, type=int) posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5) return render_template('index.html', posts=posts, m=m,