Exemple #1
0
    def __init__(self, app, session):

        self.menu = Menu()
        self.app = app
        self.session = session
        self.base_template = "frontend/base_onepage.html"
        self.template_folder = "templates"
        self.static_folder = "files/static"
        self.security_manager_class = SecurityManager
        self.sm = self.security_manager_class(self)
        self.indexview = IndexView
        self._add_admin_views()
        #self._add_addon_views()
        self._add_menu_permissions()
        self._add_global_filters()
        self.model_string_conexoes = SQLAInterface(StringConexoes, session)
        #session.close()

        #self._add_uris()

        if not self.app:
            for baseview in self.baseviews:
                # instantiate the views and add session
                self._check_and_init(baseview)
                # Register the views has blueprints
                self.register_blueprint(baseview)
                # Add missing permissions where needed
                self._add_permission(baseview)
Exemple #2
0
def lista_cidades():

    #Objeto instanciado da classe SQLAInterface para modelo de dados
    model_cidades = SQLAInterface(Cidades, db.session())

    #Objeto instanciado da classe Filters, que automatiza a criação de filtros
    filters = Filters(BaseFilterConverter, model_cidades)

    #Objeto de fomrulário
    form = FormSearchCidades()

    #O endpoint pode ser acionado via GET e POST

    if request.method == 'POST':
        #Quando o usuário clicar no botão pesquisar, o metodo POST é acionado.
        #Neste momento é possível fazer a leitura dos inputs do objeto form.
        qry_cidade = form.nome_cidade.data

    #Se o botão pesquisar foi clicado, validate_on_submit() retorna True.
    if form.validate_on_submit():

        #Criação do filtro SQL
        filters.add_filter('nome_cidade',FilterStartsWith, \
                                                    qry_cidade)
        #Executa a query no banco de dados
        qt, cidades = model_cidades.query(filters)

        #Renderiza o template passando os objetos de dados e de form
        return render_template('testes/lista_cidades.html', \
                              dados=cidades, form=form)

    if request.method == 'GET':
        return render_template('testes/lista_cidades.html', \
                                          dados=[], form=form)
Exemple #3
0
class NotasFiscaisView(ModelView):
    datamodel = SQLAInterface(NotasFiscais)

    list_columns = [
        'chave_nfe', 'numero_nota_fiscal', 'data_emissao', 'valor',
        'remetente.nome_cliente'
    ]
Exemple #4
0
class Filial(ModelSoftlogView):
    datamodel = SQLAInterface(Filial)

    list_columns = ['razao_social', 'cnpj', 'codigo_filial', 'codigo_empresa']

    add_columns = ['razao_social', 'cnpj', 'codigo_filial', 'codigo_empresa']

    show_columns = edit_columns = add_columns

    start_empty = False
Exemple #5
0
class EmpresasBrasil(ModelSoftlogView):
    datamodel = SQLAInterface(ClientesBaseSefaz)
    base_permissions = ['Listar', 'Visualizar']

    list_columns = [
        'cnpj', 'razao_social', 'situacao', 'endereco', 'numero',
        'complemento', 'bairro', 'nome_cidade', 'cep', 'telefones', 'email',
        'cnae', 'inicio_atividade'
    ]

    show_coluns = list_columns
Exemple #6
0
    def __init__(self, appbuilder):
        """
            SecurityManager contructor
            param appbuilder:              
        """
        super(SecurityManager, self).__init__(appbuilder)
        user_datamodel = SQLAInterface(self.user_model)

        if self.auth_type == c.AUTH_DB:
            self.userdbmodelview.datamodel = user_datamodel
        #elif self.auth_type == c.AUTH_LDAP:
        #    self.userldapmodelview.datamodel = user_datamodel
        #elif self.auth_type == c.AUTH_OID:
        #    self.useroidmodelview.datamodel = user_datamodel
        #elif self.auth_type == c.AUTH_OAUTH:
        #    self.useroauthmodelview.datamodel = user_datamodel
        #elif self.auth_type == c.AUTH_REMOTE_USER:
        #    self.userremoteusermodelview.datamodel = user_datamodel
        #elif self.auth_type == c.AUTH_DB_TNY:
        #    self.userdbmodelview.datamodel = user_datamodel

        self.userstatschartview.datamodel = user_datamodel
        if self.auth_user_registration:
            self.registerusermodelview.datamodel = SQLAInterface(
                self.registeruser_model)

        self.rolemodelview.datamodel = SQLAInterface(self.role_model)
        self.permissionmodelview.datamodel = SQLAInterface(
            self.permission_model)
        self.viewmenumodelview.datamodel = SQLAInterface(self.viewmenu_model)
        self.permissionviewmodelview.datamodel = SQLAInterface(
            self.permissionview_model)
        #self.stringconexoesview.datamodel = SQLAInterface(self.stringconexoes_model)
        #self.cidadeview.datamodel = SQLAInterface(self.cidade_model)
        #self.filialview.datamodel = SQLAInterface(self.filial_model)
        #self.empresaview.datamodel = SQLAInterface(self.empresa_model)
        super(SecurityManager, self).__init__(appbuilder)
