|
1 | | -from django.shortcuts import redirect |
2 | | -from django.utils.functional import cached_property |
| 1 | +from typing import List |
3 | 2 |
|
4 | | -from django_unicorn.components import HashUpdate, LocationUpdate, UnicornView |
5 | | -from django_unicorn.db import DbModel |
6 | | -from django_unicorn.decorators import db_model |
| 3 | +from django_unicorn.components import UnicornView |
7 | 4 | from example.coffee.models import Flavor |
8 | 5 |
|
9 | 6 |
|
10 | 7 | class ModelsView(UnicornView): |
11 | | - flavors = Flavor.objects.none() |
12 | | - |
13 | | - # Demonstrates how to use an instantiated model; class attributes get stored on |
14 | | - # the class, so django-unicorn handles clearing this with `_resettable_attributes_cache` |
15 | | - # in components.py |
16 | | - class_flavor: Flavor = Flavor() |
17 | | - |
18 | | - def __init__(self, **kwargs): |
19 | | - # Demonstrates how to use an instance variable on the class |
20 | | - self.instance_flavor = Flavor() |
21 | | - |
22 | | - # super() `has` to be called at the end |
23 | | - super().__init__(**kwargs) |
24 | | - |
25 | | - def hydrate(self): |
26 | | - # Using `hydrate` is the best way to make sure that QuerySets |
27 | | - # are re-queried every time the component is loaded |
28 | | - self.flavors = Flavor.objects.all().order_by("-id")[:2] |
29 | | - |
30 | | - def add_instance_flavor(self): |
31 | | - self.instance_flavor.save() |
32 | | - id = self.instance_flavor.id |
33 | | - self.reset() |
34 | | - |
35 | | - # return HashUpdate(f"#createdId={id}") |
36 | | - return LocationUpdate(redirect(f"/models?createdId={id}"), title="new title") |
37 | | - |
38 | | - def add_class_flavor(self): |
39 | | - self.class_flavor.save() |
40 | | - id = self.class_flavor.id |
41 | | - self.reset() |
42 | | - |
43 | | - # Note: this can cause inputs to appear to be cached |
44 | | - return redirect(f"/models?createdId={id}") |
45 | | - |
46 | | - @db_model |
47 | | - def delete(self, model): |
48 | | - model.delete() |
49 | | - |
50 | | - @cached_property |
51 | | - def available_flavors(self): |
52 | | - flavors = Flavor.objects.all() |
53 | | - |
54 | | - if self.instance_flavor and self.instance_flavor.pk: |
55 | | - return flavors.exclude(pk=self.instance_flavor.pk) |
56 | | - |
57 | | - return flavors |
58 | | - |
59 | | - class Meta: |
60 | | - db_models = [DbModel("flavor", Flavor)] |
| 8 | + # class attributes get stored on the class, so django-unicorn handles clearing |
| 9 | + # this with `_resettable_attributes_cache` in components.py |
| 10 | + flavor: Flavor = Flavor() |
| 11 | + flavors: List[Flavor] = [] |
| 12 | + |
| 13 | + def mount(self): |
| 14 | + self.flavors = list(Flavor.objects.all().order_by("-id")[:2]) |
| 15 | + |
| 16 | + def save_flavor(self): |
| 17 | + self.flavor.save() |
| 18 | + self.flavor = Flavor() |
| 19 | + |
| 20 | + def save(self, flavors_idx): |
| 21 | + flavor_data = self.flavors[flavors_idx] |
| 22 | + print("call save for idx", flavors_idx) |
| 23 | + flavor = Flavor(**flavor_data) |
| 24 | + flavor.save() |
| 25 | + |
| 26 | + def save_specific(self, flavor: Flavor): |
| 27 | + flavor.save() |
| 28 | + print("call save on flavor", flavor) |
0 commit comments