def connect_contributor_to_user(cn: Contributor, create: bool = False) -> User: """Connect contributor to user""" if cn.user: return cn.user user = cn.get_user() if user: logger.info(f'found {user}') try: cn0 = user.contributor except ObjectDoesNotExist: cn.user = user cn.save(update_fields=['user']) else: merge_instances(cn, cn0) elif create: if not cn.email: logger.warning('Cannot create user for %s without an email' % cn) return None username = cn.email.split('@')[0].replace('.', '').lower() usernamebase = username n = 1 while User.objects.filter(username=username): username = f'{usernamebase}{n}' user = User.objects.create_user( username=username, email=cn.email, first_name=cn.first_name, last_name=cn.last_name, is_active=True, ) user.groups.add(default_groups().staff) cn.user = user cn.save(update_fields=['user']) logger.info(f'created {user}') return user
def merge_contributors(modeladmin, request, queryset): """Merge multiple contributors into one.""" first, *rest = list(queryset) messages.add_message( request, messages.INFO, _('merging %(number)s instances into %(first)s') % {'number': len(rest), 'first': first} ) merge_instances(first, *rest)
def merge_contributors(modeladmin, request, queryset): """Merge multiple contributors into one.""" first, *rest = list(queryset) messages.add_message( request, messages.INFO, _('merging %(number)s instances into %(first)s') % { 'number': len(rest), 'first': first }) merge_instances(first, *rest)
def dedupe_storyimages(apps, schema_editor): """merge storyimages with same parent_story/imagefile.""" StoryImage = apps.get_model("stories", "StoryImage") pairs = StoryImage.objects.values_list('imagefile_id', 'parent_story_id') dupes = (key for key, val in Counter(pairs).items() if val > 1) for imagefile, parent_story in dupes: story_images = StoryImage.objects.filter( imagefile=imagefile, parent_story=parent_story, ).order_by('-top', 'index') merge_instances(*story_images)
def merge_stints( stints: QuerySet, max_gap=timezone.timedelta(days=180) ) -> Stint: """Merge one or more Stints""" last: Optional[Stint] = None for stint in stints: if ( last and (last.contributor, last.position) == (stint.contributor, stint.position) and max_or_none( last.end_date, stint.start_date - max_gap, ) == last.end_date ): last.start_date = min(stint.start_date, last.start_date) last.end_date = max_or_none(stint.end_date, last.end_date) last.save() last = merge_instances(last, stint) else: last = stint
def merge_with(self, others): """Merge self with duplicate images.""" merge_instances(self, *list(others))
def test_contributor_exists(jimmy, jimmy_twin): assert str(jimmy) == 'Jimmy Foobarson' assert Contributor.objects.count() == 2 merge_instances(jimmy, jimmy_twin) assert Contributor.objects.count() == 1