def _remove_items(self, *objs, **kwargs): qs = self.filter(pk__in=objs) kwargs = { self.column: ArrayRemove(self.column, self.to_field_value) } with transaction.atomic(): qs.update(**kwargs)
def _remove_items(self, *objs, **kwargs): if objs: chunks = [objs[x:x + 100] for x in range(0, len(objs), 100)] for chunk in chunks: kwargs = {self.column: multi_array_remove(self.column, *chunk)} self._update_instance(**kwargs) # If this is a symmetrical m2m relation to self, add the mirror entry to the other objs array if self.symmetrical: kwargs = {self.column: ArrayRemove(self.column, self.instance.pk, output_field=self.field)} self.model.objects.filter(pk__in=list(objs)).update(**kwargs)
def get_update_type(self, indexes, value): if indexes == 'del': return ArrayRemove(self.name, value) if '__' in indexes: indexes = indexes.split('__') try: indexes = [int(index) + 1 for index in indexes] return UpdateArrayByIndex(indexes, value, self) except ValueError: raise ValueError('Update lookup type %s not found for field %s' % (indexes, self.name))
def _clear(self): kwargs = {self.column: []} self._update_instance(**kwargs) if self.symmetrical: kwargs = { self.column: ArrayRemove(self.column, self.instance.pk, output_field=self.field) } self.model.objects.update(**kwargs)
def delete_tag(request): if request.method != "POST": return HttpResponse(status=405) image_id = request.POST.get('imageid') tag = request.POST.get('tag') try: image_id = int(image_id) except ValueError: return HttpResponse(status=400, content="Invalid image id provided") Image.objects.filter(id=image_id).update(tags=ArrayRemove('tags', tag)) return HttpResponse(status=200)
def remove(self, observer): try: self.subscribers = ArrayRemove('subscribers', observer) super(Listing, self).save() except ValueError: print('Failed to remove!')
def _clear(self): with transaction.atomic(): kwargs = { self.column: ArrayRemove(self.column, self.to_field_value) } self.model.objects.update(**kwargs)
def handle(self, *args, **options) -> None: verbosity = int(options.get('verbosity', 1)) database = options.get('database') try: from campaigns.models import Campaign from campaigns.models import CampaignStatus from campaigns.models import CampaignProblems from campaigns.models import Step from campaigns.models import StepProblems results = OrderedDict() with transaction.atomic(): results["add NO_STEPS to campaigns"] = Campaign.objects.using( database).annotate(steps_count=Count('steps')).filter( steps_count=0, ).exclude(problems__contains=[ CampaignProblems.NO_STEPS ], ).update(problems=ArrayAppend( 'problems', CampaignProblems.NO_STEPS)) results[ 'remove NO_STEPS from campaigns'] = Campaign.objects.using( database).annotate(steps_count=Count('steps')).filter( steps_count__gt=0, problems__contains=[CampaignProblems.NO_STEPS], ).update(problems=ArrayRemove( 'problems', CampaignProblems.NO_STEPS)) results[ 'add NO_CONTACTS to campaigns'] = Campaign.objects.using( database).annotate(contacts_count=Count( 'contacts')).filter(contacts_count=0, ).exclude( problems__contains=[ CampaignProblems.NO_CONTACTS ], ).update(problems=ArrayAppend( 'problems', CampaignProblems.NO_CONTACTS)) results[ 'remove NO_CONTACTS from campaigns'] = Campaign.objects.using( database).annotate( contacts_count=Count('contacts')).filter( contacts_count__gt=0, problems__contains=[ CampaignProblems.NO_CONTACTS ], ).update(problems=ArrayRemove( 'problems', CampaignProblems.NO_CONTACTS)) results[ 'add EMPTY_STEPS to campaigns'] = Campaign.objects.using( database).annotate( email_stages_count=Count('steps__emails')).filter( email_stages_count=0, ).exclude(problems__contains=[ CampaignProblems.EMPTY_STEP ], ).update(problems=ArrayAppend( 'problems', CampaignProblems.EMPTY_STEP)) results[ 'remove EMPTY_STEPS from campaigns'] = Campaign.objects.using( database).annotate( email_stages_count=Count('steps__emails')).filter( email_stages_count__gt=0, problems__contains=[ CampaignProblems.EMPTY_STEP ], ).update(problems=ArrayRemove( 'problems', CampaignProblems.EMPTY_STEP)) results['add EMPTY_STEPS to steps'] = Step.objects.using( database).annotate( email_stages_count=Count('emails')).filter( email_stages_count=0, ).exclude( problems__contains=[ StepProblems.EMPTY_STEP ], ).update(problems=ArrayAppend( 'problems', StepProblems.EMPTY_STEP)) results['remove EMPTY_STEPS from steps'] = Step.objects.using( database).annotate( email_stages_count=Count('emails')).filter( email_stages_count__gt=0, problems__contains=[StepProblems.EMPTY_STEP], ).update(problems=ArrayRemove('problems', StepProblems.EMPTY_STEP)) results[ 'set campaigns status from DRAFT to PAUSED'] = Campaign.objects.using( database).filter( status=CampaignStatus.DRAFT, problems__len=0, # steps__problems__len=0, ).update(status=CampaignStatus.PAUSED) results[ 'set campaigns status from ACTIVE or PAUSED back to DRAFT'] = ( Campaign.objects.using(database).filter( problems__len__gt=0, # Q(problems__len__gt=0) | Q(steps__problems__len__gt=0), ).exclude(status=CampaignStatus.DRAFT).update( status=CampaignStatus.DRAFT)) if verbosity >= 1: self.stdout.write( 'Problems successfully rechecked:\n\t%s' % '\n\t'.join('%s: %d' % (name, count) for name, count in results.items())) except KeyboardInterrupt: self.stderr.write("\nOperation cancelled.") sys.exit(1) except BaseException as e: self.stderr.write("Error: %s" % str(e)) sys.exit(1)