return { 'id': self.id, 'team_name': self.team_name, 'spi': self.spi, 'off': self.off, 'defi': self.defi, 'goal_dif': self.goal_dif, 'pts': self.pts, 'relegated': self.relegated, 'make_from_playoffs': self.make_from_playoffs, 'promoted': self.promoted, 'win_championship': self.win_championship } @classmethod def find_all(cls): return cls.query.all() @classmethod def find_by_name(cls, team_name): return cls.query.filter_by(team_name=team_name).first() def save_to_db(self): db.session.add(self) db.session.commit() if os.getenv('FLASK_ENV') == 'development': from project import admin admin.add_view(ModelView(TeamModel, db.session))
# project/api/models.py from sqlalchemy.sql import func from flask_admin.contrib.sqla import ModelView from project import db class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(128), nullable=False) email = db.Column(db.String(128), nullable=False) active = db.Column(db.Boolean(), default=True, nullable=False) created_date = db.Column(db.DateTime, default=func.now(), nullable=False) def __init__(self, username, email): self.username = username self.email = email def atom(): return from project import admin admin.add_view(ModelView(User, db.session))
form_args = dict(image=dict( base_path=app.config['UPLOADS_FOLDER'], relative_path=app.config['THINGY_IMAGE_RELATIVE_PATH'], url_relative_path=app.config['UPLOADS_RELATIVE_PATH'], namegen=prefix_file_utcnow, storage_type_field='image_storage_type', bucket_name_field='image_storage_bucket_name', )) def scaffold_form(self): form_class = super(ThingyView, self).scaffold_form() static_root_parent = path.abspath( path.join(app.config['PROJECT_ROOT'], '..')) if app.config['USE_S3']: form_class.image.kwargs['storage_type'] = 's3' form_class.image.kwargs['bucket_name'] = \ app.config['S3_BUCKET_NAME'] form_class.image.kwargs['access_key_id'] = \ app.config['AWS_ACCESS_KEY_ID'] form_class.image.kwargs['access_key_secret'] = \ app.config['AWS_SECRET_ACCESS_KEY'] form_class.image.kwargs['static_root_parent'] = \ static_root_parent return form_class admin.add_view(ThingyView(Thingy, db.session, name='Thingies'))
from project.models.test_cases import Testcases, testCaseView from project.models.subject_list import subjectlist, subject_listView from project.models.upload_docs import Uploaddocs, upload_docsView from flask_login import login_user, current_user, logout_user, login_required from flask_mail import Message from flask_admin.contrib.fileadmin import FileAdmin from project.recipe.mail_config import send_mail from project.recipe.mail_config import send_otp class custom_FileAdmin(FileAdmin): can_download = True code_path = os.path.join(os.getcwd(), 'project/temp_codefolder') admin.add_view(custom_FileAdmin(code_path, '/project/', name='Code Dir')) # admin.add_view(FileAdmin(code_path, '/project/static/profile_pics', name='Profile Pics')) admin.add_view(probView(Prob, db.session)) admin.add_view(codeBaseView(CodeBase, db.session)) admin.add_view(userView(UserModel, db.session)) admin.add_view(testCaseView(Testcases, db.session)) admin.add_view(subject_listView(subjectlist, db.session)) admin.add_view(upload_docsView(Uploaddocs, db.session)) @application.before_first_request def create_tables(): db.create_all() @application.route('/rahul')
from flask import Blueprint from project.models import User, Game, Genres from project import admin, db from flask_admin.contrib.sqla import ModelView adm = Blueprint('adm', __name__) admin.add_view(ModelView(User, db.session)) admin.add_view(ModelView(Game, db.session)) admin.add_view(ModelView(Genres, db.session))
self.match_date = match_date self.round_name = round_name self.first_squad_name = first_squad_name self.first_squad_score = first_squad_score self.first_squad_points = first_squad_points self.second_squad_name = second_squad_name self.second_squad_score = second_squad_score self.second_squad_points = second_squad_points def json(self): return { 'match_id': self.match_id, 'match_date': str(self.match_date), 'round_name': self.round_name, 'first_squad_name': self.first_squad_name, 'first_squad_score': self.first_squad_score, 'first_squad_points': self.first_squad_points, 'second_squad_name': self.second_squad_name, 'second_squad_score': self.second_squad_score, 'second_squad_points': self.second_squad_points } @classmethod def find_all(cls): return cls.query.all() if os.getenv('FLASK_ENV') == 'development': from project import admin admin.add_view(ModelView(MatchModel, db.session))
def json(self): return { 'name': self.DISTRICT_NAME, 'hc-key': self.Key, "cand1": self.candate1, "cand2": self.candate2, "cand3": self.candate3, "cand4": self.candate4, "cand5": self.candate5, "cand6": self.candate6, "cand7": self.candate7, "cand8": self.candate8, "winner": self.winner, "value": self.margin } class Controller(ModelView): def is_accessible(self): return current_user.is_authenticated def not_auth(self): return "you are not authorised" admin.add_view(Controller(Results, db.session)) admin.add_view(Controller(PresidentialTable, db.session)) db.create_all()
import os from flask_admin.contrib.sqla import ModelView from sqlalchemy.sql import func from project import db class BudgetItem(db.Model): __tablename__ = 'budget_items' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(128), nullable=False) cost = db.Column(db.String(128), nullable=False) paid = db.Column(db.Boolean(), default=False, nullable=False) created_date = db.Column(db.DateTime, default=func.now(), nullable=False) def __init__(self, name, cost): self.name = name self.cost = cost if os.getenv("FLASK_ENV") == "development": from project import admin admin.add_view(ModelView(BudgetItem, db.session))
"""Validates that a password is required if this is a new user.""" if form.password2_confirm.data and not field.data: raise wtforms.validators.ValidationError('Password is required if \'confirm password\' is given') class UserView(ProtectedModelView): column_list = ('email',) form_excluded_columns = ('password',) form_args = dict( email=dict( validators=[wtforms.validators.InputRequired()], )) def scaffold_form(self): from project import app form_class = super(UserView, self).scaffold_form() form_class.password2 = wtforms.PasswordField('New Password', validators=[validate_password_required_if_confirm, wtforms.validators.EqualTo('password2_confirm', message='Passwords must match')]) form_class.password2_confirm = wtforms.PasswordField('Repeat Password') return form_class def on_model_change(self, form, model, is_created): if len(model.password2): model.set_password(db.session, form.password2.data) admin.add_view(UserView(User, db.session, name='Users'))
from flask import Blueprint from project import admin, db, models from project.gui.views import AdminView, AdminUserView, AnalystView, CampaignView, IndicatorView, LoginMenuLink, \ LogoutMenuLink bp = Blueprint('gui', __name__) admin.add_view(CampaignView(models.Campaign, db.session, category='Campaigns')) admin.add_view( AnalystView(models.CampaignAlias, db.session, category='Campaigns')) admin.add_view( IndicatorView(models.Indicator, db.session, category='Indicators')) admin.add_view( AnalystView(models.IndicatorConfidence, db.session, category='Indicators')) admin.add_view( AnalystView(models.IndicatorImpact, db.session, category='Indicators')) admin.add_view( AnalystView(models.IndicatorStatus, db.session, category='Indicators')) admin.add_view( AnalystView(models.IndicatorType, db.session, category='Indicators')) admin.add_view(AnalystView(models.IntelReference, db.session, category='Intel')) admin.add_view(AnalystView(models.IntelSource, db.session, category='Intel')) admin.add_view(AnalystView(models.Tag, db.session)) admin.add_view(AdminView(models.Role, db.session, category='Admin')) admin.add_view(AdminUserView(models.User, db.session, category='Admin'))
class UserView(ProtectedModelView): column_list = ('email', ) form_excluded_columns = ('password', ) form_args = dict( email=dict(validators=[wtforms.validators.InputRequired()], )) def scaffold_form(self): from project import app form_class = super(UserView, self).scaffold_form() form_class.password2 = wtforms.PasswordField( 'New Password', validators=[ validate_password_required_if_confirm, wtforms.validators.EqualTo('password2_confirm', message='Passwords must match') ]) form_class.password2_confirm = wtforms.PasswordField('Repeat Password') return form_class def on_model_change(self, form, model, is_created): if len(model.password2): model.set_password(db.session, form.password2.data) admin.add_view(UserView(User, db.session, name='Users'))
from flask_admin.contrib.sqla import ModelView from flask_admin.base import BaseView from datetime import datetime from psycopg2 import errors class AdminView(ModelView): def is_accessible(self): return current_user.email == 'XXX' # admin.add_view(AdminView(User, db.session)) admin.add_view(AdminView(Destination, db.session)) admin.add_view(AdminView(NewCandidateInformation, db.session)) login_manager = LoginManager(app) login_manager.init_app(app) @login_manager.user_loader def load_user(id): return User.query.get(int(id)) @app.route('/login', methods=['GET', 'POST']) def login(): if current_user.is_authenticated: return redirect(url_for('home'))
from project.models.Admin import Admin from project.models.MetaTag import MetaTag from project.models.Store import Store from project.models.Survey import Survey from project.models.Tag import Tag from project.models.User import User from project.utils.form_utils import LoginForm from project.utils.view_utils import MyModelView, UserModelView, TagModelView, StoreModelView @login.user_loader def load_user(user_id): return Admin.query.get(user_id) admin.add_view(UserModelView(User, db.session)) admin.add_view(MyModelView(Survey, db.session)) admin.add_view(TagModelView(Tag, db.session)) admin.add_view(TagModelView(MetaTag, db.session)) admin.add_view(StoreModelView(Store, db.session)) @app.route('/admin/login', methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): username = form.username.data password = form.password.data admin_user = Admin.query.filter_by(name=username, pwd=password).first() if admin_user is not None: user = Admin.query.get(admin_user.id)
image=dict( base_path=app.config['UPLOADS_FOLDER'], relative_path=app.config['THINGY_IMAGE_RELATIVE_PATH'], url_relative_path=app.config['UPLOADS_RELATIVE_PATH'], namegen=prefix_file_utcnow, storage_type_field='image_storage_type', bucket_name_field='image_storage_bucket_name', )) def scaffold_form(self): form_class = super(ThingyView, self).scaffold_form() static_root_parent = path.abspath( path.join(app.config['PROJECT_ROOT'], '..')) if app.config['USE_S3']: form_class.image.kwargs['storage_type'] = 's3' form_class.image.kwargs['bucket_name'] = \ app.config['S3_BUCKET_NAME'] form_class.image.kwargs['access_key_id'] = \ app.config['AWS_ACCESS_KEY_ID'] form_class.image.kwargs['access_key_secret'] = \ app.config['AWS_SECRET_ACCESS_KEY'] form_class.image.kwargs['static_root_parent'] = \ static_root_parent return form_class admin.add_view(ThingyView(Thingy, db.session, name='Thingies'))
if not current_user.is_active or not current_user.is_authenticated: return False if current_user.has_role('superuser'): return True return False def _handle_view(self, name, **kwargs): """ Override builtin _handle_view in order to redirect users when a view is not accessible. """ if not self.is_accessible(): if current_user.is_authenticated: # permission denied abort(403) else: # login return redirect(url_for('login')) # can_edit = True edit_modal = True create_modal = True can_export = True can_view_details = True details_modal = True admin.add_view(MyModelView(Project, db.session)) admin.add_view(MyModelView(User, db.session))
import os from sqlalchemy.sql import func from project import db class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(128), nullable=False) email = db.Column(db.String(128), nullable=False) active = db.Column(db.Boolean(), default=True, nullable=False) created_date = db.Column(db.DateTime, default=func.now(), nullable=False) def __init__(self, username, email): self.username = username self.email = email if os.getenv("FLASK_ENV") == "development": from project import admin from project.api.users.admin import UserAdminView admin.add_view(UserAdminView(User, db.session))
import jwt from flask import current_app from sqlalchemy.sql import func from project import db class Event(db.Model): __tablename__ = "events" id = db.Column(db.Integer, primary_key=True, autoincrement=True) description = db.Column(db.String(128), nullable=False) points = db.Column(db.Integer, nullable=False) created_date = db.Column(db.DateTime, default=func.now(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) sponsor_name = db.Column(db.String, nullable=False) def __init__(self, description="", points="", user_id="", sponsor_name=""): self.description = description self.points = points self.user_id = user_id self.sponsor_name = sponsor_name if os.getenv("FLASK_ENV") == "development": from project import admin from project.api.events.admin import EventsAdminView admin.add_view(EventsAdminView(Event, db.session))