|
| 1 | +from django.contrib.auth import get_user_model |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from docs.models import * |
3 | 5 | from iommi import * |
| 6 | +from iommi.form import save_nested_forms |
4 | 7 | from tests.helpers import ( |
5 | 8 | req, |
6 | 9 | show_output, |
@@ -46,3 +49,104 @@ def test_edit_tables(really_big_discography): |
46 | 49 |
|
47 | 50 | show_output(t.bind(request=request)) |
48 | 51 | # @end |
| 52 | + |
| 53 | + |
| 54 | +def test_orderable_edit_tables(fav_artists): |
| 55 | + # language=rst |
| 56 | + """ |
| 57 | + .. _orderable-edit-tables: |
| 58 | +
|
| 59 | + Orderable edit tables |
| 60 | + ===================== |
| 61 | +
|
| 62 | + iommi edit tables also support ordering. That can be especially useful for editing reverse FK's in nested forms. |
| 63 | + """ |
| 64 | + |
| 65 | + # language=rst |
| 66 | + """ |
| 67 | + The easiest way to make an EditTable orderable is to inherit your model from `iommi.models.Orderable`: |
| 68 | +
|
| 69 | + .. code-block:: python |
| 70 | +
|
| 71 | + class FavoriteArtist(Orderable): |
| 72 | + user = ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='favorite_artists') |
| 73 | + artist = ForeignKey(Artist, on_delete=CASCADE, related_name='+') |
| 74 | + comment = CharField(max_length=255) |
| 75 | +
|
| 76 | + def __str__(self): |
| 77 | + return self.artist.name |
| 78 | + """ |
| 79 | + |
| 80 | + class UserForm(Form): |
| 81 | + class Meta: |
| 82 | + auto__model = get_user_model() |
| 83 | + auto__include = ['username', 'email'] |
| 84 | + fields__username__editable = False |
| 85 | + |
| 86 | + class FavoriteArtists(EditTable): |
| 87 | + class Meta: |
| 88 | + auto__model = FavoriteArtist |
| 89 | + auto__include = ['artist__name', 'comment', 'sort_order'] |
| 90 | + columns__comment__field__include = True |
| 91 | + |
| 92 | + user = get_user_model().objects.get(username='john.doe') |
| 93 | + |
| 94 | + class ParentForm(Form): |
| 95 | + user_form = UserForm.edit(auto__instance=user) |
| 96 | + favorite_artists = FavoriteArtists(rows=user.favorite_artists.all()) |
| 97 | + |
| 98 | + class Meta: |
| 99 | + actions__submit__post_handler = save_nested_forms |
| 100 | + |
| 101 | + # @test |
| 102 | + t = ( |
| 103 | + # @end |
| 104 | + |
| 105 | + ParentForm() |
| 106 | + |
| 107 | + # @test |
| 108 | + ) |
| 109 | + |
| 110 | + show_output(t.bind(request=request)) |
| 111 | + # @end |
| 112 | + |
| 113 | + # language=rst |
| 114 | + """ |
| 115 | + If you already have a custom model field for ordering, you can register it as a reorderderin column: |
| 116 | +
|
| 117 | + .. code-block:: python |
| 118 | +
|
| 119 | + from iommi import register_edit_column_factory |
| 120 | +
|
| 121 | + register_edit_column_factory(YourOrderField, shortcut_name='reorder_handle') |
| 122 | + """ |
| 123 | + |
| 124 | + # language=rst |
| 125 | + """ |
| 126 | + If you just use any django integer field, you can still make your EditTable orderable with: |
| 127 | +
|
| 128 | + .. code-block:: python |
| 129 | +
|
| 130 | + class MyEditTable(EditTable): |
| 131 | + class Meta: |
| 132 | + auto__model = Foo |
| 133 | + columns__bar = EditColumn.reorder_handle() |
| 134 | + """ |
| 135 | + |
| 136 | + # language=rst |
| 137 | + """ |
| 138 | + We use SortableJS and if you want, you can also pass other `options <https://github.com/SortableJS/Sortable?tab=readme-ov-file#options>`__. |
| 139 | + For example this allows multi-drag: |
| 140 | +
|
| 141 | + .. code-block:: python |
| 142 | +
|
| 143 | + class MyEditTable(EditTable): |
| 144 | + class Meta: |
| 145 | + auto__model = Foo |
| 146 | + columns__bar = EditColumn.reorder_handle() |
| 147 | + reorderable = { |
| 148 | + "multiDrag": True, # Enable multi-drag |
| 149 | + "selectedClass": 'selected', # The class applied to the selected items |
| 150 | + "fallbackTolerance": 3, # So that we can select items on mobile |
| 151 | + } |
| 152 | + """ |
0 commit comments