-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend] add activity to admin management
* minor modifications to the admin model * add admin models management to admin.py * implement effective signals to handle admin models instances, addition, modification and deletion with proper cleanup Issue: #1094 Signed-off-by: Ndibe Raymond Olisaemeka <[email protected]>
- Loading branch information
Ndibe Raymond Olisaemeka
committed
Jul 16, 2024
1 parent
85aad49
commit f122b76
Showing
6 changed files
with
414 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,102 @@ | ||
from django.contrib import admin | ||
from django.contrib import admin, messages | ||
|
||
# Register your models here. | ||
from .models import ( | ||
Activity, | ||
ActivityImage, | ||
ActivityMakingStep, | ||
Image, | ||
InspiringArtist, | ||
InspiringExample, | ||
) | ||
|
||
|
||
class InlineActivityImages(admin.StackedInline): | ||
model = ActivityImage | ||
|
||
|
||
class InlineActivityMakingSteps(admin.StackedInline): | ||
model = ActivityMakingStep | ||
|
||
|
||
class InlineInspiringExamples(admin.StackedInline): | ||
model = InspiringExample | ||
|
||
|
||
class InspiringArtistAdmin(admin.ModelAdmin): | ||
search_fields = ("name",) | ||
list_display = ( | ||
"name", | ||
"image", | ||
) | ||
list_filter = ("name",) | ||
|
||
|
||
class ActivityAdmin(admin.ModelAdmin): | ||
list_display = ("title", "id", "created_on", "publish") | ||
list_filter = ( | ||
"created_on", | ||
"publish", | ||
) | ||
search_fields = ( | ||
"title", | ||
"id", | ||
"category", | ||
) | ||
ordering = ["-created_on"] | ||
actions = ["publish", "un_publish", "delete_selected"] | ||
inlines = [InlineActivityImages, InlineActivityMakingSteps, InlineInspiringExamples] | ||
list_per_page = 50 # paginate when more than 50 items | ||
|
||
def un_publish(self, request, queryset): | ||
""" | ||
This function is used to unpublish selected activities | ||
""" | ||
queryset.update(publish=False) | ||
messages.success(request, "Selected records were unpublished successfully.") | ||
|
||
def publish(self, request, queryset): | ||
""" | ||
This function is used to publish selected activities | ||
""" | ||
queryset = queryset.filter(publish=False) | ||
queryset.update(publish=True) | ||
messages.success(request, "Selected records were published successfully.") | ||
|
||
def delete_selected(self, request, queryset): | ||
""" | ||
This function is used to delete selected activities | ||
""" | ||
queryset.delete() | ||
messages.success(request, "Selected records were deleted successfully.") | ||
|
||
def get_readonly_fields(self, request, obj=None): | ||
return [ | ||
"id", | ||
"created_on", | ||
"views_count", | ||
"saved_by", | ||
"views", | ||
"saved_count", | ||
"slug", | ||
] | ||
|
||
un_publish.short_description = "Unpublish selected activities" | ||
publish.short_description = "Publish selected activities" | ||
delete_selected.short_description = "Delete selected activities" | ||
|
||
|
||
class ActivityImageAdmin(admin.ModelAdmin): | ||
search_fields = ["activity__title", "activity__id", "image__public_id"] | ||
list_display = ["activity", "image"] | ||
|
||
|
||
class ImageAdmin(admin.ModelAdmin): | ||
search_fields = ["public_id"] | ||
list_display = ["public_id", "file_url"] | ||
# should not be able to edit this from the admin panel ? | ||
|
||
|
||
admin.site.register(InspiringArtist, InspiringArtistAdmin) | ||
admin.site.register(Activity, ActivityAdmin) | ||
admin.site.register(ActivityImage, ActivityImageAdmin) | ||
admin.site.register(Image, ImageAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
zubhub_backend/zubhub/activities/migrations/0018_alter_activity_options.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Generated by Django 3.2 on 2024-06-24 16:36 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("activities", "0017_alter_activity_materials_used"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name="activity", | ||
options={"verbose_name_plural": "Activities"}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.