Skip to content

Commit

Permalink
Merge pull request #176 from rapidpro/prep-django5
Browse files Browse the repository at this point in the history
Remove use of deprecated pytz, self.assertEquals and index_together
  • Loading branch information
rowanseymour authored Dec 6, 2023
2 parents b3ca9f5 + cedc7d4 commit a28798d
Show file tree
Hide file tree
Showing 12 changed files with 558 additions and 526 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.8 on 2023-12-05 16:51

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("dashblocks", "0011_alter_dashblock_index_together_and_more"),
]

operations = [
migrations.RenameIndex(
model_name="dashblock",
new_name="dashblocks__org_id_024805_idx",
old_fields=("org", "is_active", "dashblock_type", "priority"),
),
migrations.RenameIndex(
model_name="dashblocktype",
new_name="dashblocks__slug_c0c6c6_idx",
old_fields=("slug", "name"),
),
]
4 changes: 2 additions & 2 deletions dash/dashblocks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __str__(self):

class Meta:
ordering = ["name"]
index_together = ("slug", "name")
indexes = [models.Index(fields=["slug", "name"])]


class DashBlock(SmartModel):
Expand Down Expand Up @@ -149,7 +149,7 @@ def __str__(self):

class Meta:
ordering = ["dashblock_type", "title"]
index_together = ("org", "is_active", "dashblock_type", "priority")
indexes = [models.Index(fields=["org", "is_active", "dashblock_type", "priority"])]


class DashBlockImage(SmartModel):
Expand Down
5 changes: 3 additions & 2 deletions dash/orgs/management/commands/orgtasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import timezone as tzone

from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

from dash.orgs.models import Org, TaskState

Expand Down Expand Up @@ -104,4 +105,4 @@ def cell(val, width):


def format_date(dt):
return dt.astimezone(timezone.utc).strftime("%b %d, %Y %H:%M") if dt else ""
return dt.astimezone(tzone.utc).strftime("%b %d, %Y %H:%M") if dt else ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.8 on 2023-12-05 16:51

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("orgs", "0031_alter_orgbackend_index_together"),
]

operations = [
migrations.RenameIndex(
model_name="orgbackend",
new_name="orgs_orgbac_org_id_607508_idx",
old_fields=("org", "is_active", "slug"),
),
]
2 changes: 1 addition & 1 deletion dash/orgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,4 @@ def __str__(self):

class Meta:
unique_together = ("org", "slug")
index_together = ("org", "is_active", "slug")
indexes = [models.Index(fields=["org", "is_active", "slug"])]
5 changes: 2 additions & 3 deletions dash/orgs/templatetags/dashorgs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime
from datetime import datetime, timezone as tzone

import phonenumbers

from django import template
from django.utils import timezone

register = template.Library()

Expand All @@ -14,7 +13,7 @@ def display_time(text_timestamp, org, time_format=None):
if not time_format:
time_format = "%b %d, %Y %H:%M"

parsed_time = datetime.strptime(text_timestamp, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
parsed_time = datetime.strptime(text_timestamp, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=tzone.utc)
output_time = parsed_time.astimezone(org.timezone)

return output_time.strftime(time_format)
Expand Down
6 changes: 3 additions & 3 deletions dash/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import calendar
import datetime
import json
import os
import random
from collections import OrderedDict
from datetime import datetime, timezone as tzone
from itertools import islice
from uuid import uuid4

Expand Down Expand Up @@ -101,8 +101,8 @@ def ms_to_datetime(ms):
"""
Converts a millisecond accuracy timestamp to a datetime
"""
dt = datetime.datetime.utcfromtimestamp(ms / 1000)
return dt.replace(microsecond=(ms % 1000) * 1000).replace(tzinfo=timezone.utc)
dt = datetime.utcfromtimestamp(ms / 1000)
return dt.replace(microsecond=(ms % 1000) * 1000).replace(tzinfo=tzone.utc)


def get_month_range(d=None):
Expand Down
9 changes: 4 additions & 5 deletions dash/utils/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import json
import zoneinfo
from datetime import datetime
from datetime import datetime, timezone as tzone
from itertools import chain

from django.core.cache import cache
from django.utils import timezone

from dash.test import DashTest

Expand Down Expand Up @@ -77,16 +76,16 @@ def calculate():
self.assertEqual(get_obj_cacheable(self, "_test_value", calculate, recalculate=True), "CALCULATED")

def test_datetime_to_ms(self):
d1 = datetime(2014, 1, 2, 3, 4, 5, 678900, tzinfo=timezone.utc)
d1 = datetime(2014, 1, 2, 3, 4, 5, 678900, tzinfo=tzone.utc)
self.assertEqual(datetime_to_ms(d1), 1388631845678) # from http://unixtimestamp.50x.eu

# conversion to millis loses some accuracy
self.assertEqual(ms_to_datetime(1388631845678), datetime(2014, 1, 2, 3, 4, 5, 678000, tzinfo=timezone.utc))
self.assertEqual(ms_to_datetime(1388631845678), datetime(2014, 1, 2, 3, 4, 5, 678000, tzinfo=tzone.utc))

tz = zoneinfo.ZoneInfo("Africa/Kigali")
d2 = datetime(2014, 1, 2, 3, 4, 5, 600000, tz)
self.assertEqual(datetime_to_ms(d2), 1388624645600)
self.assertEqual(ms_to_datetime(1388624645600), d2.astimezone(timezone.utc))
self.assertEqual(ms_to_datetime(1388624645600), d2.astimezone(tzone.utc))

def test_get_month_range(self):
self.assertEqual(
Expand Down
Loading

0 comments on commit a28798d

Please sign in to comment.