Example #1
0
def _provide_app_info():
    from stoqlib.lib.interfaces import IAppInfo
    from stoqlib.lib.appinfo import AppInfo
    info = AppInfo()
    info.set(u"name", u"Stoqlib")
    info.set(u"version", u"1.0.0")
    provide_utility(IAppInfo, info)
Example #2
0
    def _check_user(self, username, password):
        username = unicode(username)
        password = unicode(password)
        # This function is really just a post-validation item.
        default_store = api.get_default_store()
        user = default_store.find(LoginUser, username=username).one()
        if not user:
            raise LoginError(_("Invalid user or password"))

        if not user.is_active:
            raise LoginError(_('This user is inactive'))

        branch = api.get_current_branch(default_store)
        # current_branch may not be set if we are registering a new station
        if branch and not user.has_access_to(branch):
            raise LoginError(_('This user does not have access to this branch'))

        if user.pw_hash != password:
            raise LoginError(_("Invalid user or password"))

        # Dont know why, but some users have this empty. Prevent user from
        # login in, since it will break later
        if not user.profile:
            msg = (_("User '%s' has no profile set, "
                     "but this should not happen.") % user.username + '\n\n' +
                   _("Please contact your system administrator or Stoq team."))
            warning(msg)
            raise LoginError(_("User does not have a profile"))

        user.login()

        # ICurrentUser might already be provided which is the case when
        # creating a new database, thus we need to replace it.
        provide_utility(ICurrentUser, user, replace=True)
        return user
Example #3
0
 def _setup_domain_slave_mapper(self):
     from kiwi.component import provide_utility
     from stoqlib.gui.interfaces import IDomainSlaveMapper
     from stoqlib.gui.slaves.domainslavemapper import DefaultDomainSlaveMapper
     provide_utility(IDomainSlaveMapper,
                     DefaultDomainSlaveMapper(),
                     replace=True)
Example #4
0
    def _setup_ui_dialogs(self):
        # This needs to be here otherwise we can't install the dialog
        if 'STOQ_TEST_MODE' in os.environ:
            return
        log.debug('providing graphical notification dialogs')
        from stoqlib.gui.base.dialogs import DialogSystemNotifier
        from stoqlib.lib.interfaces import ISystemNotifier
        from kiwi.component import provide_utility
        provide_utility(ISystemNotifier, DialogSystemNotifier(), replace=True)

        import gtk
        from kiwi.environ import environ
        from kiwi.ui.pixbufutils import pixbuf_from_string
        data = environ.get_resource_string(
            'stoq', 'pixmaps', 'stoq-stock-app-24x24.png')
        gtk.window_set_default_icon(pixbuf_from_string(data))

        if platform.system() == 'Darwin':
            from AppKit import NSApplication, NSData, NSImage
            bytes = environ.get_resource_string(
                'stoq', 'pixmaps', 'stoq-stock-app-48x48.png')
            data = NSData.alloc().initWithBytes_length_(bytes, len(bytes))
            icon = NSImage.alloc().initWithData_(data)
            app = NSApplication.sharedApplication()
            app.setApplicationIconImage_(icon)
Example #5
0
def _provide_app_info():
    from stoqlib.lib.interfaces import IAppInfo
    from stoqlib.lib.appinfo import AppInfo
    info = AppInfo()
    info.set(u"name", u"Stoqlib")
    info.set(u"version", u"1.0.0")
    provide_utility(IAppInfo, info)
Example #6
0
    def _setup_ui_dialogs(self):
        # This needs to be here otherwise we can't install the dialog
        if 'STOQ_TEST_MODE' in os.environ:
            return
        log.debug('providing graphical notification dialogs')
        from stoqlib.gui.base.dialogs import DialogSystemNotifier
        from stoqlib.lib.interfaces import ISystemNotifier
        from kiwi.component import provide_utility
        provide_utility(ISystemNotifier, DialogSystemNotifier(), replace=True)

        from gi.repository import Gtk
        from kiwi.environ import environ
        from kiwi.ui.pixbufutils import pixbuf_from_string
        data = environ.get_resource_string('stoq', 'pixmaps',
                                           'stoq-stock-app-24x24.png')
        Gtk.Window.set_default_icon(pixbuf_from_string(data))

        if platform.system() == 'Darwin':
            from AppKit import NSApplication, NSData, NSImage
            bytes = environ.get_resource_string('stoq', 'pixmaps',
                                                'stoq-stock-app-48x48.png')
            data = NSData.alloc().initWithBytes_length_(bytes, len(bytes))
            icon = NSImage.alloc().initWithData_(data)
            app = NSApplication.sharedApplication()
            app.setApplicationIconImage_(icon)
Example #7
0
    def _setup_ui_dialogs(self):
        # This needs to be here otherwise we can't install the dialog
        if 'STOQ_TEST_MODE' in os.environ:
            return
        log.debug('providing graphical notification dialogs')
        from stoqlib.gui.base.dialogs import DialogSystemNotifier
        from stoqlib.lib.interfaces import ISystemNotifier
        from kiwi.component import provide_utility
        provide_utility(ISystemNotifier, DialogSystemNotifier(), replace=True)

        from gi.repository import Gtk
        from kiwi.environ import environ
        from stoqlib.gui.stockicons import STOQ_LAUNCHER
        Gtk.Window.set_default_icon_name(STOQ_LAUNCHER)

        if platform.system() == 'Darwin':
            from AppKit import NSApplication, NSData, NSImage
            # FIXME: This should be a 48x48 icon
            data = environ.get_resource_string('stoq', 'pixmaps', 'hicolor',
                                               '24x24', 'actions',
                                               'stoq-launcher.png')
            data = NSData.alloc().initWithBytes_length_(data, len(data))
            icon = NSImage.alloc().initWithData_(data)
            app = NSApplication.sharedApplication()
            app.setApplicationIconImage_(icon)
