Skip to content

Commit

Permalink
MealPlan from/to date to datetime, added time to MealType
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed May 1, 2024
1 parent c87964f commit 4b551c5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 4.2.11 on 2024-05-01 10:56
from datetime import timedelta

from django.db import migrations, models
from django_scopes import scopes_disabled


def timezone_correction(apps, schema_editor):
# when converting from date to datetime the field becomes timezone aware and defaults to 00:00:00 UTC
# this will be converted to a local datetime on the client which, for all negative offset timezones,
# would mean that the plan item shows one day prior to the previous planning date
# by setting the time on the old entries to 11:00 this issue will be limited to a very small number of people (see https://en.wikipedia.org/wiki/Time_zone#/media/File:World_Time_Zones_Map.svg)
# the server timezone could be used to guess zone of most of the clients but that would also not be perfect so this much simpler alternative is chosen
with scopes_disabled():
MealPlan = apps.get_model('cookbook', 'MealPlan')
meal_plans = MealPlan.objects.all()
for mp in meal_plans:
mp.from_date += timedelta(hours=12)
mp.to_date += timedelta(hours=12)

MealPlan.objects.bulk_update(meal_plans, ['from_date', 'to_date'], batch_size=1000)


class Migration(migrations.Migration):
dependencies = [
('cookbook', '0217_alter_userpreference_default_page'),
]

operations = [
migrations.AddField(
model_name='mealtype',
name='time',
field=models.TimeField(blank=True, null=True),
),
migrations.AlterField(
model_name='mealplan',
name='from_date',
field=models.DateTimeField(),
),
migrations.AlterField(
model_name='mealplan',
name='to_date',
field=models.DateTimeField(),
),
migrations.RunPython(timezone_correction)
]
6 changes: 3 additions & 3 deletions cookbook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ class ConnectorConfig(models.Model, PermissionModelMixin):
objects = ScopedManager(space='space')



class UserPreference(models.Model, PermissionModelMixin):
# Themes
BOOTSTRAP = 'BOOTSTRAP'
Expand Down Expand Up @@ -1135,6 +1134,7 @@ class MealType(models.Model, PermissionModelMixin):
name = models.CharField(max_length=128)
order = models.IntegerField(default=0)
color = models.CharField(max_length=7, blank=True, null=True)
time = models.TimeField(null=True, blank=True)
default = models.BooleanField(default=False, blank=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)

Expand All @@ -1158,8 +1158,8 @@ class MealPlan(ExportModelOperationsMixin('meal_plan'), models.Model, Permission
shared = models.ManyToManyField(User, blank=True, related_name='plan_share')
meal_type = models.ForeignKey(MealType, on_delete=models.CASCADE)
note = models.TextField(blank=True)
from_date = models.DateField()
to_date = models.DateField()
from_date = models.DateTimeField()
to_date = models.DateTimeField()

space = models.ForeignKey(Space, on_delete=models.CASCADE)
objects = ScopedManager(space='space')
Expand Down
4 changes: 2 additions & 2 deletions cookbook/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def create(self, validated_data):
class Meta:
list_serializer_class = SpaceFilterSerializer
model = MealType
fields = ('id', 'name', 'order', 'color', 'default', 'created_by')
fields = ('id', 'name', 'order', 'time', 'color', 'default', 'created_by')
read_only_fields = ('created_by',)


Expand Down Expand Up @@ -1030,7 +1030,7 @@ class MealPlanSerializer(SpacedModelSerializer, WritableNestedModelSerializer):
shared = UserSerializer(many=True, required=False, allow_null=True)
shopping = serializers.SerializerMethodField('in_shopping')

to_date = serializers.DateField(required=False)
to_date = serializers.DateTimeField(required=False)

def get_note_markdown(self, obj):
return markdown(obj.note)
Expand Down

0 comments on commit 4b551c5

Please sign in to comment.