from datetime import datetime from django.db import transaction from django.utils import timezone from django_q.tasks import async_task from cmsmanage.django_q2_helper import q_task_group from doorcontrol.models import Door, DoorCardholderMember, HIDEvent def get_cardholders(door: Door): def make_ch_member(cardholder): return DoorCardholderMember( door=door, cardholder_id=cardholder.attrib["cardholderID"], member_id=cardholder.attrib.get("custom2"), ) DoorCardholderMember.objects.bulk_create( ( make_ch_member(cardholder) for cardholder in door.controller.get_cardholders() if "custom2" in cardholder.attrib ), update_conflicts=True, update_fields=("member",), ) @transaction.atomic() def getMessages(door: Door): # TODO: this should in the cardholder syncing task get_cardholders(door) last_event = door.hidevent_set.order_by("timestamp").last() if last_event is not None: last_ts = timezone.make_naive(last_event.timestamp) else: last_ts = datetime(2010, 1, 1) retrieved_message_count = 0 created_message_count = 0 for events_page in door.controller.get_events(last_ts): print(f"Importing {len(events_page)} events for {door.name}") created_messages = HIDEvent.objects.bulk_create( (HIDEvent.from_xml_attributes(door, event.attrib) for event in events_page), ignore_conflicts=True, ) retrieved_message_count += len(events_page) created_message_count += len(created_messages) return { "retrieved": retrieved_message_count, "created": created_message_count, } @q_task_group("Fetch HID Events") def q_getMessagesAllDoors(): # TODO: this should probably use async_iter for door in Door.objects.all(): async_task( getMessages, door, cluster="internal", group=f"Fetch HID Events - {door.name}", )