Skip to content

Commit

Permalink
updating our merge_tags_view method
Browse files Browse the repository at this point in the history
- We do not need to get the tag ids from the query params
- Instead we just grab from session data
  • Loading branch information
guel-codes committed Jul 15, 2024
1 parent 6aef624 commit 1988558
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
12 changes: 10 additions & 2 deletions sample_taggit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,17 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
# "default": {
# "ENGINE": "django.db.backends.sqlite3",
# "NAME": BASE_DIR / "db.sqlite3",
# }
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
"ENGINE": "django.db.backends.postgresql",
"NAME": "sample_taggit_app",
"USER": "postgres",
"PASSWORD": "postgres",
"HOST": "localhost",
"PORT": "5432",
}
}

Expand Down
21 changes: 6 additions & 15 deletions taggit/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,22 @@ def get_urls(self):
]
return custom_urls + urls

@admin.action(
description="Merge selected tags"
)
@admin.action(description="Merge selected tags")
def render_tag_form(self, request, queryset):
selected = request.POST.getlist(admin.helpers.ACTION_CHECKBOX_NAME)
if not selected:
self.message_user(request, "Please select at least one tag.")
return redirect(request.get_full_path())

selected_tag_ids = ",".join(selected)
redirect_url = (
f"{request.get_full_path()}merge-tags/?selected_tags={selected_tag_ids}"
)
redirect_url = f"{request.get_full_path()}merge-tags/"

request.session["selected_tag_ids"] = selected_tag_ids

return redirect(redirect_url)

def merge_tags_view(self, request):
if request.method == "GET":
selected_tag_ids = request.GET.get("selected_tags", "").split(",")

# store selected_tag_ids in session data until they are merged
request.session["selected_tag_ids"] = selected_tag_ids
else:
selected_tag_ids = request.session.get("selected_tag_ids", [])

selected_tag_ids = request.session.get("selected_tag_ids", "").split(",")
if request.method == "POST":
form = MergeTagsForm(request.POST)
if form.is_valid():
Expand All @@ -71,6 +62,7 @@ def merge_tags_view(self, request):
tagged_item.save()
# tag.delete() #this will delete the selected tags after merge

self.message_user(request, "Tags have been merged")
# clear the selected_tag_ids from session after merge is complete
request.session.pop("selected_tag_ids", None)
return redirect("..")
Expand All @@ -84,4 +76,3 @@ def merge_tags_view(self, request):
}

return render(request, "admin/taggit/merge_tags_form.html", context)

0 comments on commit 1988558

Please sign in to comment.