Changeset 9f580fc in rattail


Ignore:
Timestamp:
11/15/2022 01:36:43 PM (3 months ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
b89af59
Parents:
606b3f7
Message:

Add merge logic for users, to AuthHandler?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail/auth.py

    r606b3f7 r9f580fc  
    3333import sqlalchemy_continuum as continuum
    3434
    35 from rattail.app import GenericHandler
    36 
    37 
    38 class AuthHandler(GenericHandler):
     35from rattail.app import GenericHandler, MergeMixin
     36
     37
     38class AuthHandler(GenericHandler, MergeMixin):
    3939    """
    4040    Base class and default implementation for the so-called "auth"
     
    328328        return user
    329329
     330    def get_merge_preview_fields(self, **kwargs):
     331        """
     332        Returns a sequence of fields which will be used during a merge
     333        preview.
     334        """
     335        F = self.make_merge_field
     336        return [
     337            F('uuid'),
     338            F('username'),
     339            F('person_uuid', coalesce=True),
     340            F('person_name', coalesce=True),
     341            F('role_count'),    # coalesced manually
     342            F('active', coalesce=True),
     343            F('sent_message_count', additive=True),
     344            F('received_message_count', additive=True),
     345        ]
     346
     347    def get_merge_preview_data(self, user, **kwargs):
     348        return {
     349            'uuid': user.uuid,
     350            'username': user.username,
     351            'person_uuid': user.person_uuid,
     352            'person_name': user.person.display_name if user.person else None,
     353            '_roles': user.roles, # needed for final role count
     354            'role_count': len(user.roles),
     355            'active': user.active,
     356            'sent_message_count': len(user.sent_messages),
     357            'received_message_count': len(user._messages),
     358        }
     359
     360    def get_merge_resulting_data(self, removing, keeping, **kwargs):
     361        result = super(AuthHandler, self).get_merge_resulting_data(
     362            removing, keeping, **kwargs)
     363
     364        # nb. must "manually" coalesce the role count
     365        result['role_count'] = len(set(removing['_roles'] + keeping['_roles']))
     366
     367        return result
     368
     369    def why_not_merge(self, removing, keeping, **kwargs):
     370
     371        if removing.sent_messages:
     372            return "Cannot (yet) remove a user who has sent messages"
     373
     374        if removing._messages:
     375            return "Cannot (yet) remove a user who has received messages"
     376
     377        if removing._roles:
     378            return "Cannot (yet) remove a user who is assigned to roles"
     379
     380    def merge_update_keeping_object(self, removing, keeping):
     381        super(AuthHandler, self).merge_update_keeping_object(removing, keeping)
     382        session = self.app.get_session(keeping)
     383        model = self.model
     384
     385        # update any notes authored by old user, to reflect new user
     386        notes = session.query(model.Note)\
     387                       .filter(model.Note.created_by == removing)\
     388                       .all()
     389        for note in notes:
     390            note.created_by = keeping
     391
    330392    def delete_user(self, user, **kwargs):
    331393        """
Note: See TracChangeset for help on using the changeset viewer.