Exemple #7
0

class Regiao(Model):
    __tablename__ = "regiao"

    id_regiao = Column(Integer(),Sequence('regiao_id_regiao_seq'),primary_key=True )
    descricao = Column(String(35))
    id_cidade_polo = Column(Integer(), ForeignKey("cidades.id_cidade"))
    cidade = relationship(Cidades)

    def __repr__(self):
        return self.descricao or ""

if __name__ == '__main__':

    model_nf = SQLAInterface(NotasFiscais,db.session)

    nf = model_nf.get(477224)

    model_regiao = SQLAInterface(Regiao, db.session)

    qt, regioes = model_regiao.query()

    for regiao in regioes:
        #regiao.descricao = regiao.descricao.upper()
        #model_regiao.edit(regiao)
        print(regiao.descricao)



Exemple #8
0
class UsuariosView(ModelView):
    datamodel = SQLAInterface(User)

    list_columns = ['nome_usuario', 'email', 'login_name', 'id_usuario']
Exemple #9
0
class AppBuilder(object):
    """
        Classe principal do Builder de Aplicação. Responsável por 
        registrar as views, inicializar base de permissões.
    """
    baseviews = []
    security_manager_class = None
    # Flask app
    app = None
    # Database Session
    session = None
    # Security Manager Class
    sm = None
    # Babel Manager Class
    bm = None
    # dict with addon name has key and intantiated class has value
    addon_managers = None
    # temporary list that hold addon_managers config key
    _addon_managers = None

    baseviews = []
    menu = None
    endpoint = None
    route_base = None
    indexview = None
    template_folder = None
    static_folder = None
    static_url_path = None

    template_filters = None

    model_string_conexoes = None

    string_conexoes = None

    uris = None

    def __init__(self, app, session):

        self.menu = Menu()
        self.app = app
        self.session = session
        self.base_template = "frontend/base_onepage.html"
        self.template_folder = "templates"
        self.static_folder = "files/static"
        self.security_manager_class = SecurityManager
        self.sm = self.security_manager_class(self)
        self.indexview = IndexView
        self._add_admin_views()
        #self._add_addon_views()
        self._add_menu_permissions()
        self._add_global_filters()
        self.model_string_conexoes = SQLAInterface(StringConexoes, session)
        #session.close()

        #self._add_uris()

        if not self.app:
            for baseview in self.baseviews:
                # instantiate the views and add session
                self._check_and_init(baseview)
                # Register the views has blueprints
                self.register_blueprint(baseview)
                # Add missing permissions where needed
                self._add_permission(baseview)

    def _check_and_init(self, baseview):
        # If class if not instantiated, instantiate it
        # and add db session from security models.
        if hasattr(baseview, 'datamodel'):
            if baseview.datamodel.session is None:
                baseview.datamodel.session = self.session
        if hasattr(baseview, '__call__'):
            baseview = baseview()
        return baseview

    def add_view(self,
                 baseview,
                 name,
                 href="",
                 icon="",
                 label="",
                 category="",
                 category_icon="",
                 category_label=""):
        """
        Add your views associated with menus using this method.

        :param baseview:
            A BaseView type class instantiated or not.
            This method will instantiate the class for you if needed.
        :param name:
            The string name that identifies the menu.
        :param href:
            Override the generated href for the menu.
            You can use an url string or an endpoint name
            if non provided default_view from view will be set as href.
        :param icon:
            Font-Awesome icon name, optional.
        :param label:
            The label that will be displayed on the menu,
            if absent param name will be used
        :param category:
            The menu category where the menu will be included,
            if non provided the view will be acessible as a top menu.
        :param category_icon:
            Font-Awesome icon name for the category, optional.
        :param category_label:
            The label that will be displayed on the menu,
            if absent param name will be used

        Examples::

            appbuilder = AppBuilder(app, db)
            # Register a view, rendering a top menu without icon.
            appbuilder.add_view(MyModelView(), "My View")
            # or not instantiated
            appbuilder.add_view(MyModelView, "My View")
            # Register a view, a submenu "Other View" from "Other" with a phone icon.
            appbuilder.add_view(MyOtherModelView, "Other View", icon='fa-phone', category="Others")
            # Register a view, with category icon and translation.
            appbuilder.add_view(YetOtherModelView(), "Other View", icon='fa-phone',
                            label=_('Other View'), category="Others", category_icon='fa-envelop',
                            category_label=_('Other View'))
            # Add a link
            appbuilder.add_link("google", href="www.google.com", icon = "fa-google-plus")
        """
        baseview = self._check_and_init(baseview)
        #log.info(LOGMSG_INF_FAB_ADD_VIEW.format(baseview.__class__.__name__, name))

        if not self._view_exists(baseview):
            baseview.appbuilder = self
            self.baseviews.append(baseview)
            #self._process_inner_views()
            if self.app:
                self.register_blueprint(baseview)
                self._add_permission(baseview)
        self.add_link(name=name,
                      href=href,
                      icon=icon,
                      label=label,
                      category=category,
                      category_icon=category_icon,
                      category_label=category_label,
                      baseview=baseview)
        return baseview

    def add_link(self,
                 name,
                 href,
                 icon="",
                 label="",
                 category="",
                 category_icon="",
                 category_label="",
                 baseview=None):
        """
            Add your own links to menu using this method

            :param name:
                The string name that identifies the menu.
            :param href:
                Override the generated href for the menu.
                You can use an url string or an endpoint name
            :param icon:
                Font-Awesome icon name, optional.
            :param label:
                The label that will be displayed on the menu,
                if absent param name will be used
            :param category:
                The menu category where the menu will be included,
                if non provided the view will be accessible as a top menu.
            :param category_icon:
                Font-Awesome icon name for the category, optional.
            :param category_label:
                The label that will be displayed on the menu,
                if absent param name will be used

        """
        self.menu.add_link(name=name,
                           href=href,
                           icon=icon,
                           label=label,
                           category=category,
                           category_icon=category_icon,
                           category_label=category_label,
                           baseview=baseview)
        if self.app:
            self._add_permissions_menu(name)
            if category:
                self._add_permissions_menu(category)

    def add_view_no_menu(self, baseview, endpoint=None, static_folder=None):
        """
            Add your views without creating a menu.

            :param baseview:
                A BaseView type class instantiated.

        """
        baseview = self._check_and_init(baseview)
        #log.info(LOGMSG_INF_FAB_ADD_VIEW.format(baseview.__class__.__name__, ""))

        if not self._view_exists(baseview):
            baseview.appbuilder = self
            self.baseviews.append(baseview)
            #self._process_inner_views()
            if self.app:
                self.register_blueprint(baseview,
                                        endpoint=endpoint,
                                        static_folder=static_folder)
                #self._add_permission(baseview)
        #else:
        #log.warning(LOGMSG_WAR_FAB_VIEW_EXISTS.format(baseview.__class__.__name__))
        return baseview

    def _view_exists(self, view):
        for baseview in self.baseviews:
            if baseview.__class__ == view.__class__:
                return True
        return False

    def register_blueprint(self, baseview, endpoint=None, static_folder=None):
        self.get_app.register_blueprint(
            baseview.create_blueprint(self,
                                      endpoint=endpoint,
                                      static_folder=static_folder))

    def _add_uris(self):
        qt, string_conexoes = self.model_string_conexoes.query()

        uris = {}
        for s in string_conexoes:
            uri = 'postgresql://%s:%s@%s:%s/%s' % (
                s.usuario.strip(), s.senha.strip(), s.host.strip(), str(
                    s.port), s.banco_dados.strip())

            uris[str(s.id_string_conexao)] = uri

    def get_uri(self, id_string_conexao):

        return self.uris.get(str(id_string_conexao))

    def _add_global_filters(self):
        self.template_filters = TemplateFilters(self.get_app, self.sm)

    @property
    def get_app(self):
        """
            Get current or configured flask app

            :return: Flask App
        """
        if self.app:
            return self.app
        else:
            return current_app

    @property
    def get_session(self):
        """
            Get the current sqlalchemy session.

            :return: SQLAlchemy Session
        """
        return self.session

    @property
    def app_name(self):
        """
            Get the App name

            :return: String with app name
        """
        return self.get_app.config['APP_NAME']

    @property
    def app_theme(self):
        """
            Get the App theme name

            :return: String app theme name
        """
        return self.get_app.config['APP_THEME']

    @property
    def app_icon(self):
        """
            Get the App icon location

            :return: String with relative app icon location
        """
        return self.get_app.config['APP_ICON']

    @property
    def languages(self):
        return self.get_app.config['LANGUAGES']

    @property
    def version(self):
        """
            Get the current F.A.B. version

            :return: String with the current F.A.B. version
        """
        return VERSION_STRING

    def _add_admin_views(self):
        """
            Registers indexview, utilview (back function), babel views and Security views.
        """
        self.indexview = self._check_and_init(self.indexview)
        self.add_view_no_menu(self.indexview)
        self.add_view_no_menu(UtilView())
        #self.bm.register_views()
        self.sm.register_views()

    @property
    def get_url_for_index(self):
        return url_for('%s.%s' %
                       (self.indexview.endpoint, self.indexview.default_view))

    @property
    def get_url_for_userinfo(self):
        return url_for('%s.%s' % (self.sm.user_view.endpoint, 'userinfo'))

    def _add_permission(self, baseview):
        self.sm.add_permissions_view(baseview.base_permissions,
                                     baseview.__class__.__name__)
        try:
            pass
            #self.sm.add_permissions_view(baseview.base_permissions, baseview.__class__.__name__)
        except Exception as e:
            log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW.format(str(e)))

    def _add_permissions_menu(self, name):
        try:
            self.sm.add_permissions_menu(name)
        except Exception as e:
            log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_MENU.format(str(e)))

    def _add_menu_permissions(self):
        for category in self.menu.get_list():
            self._add_permissions_menu(category.name)
            for item in category.childs:
                #dont add permission for menu separator
                if item.name != '-':
                    self._add_permissions_menu(item.name)