Example #8
0
    def _check_user(self, username, pw_hash):
        username = str(username)
        pw_hash = str(pw_hash)
        # This function is really just a post-validation item.
        default_store = api.get_default_store()
        current_branch = api.get_current_branch(default_store)

        user = LoginUser.authenticate(default_store, username, pw_hash,
                                      current_branch)

        # Dont know why, but some users have this empty. Prevent user from
        # login in, since it will break later
        if not user.profile:
            msg = (_("User '%s' has no profile set, "
                     "but this should not happen.") % user.username + '\n\n' +
                   _("Please contact your system administrator or Stoq team."))
            warning(msg)
            raise LoginError(_("User does not have a profile"))

        user.login()

        # ICurrentUser might already be provided which is the case when
        # creating a new database, thus we need to replace it.
        provide_utility(ICurrentUser, user, replace=True)
        return user
Example #9
0
def set_current_branch_station(store, station_name):
    """Registers the current station and the branch of the station
    as the current branch for the system
    :param store: a store
    :param station_name: name of the station to register
    """
    # This is called from stoq-daemon, which doesn't know about Branch yet
    from stoqlib.domain.person import Branch
    Branch  # pylint: disable=W0104

    if station_name is None:
        station_name = get_hostname()

    station_name = unicode(station_name)
    from stoqlib.domain.station import BranchStation
    station = store.find(BranchStation, name=station_name).one()
    if station is None:
        station = _register_branch(store, station_name)

    if not station.is_active:
        error(_("The computer <u>%s</u> is not active in Stoq") %
              station_name,
              _("To solve this, open the administrator application "
                "and re-activate this computer."))

    provide_utility(ICurrentBranchStation, station, replace=True)

    # The station may still not be associated with a branch when creating an
    # empty database
    if station.branch:
        provide_utility(ICurrentBranch, station.branch, replace=True)
Example #10
0
def get_payment_operation_manager():
    """Returns the payment operation manager"""
    pmm = get_utility(IPaymentOperationManager, None)

    if not pmm:
        from stoqlib.lib.payment import PaymentOperationManager
        pmm = PaymentOperationManager()
        provide_utility(IPaymentOperationManager, pmm)

        for method_name, klass in [
            (u'money', MoneyPaymentOperation),
            (u'check', CheckPaymentOperation),
            (u'bill', BillPaymentOperation),
            (u'card', CardPaymentOperation),
            (u'store_credit', StoreCreditPaymentOperation),
            (u'trade', TradePaymentOperation),
            (u'multiple', MultiplePaymentOperation),
            (u'deposit', DepositPaymentOperation),
            (u'online', OnlinePaymentOperation),
            (u'credit', CreditPaymentOperation),
        ]:
            pmm.register(method_name, klass())
        # Also, register InvalidPaymentOperation as a fallback operation
        pmm.register_fallback(InvalidPaymentOperation())

    return pmm
Example #11
0
    def wrapper(*args, **kwargs):
        session_id = request.headers.get('stoq-session', None)
        # FIXME: Remove this once all frontends are updated.
        if session_id is None:
            abort(401, 'No session id provided in header')

        user_id = request.headers.get('stoq-user', None)
        with api.new_store() as store:
            user = store.get(LoginUser, user_id or session_id)
            if user:
                provide_utility(ICurrentUser, user, replace=True)
                return f(*args, **kwargs)

        with _get_session() as s:
            session_data = s.get(session_id, None)
            if session_data is None:
                abort(401, 'Session does not exist')

            if localnow() - session_data['date'] > _expire_time:
                abort(401, 'Session expired')

            # Refresh last date to avoid it expiring while being used
            session_data['date'] = localnow()
            session['user_id'] = session_data['user_id']
            with api.new_store() as store:
                user = store.get(LoginUser, session['user_id'])
                provide_utility(ICurrentUser, user, replace=True)

        return f(*args, **kwargs)
Example #12
0
def _provide_current_station(station_name=None, branch_name=None):
    if not station_name:
        station_name = get_hostname()
    store = new_store()
    if branch_name:
        branch = store.find(
            Person,
            And(Person.name == branch_name,
                Branch.person_id == Person.id)).one()
    else:
        branches = store.find(Branch)
        if branches.count() == 0:
            person = Person(name=u"test", store=store)
            branch = Branch(person=person, store=store)
        else:
            branch = branches[0]

    provide_utility(ICurrentBranch, branch)

    station = BranchStation.get_station(store, branch, station_name)
    if not station:
        station = BranchStation.create(store, branch, station_name)

    assert station
    assert station.is_active

    provide_utility(ICurrentBranchStation, station)
    store.commit(close=True)
