Changeset - 08bb030a5298
[Not reviewed]
1 4 0
Lance Edgar (lance) - 3 months ago 2024-07-16 17:45:38
lance@edbob.org
fix: avoid deprecated calls to `administrator_role()`
5 files changed with 30 insertions and 62 deletions:
0 comments (0 inline, 0 general)
rattail/commands/make_user.py
Show inline comments
 
@@ -93,11 +93,12 @@ def make_user(
 

	
 
def mkuser_rattail(config, params):
 
    from sqlalchemy import orm
 
    from rattail.db import auth
 

	
 
    app = config.get_app()
 
    auth = app.get_auth_handler()
 
    session = app.make_session()
 
    model = app.model
 

	
 
    if session.query(model.User).filter_by(username=params['username']).count():
 
        session.close()
 
        return user_exists(params)
 
@@ -121,7 +122,7 @@ def mkuser_rattail(config, params):
 
        auth.set_user_password(user, obtain_password(params))
 

	
 
    if params['admin']:
 
        user.roles.append(auth.administrator_role(session))
 
        user.roles.append(auth.get_role_administrator(session))
 
    for role in roles:
 
        user.roles.append(role)
 

	
rattail/importing/model.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2023 Lance Edgar
 
#  Copyright © 2010-2024 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -30,12 +30,10 @@ import warnings
 

	
 
import sqlalchemy as sa
 
from sqlalchemy import orm
 
from sqlalchemy.orm.exc import NoResultFound
 

	
 
from rattail.db import model, auth
 
from rattail.db import model
 
from rattail.importing import ToSQLAlchemy, BatchImporter
 
from rattail.db.util import maxlen, normalize_full_name, format_phone_number, normalize_phone_number
 
from rattail.time import localtime, make_utc
 

	
 

	
 
log = logging.getLogger(__name__)
 
@@ -189,10 +187,10 @@ class ContactMixin(object):
 
        self.cache_contact_maxlens()
 

	
 
    def cache_contact_maxlens(self):
 
        model = self.model
 
        model = self.app.model
 

	
 
        self.maxlen_phone_number = maxlen(model.PhoneNumber.number)
 
        self.maxlen_email_address = maxlen(model.EmailAddress.address)
 
        self.maxlen_phone_number = self.app.maxlen(model.PhoneNumber.number)
 
        self.maxlen_email_address = self.app.maxlen(model.EmailAddress.address)
 

	
 
    @property
 
    def supported_fields(self):
 
@@ -565,12 +563,11 @@ class UserImporter(ToRattail):
 
        return data
 

	
 
    def update_object(self, user, data, local_data=None):
 
        from rattail.db import auth
 

	
 
        user = super().update_object(user, data, local_data)
 
        if user:
 
            if 'plain_password' in self.fields:
 
                if data['plain_password']:
 
                    auth = self.app.get_auth_handler()
 
                    auth.set_user_password(user, data['plain_password'])
 
            return user
 

	
 
@@ -595,7 +592,9 @@ class AdminUserImporter(UserImporter):
 

	
 
    def setup(self):
 
        super().setup()
 
        self.admin_uuid = auth.administrator_role(self.session).uuid
 
        auth = self.app.get_auth_handler()
 
        admin = auth.get_role_administrator(self.session)
 
        self.admin_uuid = admin.uuid
 

	
 
    def cache_query_options(self):
 
        options = super().cache_query_options()
 
@@ -604,8 +603,8 @@ class AdminUserImporter(UserImporter):
 
        return options
 

	
 
    def get_admin(self, session=None):
 
        from rattail.db import auth
 
        return auth.administrator_role(session or self.session)
 
        auth = self.app.get_auth_handler()
 
        return auth.get_role_administrator(session or self.session)
 

	
 
    def normalize_local_object(self, user):
 
        data = super().normalize_local_object(user)
 
@@ -1893,7 +1892,7 @@ class SubdepartmentImporter(ToRattail):
 
            return self.session.query(model.Department)\
 
                               .filter(model.Department.number == number)\
 
                               .one()
 
        except NoResultFound:
 
        except orm.exc.NoResultFound:
 
            pass
 

	
 
    def update_object(self, subdepartment, data, local_data=None):
 
@@ -2861,7 +2860,7 @@ class ProductCodeImporter(ToRattail):
 
            query = query.join(model.Product).filter(model.Product.upc == key[i])
 
        try:
 
            return query.one()
 
        except NoResultFound:
 
        except orm.exc.NoResultFound:
 
            pass
 

	
 
    def normalize_local_object(self, code):
 
@@ -3005,7 +3004,7 @@ class ProductCostImporter(ToRattail):
 

	
 
        try:
 
            return query.one()
 
        except NoResultFound:
 
        except orm.exc.NoResultFound:
 
            pass
 

	
 
    def normalize_local_object(self, cost):
 
@@ -3285,17 +3284,17 @@ class CustomerOrderItemEventImporter(ToRattail):
 
        self.start_date = self.args.start_date
 
        if self.start_date:
 
            midnight = datetime.datetime.combine(self.start_date, datetime.time(0))
 
            self.start_time = localtime(self.config, midnight)
 
            self.start_time = self.app.localtime(midnight)
 

	
 
        self.end_date = self.args.end_date
 
        if self.end_date:
 
            midnight = datetime.datetime.combine(self.end_date + datetime.timedelta(days=1), datetime.time(0))
 
            self.end_time = localtime(self.config, midnight)
 
            self.end_time = self.app.localtime(midnight)
 

	
 
    def cache_query(self):
 
        query = self.session.query(model.CustomerOrderItemEvent)
 
        if self.start_date:
 
            query = query.filter(model.CustomerOrderItemEvent.occurred >= make_utc(self.start_time))
 
            query = query.filter(model.CustomerOrderItemEvent.occurred >= self.app.make_utc(self.start_time))
 
        if self.end_date:
 
            query = query.filter(model.CustomerOrderItemEvent.occurred < make_utc(self.end_time))
 
            query = query.filter(model.CustomerOrderItemEvent.occurred < self.app.make_utc(self.end_time))
 
        return query
tests/db/test_auth.py
Show inline comments
 
deleted file
tests/importing/test_model.py
Show inline comments
 
@@ -8,7 +8,7 @@ from rattail.config import RattailConfig
 

	
 
try:
 
    import sqlalchemy as sa
 
    from rattail.db import model, auth, Session, ConfigExtension
 
    from rattail.db import model, Session, ConfigExtension
 
    from rattail.importing import model as import_model
 
    from .. import RattailTestCase
 
except ImportError:
 
@@ -39,7 +39,8 @@ else:
 
            return import_model.AdminUserImporter(**kwargs)
 

	
 
        def get_admin(self):
 
            return auth.administrator_role(self.session)
 
            auth = self.app.get_auth_handler()
 
            return auth.get_role_administrator(self.session)
 

	
 
        def test_supported_fields(self):
 
            importer = import_model.UserImporter(self.config)
tests/importing/test_rattail.py
Show inline comments
 
@@ -5,7 +5,7 @@ from unittest.mock import patch
 

	
 
try:
 
    import sqlalchemy as sa
 
    from rattail.db import model, Session, SessionBase, auth
 
    from rattail.db import model, Session, SessionBase
 
    from rattail.importing import rattail as rattail_importing
 
    from .. import RattailMixin, RattailTestCase
 
    from . import ImporterTester
 
@@ -16,7 +16,7 @@ else:
 
    class DualRattailMixin(RattailMixin):
 

	
 
        def setup_rattail(self):
 
            super(DualRattailMixin, self).setup_rattail()
 
            super().setup_rattail()
 

	
 
            if 'host' not in self.config.rattail_engines:
 
                self.config.rattail_engines['host'] = sa.create_engine('sqlite://')
 
@@ -30,7 +30,7 @@ else:
 
            self.host_session = Session(bind=self.host_engine)
 

	
 
        def teardown_rattail(self):
 
            super(DualRattailMixin, self).teardown_rattail()
 
            super().teardown_rattail()
 

	
 
            self.host_session.close()
 
            model = self.get_rattail_model()
 
@@ -128,7 +128,9 @@ else:
 
            return self.importer_class(**kwargs)
 

	
 
        def get_admin(self):
 
            return auth.administrator_role(self.session)
 
            app = self.config.get_app()
 
            auth = app.get_auth_handler()
 
            return auth.get_role_administrator(self.session)
 

	
 
        def test_normalize_host_object(self):
 
            importer = self.make_importer()
0 comments (0 inline, 0 general)