Files @ bd964b926682
Branch filter:

Location: rattail-project/rattail/rattail/importing/rattail.py

lance
Add support for syncing roles, with users and permissions for each

but only those roles marked for sync. also by default the GlobalRole
is *not* included in the handler's default list, so this still
requires a bit of setup
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# -*- coding: utf-8; -*-
################################################################################
#
#  Rattail -- Retail Software Framework
#  Copyright © 2010-2021 Lance Edgar
#
#  This file is part of Rattail.
#
#  Rattail is free software: you can redistribute it and/or modify it under the
#  terms of the GNU General Public License as published by the Free Software
#  Foundation, either version 3 of the License, or (at your option) any later
#  version.
#
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
#  details.
#
#  You should have received a copy of the GNU General Public License along with
#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Rattail -> Rattail data import
"""

from __future__ import unicode_literals, absolute_import

import logging

import sqlalchemy as sa

from rattail.db import Session
from rattail.importing import model
from rattail.importing.handlers import FromSQLAlchemyHandler, ToSQLAlchemyHandler
from rattail.importing.sqlalchemy import FromSQLAlchemySameToSame
from rattail.util import OrderedDict


log = logging.getLogger(__name__)


class FromRattailHandler(FromSQLAlchemyHandler):
    """
    Base class for import handlers which target a Rattail database on the local side.
    """

    @property
    def host_title(self):
        return self.config.app_title(default="Rattail")

    def make_host_session(self):
        return Session()


class ToRattailHandler(ToSQLAlchemyHandler):
    """
    Base class for import handlers which target a Rattail database on the local side.
    """

    @property
    def local_title(self):
        return self.config.app_title(default="Rattail")

    def make_session(self):
        kwargs = {}
        if hasattr(self, 'runas_user'):
            kwargs['continuum_user'] = self.runas_user
        return Session(**kwargs)

    def begin_local_transaction(self):
        self.session = self.make_session()

        # load "runas user" into current session
        if hasattr(self, 'runas_user') and self.runas_user:
            dbmodel = self.config.get_model()
            runas_user = self.session.query(dbmodel.User)\
                                     .get(self.runas_user.uuid)
            if not runas_user:
                log.info("runas_user does not exist in target session: %s",
                         self.runas_user.username)
            # this may be None if user does not exist in target session
            self.runas_user = runas_user

        # declare "runas user" is data versioning author
        if hasattr(self, 'runas_username') and self.runas_username:
            self.session.set_continuum_user(self.runas_username)


class FromRattailToRattailBase(object):
    """
    Common base class for Rattail -> Rattail data import/export handlers.
    """

    def get_importers(self):
        importers = OrderedDict()
        importers['Person'] = PersonImporter
        importers['GlobalPerson'] = GlobalPersonImporter
        importers['PersonEmailAddress'] = PersonEmailAddressImporter
        importers['PersonPhoneNumber'] = PersonPhoneNumberImporter
        importers['PersonMailingAddress'] = PersonMailingAddressImporter
        importers['MergePeopleRequest'] = MergePeopleRequestImporter
        importers['Role'] = RoleImporter
        importers['GlobalRole'] = GlobalRoleImporter
        importers['User'] = UserImporter
        importers['AdminUser'] = AdminUserImporter
        importers['GlobalUser'] = GlobalUserImporter
        importers['Message'] = MessageImporter
        importers['MessageRecipient'] = MessageRecipientImporter
        importers['Store'] = StoreImporter
        importers['StorePhoneNumber'] = StorePhoneNumberImporter
        importers['Employee'] = EmployeeImporter
        importers['EmployeeStore'] = EmployeeStoreImporter
        importers['EmployeeEmailAddress'] = EmployeeEmailAddressImporter
        importers['EmployeePhoneNumber'] = EmployeePhoneNumberImporter
        importers['ScheduledShift'] = ScheduledShiftImporter
        importers['WorkedShift'] = WorkedShiftImporter
        importers['Customer'] = CustomerImporter
        importers['CustomerGroup'] = CustomerGroupImporter
        importers['CustomerGroupAssignment'] = CustomerGroupAssignmentImporter
        importers['CustomerPerson'] = CustomerPersonImporter
        importers['CustomerEmailAddress'] = CustomerEmailAddressImporter
        importers['CustomerPhoneNumber'] = CustomerPhoneNumberImporter
        importers['Member'] = MemberImporter
        importers['MemberEmailAddress'] = MemberEmailAddressImporter
        importers['MemberPhoneNumber'] = MemberPhoneNumberImporter
        importers['Vendor'] = VendorImporter
        importers['VendorEmailAddress'] = VendorEmailAddressImporter
        importers['VendorPhoneNumber'] = VendorPhoneNumberImporter
        importers['VendorContact'] = VendorContactImporter
        importers['Department'] = DepartmentImporter
        importers['EmployeeDepartment'] = EmployeeDepartmentImporter
        importers['Subdepartment'] = SubdepartmentImporter
        importers['Category'] = CategoryImporter
        importers['Family'] = FamilyImporter
        importers['ReportCode'] = ReportCodeImporter
        importers['DepositLink'] = DepositLinkImporter
        importers['Tax'] = TaxImporter
        importers['InventoryAdjustmentReason'] = InventoryAdjustmentReasonImporter
        importers['Brand'] = BrandImporter
        importers['Product'] = ProductImporter
        importers['ProductCode'] = ProductCodeImporter
        importers['ProductCost'] = ProductCostImporter
        importers['ProductPrice'] = ProductPriceImporter
        importers['ProductPriceAssociation'] = ProductPriceAssociationImporter
        importers['ProductStoreInfo'] = ProductStoreInfoImporter
        importers['ProductVolatile'] = ProductVolatileImporter
        importers['ProductImage'] = ProductImageImporter
        importers['LabelProfile'] = LabelProfileImporter
        return importers

    def get_default_keys(self):
        keys = self.get_importer_keys()

        avoid_by_default = [
            'Role',
            'GlobalRole',
            'GlobalPerson',
            'AdminUser',
            'GlobalUser',
            'ProductImage',
            'ProductPriceAssociation',
        ]

        for key in avoid_by_default:
            if key in keys:
                keys.remove(key)

        return keys


class FromRattailToRattailImport(FromRattailToRattailBase, FromRattailHandler, ToRattailHandler):
    """
    Handler for Rattail (other) -> Rattail (local) data import.

    .. attribute:: direction

       Value is ``'import'`` - see also
       :attr:`rattail.importing.handlers.ImportHandler.direction`.
    """
    dbkey = 'other'

    @property
    def host_title(self):
        return "{} ({})".format(self.config.app_title(default="Rattail"), self.dbkey)

    @property
    def local_title(self):
        return self.config.node_title(default="Rattail (local)")

    def make_host_session(self):
        return Session(bind=self.config.rattail_engines[self.dbkey])


class FromRattailToRattailExport(FromRattailToRattailBase, FromRattailHandler, ToRattailHandler):
    """
    Handler for Rattail (local) -> Rattail (other) data export.

    .. attribute:: direction

       Value is ``'export'`` - see also
       :attr:`rattail.importing.handlers.ImportHandler.direction`.
    """
    direction = 'export'
    dbkey = 'other'

    @property
    def host_title(self):
        return self.config.node_title()

    @property
    def local_title(self):
        return "{} ({})".format(self.config.app_title(default="Rattail"), self.dbkey)

    def make_session(self):
        return Session(bind=self.config.rattail_engines[self.dbkey])


class FromRattail(FromSQLAlchemySameToSame):
    """
    Base class for Rattail -> Rattail data importers.
    """


class PersonImporter(FromRattail, model.PersonImporter):
    pass


class GlobalPersonImporter(FromRattail, model.GlobalPersonImporter):
    """
    This is a customized version of the :class:`PersonImporter`, which simply
    avoids "local only" person accounts.
    """

    def query(self):
        query = super(GlobalPersonImporter, self).query()

        # never include "local only" people
        query = query.filter(sa.or_(
            self.host_model_class.local_only == False,
            self.host_model_class.local_only == None))

        return query

    def normalize_host_object(self, person):

        # must check this here for sake of datasync
        if person.local_only:
            return

        data = super(GlobalPersonImporter, self).normalize_host_object(person)
        return data


class PersonEmailAddressImporter(FromRattail, model.PersonEmailAddressImporter):
    pass

class PersonPhoneNumberImporter(FromRattail, model.PersonPhoneNumberImporter):
    pass

class PersonMailingAddressImporter(FromRattail, model.PersonMailingAddressImporter):
    pass

class MergePeopleRequestImporter(FromRattail, model.MergePeopleRequestImporter):
    pass

class RoleImporter(FromRattail, model.RoleImporter):
    pass


class GlobalRoleImporter(RoleImporter):
    """
    Role importer which only will handle roles which have the
    :attr:`~rattail.db.model.users.Role.sync_me` flag set.  (So it
    syncs those roles but avoids others.)
    """

    @property
    def supported_fields(self):
        fields = list(super(GlobalRoleImporter, self).supported_fields)
        fields.extend([
            'permissions',
            'users',
        ])
        return fields

    # nb. we must override both cache_query() and query() b/c they use
    # different sessions!

    def cache_query(self):
        """
        Return the query to be used when caching "local" data.
        """
        query = super(GlobalRoleImporter, self).cache_query()
        model = self.model

        # only want roles which are *meant* to be synced
        query = query.filter(model.Role.sync_me == True)

        return query

    def query(self):
        query = super(GlobalRoleImporter, self).query()
        model = self.model

        # only want roles which are *meant* to be synced
        query = query.filter(model.Role.sync_me == True)

        return query

    # nb. we do not need to override normalize_host_object() b/c it
    # just calls normalize_local_object() by default

    def normalize_local_object(self, role):

        # only want roles which are *meant* to be synced
        if not role.sync_me:
            return

        data = super(GlobalRoleImporter, self).normalize_local_object(role)
        if data:

            # users
            if 'users' in self.fields:
                data['users'] = sorted([user.uuid for user in role.users])

            # permissions
            if 'permissions' in self.fields:
                auth = self.app.get_auth_handler()
                perms = auth.cache_permissions(self.session, role,
                                               include_guest=False)
                data['permissions'] = sorted(perms)

            return data

    def update_object(self, role, host_data, local_data=None, **kwargs):
        role = super(GlobalRoleImporter, self).update_object(role, host_data,
                                                             local_data=local_data,
                                                             **kwargs)
        model = self.model

        # users
        if 'users' in self.fields:
            new_users = host_data['users']
            old_users = local_data['users'] if local_data else []
            changed = False

            # add some users
            for new_user in new_users:
                if new_user not in old_users:
                    user = self.session.query(model.User).get(new_user)
                    user.roles.append(role)
                    changed = True

            # remove some users
            for old_user in old_users:
                if old_user not in new_users:
                    user = self.session.query(model.User).get(old_user)
                    user.roles.remove(role)
                    changed = True

            if changed:
                # also record a change to the role, for datasync.
                # this is done "just in case" the role is to be
                # synced to all nodes
                if self.session.rattail_record_changes:
                    self.session.add(model.Change(class_name='Role',
                                                  instance_uuid=role.uuid,
                                                  deleted=False))

        # permissions
        if 'permissions' in self.fields:
            auth = self.app.get_auth_handler()
            new_perms = host_data['permissions']
            old_perms = local_data['permissions'] if local_data else []

            # grant permissions
            for new_perm in new_perms:
                if new_perm not in old_perms:
                    auth.grant_permission(role, new_perm)

            # revoke permissions
            for old_perm in old_perms:
                if old_perm not in new_perms:
                    auth.revoke_permission(role, old_perm)

        return role


class UserImporter(FromRattail, model.UserImporter):
    pass


class GlobalUserImporter(FromRattail, model.GlobalUserImporter):
    """
    This is a customized version of the :class:`UserImporter`, which simply
    avoids "local only" user accounts.
    """

    def query(self):
        query = super(GlobalUserImporter, self).query()

        # never include "local only" users
        query = query.filter(sa.or_(
            self.host_model_class.local_only == False,
            self.host_model_class.local_only == None))

        return query

    def normalize_host_object(self, user):

        # must check this here for sake of datasync
        if user.local_only:
            return

        data = super(GlobalUserImporter, self).normalize_host_object(user)
        return data


class AdminUserImporter(FromRattail, model.AdminUserImporter):

    @property
    def supported_fields(self):
        return super(AdminUserImporter, self).supported_fields + [
            'admin',
        ]

    def normalize_host_object(self, user):
        data = super(AdminUserImporter, self).normalize_local_object(user) # sic
        if 'admin' in self.fields: # TODO: do we really need this, after the above?
            data['admin'] = self.admin_uuid in [r.role_uuid for r in user._roles]
        return data


class MessageImporter(FromRattail, model.MessageImporter):
    pass

class MessageRecipientImporter(FromRattail, model.MessageRecipientImporter):
    pass

class StoreImporter(FromRattail, model.StoreImporter):
    pass

class StorePhoneNumberImporter(FromRattail, model.StorePhoneNumberImporter):
    pass

class EmployeeImporter(FromRattail, model.EmployeeImporter):
    pass

class EmployeeStoreImporter(FromRattail, model.EmployeeStoreImporter):
    pass

class EmployeeDepartmentImporter(FromRattail, model.EmployeeDepartmentImporter):
    pass

class EmployeeEmailAddressImporter(FromRattail, model.EmployeeEmailAddressImporter):
    pass

class EmployeePhoneNumberImporter(FromRattail, model.EmployeePhoneNumberImporter):
    pass

class ScheduledShiftImporter(FromRattail, model.ScheduledShiftImporter):
    pass

class WorkedShiftImporter(FromRattail, model.WorkedShiftImporter):
    pass

class CustomerImporter(FromRattail, model.CustomerImporter):
    pass

class CustomerGroupImporter(FromRattail, model.CustomerGroupImporter):
    pass

class CustomerGroupAssignmentImporter(FromRattail, model.CustomerGroupAssignmentImporter):
    pass

class CustomerPersonImporter(FromRattail, model.CustomerPersonImporter):
    pass

class CustomerEmailAddressImporter(FromRattail, model.CustomerEmailAddressImporter):
    pass

class CustomerPhoneNumberImporter(FromRattail, model.CustomerPhoneNumberImporter):
    pass

class MemberImporter(FromRattail, model.MemberImporter):
    pass

class MemberEmailAddressImporter(FromRattail, model.MemberEmailAddressImporter):
    pass

class MemberPhoneNumberImporter(FromRattail, model.MemberPhoneNumberImporter):
    pass

class VendorImporter(FromRattail, model.VendorImporter):
    pass

class VendorEmailAddressImporter(FromRattail, model.VendorEmailAddressImporter):
    pass

class VendorPhoneNumberImporter(FromRattail, model.VendorPhoneNumberImporter):
    pass

class VendorContactImporter(FromRattail, model.VendorContactImporter):
    pass

class DepartmentImporter(FromRattail, model.DepartmentImporter):
    pass

class SubdepartmentImporter(FromRattail, model.SubdepartmentImporter):
    pass

class CategoryImporter(FromRattail, model.CategoryImporter):
    pass

class FamilyImporter(FromRattail, model.FamilyImporter):
    pass

class ReportCodeImporter(FromRattail, model.ReportCodeImporter):
    pass

class DepositLinkImporter(FromRattail, model.DepositLinkImporter):
    pass

class TaxImporter(FromRattail, model.TaxImporter):
    pass

class InventoryAdjustmentReasonImporter(FromRattail, model.InventoryAdjustmentReasonImporter):
    pass

class BrandImporter(FromRattail, model.BrandImporter):
    pass


class ProductWithPriceImporter(FromRattail, model.ProductImporter):
    """
    This can perhaps be thought of as the "complete" Product record
    importer.  The "normal" Product importer will typically avoid the
    "price uuid" reference fields, b/c of that foreign key chaos.

    Note that this importer is not (yet?) used directly, but is
    primarily useful as a base class.
    """
    # these require special handling due to the 2-way table dependency
    price_reference_fields = [
        'regular_price_uuid',
        'tpr_price_uuid',
        'sale_price_uuid',
        'current_price_uuid',
        'suggested_price_uuid',
    ]

    def query(self):
        query = super(ProductWithPriceImporter, self).query()

        # make sure potential unit items (i.e. rows with NULL unit_uuid) come
        # first, so they will be created before pack items reference them
        # cf. https://www.postgresql.org/docs/current/static/queries-order.html
        # cf. https://stackoverflow.com/a/7622046
        query = query.order_by(self.host_model_class.unit_uuid.desc())

        return query


class ProductPriceAssociationImporter(ProductWithPriceImporter):
    """
    Note that this importer is *only* for sake of handling the "price
    uuid" fields.
    """

    @property
    def simple_fields(self):
        return ['uuid'] + self.price_reference_fields


class ProductImporter(ProductWithPriceImporter):
    """
    Note that this is the "normal" Product record importer, but it
    inherits from the "complete" importer.  This one avoids the "price
    uuid" fields to avoid that foreign key chaos.
    """

    @property
    def simple_fields(self):
        fields = super(ProductImporter, self).simple_fields
        # NOTE: it seems we can't consider these "simple" due to the
        # self-referencing foreign key situation.  an importer can still
        # "support" these fields, but they're excluded from the simple set for
        # sake of rattail <-> rattail
        for field in self.price_reference_fields:
            fields.remove(field)
        return fields


class ProductCodeImporter(FromRattail, model.ProductCodeImporter):
    pass

class ProductCostImporter(FromRattail, model.ProductCostImporter):
    pass

class ProductPriceImporter(FromRattail, model.ProductPriceImporter):

    @property
    def supported_fields(self):
        return super(ProductPriceImporter, self).supported_fields + self.product_reference_fields


class ProductStoreInfoImporter(FromRattail, model.ProductStoreInfoImporter):
    pass

class ProductVolatileImporter(FromRattail, model.ProductVolatileImporter):
    pass


class ProductImageImporter(FromRattail, model.ProductImageImporter):
    """
    Importer for product images.  Note that this uses the "batch" approach
    because fetching all data up front is not performant when the host/local
    systems are on different machines etc.
    """

    def query(self):
        query = self.host_session.query(self.model_class)\
                                 .order_by(self.model_class.uuid)
        return query[self.host_index:self.host_index + self.batch_size]


class LabelProfileImporter(FromRattail, model.LabelProfileImporter):

    def query(self):
        query = super(LabelProfileImporter, self).query()

        if not self.config.getbool('rattail', 'labels.sync_all_profiles', default=False):
            # only fetch labels from host which are marked as "sync me"
            query = query .filter(self.model_class.sync_me == True)

        return query.order_by(self.model_class.ordinal)