Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace self-made sortable model with jazzband django-admin-sortable #1698

Merged
merged 2 commits into from
Sep 29, 2023
Merged
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
10 changes: 6 additions & 4 deletions forum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
# See AUTHORS file.
#

from adminsortable.admin import SortableAdmin
from django.contrib import admin
from forum.models import Forum, Thread, Post

from forum.models import Forum, Post, Thread


@admin.register(Forum)
class ForumAdmin(admin.ModelAdmin):
class ForumAdmin(SortableAdmin):
raw_id_fields = ('last_post', )
list_display = ('name', 'num_threads', 'change_order')
list_display = ('name', 'num_threads')



Expand All @@ -42,4 +45,3 @@ class PostAdmin(admin.ModelAdmin):
raw_id_fields = ('author', 'thread')
list_display = ('thread', 'author', 'created')
search_fields = ('=author__username', "body")

18 changes: 18 additions & 0 deletions forum/migrations/0004_alter_forum_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.17 on 2023-09-27 19:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('forum', '0003_auto_20230201_1102'),
]

operations = [
migrations.AlterField(
model_name='forum',
name='order',
field=models.PositiveIntegerField(default=0, editable=False),
),
]
1 change: 1 addition & 0 deletions freesound/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'silk',
'admin_reorder',
'captcha',
'adminsortable',
]

# Specify custom ordering of models in Django Admin index
Expand Down
3 changes: 0 additions & 3 deletions freesound/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import comments.views
import bookmarks.views
import follow.views
import general.views
import donations.views
import utils.tagrecommendation_utilities as tagrec
from apiv2.apiv2_utils import apiv1_end_of_life_message
Expand Down Expand Up @@ -123,8 +122,6 @@
re_path(r'^crossdomain\.xml$', TemplateView.as_view(template_name='crossdomain.xml'), name="crossdomain"),

# admin views
re_path(r'^admin/orderedmove/(?P<direction>up|down)/(?P<model_type_id>\d+)/(?P<model_id>\d+)/$',
general.views.admin_move_ordered_model, name="admin-move"),
path('admin/doc/', include('django.contrib.admindocs.urls')),
path('admin/', admin.site.urls),

Expand Down
52 changes: 5 additions & 47 deletions general/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
# Authors:
# See AUTHORS file.
#

from comments.models import Comment
from adminsortable.models import SortableMixin
from django.contrib.auth.models import User
from django.contrib.contenttypes import fields
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.db import models

from favorites.models import Favorite
from ratings.models import SoundRating
from tags.models import TaggedItem


class SocialModel(models.Model):
tags = fields.GenericRelation(TaggedItem)
fans = fields.GenericRelation(Favorite)
Expand All @@ -40,48 +38,8 @@ class AkismetSpam(SocialModel):
spam = models.TextField()
created = models.DateTimeField(auto_now_add=True)

class OrderedModel(models.Model):
order = models.PositiveIntegerField(editable=False)

def save(self, *args, **kwargs):
if not self.id:
try:
self.order = self.__class__.objects.all().order_by("-order")[0].order + 1
except IndexError:
self.order = 0
super().save(*args, **kwargs)

def change_order(self):
model_type_id = ContentType.objects.get_for_model(self.__class__).id
model_id = self.id
kwargs = {"direction": "up", "model_type_id": model_type_id, "model_id": model_id}
url_up = reverse("admin-move", kwargs=kwargs)
kwargs["direction"] = "down"
url_down = reverse("admin-move", kwargs=kwargs)
return mark_safe(f'<a href="{url_up}">up</a> | <a href="{url_down}">down</a>')
change_order.short_description = 'Move'
change_order.admin_order_field = 'order'

@staticmethod
def move(direction, model_type_id, model_id):
try:
ModelClass = ContentType.objects.get(id=model_type_id).model_class()

current_model = ModelClass.objects.get(id=model_id)

if direction == "down":
swap_model = ModelClass.objects.filter(order__gt=current_model.order).order_by("order")[0]
elif direction == "up":
swap_model = ModelClass.objects.filter(order__lt=current_model.order).order_by("-order")[0]

current_model.order, swap_model.order = swap_model.order, current_model.order

current_model.save()
swap_model.save()
except IndexError:
pass
except ModelClass.DoesNotExist:
pass
class OrderedModel(SortableMixin):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we found that there was just one model that was sortable, right? Licenses is sortable but we can't find where that logic is used. In that case, I'd just add SortableMixin and the order field directly to the Forum model

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, alternatively we could make it sortable in admin with adding LicenseAdmin(SortableAdmin).
What would happen if we remove the ordering from the License model (table)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd double-check with @ffont if we need to order licenses, and if not we remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sortable is only used in forums because, well, we wanted a custom ordering to display forums. But this is not needed for Licenses so no need to add it to them.

order = models.PositiveIntegerField(default=0, editable=False)
Bomme marked this conversation as resolved.
Show resolved Hide resolved

class Meta:
ordering = ["order"]
Expand Down
39 changes: 0 additions & 39 deletions general/views.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ boto3==1.26.130
celery==4.4.7
debugpy==1.5.1
dj-database-url==0.5.0
django-admin-sortable==2.2.4
django-amazon-ses==4.0.1
django-cors-headers==3.13.0
django-debug-toolbar==3.1.1
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ alabaster==0.7.13
# via sphinx
amqp==2.6.1
# via kombu
appnope==0.1.3
# via ipython
asgiref==3.7.2
# via django
asttokens==2.2.1
Expand Down Expand Up @@ -76,6 +78,7 @@ dj-database-url==0.5.0
django==3.2.17
# via
# -r requirements.in
# django-admin-sortable
# django-amazon-ses
# django-cors-headers
# django-debug-toolbar
Expand All @@ -87,6 +90,8 @@ django==3.2.17
# django-redis
# django-silk
# djangorestframework
django-admin-sortable==2.2.4
# via -r requirements.in
django-amazon-ses==4.0.1
# via -r requirements.in
django-cors-headers==3.13.0
Expand Down
18 changes: 18 additions & 0 deletions sounds/migrations/0050_alter_license_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.17 on 2023-09-27 19:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sounds', '0049_license_summary_for_describe_form'),
]

operations = [
migrations.AlterField(
model_name='license',
name='order',
field=models.PositiveIntegerField(default=0, editable=False),
),
]
Loading