from django.contrib import admin

from .models import Record
from .tasks import send_personnel_code


class RecordAdmin(admin.ModelAdmin):
    list_display = [
        "id", "identity", "write_to", "created_at", "updated_at"]
    list_filter = ["write_to", "created_at"]
    search_fields = ["identity", "write_to"]
    actions = ["resend_personnel_code"]

    def resend_personnel_code(self, request, queryset):
        created = 0
        skipped = 0
        for record in queryset:
            if record.write_to != "personnel_code":
                skipped += 1
                continue
            send_personnel_code.apply_async(kwargs={
                "identity": str(record.identity),
                "personnel_code": record.id})
            created += 1
        if created == 1:
            created_text = "%s Record was" % created
        else:
            created_text = "%s Records were" % created
        if skipped == 1:
            skipped_text = "%s Record was" % skipped
        else:
            skipped_text = "%s Records were" % skipped
        self.message_user(
            request, "%s successfully changed. %s skipped because they are "
            "not a HCW." % (created_text, skipped_text))

    resend_personnel_code.short_description = "Send code by SMS (personnel "\
        "code only)"

admin.site.register(Record, RecordAdmin)

from django.contrib import admin

from .models import Record
from .tasks import send_personnel_code


class RecordAdmin(admin.ModelAdmin):
    list_display = [
        "id", "identity", "write_to", "created_at", "updated_at"]
    list_filter = ["write_to", "created_at"]
    search_fields = ["identity", "write_to"]
    actions = ["resend_personnel_code"]

    def resend_personnel_code(self, request, queryset):
        created = 0
        skipped = 0
        for record in queryset.iterator():
            if record.write_to != "personnel_code":
                skipped += 1
                continue
            send_personnel_code.apply_async(kwargs={
                "identity": str(record.identity),
                "personnel_code": record.id})
            created += 1
        if created == 1:
            created_text = "%s Record was" % created
        else:
            created_text = "%s Records were" % created
        if skipped == 1:
            skipped_text = "%s Record was" % skipped
        else:
            skipped_text = "%s Records were" % skipped
        self.message_user(
            request, "%s successfully changed. %s skipped because they are "
            "not a HCW." % (created_text, skipped_text))

    resend_personnel_code.short_description = "Send code by SMS (personnel "\
        "code only)"

admin.site.register(Record, RecordAdmin)

Use Iterator to iterate through records
Use Iterator to iterate through records

  from django.contrib import admin
  
  from .models import Record
  from .tasks import send_personnel_code
  
  
  class RecordAdmin(admin.ModelAdmin):
      list_display = [
          "id", "identity", "write_to", "created_at", "updated_at"]
      list_filter = ["write_to", "created_at"]
      search_fields = ["identity", "write_to"]
      actions = ["resend_personnel_code"]
  
      def resend_personnel_code(self, request, queryset):
          created = 0
          skipped = 0
-         for record in queryset:
+         for record in queryset.iterator():
              if record.write_to != "personnel_code":
                  skipped += 1
                  continue
              send_personnel_code.apply_async(kwargs={
                  "identity": str(record.identity),
                  "personnel_code": record.id})
              created += 1
          if created == 1:
              created_text = "%s Record was" % created
          else:
              created_text = "%s Records were" % created
          if skipped == 1:
              skipped_text = "%s Record was" % skipped
          else:
              skipped_text = "%s Records were" % skipped
          self.message_user(
              request, "%s successfully changed. %s skipped because they are "
              "not a HCW." % (created_text, skipped_text))
  
      resend_personnel_code.short_description = "Send code by SMS (personnel "\
          "code only)"
  
  admin.site.register(Record, RecordAdmin)
  
Use Iterator to iterate through records
## Code Before:
from django.contrib import admin

from .models import Record
from .tasks import send_personnel_code


class RecordAdmin(admin.ModelAdmin):
    list_display = [
        "id", "identity", "write_to", "created_at", "updated_at"]
    list_filter = ["write_to", "created_at"]
    search_fields = ["identity", "write_to"]
    actions = ["resend_personnel_code"]

    def resend_personnel_code(self, request, queryset):
        created = 0
        skipped = 0
        for record in queryset:
            if record.write_to != "personnel_code":
                skipped += 1
                continue
            send_personnel_code.apply_async(kwargs={
                "identity": str(record.identity),
                "personnel_code": record.id})
            created += 1
        if created == 1:
            created_text = "%s Record was" % created
        else:
            created_text = "%s Records were" % created
        if skipped == 1:
            skipped_text = "%s Record was" % skipped
        else:
            skipped_text = "%s Records were" % skipped
        self.message_user(
            request, "%s successfully changed. %s skipped because they are "
            "not a HCW." % (created_text, skipped_text))

    resend_personnel_code.short_description = "Send code by SMS (personnel "\
        "code only)"

admin.site.register(Record, RecordAdmin)

## Instruction:
Use Iterator to iterate through records
## Code After:
from django.contrib import admin

from .models import Record
from .tasks import send_personnel_code


class RecordAdmin(admin.ModelAdmin):
    list_display = [
        "id", "identity", "write_to", "created_at", "updated_at"]
    list_filter = ["write_to", "created_at"]
    search_fields = ["identity", "write_to"]
    actions = ["resend_personnel_code"]

    def resend_personnel_code(self, request, queryset):
        created = 0
        skipped = 0
        for record in queryset.iterator():
            if record.write_to != "personnel_code":
                skipped += 1
                continue
            send_personnel_code.apply_async(kwargs={
                "identity": str(record.identity),
                "personnel_code": record.id})
            created += 1
        if created == 1:
            created_text = "%s Record was" % created
        else:
            created_text = "%s Records were" % created
        if skipped == 1:
            skipped_text = "%s Record was" % skipped
        else:
            skipped_text = "%s Records were" % skipped
        self.message_user(
            request, "%s successfully changed. %s skipped because they are "
            "not a HCW." % (created_text, skipped_text))

    resend_personnel_code.short_description = "Send code by SMS (personnel "\
        "code only)"

admin.site.register(Record, RecordAdmin)
