Skip to content

Commit

Permalink
feat(Mailbox): Integrate django-autoslug and update Mailbox model wit…
Browse files Browse the repository at this point in the history
…h AutoSlugField

- Added `django-autoslug` package to the project dependencies.
  - Updated `pyproject.toml` and `poetry.lock` to include the new package.

- Modified `Mailbox` model in `sage_mailbox/models/mailbox.py`:
  - Replaced the existing slug field with `AutoSlugField` from `django-autoslug`.
  - Configured `AutoSlugField` to automatically convert periods (.) to hyphens (-) in slugs for better URL compatibility.

- Updated `sage_mailbox/admin/mailbox.py`:
  - Ensured the admin interface reflects the changes in the `Mailbox` model, particularly the use of `AutoSlugField` for generating slugs.

closes #16.
  • Loading branch information
MohmdFo committed Aug 31, 2024
1 parent 3dd775c commit 28b65c5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ django = "^5.0.7"
python-sage-imap = "^0.4.2"
django-jsonform = "^2.22.0"
python-dateutil = "^2.9.0.post0"
django-autoslug = "^1.9.9"

[tool.poetry.group.dev.dependencies]
black = "^24.4.2"
Expand Down
2 changes: 1 addition & 1 deletion sage_mailbox/admin/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MailboxAdmin(admin.ModelAdmin):
(None, {"fields": ("name", "slug", "folder_type")}),
(_("Change Log"), {"fields": ("created_at", "modified_at")}),
)
readonly_fields = ("created_at", "modified_at")
readonly_fields = ("created_at", "modified_at", "slug")
actions = [delete_selected]

def get_readonly_fields(self, request, obj=None):
Expand Down
37 changes: 14 additions & 23 deletions sage_mailbox/models/mailbox.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from autoslug import AutoSlugField
from django.db import models
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _

from sage_mailbox.models.mixins import TimestampMixin
from sage_mailbox.utils import map_to_standard_name
from sage_mailbox.validators import validate_folder_name


def custom_slugify(value):
return value.replace(".", "-").replace(" ", "-")


class StandardMailboxNames(models.TextChoices):
INBOX = "INBOX", _("Inbox")
SENT = "SENT", _("Sent")
DRAFTS = "DRAFTS", _("Drafts")
SPAM = "SPAM", _("Spam")
JUNK = "JUNK", _("Junk")
TRASH = "TRASH", _("Trash")
CUSTOM = "CUSTOM", _("Custom")

Expand All @@ -21,7 +26,7 @@ class StandardMailboxNames(models.TextChoices):
"Sent Items": StandardMailboxNames.SENT,
"Sent": StandardMailboxNames.SENT,
"Drafts": StandardMailboxNames.DRAFTS,
"Junk": StandardMailboxNames.SPAM,
"Junk": StandardMailboxNames.JUNK,
"Spam": StandardMailboxNames.SPAM,
"Trash": StandardMailboxNames.TRASH,
"Deleted Items": StandardMailboxNames.TRASH,
Expand All @@ -47,11 +52,14 @@ class Mailbox(TimestampMixin):
choices=StandardMailboxNames.choices,
default=StandardMailboxNames.CUSTOM,
)
slug = models.SlugField(
slug = AutoSlugField(
verbose_name=_("Slug"),
max_length=255,
unique=True,
always_update=True,
allow_unicode=True,
verbose_name=_("Slug"),
slugify=custom_slugify,
populate_from="name",
help_text=_("The unique slug for the mailbox, generated from the name."),
db_comment="The unique slug generated from the mailbox name.",
)
Expand All @@ -69,28 +77,11 @@ class Meta:
)

def save(self, *args, **kwargs):
# Generate slug if not provided or if it's not unique
if (
not self.slug
or Mailbox.objects.filter(slug=self.slug).exclude(pk=self.pk).exists()
):
self.slug = self.generate_unique_slug()

# Map the name to the standard folder type
self.folder_type = map_to_standard_name(self.name)
if not self.pk:
self.folder_type = map_to_standard_name(self.name)

super(Mailbox, self).save(*args, **kwargs)

def generate_unique_slug(self):
original_slug = slugify(self.name.lower())
unique_slug = original_slug
num = 1

while Mailbox.objects.filter(slug=unique_slug).exclude(pk=self.pk).exists():
unique_slug = f"{original_slug}-{num}"
num += 1

return unique_slug

def __str__(self):
return self.name

0 comments on commit 28b65c5

Please sign in to comment.