Enhancements
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
from decimal import Decimal
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [('food', '0007_inventory_reversal_state')]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(model_name='foodproduct', name='is_beverage', field=models.BooleanField(default=False)),
|
||||
migrations.AddField(model_name='foodproduct', name='hydration_enabled', field=models.BooleanField(default=True)),
|
||||
migrations.AddField(model_name='foodproduct', name='hydration_factor', field=models.DecimalField(
|
||||
decimal_places=3, default=1, max_digits=4,
|
||||
validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(1)])),
|
||||
migrations.AddField(model_name='foodproduct', name='hydration_source', field=models.CharField(
|
||||
choices=[('manual', 'Manual'), ('product_metadata', 'Product metadata'), ('inferred', 'Inferred')],
|
||||
default='inferred', max_length=16)),
|
||||
migrations.AddField(model_name='foodlogentry', name='beverage_volume_ml', field=models.DecimalField(
|
||||
blank=True, decimal_places=3, max_digits=10, null=True,
|
||||
validators=[django.core.validators.MinValueValidator(0)])),
|
||||
migrations.AddField(model_name='foodlogentry', name='hydration_amount_ml', field=models.DecimalField(
|
||||
decimal_places=3, default=0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
|
||||
migrations.AddField(model_name='foodlogentry', name='hydration_factor_snapshot', field=models.DecimalField(
|
||||
decimal_places=3, default=0, max_digits=4, validators=[django.core.validators.MinValueValidator(0)])),
|
||||
migrations.AddField(model_name='foodlogentry', name='hydration_kind', field=models.CharField(blank=True, max_length=24)),
|
||||
migrations.AddField(model_name='foodlogentry', name='hydration_source', field=models.CharField(blank=True, max_length=16)),
|
||||
]
|
||||
@@ -0,0 +1,34 @@
|
||||
import re
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
PATTERNS = (
|
||||
('water', r'water'), ('juice', r'juice|sap|limonade|lemonade'),
|
||||
('milk', r'milk|melk|chocolademelk'),
|
||||
('coffee', r'coffee|koffie|espresso|cappuccino|latte'),
|
||||
('tea', r'tea|thee'), ('smoothie', r'smoothie|shake'),
|
||||
('other_drink', r'soda|cola|drink|drank|bier|beer|wine|wijn'),
|
||||
)
|
||||
|
||||
|
||||
def backfill_food_drinks(apps, schema_editor):
|
||||
Entry = apps.get_model('food', 'FoodLogEntry')
|
||||
for entry in Entry.objects.filter(unit='ml', hydration_source='').iterator():
|
||||
name = entry.product_name_snapshot.casefold()
|
||||
kind = next((kind for kind, words in PATTERNS
|
||||
if re.search(rf'(?<!\w)(?:{words})(?!\w)', name)), '')
|
||||
if not kind:
|
||||
Entry.objects.filter(pk=entry.pk).update(hydration_source='inferred')
|
||||
continue
|
||||
amount = Decimal(entry.amount)
|
||||
Entry.objects.filter(pk=entry.pk).update(beverage_volume_ml=amount,
|
||||
hydration_amount_ml=amount, hydration_factor_snapshot=Decimal('1'),
|
||||
hydration_kind=kind, hydration_source='inferred')
|
||||
Entry.objects.filter(hydration_source='').update(hydration_source='inferred')
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [('food', '0008_food_hydration'), ('planner', '0021_automatic_hydration')]
|
||||
operations = [migrations.RunPython(backfill_food_drinks, migrations.RunPython.noop)]
|
||||
Reference in New Issue
Block a user