Example #13
0
    def _check_user(self, username, pw_hash):
        username = unicode(username)
        pw_hash = unicode(pw_hash)
        # This function is really just a post-validation item.
        default_store = api.get_default_store()
        current_branch = api.get_current_branch(default_store)

        user = LoginUser.authenticate(default_store, username, pw_hash,
                                      current_branch)

        # Dont know why, but some users have this empty. Prevent user from
        # login in, since it will break later
        if not user.profile:
            msg = (_("User '%s' has no profile set, "
                     "but this should not happen.") % user.username + '\n\n' +
                   _("Please contact your system administrator or Stoq team."))
            warning(msg)
            raise LoginError(_("User does not have a profile"))

        user.login()

        # ICurrentUser might already be provided which is the case when
        # creating a new database, thus we need to replace it.
        provide_utility(ICurrentUser, user, replace=True)
        return user
Example #14
0
def get_payment_operation_manager():
    """Returns the payment operation manager"""
    pmm = get_utility(IPaymentOperationManager, None)

    if not pmm:
        from stoqlib.lib.payment import PaymentOperationManager
        pmm = PaymentOperationManager()
        provide_utility(IPaymentOperationManager, pmm)

        for method_name, klass in [
                (u'money', MoneyPaymentOperation),
                (u'check', CheckPaymentOperation),
                (u'bill', BillPaymentOperation),
                (u'card', CardPaymentOperation),
                (u'store_credit', StoreCreditPaymentOperation),
                (u'trade', TradePaymentOperation),
                (u'multiple', MultiplePaymentOperation),
                (u'deposit', DepositPaymentOperation),
                (u'online', OnlinePaymentOperation),
                (u'credit', CreditPaymentOperation),
        ]:
            pmm.register(method_name, klass())
        # Also, register InvalidPaymentOperation as a fallback operation
        pmm.register_fallback(InvalidPaymentOperation())

    return pmm
Example #15
0
def _provide_current_station(station_name=None, branch_name=None):
    if not station_name:
        station_name = get_hostname()
    store = new_store()
    if branch_name:
        branch = store.find(Person,
                            And(Person.name == branch_name,
                                Branch.person_id == Person.id)).one()
    else:
        branches = store.find(Branch)
        if branches.count() == 0:
            person = Person(name=u"test", store=store)
            branch = Branch(person=person, store=store)
        else:
            branch = branches[0]

    provide_utility(ICurrentBranch, branch)

    station = BranchStation.get_station(store, branch, station_name)
    if not station:
        station = BranchStation.create(store, branch, station_name)

    assert station
    assert station.is_active

    provide_utility(ICurrentBranchStation, station)
    store.commit(close=True)
Example #16
0
    def get_permission_manager(cls):
        """Returns the payment operation manager"""
        pm = get_utility(IPermissionManager, None)

        if not pm:
            pm = PermissionManager()
            provide_utility(IPermissionManager, pm)
        return pm
Example #17
0
    def get_permission_manager(cls):
        """Returns the payment operation manager"""
        pm = get_utility(IPermissionManager, None)

        if not pm:
            pm = PermissionManager()
            provide_utility(IPermissionManager, pm)
        return pm
Example #18
0
    def init(self):
        self.__main_window = self.service.boss.get_main_window()
        self.__gazpacho = gazpacho_application(self.__main_window, self)

        from gazpacho.interfaces import IGazpachoApp
        from kiwi.component import provide_utility
        provide_utility(IGazpachoApp, self.__gazpacho)
        self.widget.pack_start(self.__gazpacho.get_container())
        self.set_border_width(2)
Example #19
0
 def _setup_cookiefile(self):
     log.debug('setting up cookie file')
     from kiwi.component import provide_utility
     from stoqlib.lib.cookie import Base64CookieFile
     from stoqlib.lib.interfaces import ICookieFile
     from stoqlib.lib.osutils import get_application_dir
     app_dir = get_application_dir()
     cookiefile = os.path.join(app_dir, "cookie")
     provide_utility(ICookieFile, Base64CookieFile(cookiefile))
Example #20
0
def _set_person_utilities():
    store = new_store()
    branch = sysparam.get_object(store, 'MAIN_COMPANY')
    provide_utility(ICurrentBranch, branch)

    station = BranchStation(name=u"Stoqlib station", branch=branch,
                            store=store, is_active=True)
    provide_utility(ICurrentBranchStation, station)
    store.commit(close=True)
Example #21
0
 def _setup_cookiefile(self):
     log.debug('setting up cookie file')
     from kiwi.component import provide_utility
     from stoqlib.lib.cookie import Base64CookieFile
     from stoqlib.lib.interfaces import ICookieFile
     from stoqlib.lib.osutils import get_application_dir
     app_dir = get_application_dir()
     cookiefile = os.path.join(app_dir, "cookie")
     provide_utility(ICookieFile, Base64CookieFile(cookiefile))
Example #22
0
 def init(self):
     self.__main_window = self.service.boss.get_main_window()
     self.__gazpacho = gazpacho_application(self.__main_window, self)
     
     from gazpacho.interfaces import IGazpachoApp
     from kiwi.component import provide_utility
     provide_utility(IGazpachoApp, self.__gazpacho)
     self.widget.pack_start(self.__gazpacho.get_container())
     self.set_border_width(2)
