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

Added: Add translated_content model and update Experiment model #1208

Merged
merged 14 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
111 changes: 50 additions & 61 deletions backend/experiment/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Phase,
Feedback,
SocialMediaConfig,
ExperimentTranslatedContent,
)
from question.admin import QuestionSeriesInline
from experiment.forms import (
Expand All @@ -45,6 +46,16 @@ class FeedbackInline(admin.TabularInline):
extra = 0


class ExperimentTranslatedContentInline(NestedStackedInline):
model = ExperimentTranslatedContent
sortable_field_name = "index"

def get_extra(self, request, obj=None, **kwargs):
if obj:
return 0
return 1


class BlockAdmin(InlineActionsModelAdminMixin, admin.ModelAdmin):
list_display = (
"image_preview",
Expand Down Expand Up @@ -112,36 +123,22 @@ def export(self, request, obj, parent_obj=None):
zip_buffer = BytesIO()
with ZipFile(zip_buffer, "w") as new_zip:
# serialize data to new json files within the zip file
new_zip.writestr(
"sessions.json", data=str(serializers.serialize("json", all_sessions))
)
new_zip.writestr("sessions.json", data=str(serializers.serialize("json", all_sessions)))
new_zip.writestr(
"participants.json",
data=str(
serializers.serialize("json", all_participants.order_by("pk"))
),
data=str(serializers.serialize("json", all_participants.order_by("pk"))),
)
new_zip.writestr(
"profiles.json",
data=str(
serializers.serialize(
"json", all_profiles.order_by("participant", "pk")
)
),
data=str(serializers.serialize("json", all_profiles.order_by("participant", "pk"))),
)
new_zip.writestr(
"results.json",
data=str(
serializers.serialize("json", all_results.order_by("session"))
),
data=str(serializers.serialize("json", all_results.order_by("session"))),
)
new_zip.writestr(
"sections.json",
data=str(
serializers.serialize(
"json", all_sections.order_by("playlist", "pk")
)
),
data=str(serializers.serialize("json", all_sections.order_by("playlist", "pk"))),
)
new_zip.writestr(
"songs.json",
Expand All @@ -152,11 +149,7 @@ def export(self, request, obj, parent_obj=None):
response = HttpResponse(zip_buffer.getbuffer())
response["Content-Type"] = "application/x-zip-compressed"
response["Content-Disposition"] = (
'attachment; filename="'
+ obj.slug
+ "-"
+ timezone.now().isoformat()
+ '.zip"'
'attachment; filename="' + obj.slug + "-" + timezone.now().isoformat() + '.zip"'
)
return response

Expand All @@ -170,20 +163,14 @@ def export_csv(self, request, obj, parent_obj=None):
result_keys = []
export_options = []
# Get all export options
session_keys = [
key for key in request.POST.getlist("export_session_fields")
]
session_keys = [key for key in request.POST.getlist("export_session_fields")]
result_keys = [key for key in request.POST.getlist("export_result_fields")]
export_options = [key for key in request.POST.getlist("export_options")]

response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename="{}.csv"'.format(
obj.slug
)
response["Content-Disposition"] = 'attachment; filename="{}.csv"'.format(obj.slug)
# Get filtered data
block_table, fieldnames = obj.export_table(
session_keys, result_keys, export_options
)
block_table, fieldnames = obj.export_table(session_keys, result_keys, export_options)
fieldnames.sort()
writer = csv.DictWriter(response, fieldnames)
writer.writeheader()
Expand Down Expand Up @@ -227,11 +214,7 @@ def block_name_link(self, obj):

def block_slug_link(self, obj):
dev_mode = settings.DEBUG is True
url = (
f"http://localhost:3000/block/{obj.slug}"
if dev_mode
else f"/block/{obj.slug}"
)
url = f"http://localhost:3000/block/{obj.slug}" if dev_mode else f"/block/{obj.slug}"

return format_html(
f'<a href="{url}" target="_blank" rel="noopener noreferrer" title="Open {obj.slug} block in new tab" >{obj.slug}&nbsp;<small>&#8599;</small></a>'
Expand All @@ -248,21 +231,33 @@ def block_slug_link(self, obj):

class BlockInline(NestedStackedInline):
model = Block
extra = 1
sortable_field_name = "index"

def get_extra(self, request, obj=None, **kwargs):
if obj:
return 0
return 1


class PhaseInline(NestedTabularInline):
model = Phase
extra = 1
sortable_field_name = "index"
inlines = [BlockInline]

def get_extra(self, request, obj=None, **kwargs):
if obj:
return 0
return 1


class SocialMediaConfigInline(NestedStackedInline):
form = SocialMediaConfigForm
model = SocialMediaConfig
extra = 0

def get_extra(self, request, obj=None, **kwargs):
if obj:
return 0
return 1


class ExperimentAdmin(InlineActionsModelAdminMixin, NestedModelAdmin):
Expand All @@ -276,21 +271,22 @@ class ExperimentAdmin(InlineActionsModelAdminMixin, NestedModelAdmin):
)
fields = [
"slug",
"name",
"active",
"description",
"consent",
"theme_config",
"dashboard",
"about_content",
]
inline_actions = ["dashboard"]
form = ExperimentForm
inlines = [
ExperimentTranslatedContentInline,
PhaseInline,
SocialMediaConfigInline,
]

def name(self, obj):
content = obj.get_fallback_content()
return content.name if content else "No name"

def slug_link(self, obj):
dev_mode = settings.DEBUG is True
url = f"http://localhost:3000/{obj.slug}" if dev_mode else f"/{obj.slug}"
Expand All @@ -300,20 +296,16 @@ def slug_link(self, obj):
)

def description_excerpt(self, obj):
if len(obj.description) < 50:
return obj.description
description = obj.get_fallback_content().description or "No description"
if len(description) < 50:
return description

return obj.description[:50] + "..."
return description[:50] + "..."

def phases(self, obj):
phases = Phase.objects.filter(series=obj)
return format_html(
", ".join(
[
f'<a href="/admin/experiment/phase/{phase.id}/change/">{phase.name}</a>'
for phase in phases
]
)
", ".join([f'<a href="/admin/experiment/phase/{phase.id}/change/">{phase.name}</a>' for phase in phases])
)

slug_link.short_description = "Slug"
Expand Down Expand Up @@ -380,7 +372,9 @@ def name_link(self, obj):

def related_experiment(self, obj):
url = reverse("admin:experiment_experiment_change", args=[obj.series.pk])
return format_html('<a href="{}">{}</a>', url, obj.series.name)
content = obj.series.get_fallback_content()
experiment_name = content.name if content else "No name"
return format_html('<a href="{}">{}</a>', url, experiment_name)

def blocks(self, obj):
blocks = Block.objects.filter(phase=obj)
Expand All @@ -389,12 +383,7 @@ def blocks(self, obj):
return "No blocks"

return format_html(
", ".join(
[
f'<a href="/admin/experiment/block/{block.id}/change/">{block.name}</a>'
for block in blocks
]
)
", ".join([f'<a href="/admin/experiment/block/{block.id}/change/">{block.name}</a>' for block in blocks])
)


Expand Down
Loading
Loading