Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
403 changes: 403 additions & 0 deletions STORAGE.md

Large diffs are not rendered by default.

52 changes: 45 additions & 7 deletions django-icon-picker/django_icon_picker/field.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# fields.py
# field.py
from django.db import models
from .widgets import IconPicker
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.conf import settings
from django.db.models.signals import pre_delete
from .widgets import IconPicker
import os
import uuid


class IconField(models.CharField):
description = "A custom field to store icon information."
description = "A custom field to store icon information with Django-storages support."

def __init__(self, *args, **kwargs):
# Allow custom storage backend
self.storage = kwargs.pop('storage', None)
kwargs["max_length"] = 255
super().__init__(*args, **kwargs)

Expand All @@ -25,8 +31,40 @@ def contribute_to_class(self, cls, name, **kwargs):

def _delete_file(self, sender, instance, **kwargs):
file_path = getattr(instance, self.attname)
if file_path and os.path.exists(file_path):
if file_path:
try:
os.remove(file_path)
except OSError as e:
pass
# Use the configured storage backend
storage = self.storage or default_storage

# Check if using cloud storage or local storage
if hasattr(storage, 'delete'):
# For cloud storage backends
if storage.exists(file_path):
storage.delete(file_path)
else:
# Fallback for local file system
if os.path.exists(file_path):
os.remove(file_path)
except Exception as e:
# Log the error but don't raise it to prevent deletion failures
import logging
logger = logging.getLogger(__name__)
logger.warning(f"Failed to delete icon file {file_path}: {str(e)}")

def get_storage(self):
"""Get the storage backend for this field"""
return self.storage or default_storage

def get_file_url(self, file_path):
"""Get the URL for accessing the file"""
if not file_path:
return None

storage = self.get_storage()

# For cloud storage, use the storage's url method
if hasattr(storage, 'url'):
return storage.url(file_path)

# For local storage fallback
return f"/{file_path}" if not file_path.startswith('/') else file_path
Loading