Example #23
0
def _set_person_utilities():
    store = new_store()
    branch = sysparam.get_object(store, 'MAIN_COMPANY')
    provide_utility(ICurrentBranch, branch)

    station = BranchStation(name=u"Stoqlib station", branch=branch,
                            store=store, is_active=True)
    provide_utility(ICurrentBranchStation, station)
    store.commit(close=True)
Example #24
0
    def tearDown(self):
        provide_utility(IAppInfo, self._iappinfo, replace=True)
        # Shell.bootstrap calls
        # ProxyLabel.replace('$CURRENCY', get_localeconv()['currency_symbol'])
        # and that will break uitests
        if '$CURRENCY' in ProxyLabel._label_replacements:
            del ProxyLabel._label_replacements['$CURRENCY']

        for mocked in self._mocks:
            mocked.stop()
Example #25
0
def setup_stoq():
    info = AppInfo()
    info.set('name', "stoqserver")
    info.set('version', stoqserver.version_str)
    info.set('ver', stoqserver.version_str)
    provide_utility(IAppInfo, info, replace=True)

    # FIXME: Maybe we should check_schema and load plugins here?
    setup(config=get_config(), options=None, register_station=False,
          check_schema=False, load_plugins=True)
Example #26
0
    def _setup_stoq(self):
        info = AppInfo()
        info.set('name', "stoqserver")
        info.set('version', stoqserver.version_str)
        info.set('ver', stoqserver.version_str)
        provide_utility(IAppInfo, info, replace=True)

        # FIXME: Maybe we should check_schema and load plugins here?
        setup(config=get_config(), options=None, register_station=False,
              check_schema=False, load_plugins=True)
Example #27
0
    def tearDown(self):
        provide_utility(IAppInfo, self._iappinfo, replace=True)
        # Shell.bootstrap calls
        # ProxyLabel.replace('$CURRENCY', get_localeconv()['currency_symbol'])
        # and that will break uitests
        if '$CURRENCY' in ProxyLabel._label_replacements:
            del ProxyLabel._label_replacements['$CURRENCY']

        for mocked in self._mocks:
            mocked.stop()
Example #28
0
def get_payment_operation_manager():
    """Returns the payment operation manager"""
    pmm = get_utility(IPaymentOperationManager, None)

    if not pmm:
        from stoqlib.lib.payment import PaymentOperationManager
        pmm = PaymentOperationManager()
        provide_utility(IPaymentOperationManager, pmm)

    return pmm
Example #29
0
    def test_only_admin_can_add_client_credit(self):
        client = self.create_client()
        editor = ClientEditor(self.store, client, role_type=Person.ROLE_INDIVIDUAL)
        self.check_editor(editor, "client-editor-admin-user")

        admin_user = api.get_current_user(self.store)
        salesperson_user = self.store.find(LoginUser, username=u"elias").one()
        provide_utility(ICurrentUser, salesperson_user, replace=True)
        editor = ClientEditor(self.store, client, role_type=Person.ROLE_INDIVIDUAL)
        self.check_editor(editor, "client-editor-salesperson-user")
        provide_utility(ICurrentUser, admin_user, replace=True)
Example #30
0
    def testZopeInterface(self):
        try:
            from zope.interface import Interface
        except ImportError:
            return

        class IApple(Interface):
            pass

        self.assertRaises(NotImplementedError, get_utility, IApple)
        provide_utility(IApple, o)
        self.assertRaises(AlreadyImplementedError, provide_utility, IApple, o)
Example #31
0
def launch(options, filenames=[]):
    if options.debug:
        print 'Loading gazpacho'

    # Delay imports, so command line parsing is not slowed down
    from kiwi.component import provide_utility
    from gazpacho.interfaces import IGazpachoApp, IPluginManager
    from gazpacho.app.app import Application
    from gazpacho.app.debugwindow import DebugWindow, show
    from gazpacho.plugins import PluginManager

    plugin_manager = PluginManager()
    provide_utility(IPluginManager, plugin_manager)

    gazpacho = Application()
    provide_utility(IGazpachoApp, gazpacho)

    if options.debug:
        sys.excepthook = debug_hook
    else:
        DebugWindow.application = gazpacho
        sys.excepthook = show

    for filename in filenames:
        if not os.path.exists(filename):
            raise SystemExit('%s: no such a file or directory' % filename)

        if not os.access(filename, os.R_OK):
            raise SystemExit('Could not open file %s: Permission denied.' %
                             filename)
        open_project(gazpacho, filename, options.profile)

    if options.update:
        for project in gazpacho.get_projects():
            project.save(project.path)

        return

    # If no filenames were specified, open up an empty project
    if not filenames:
        gazpacho.new_project()

    if options.batch:
        run_batch(gazpacho, options.batch)
        return
    elif options.console:
        run_console(gazpacho)
        return

    if options.debug:
        print 'Running gazpacho'

    gazpacho.run()
Example #32
0
    def testZopeInterface(self):
        try:
            from zope.interface import Interface
        except ImportError:
            return

        class IApple(Interface):
            pass

        self.assertRaises(NotImplementedError, get_utility, IApple)
        provide_utility(IApple, o)
        self.assertRaises(AlreadyImplementedError, provide_utility, IApple, o)
Example #33
0
 def _provide_app_info(self):
     # FIXME: The webservice need the IAppInfo provided to get the stoq
     # version. We cannot do that workaround there because we don't want to
     # import stoq inside stoqlib. Either way, this code to download the
     # egg will move to the plugin dialog soon.
     from kiwi.component import provide_utility
     from stoqlib.lib.appinfo import AppInfo
     from stoqlib.lib.interfaces import IAppInfo
     import stoq
     info = AppInfo()
     info.set("version", stoq.version)
     provide_utility(IAppInfo, info)
Example #34
0
 def _provide_app_info(self):
     # FIXME: The webservice need the IAppInfo provided to get the stoq
     # version. We cannot do that workaround there because we don't want to
     # import stoq inside stoqlib. Either way, this code to download the
     # egg will move to the plugin dialog soon.
     from kiwi.component import provide_utility
     from stoqlib.lib.appinfo import AppInfo
     from stoqlib.lib.interfaces import IAppInfo
     import stoq
     info = AppInfo()
     info.set("version", stoq.version)
     provide_utility(IAppInfo, info)
Example #35
0
    def testOnlyAdminCanAddClientCredit(self):
        client = self.create_client()
        editor = ClientEditor(self.store, client,
                              role_type=Person.ROLE_INDIVIDUAL)
        self.check_editor(editor, 'client-editor-admin-user')

        admin_user = api.get_current_user(self.store)
        salesperson_user = self.store.find(LoginUser, username=u'elias').one()
        provide_utility(ICurrentUser, salesperson_user, replace=True)
        editor = ClientEditor(self.store, client,
                              role_type=Person.ROLE_INDIVIDUAL)
        self.check_editor(editor, 'client-editor-salesperson-user')
        provide_utility(ICurrentUser, admin_user, replace=True)
Example #36
0
    def post(self):
        username = self.get_arg('user')
        pw_hash = self.get_arg('pw_hash')

        with api.new_store() as store:
            try:
                # FIXME: Respect the branch the user is in.
                user = LoginUser.authenticate(store, username, pw_hash, current_branch=None)
                provide_utility(ICurrentUser, user, replace=True)
            except LoginError as e:
                abort(403, str(e))

        return user.id
Example #37
0
    def test_only_admin_can_add_client_credit(self):
        client = self.create_client()
        editor = ClientEditor(self.store, client,
                              role_type=Person.ROLE_INDIVIDUAL)
        self.check_editor(editor, 'client-editor-admin-user')

        admin_user = api.get_current_user(self.store)
        salesperson_user = self.store.find(LoginUser, username=u'elias').one()
        provide_utility(ICurrentUser, salesperson_user, replace=True)
        editor = ClientEditor(self.store, client,
                              role_type=Person.ROLE_INDIVIDUAL)
        self.check_editor(editor, 'client-editor-salesperson-user')
        provide_utility(ICurrentUser, admin_user, replace=True)
Example #38
0
def register_config(config):
    global _config
    _config = config

    try:
        provide_utility(IStoqConfig, config, replace=True)
    except NoConfigurationError:
        msg = _(u"Error: Stoq configuration is not avaiable. Check that the "
                "current user has a configuration file (~/.stoq/stoq.conf).")
        if os.geteuid() == 0:
            msg += _('\n\nYou are running stoq using sudo. That is not '
                     'recommended.')
        raise SystemExit(msg)
Example #39
0
def get_plugin_manager():
    """Provides and returns the plugin manager

    @attention: Try to always use this instead of getting the utillity
        by hand, as that could not have been provided before.

    :returns: an :class:`PluginManager` instance
    """
    manager = get_utility(IPluginManager, None)
    if not manager:
        manager = PluginManager()
        provide_utility(IPluginManager, manager)

    return manager
Example #40
0
def get_plugin_manager():
    """Provides and returns the plugin manager

    @attention: Try to always use this instead of getting the utillity
        by hand, as that could not have been provided before.

    :returns: an :class:`PluginManager` instance
    """
    manager = get_utility(IPluginManager, None)
    if not manager:
        manager = PluginManager()
        provide_utility(IPluginManager, manager)

    return manager
Example #41
0
def ensure_admin_user(administrator_password):
    log.info("Creating administrator user")

    default_store = get_default_store()
    user = get_admin_user(default_store)

    if user is None:
        store = new_store()
        person = Person(name=_(u'Administrator'), store=store)

        # Dependencies to create an user.
        role = EmployeeRole(name=_(u'System Administrator'), store=store)
        Individual(person=person, store=store)
        employee = Employee(person=person, role=role, store=store)
        EmployeeRoleHistory(store=store,
                            role=role,
                            employee=employee,
                            is_active=True,
                            salary=currency(800))

        # This is usefull when testing a initial database. Admin user actually
        # must have all the facets.
        SalesPerson(person=person, store=store)

        profile = store.find(UserProfile, name=_(u'Administrator')).one()
        # Backwards compatibility. this profile used to be in english
        # FIXME: Maybe its safe to assume the first profile in the table is
        # the admin.
        if not profile:
            profile = store.find(UserProfile, name=u'Administrator').one()

        log.info("Attaching a LoginUser (%s)" % (USER_ADMIN_DEFAULT_NAME, ))
        LoginUser(person=person,
                  username=USER_ADMIN_DEFAULT_NAME,
                  password=administrator_password,
                  profile=profile,
                  store=store)

        store.commit(close=True)

    # Fetch the user again, this time from the right connection
    user = get_admin_user(default_store)
    assert user

    user.set_password(administrator_password)

    # We can't provide the utility until it's actually in the database
    log.info('providing utility ICurrentUser')
    provide_utility(ICurrentUser, user)
