Skip to content

Commit

Permalink
fix: CSV Export contains the polymorphic content type when it shouldn…
Browse files Browse the repository at this point in the history
…'t (#43)
  • Loading branch information
Aiky30 authored Nov 24, 2021
1 parent f02691a commit 83e264e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog

unreleased
==========
* fix: CSV Export contains the polymorphic content type when it shouldn't

0.0.6 (2021-11-23)
==================
Expand Down
3 changes: 1 addition & 2 deletions djangocms_content_expiry/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.apps import apps
from django.conf.urls import url
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.urls import reverse
Expand Down Expand Up @@ -228,7 +227,7 @@ def export_to_csv(self, request):
])

for content_expiry in queryset:
content_type = ContentType.objects.get_for_model(content_expiry.version.content)
content_type = self.content_type(content_expiry)
expiry_date = self._format_export_datetime(content_expiry.expires)
version_state = content_expiry.version.get_state_display()
# Get an external / sharable link
Expand Down
4 changes: 2 additions & 2 deletions djangocms_content_expiry/test_utils/polls/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.conf.urls import url
from django.contrib import admin
from django.urls import re_path

from .models import Answer, Poll, PollContent
from .views import PreviewView
Expand All @@ -10,7 +10,7 @@ class PollContentAdmin(admin.ModelAdmin):
def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
return [
url(
re_path(
r"^(?P<id>\d+)/preview/$",
self.admin_site.admin_view(PreviewView.as_view()),
name="{}_{}_preview".format(*info),
Expand Down
4 changes: 2 additions & 2 deletions djangocms_content_expiry/test_utils/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def __str__(self):
return self.text

def get_absolute_url(self):
return reverse("admin:polls_pollcontent_change", args=[self.id])
return reverse(f"admin:{self._meta.app_label}_{self._meta.model_name}_change", args=[self.id])

def get_preview_url(self):
return reverse("admin:polls_pollcontent_preview", args=[self.id])
return reverse(f"admin:{self._meta.app_label}_{self._meta.model_name}_preview", args=[self.id])


class Answer(models.Model):
Expand Down
24 changes: 19 additions & 5 deletions djangocms_content_expiry/test_utils/polymorphic_project/admin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
from django.contrib import admin
from django.urls import re_path

from .models import (
ArtProjectContent,
ProjectContent,
ProjectGrouper,
ResearchProjectContent,
)
from .views import PreviewView


@admin.register(ArtProjectContent)
class ArtProjectContentAdmin(admin.ModelAdmin):
pass
class VersionedContentAdmin(admin.ModelAdmin):
def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
return [
re_path(
r"^(?P<id>\d+)/preview/$",
self.admin_site.admin_view(PreviewView.as_view()),
name="{}_{}_preview".format(*info),
)
] + super().get_urls()


@admin.register(ProjectGrouper)
Expand All @@ -19,10 +28,15 @@ class ProjectGrouperAdmin(admin.ModelAdmin):


@admin.register(ProjectContent)
class ProjectContentAdmin(admin.ModelAdmin):
class ProjectContentAdmin(VersionedContentAdmin):
pass


@admin.register(ArtProjectContent)
class ArtProjectContentAdmin(VersionedContentAdmin):
pass


@admin.register(ResearchProjectContent)
class ResearchProjectContentAdmin(admin.ModelAdmin):
class ResearchProjectContentAdmin(VersionedContentAdmin):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def __str__(self):
return self.topic

def get_absolute_url(self):
return reverse("admin:polymorphic_project_projectcontent_changelist")
return reverse(f"admin:{self._meta.app_label}_{self._meta.model_name}_change", args=[self.id])

def get_preview_url(self):
return reverse(f"admin:{self._meta.app_label}_{self._meta.model_name}_preview", args=[self.id])

class Meta:
# Important for djangocms-versioning support, without this the GenericForeignKey
Expand All @@ -33,9 +36,6 @@ class ArtProjectContent(ProjectContent):
def __str__(self):
return self.artist

def get_absolute_url(self):
return reverse("admin:polymorphic_project_artprojectcontent_changelist")

class Meta:
# Important for djangocms-versioning support, without this the GenericForeignKey
# on the Version will yield None for the "content"
Expand All @@ -48,9 +48,6 @@ class ResearchProjectContent(ProjectContent):
def __str__(self):
return self.supervisor

def get_absolute_url(self):
return reverse("admin:polymorphic_project_researchprojectcontent_changelist")

class Meta:
# Important for djangocms-versioning support, without this the GenericForeignKey
# on the Version will yield None for the "content"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.views.generic import View


class PreviewView(View):
pass
56 changes: 47 additions & 9 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
DefaultContentExpiryConfigurationFactory,
)
from djangocms_content_expiry.test_utils.polls.factories import PollContentExpiryFactory
from djangocms_content_expiry.test_utils.polymorphic_project.factories import (
ArtProjectContentExpiryFactory,
)


class ContentExpiryAdminViewsPermissionsTestCase(CMSTestCase):
Expand Down Expand Up @@ -216,45 +219,80 @@ def test_file_content_contains_values(self):
is exported as UTC so the date time will be converted hence the need to use a timezone aware
datetime expiry date object.
"""
content_expiry = PollContentExpiryFactory(expires=self.date, version__state=DRAFT)
preview_url = content_expiry.version.content.get_preview_url()
art_content_expiry = ArtProjectContentExpiryFactory(expires=self.date, version__state=DRAFT)
art_preview_url = art_content_expiry.version.content.get_preview_url()
poll_content_expiry = PollContentExpiryFactory(expires=self.date, version__state=DRAFT)
poll_preview_url = poll_content_expiry.version.content.get_preview_url()

with self.login_user_context(self.get_superuser()):
response = self.client.get(self.export_admin_endpoint)

self.assertEqual(response.status_code, 200)

csv_lines = response.content.decode().splitlines()

# The following contents should be present for poll
content_row_1 = csv_lines[1].split(",")

# The following contents should be present
self.assertEqual(
content_row_1[self.headings_map["title"]],
content_expiry.version.content.text
poll_content_expiry.version.content.text
)
self.assertEqual(
content_row_1[self.headings_map["ctype"]],
content_expiry.version.content_type.name
poll_content_expiry.version.content_type.name
)
self.assertEqual(
content_row_1[self.headings_map["expiry_date"]],
content_expiry.expires.strftime(DEFAULT_CONTENT_EXPIRY_EXPORT_DATE_FORMAT)
poll_content_expiry.expires.strftime(DEFAULT_CONTENT_EXPIRY_EXPORT_DATE_FORMAT)
)
self.assertEqual(
content_row_1[self.headings_map["version_state"]],
"Draft"
)
self.assertEqual(
content_row_1[self.headings_map["version_author"]],
content_expiry.version.created_by.username
poll_content_expiry.version.created_by.username
)
self.assertNotEqual(
content_row_1[self.headings_map["url"]],
preview_url
poll_preview_url
)
self.assertEqual(
content_row_1[self.headings_map["url"]],
response.wsgi_request.build_absolute_uri(preview_url)
response.wsgi_request.build_absolute_uri(poll_preview_url)
)

# The following contents should be present for art
content_row_2 = csv_lines[2].split(",")

self.assertEqual(
content_row_2[self.headings_map["title"]],
art_content_expiry.version.content.artist
)
self.assertEqual(
content_row_2[self.headings_map["ctype"]],
art_content_expiry.version.content_type.name
)
self.assertEqual(
content_row_2[self.headings_map["expiry_date"]],
art_content_expiry.expires.strftime(DEFAULT_CONTENT_EXPIRY_EXPORT_DATE_FORMAT)
)
self.assertEqual(
content_row_2[self.headings_map["version_state"]],
"Draft"
)
self.assertEqual(
content_row_2[self.headings_map["version_author"]],
art_content_expiry.version.created_by.username
)
self.assertNotEqual(
content_row_2[self.headings_map["url"]],
art_preview_url
)
self.assertEqual(
content_row_2[self.headings_map["url"]],
response.wsgi_request.build_absolute_uri(art_preview_url)
)

def test_export_button_is_visible(self):
Expand Down

0 comments on commit 83e264e

Please sign in to comment.