Моя среда:
- ОС: Windows 10
- Outlook: 2016
- Tech Stack: Python 3.13, Django 5.1.4, pywin32
- База данных: SQLite
часовой пояс
TIME_ZONE = 'UTC' # I also tried 'Asia/Ho_Chi_Minh'
USE_TZ = True
модель
class FloorRequest(models.Model):
request_type = models.CharField(max_length=10) # 'BADGE' or 'FLOOR'
email = models.EmailField()
request_time = models.DateTimeField() # This is where I compare timestamps
mail_entry_id = models.CharField(max_length=255, unique=True)
Питон
def backfill_all() -> None:
items = t1_folder.Items
item_list =
existing_records = set(
FloorRequest.objects.values_list('email', 'request_time', 'request_type')
)
count = 0
for item in item_list:
try:
sender = str(getattr(item, "SenderEmailAddress", "")).lower().strip()
received_time = item.ReceivedTime.replace(tzinfo=None) # Naive datetime
current_type = 'BADGE' if "badge" in item.Subject.lower() else 'FLOOR'
if (sender, received_time, current_type) not in existing_records:
FloorRequest.objects.create(
email=sender,
request_time=received_time,
request_type=current_type,
)
existing_records.add((sender, received_time, current_type))
count += 1
except Exception as e:
continue
Мобильная версия