Example #42
0
def setup_stoq(register_station=False, name='stoqserver', version=stoqserver.version_str):
    info = AppInfo()
    info.set('name', name)
    info.set('version', version)
    info.set('ver', version)
    provide_utility(IAppInfo, info, replace=True)

    setup(config=get_config(), options=None, register_station=register_station,
          check_schema=True, load_plugins=True)

    # This is needed for api calls that requires the current branch set,
    # e.g. Sale.confirm
    main_company = api.sysparam.get_object(
        api.get_default_store(), 'MAIN_COMPANY')
    provide_utility(ICurrentBranch, main_company, replace=True)
Example #43
0
File: admin.py Project: romaia/stoq
def ensure_admin_user(administrator_password):
    log.info("Creating administrator user")

    default_store = get_default_store()
    user = get_admin_user(default_store)

    if user is None:
        store = new_store()
        person = Person(name=_(u'Administrator'), store=store)

        # Dependencies to create an user.
        role = EmployeeRole(name=_(u'System Administrator'), store=store)
        Individual(person=person, store=store)
        employee = Employee(person=person, role=role, store=store)
        EmployeeRoleHistory(store=store,
                            role=role,
                            employee=employee,
                            is_active=True,
                            salary=currency(800))

        # This is usefull when testing a initial database. Admin user actually
        # must have all the facets.
        SalesPerson(person=person, store=store)

        profile = store.find(UserProfile, name=_(u'Administrator')).one()
        # Backwards compatibility. this profile used to be in english
        # FIXME: Maybe its safe to assume the first profile in the table is
        # the admin.
        if not profile:
            profile = store.find(UserProfile, name=u'Administrator').one()

        log.info("Attaching a LoginUser (%s)" % (USER_ADMIN_DEFAULT_NAME, ))
        LoginUser(person=person,
                  username=USER_ADMIN_DEFAULT_NAME,
                  password=administrator_password,
                  profile=profile, store=store)

        store.commit(close=True)

    # Fetch the user again, this time from the right connection
    user = get_admin_user(default_store)
    assert user

    user.set_password(administrator_password)

    # We can't provide the utility until it's actually in the database
    log.info('providing utility ICurrentUser')
    provide_utility(ICurrentUser, user)
Example #44
0
File: admin.py Project: romaia/stoq
def create_main_branch(store, name):
    """Creates a new branch and sets it as the main branch for the system
    :param store: a store
    :param name: name of the new branch
    """
    person = Person(name=name, store=store)
    Company(person=person, store=store)
    branch = Branch(person=person, store=store)

    sysparam(store).MAIN_COMPANY = branch.id

    provide_utility(ICurrentBranch, branch)
    admin = get_admin_user(store)
    admin.add_access_to(branch)

    return branch
Example #45
0
def create_main_branch(store, name):
    """Creates a new branch and sets it as the main branch for the system
    :param store: a store
    :param name: name of the new branch
    """
    person = Person(name=name, store=store)
    Company(person=person, store=store)
    branch = Branch(person=person, store=store)

    sysparam.set_object(store, 'MAIN_COMPANY', branch)

    provide_utility(ICurrentBranch, branch)
    admin = get_admin_user(store)
    admin.add_access_to(branch)

    return branch
Example #46
0
def setup_stoq(register_station=False, name='stoqserver', version=stoqserver.version_str):
    info = AppInfo()
    info.set('name', name)
    info.set('version', version)
    info.set('ver', version)
    provide_utility(IAppInfo, info, replace=True)

    # FIXME: Maybe we should check_schema and load plugins here?
    setup(config=get_config(), options=None, register_station=register_station,
          check_schema=False, load_plugins=True)

    # This is needed for api calls that requires the current branch set,
    # e.g. Sale.confirm
    main_company = api.sysparam.get_object(
        api.get_default_store(), 'MAIN_COMPANY')
    provide_utility(ICurrentBranch, main_company, replace=True)
Example #47
0
def provide_database_settings(dbname=None,
                              address=None,
                              port=None,
                              username=None,
                              password=None,
                              createdb=True):
    """
    Provide database settings.
    :param dbname:
    :param address:
    :param port:
    :param username:
    :param password:
    :param create: Create a new empty database if one is missing
    """
    if not username:
        username = get_username()
    if not dbname:
        dbname = username + u'_test'
    if not address:
        address = os.environ.get(u'PGHOST', u'')
    if not port:
        port = os.environ.get(u'PGPORT', u'5432')
    if not password:
        password = u""

    # Remove all old utilities pointing to the previous database.
    utilities.clean()
    provide_utility(ISystemNotifier, test_system_notifier, replace=True)
    _provide_application_descriptions()
    _provide_domain_slave_mapper()
    _provide_app_info()

    db_settings.address = address
    db_settings.port = port
    db_settings.dbname = dbname
    db_settings.username = username
    db_settings.password = password

    rv = False
    if createdb or not db_settings.database_exists(dbname):
        db_settings.clean_database(dbname, force=True)
        rv = True

    return rv
