Browse Source
Add share link property to various models and update serializers
Add share link property to various models and update serializers
- Introduced a `share_link` property in the `Article`, `Hadis`, `Book`, `Podcast`, `Video`, and `Transmitter` models to generate shareable links based on their slugs. - Updated corresponding serializers to include the `share_link` field as read-only. - Added a management command to refresh share links for existing Hadis models, ensuring all instances have updated slugs and share links. - Adjusted the `settings` to define `DOVODI_DOMAIN` for consistent link generation across models.master
15 changed files with 128 additions and 28 deletions
-
7apps/article/models.py
-
6apps/article/serializers.py
-
44apps/hadis/management/commands/refresh_share_links.py
-
28apps/hadis/models/hadis.py
-
7apps/hadis/models/reference.py
-
12apps/hadis/models/transmitter.py
-
6apps/hadis/serializers/hadis.py
-
7apps/hadis/serializers/reference.py
-
7apps/library/models.py
-
3apps/library/serializers.py
-
7apps/podcast/models.py
-
6apps/podcast/serializers.py
-
7apps/video/models.py
-
6apps/video/serializers.py
-
3config/settings/base.py
@ -0,0 +1,44 @@ |
|||
from django.core.management.base import BaseCommand |
|||
from django.db import transaction |
|||
from apps.hadis.models import Hadis, HadisCorrection, TransmitterOriginalText |
|||
|
|||
class Command(BaseCommand): |
|||
help = 'Refreshes slugs and share_links by re-saving all existing instances of Hadis models.' |
|||
|
|||
def handle(self, *args, **options): |
|||
models_to_refresh = [ |
|||
(Hadis, "Hadis"), |
|||
(HadisCorrection, "Hadis Correction"), |
|||
(TransmitterOriginalText, "Transmitter Original Text"), |
|||
] |
|||
|
|||
for model_class, name in models_to_refresh: |
|||
self.stdout.write(self.style.WARNING(f'--- Processing {name} ---')) |
|||
|
|||
instances = model_class.objects.all() |
|||
count = instances.count() |
|||
|
|||
if count == 0: |
|||
self.stdout.write(f"No {name} instances found.") |
|||
continue |
|||
|
|||
processed = 0 |
|||
# Using transaction.atomic to ensure database integrity |
|||
with transaction.atomic(): |
|||
for instance in instances: |
|||
try: |
|||
# This triggers your updated .save() logic |
|||
instance.save() |
|||
processed += 1 |
|||
|
|||
if processed % 100 == 0: |
|||
self.stdout.write(f"Updated {processed}/{count} {name}...") |
|||
|
|||
except Exception as e: |
|||
self.stdout.write(self.style.ERROR( |
|||
f"Error saving {name} ID {instance.id}: {str(e)}" |
|||
)) |
|||
|
|||
self.stdout.write(self.style.SUCCESS(f'Successfully refreshed {processed} {name} instances.')) |
|||
|
|||
self.stdout.write(self.style.SUCCESS('--- All models refreshed successfully ---')) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue