|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import zipfile |
1 | 4 | from django.db import models |
2 | 5 | from django.utils.translation import gettext_lazy as _ |
3 | 6 | from django.contrib.postgres.fields import ArrayField |
4 | 7 | from django.dispatch import receiver |
5 | 8 | from django.db.models import signals |
6 | 9 | from django.core.cache import caches |
7 | | - |
| 10 | +from django.db import models |
| 11 | +from geonode_mapstore_client.utils import validate_zip_file, clear_extension_caches |
8 | 12 | from geonode_mapstore_client.templatetags.get_search_services import ( |
9 | 13 | populate_search_service_options, |
10 | 14 | ) |
| 15 | +from django.conf import settings |
11 | 16 |
|
12 | 17 |
|
13 | 18 | class SearchService(models.Model): |
@@ -73,3 +78,84 @@ def post_save_search_service(instance, sender, created, **kwargs): |
73 | 78 | services_cache.delete("search_services") |
74 | 79 |
|
75 | 80 | services_cache.set("search_services", populate_search_service_options(), 300) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +def extension_upload_path(instance, filename): |
| 85 | + return f"mapstore_extensions/{filename}" |
| 86 | + |
| 87 | + |
| 88 | +class Extension(models.Model): |
| 89 | + name = models.CharField( |
| 90 | + max_length=255, |
| 91 | + unique=True, |
| 92 | + blank=True, # Will be populated from the zip filename |
| 93 | + help_text="Name of the extension, derived from the zip file name. Must be unique.", |
| 94 | + ) |
| 95 | + uploaded_file = models.FileField( |
| 96 | + upload_to=extension_upload_path, |
| 97 | + validators=[validate_zip_file], |
| 98 | + help_text="Upload the MapStore extension as a zip folder.", |
| 99 | + ) |
| 100 | + active = models.BooleanField( |
| 101 | + default=True, |
| 102 | + help_text="Whether the extension is active and should be included in the index.", |
| 103 | + ) |
| 104 | + is_map_extension = models.BooleanField( |
| 105 | + default=False, |
| 106 | + help_text="Check if this extension is a map-specific plugin for Map Viewers.", |
| 107 | + ) |
| 108 | + created_at = models.DateTimeField(auto_now_add=True) |
| 109 | + updated_at = models.DateTimeField(auto_now=True) |
| 110 | + |
| 111 | + def __str__(self): |
| 112 | + return self.name |
| 113 | + |
| 114 | + def save(self, *args, **kwargs): |
| 115 | + if not self.name and self.uploaded_file: |
| 116 | + self.name = os.path.splitext(os.path.basename(self.uploaded_file.name))[0] |
| 117 | + super().save(*args, **kwargs) |
| 118 | + |
| 119 | + class Meta: |
| 120 | + ordering = ("name",) |
| 121 | + verbose_name = "MapStore Extension" |
| 122 | + verbose_name_plural = "MapStore Extensions" |
| 123 | + |
| 124 | + |
| 125 | +@receiver(signals.post_save, sender=Extension) |
| 126 | +def handle_extension_upload(sender, instance, **kwargs): |
| 127 | + """ |
| 128 | + Unzips the extension file and clears the API cache after saving. |
| 129 | + """ |
| 130 | + target_path = os.path.join( |
| 131 | + settings.STATIC_ROOT, settings.MAPSTORE_EXTENSIONS_FOLDER_PATH, instance.name |
| 132 | + ) |
| 133 | + |
| 134 | + if os.path.exists(target_path): |
| 135 | + shutil.rmtree(target_path) |
| 136 | + |
| 137 | + try: |
| 138 | + with zipfile.ZipFile(instance.uploaded_file.path, "r") as zip_ref: |
| 139 | + zip_ref.extractall(target_path) |
| 140 | + except FileNotFoundError: |
| 141 | + pass |
| 142 | + |
| 143 | + clear_extension_caches() |
| 144 | + |
| 145 | + |
| 146 | +@receiver(signals.post_delete, sender=Extension) |
| 147 | +def handle_extension_delete(sender, instance, **kwargs): |
| 148 | + """ |
| 149 | + Removes the extension's files and clears the API cache on deletion. |
| 150 | + """ |
| 151 | + if instance.name: |
| 152 | + extension_path = os.path.join( |
| 153 | + settings.STATIC_ROOT, settings.MAPSTORE_EXTENSIONS_FOLDER_PATH, instance.name |
| 154 | + ) |
| 155 | + if os.path.exists(extension_path): |
| 156 | + shutil.rmtree(extension_path) |
| 157 | + |
| 158 | + if instance.uploaded_file and os.path.exists(instance.uploaded_file.path): |
| 159 | + os.remove(instance.uploaded_file.path) |
| 160 | + |
| 161 | + clear_extension_caches() |
0 commit comments