Example #48
0
    def _find_branch(self, branch_id=None):
        cnpj = self._format_cnpj(self._get_text('/infNFe/dest/CNPJ',
                                                self.root))
        if branch_id:
            branch = self.store.get(Branch, branch_id)
            if cnpj != branch.person.company.cnpj:
                raise NFeDifferentCNPJ(cnpj)
        try:
            person = Person.get_by_document(self.store, cnpj)
        except NotOneError:
            # Found multiple branchs with the same CNPJ, so we get it by id
            person = self.store.get(Branch,
                                    branch_id).person if branch_id else None

        if person is None:
            return None, cnpj

        provide_utility(ICurrentBranch, person.branch, replace=True)
        return person.branch, cnpj
Example #49
0
    def wrapper(*args, **kwargs):
        # token should be sent through a header using the format 'Bearer <token>'
        auth = request.headers.get('Authorization', '').split('Bearer ')
        if len(auth) != 2:
            abort(401)

        with api.new_store() as store:
            access_token = AccessToken.get_by_token(store=store, token=auth[1])
            if not access_token.is_valid():
                abort(403, "token {}".format(access_token.status))

            # FIXME: the user utility acts as a singleton and since we'd like to have stoqserver API
            # accepting requests from different stations (users), we cannot use this pattern as it
            # can lead to racing problems. For now we are willing to put a lock in every request,
            # but the final solution should be a refactor that makes every endpoint use the user
            # provided in the token payload instead of this 'global' one.
            user = store.get(LoginUser, access_token.payload['user_id'])
            provide_utility(ICurrentUser, user, replace=True)

        return f(*args, **kwargs)
Example #50
0
def provide_database_settings(dbname=None, address=None, port=None, username=None,
                              password=None, create=True):
    """
    Provide database settings.
    :param dbname:
    :param address:
    :param port:
    :param username:
    :param password:
    :param create: Create a new empty database if one is missing
    """
    if not username:
        username = get_username()
    if not dbname:
        dbname = username + u'_test'
    if not address:
        address = os.environ.get(u'PGHOST', u'')
    if not port:
        port = os.environ.get(u'PGPORT', u'5432')
    if not password:
        password = u""

    # Remove all old utilities pointing to the previous database.
    utilities.clean()
    provide_utility(ISystemNotifier, test_system_notifier, replace=True)
    _provide_application_descriptions()
    _provide_domain_slave_mapper()
    _provide_app_info()

    db_settings.address = address
    db_settings.port = port
    db_settings.dbname = dbname
    db_settings.username = username
    db_settings.password = password

    rv = False
    if create or not db_settings.database_exists(dbname):
        db_settings.clean_database(dbname, force=True)
        rv = True

    return rv
Example #51
0
 def _set_app_info(self):
     from kiwi.component import provide_utility
     from stoqlib.lib.appinfo import AppInfo
     from stoqlib.lib.kiwilibrary import library
     from stoqlib.lib.interfaces import IAppInfo
     import stoq
     # FIXME: use only stoq.stoq_version here and all other callsites of
     # IAppInfo
     stoq_version = stoq.version
     stoq_ver = stoq.stoq_version
     if hasattr(library, 'get_revision'):
         rev = library.get_revision()
         if rev is not None:
             stoq_version += ' ' + rev
             stoq_ver += (rev, )
     info = AppInfo()
     info.set("name", "Stoq")
     info.set("version", stoq_version)
     info.set("ver", stoq_ver)
     info.set("log", self._log_filename)
     provide_utility(IAppInfo, info)
Example #52
0
 def _set_app_info(self):
     from kiwi.component import provide_utility
     from stoqlib.lib.appinfo import AppInfo
     from stoqlib.lib.kiwilibrary import library
     from stoqlib.lib.interfaces import IAppInfo
     import stoq
     # FIXME: use only stoq.stoq_version here and all other callsites of
     # IAppInfo
     stoq_version = stoq.version
     stoq_ver = stoq.stoq_version
     if hasattr(library, 'get_revision'):
         rev = library.get_revision()
         if rev is not None:
             stoq_version += ' ' + rev
             stoq_ver += (rev,)
     info = AppInfo()
     info.set("name", "Stoq")
     info.set("version", stoq_version)
     info.set("ver", stoq_ver)
     info.set("log", self._log_filename)
     provide_utility(IAppInfo, info)
Example #53
0
def set_current_branch_station(store, station_name, confirm=True):
    """Registers the current station and the branch of the station
    as the current branch for the system
    :param store: a store
    :param station_name: name of the station to register
    """
    # This is called from stoq-daemon, which doesn't know about Branch yet
    from stoqlib.lib.parameters import sysparam
    from stoqlib.domain.person import Branch
    Branch  # pylint: disable=W0104

    if station_name is None:
        station_name = get_hostname()

    station_name = str(station_name)
    from stoqlib.domain.station import BranchStation
    station = store.find(BranchStation, name=station_name).one()
    if station is None:
        station = _register_branch_station(store,
                                           station_name,
                                           confirm=confirm)

    if not station.is_active:
        error(
            _("The computer <u>%s</u> is not active in Stoq") % station_name,
            _("To solve this, open the administrator application "
              "and re-activate this computer."))

    provide_utility(ICurrentBranchStation, station, replace=True)

    main_company = sysparam.get_object(store, 'MAIN_COMPANY')
    if not station.branch and main_company:
        with new_store() as commit_store:
            commit_station = commit_store.fetch(station)
            commit_station.branch = commit_store.fetch(main_company)

    # The station may still not be associated with a branch when creating an
    # empty database
    if station.branch:
        provide_utility(ICurrentBranch, station.branch, replace=True)
