|
| 1 | +import re |
| 2 | + |
1 | 3 | from django.contrib import admin |
2 | 4 | from django.conf import settings |
3 | 5 | from django.utils.translation import gettext_lazy as _ |
|
6 | 8 |
|
7 | 9 | from geotrek.flatpages import models as flatpages_models |
8 | 10 | from geotrek.flatpages.forms import FlatPageForm, MenuItemForm |
9 | | - |
| 11 | +from geotrek.flatpages.models import FlatPage |
| 12 | +from geotrek.common.models import FileType, Attachment |
| 13 | +from django.contrib.auth.models import User |
10 | 14 |
|
11 | 15 | if 'modeltranslation' in settings.INSTALLED_APPS: |
12 | 16 | from modeltranslation.admin import TabbedTranslationAdmin |
@@ -59,13 +63,37 @@ def get_form(self, request, *args, **kwargs): |
59 | 63 | return form_class |
60 | 64 |
|
61 | 65 | def save_related(self, request, form, formsets, change): |
| 66 | + |
62 | 67 | # We override `ModelAdmin.save_related` to add/update/delete the attachment. Why not in |
63 | 68 | # `ModelAdmin.save_form` which may have looked like a better place? This is because Django's ModelAdmin |
64 | 69 | # does not commit the object in `save_form`, it waits for formsets' validations. We override |
65 | 70 | # `save_related` because we need the committed object (with an ID). |
66 | 71 | # See `django.contrib.admin.options.py:L1578` |
67 | 72 | super().save_related(request, form, formsets, change) |
68 | 73 | form.save_cover_image() |
| 74 | + all_attachments = form.instance.attachments.all() |
| 75 | + attachments_url = [] |
| 76 | + for field in form.instance._meta.get_fields(): |
| 77 | + if field.get_internal_type() == "TextField": |
| 78 | + field_value = getattr(form.instance, field.name) |
| 79 | + if field_value is not None: |
| 80 | + matches = re.finditer(r'(src=\".+?/media/)(?P<url>.+?)\" ', field_value) |
| 81 | + for match in matches: |
| 82 | + if match["url"] not in all_attachments: |
| 83 | + page = FlatPage.add_root(title="tinymceAttachment") |
| 84 | + filetype = FileType.objects.get(type="Photographie") |
| 85 | + attachment = Attachment.objects.create( |
| 86 | + content_object=page, |
| 87 | + attachment_file=match["url"], |
| 88 | + author='', |
| 89 | + filetype=filetype, |
| 90 | + creator=request.user, |
| 91 | + ) |
| 92 | + form.instance.attachments.add(attachment) |
| 93 | + attachments_url.append(match["url"]) |
| 94 | + |
| 95 | + # print("Attachements_URL => ", attachments_url) |
| 96 | + # print("Attachements => ", form.instance.attachments.all()) |
69 | 97 |
|
70 | 98 |
|
71 | 99 | class MenuItemAdmin(BaseAdmin): |
@@ -125,6 +153,7 @@ def save_related(self, request, form, formsets, change): |
125 | 153 | # does not commit the object in `save_form`, it waits for formsets' validations. We override |
126 | 154 | # `save_related` because we need the committed object (with an ID). |
127 | 155 | # See `django.contrib.admin.options.py:L1578` |
| 156 | + print("trolololool") |
128 | 157 | super().save_related(request, form, formsets, change) |
129 | 158 | form.save_thumbnail() |
130 | 159 |
|
|
0 commit comments