More DRY code cleanup.

This commit is contained in:
RecursiveGreen 2018-01-14 15:21:04 -05:00
parent 25485b54b9
commit a7ccb61e07
3 changed files with 40 additions and 37 deletions

View file

@ -76,3 +76,16 @@ def naturalize(text):
text = re.sub(r'\d+', naturalize_int_match, text) text = re.sub(r'\d+', naturalize_int_match, text)
return text return text
def build_message_start(quantity, model):
"""
The beggining of a message based on the quantity and singular/plural name
of the model involved.
"""
if quantity == 1:
message = '1 {} was'.format(model._meta.verbose_name)
else:
message = '{} {} were'.format(str(quantity),
model._meta.verbose_name_plural)
return message

View file

@ -0,0 +1,10 @@
from django.contrib import messages
from django.utils import timezone
from core.utils import build_message_start
def publish_items(request, queryset):
rows_updated = queryset.update(published_date=timezone.now())
message = build_message_start(rows_updated, queryset.model)
messages.success(request, '{} successfully published.'.format(message))

View file

@ -3,8 +3,8 @@ from django.db import models
from django.forms import TextInput from django.forms import TextInput
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.utils import timezone
from .actions import publish_items
from .forms import ArtistFormSet from .forms import ArtistFormSet
from .models import Album, Artist, Game, Song from .models import Album, Artist, Game, Song
@ -21,7 +21,7 @@ class AlbumAdmin(admin.ModelAdmin):
# Detail List display # Detail List display
list_display = ('title', '_is_enabled', '_is_published') list_display = ('title', '_is_enabled', '_is_published')
search_fields = ['title'] search_fields = ['title']
actions = ['publish_items'] actions = ['publish_albums']
# Edit Form display # Edit Form display
readonly_fields = ('created_date', 'modified_date') readonly_fields = ('created_date', 'modified_date')
@ -39,14 +39,9 @@ class AlbumAdmin(admin.ModelAdmin):
}) })
) )
def publish_items(self, request, queryset): def publish_albums(self, request, queryset):
rows_updated = queryset.update(published_date=timezone.now()) publish_items(request, queryset)
if rows_updated == 1: publish_albums.short_description = "Publish selected albums"
msg = '1 album was'
else:
msg = '{} albums were'.format(str(rows_updated))
self.message_user(request, '{} successfully published.'.format(msg))
publish_items.short_description = "Publish selected items"
@admin.register(Artist) @admin.register(Artist)
@ -58,7 +53,7 @@ class ArtistAdmin(admin.ModelAdmin):
'_is_enabled', '_is_enabled',
'_is_published') '_is_published')
search_fields = ['first_name', 'alias', 'last_name'] search_fields = ['first_name', 'alias', 'last_name']
actions = ['publish_items'] actions = ['publish_artists']
# Edit Form display # Edit Form display
readonly_fields = ('created_date', 'modified_date') readonly_fields = ('created_date', 'modified_date')
@ -76,14 +71,9 @@ class ArtistAdmin(admin.ModelAdmin):
}) })
) )
def publish_items(self, request, queryset): def publish_artists(self, request, queryset):
rows_updated = queryset.update(published_date=timezone.now()) publish_items(request, queryset)
if rows_updated == 1: publish_artists.short_description = "Publish selected artists"
msg = '1 artist was'
else:
msg = '{} artists were'.format(str(rows_updated))
self.message_user(request, '{} successfully published.'.format(msg))
publish_items.short_description = "Publish selected items"
@admin.register(Game) @admin.register(Game)
@ -91,7 +81,7 @@ class GameAdmin(admin.ModelAdmin):
# Detail List display # Detail List display
list_display = ('title', '_is_enabled', '_is_published') list_display = ('title', '_is_enabled', '_is_published')
search_fields = ['title'] search_fields = ['title']
actions = ['publish_items'] actions = ['publish_games']
# Edit Form display # Edit Form display
readonly_fields = ('created_date', 'modified_date') readonly_fields = ('created_date', 'modified_date')
@ -109,14 +99,9 @@ class GameAdmin(admin.ModelAdmin):
}) })
) )
def publish_items(self, request, queryset): def publish_games(self, request, queryset):
rows_updated = queryset.update(published_date=timezone.now()) publish_items(request, queryset)
if rows_updated == 1: publish_games.short_description = "Publish selected games"
msg = '1 game was'
else:
msg = '{} games were'.format(str(rows_updated))
self.message_user(request, '{} successfully published.'.format(msg))
publish_items.short_description = "Publish selected items"
@admin.register(Song) @admin.register(Song)
@ -133,7 +118,7 @@ class SongAdmin(admin.ModelAdmin):
'_is_enabled', '_is_enabled',
'_is_published') '_is_published')
search_fields = ['title'] search_fields = ['title']
actions = ['publish_items', 'add_artists', 'remove_artists'] actions = ['publish_songs', 'add_artists', 'remove_artists']
# Edit Form display # Edit Form display
exclude = ('artists',) exclude = ('artists',)
@ -230,11 +215,6 @@ class SongAdmin(admin.ModelAdmin):
return self.change_artists(request, queryset, remove=True) return self.change_artists(request, queryset, remove=True)
remove_artists.short_description = "Remove artists from selected items" remove_artists.short_description = "Remove artists from selected items"
def publish_items(self, request, queryset): def publish_songs(self, request, queryset):
rows_updated = queryset.update(published_date=timezone.now()) publish_items(request, queryset)
if rows_updated == 1: publish_songs.short_description = "Publish selected songs"
msg = '1 song was'
else:
msg = '{} songs were'.format(str(rows_updated))
self.message_user(request, '{} successfully published.'.format(msg))
publish_items.short_description = "Publish selected items"