Example #54
0
    def _create_station(self, store):
        # FIXME: This is fishy, we can probably simplify this significantly by
        #        allowing users to connect to the initial database without
        #        having a branch station nor branch registered.
        #        The whole BranchStation/Branch creation is weird, it should
        #        be done at the same place.
        logger.info('_create_station')
        if self.create_examples:
            branch = api.sysparam.get_object(store, 'MAIN_COMPANY')
            assert branch
            provide_utility(ICurrentBranch, branch)
        else:
            branch = None

        station_name = get_hostname()
        if store.find(BranchStation, branch=branch, name=station_name).one():
            return
        station = BranchStation(store=store,
                                is_active=True,
                                branch=branch,
                                name=station_name)
        provide_utility(ICurrentBranchStation, station)
Example #55
0
    def _create_station(self, store):
        # FIXME: This is fishy, we can probably simplify this significantly by
        #        allowing users to connect to the initial database without
        #        having a branch station nor branch registered.
        #        The whole BranchStation/Branch creation is weird, it should
        #        be done at the same place.
        logger.info('_create_station')
        if self.create_examples:
            branch = api.sysparam.get_object(store, 'MAIN_COMPANY')
            assert branch
            provide_utility(ICurrentBranch, branch)
        else:
            branch = None

        station_name = get_hostname()
        if store.find(BranchStation, branch=branch, name=station_name).one():
            return
        station = BranchStation(store=store,
                                is_active=True,
                                branch=branch,
                                name=station_name)
        provide_utility(ICurrentBranchStation, station)
Example #56
0
def set_current_branch_station(store, station_name):
    """Registers the current station and the branch of the station
    as the current branch for the system
    :param store: a store
    :param station_name: name of the station to register
    """

    # This might be called early, so make sure SQLObject
    # knows about Branch which might not have
    # been imported yet
    from stoqlib.domain.person import Branch

    Branch  # pylint: disable=W0104

    if station_name is None:
        # For LTSP systems we cannot use the hostname as stoq is run
        # on a shared serve system. Instead the ip of the client system
        # is available in the LTSP_CLIENT environment variable
        station_name = os.environ.get("LTSP_CLIENT_HOSTNAME", None)
        if station_name is None:
            station_name = get_hostname()

    station_name = unicode(station_name)
    from stoqlib.domain.station import BranchStation

    station = store.find(BranchStation, name=station_name).one()
    if station is None:
        station = _register_branch(store, station_name)

    if not station.is_active:
        error(
            _("The computer <u>%s</u> is not active in Stoq") % station_name,
            _("To solve this, open the administrator application " "and re-activate this computer."),
        )

    provide_utility(ICurrentBranchStation, station, replace=True)

    if station.branch:
        provide_utility(ICurrentBranch, station.branch, replace=True)
Example #57
0
    def _download_plugin(self, manager, plugin_name):
        # FIXME: The webservice need the IAppInfo provided to get the stoq
        # version. We cannot do that workaround there because we don't want to
        # import stoq inside stoqlib. Either way, this code to download the
        # egg will move to the plugin dialog soon.
        from kiwi.component import provide_utility
        from stoqlib.lib.appinfo import AppInfo
        from stoqlib.lib.interfaces import IAppInfo
        import stoq
        info = AppInfo()
        info.set("version", stoq.version)
        provide_utility(IAppInfo, info)

        from twisted.internet import reactor
        d = manager.download_plugin(plugin_name)

        def stop_reactor(*args):
            if reactor.running:
                reactor.stop()

        d.addCallback(stop_reactor)
        d.addErrback(stop_reactor)
        reactor.run()
Example #58
0
    def _setup_ui_dialogs(self):
        # This needs to be here otherwise we can't install the dialog
        if 'STOQ_TEST_MODE' in os.environ:
            return
        log.debug('providing graphical notification dialogs')
        from stoqlib.gui.base.dialogs import DialogSystemNotifier
        from stoqlib.lib.interfaces import ISystemNotifier
        from kiwi.component import provide_utility
        provide_utility(ISystemNotifier, DialogSystemNotifier(), replace=True)

        from gi.repository import Gtk
        from kiwi.environ import environ
        from stoqlib.gui.stockicons import STOQ_LAUNCHER
        Gtk.Window.set_default_icon_name(STOQ_LAUNCHER)

        if platform.system() == 'Darwin':
            from AppKit import NSApplication, NSData, NSImage
            # FIXME: This should be a 48x48 icon
            data = environ.get_resource_string(
                'stoq', 'pixmaps', 'hicolor', '24x24', 'actions', 'stoq-launcher.png')
            data = NSData.alloc().initWithBytes_length_(data, len(data))
            icon = NSImage.alloc().initWithData_(data)
            app = NSApplication.sharedApplication()
            app.setApplicationIconImage_(icon)