Skip to content

Commit

Permalink
Merge pull request #1263 from Amsterdam-Music-Lab/feature/admin-cleanup
Browse files Browse the repository at this point in the history
Remove unnecessary fields from admin
  • Loading branch information
BeritJanssen authored Sep 17, 2024
2 parents 83ac6c1 + 59b6fa4 commit 9c92383
Show file tree
Hide file tree
Showing 34 changed files with 197 additions and 275 deletions.
2 changes: 1 addition & 1 deletion backend/experiment/actions/tests/test_action_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestActions(TestCase):
def setUp(self) -> None:
self.playlist = Playlist.objects.create(name='TestPlaylist')
self.participant = Participant.objects.create()
self.block = Block.objects.create(name='TestBlock')
self.block = Block.objects.create(slug="TestBlock")
self.session = Session.objects.create(
block=self.block, participant=self.participant, playlist=self.playlist)

Expand Down
2 changes: 1 addition & 1 deletion backend/experiment/actions/tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def setUp(self):
self.participant = Participant.objects.create()
self.section = Section.objects.create(
filename='some/audio/file.mp3', playlist=self.playlist)
self.block = Block.objects.create(name='TestBlock')
self.block = Block.objects.create(slug="test-block")
self.session = Session.objects.create(
block=self.block, participant=self.participant, playlist=self.playlist)

Expand Down
56 changes: 6 additions & 50 deletions backend/experiment/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,17 @@ def get_extra(self, request, obj=None, **kwargs):
class BlockAdmin(InlineActionsModelAdminMixin, admin.ModelAdmin):
list_display = (
"image_preview",
"block_name_link",
"block_slug_link",
"rules",
"rounds",
"playlist_count",
"session_count",
"active",
)
list_filter = ["active"]
search_fields = ["name"]
inline_actions = ["export", "export_csv"]
fields = [
"name",
"description",
"image",
"slug",
"theme_config",
"active",
"rules",
"rounds",
"bonus_points",
Expand Down Expand Up @@ -187,9 +180,7 @@ def export_csv(self, request, obj, parent_obj=None):
writer.writeheader()
writer.writerows(block_table)
return response
# Go back to admin block overview
if "_back" in request.POST:
return redirect("/admin/experiment/block")

# Load a template in the export form
if "_template" in request.POST:
selected_template = request.POST.get("select_template")
Expand Down Expand Up @@ -217,12 +208,6 @@ def image_preview(self, obj):
return format_html(f'<img src="{img_src}" style="max-height: 50px;"/>')
return ""

def block_name_link(self, obj):
"""Generate a link to the block's admin change page."""
url = reverse("admin:experiment_block_change", args=[obj.pk])
name = obj.name or obj.slug or "No name"
return format_html('<a href="{}">{}</a>', url, name)

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}"
Expand All @@ -233,13 +218,9 @@ def block_slug_link(self, obj):

# Name the columns
image_preview.short_description = "Image"
block_name_link.short_description = "Name"
block_slug_link.short_description = "Slug"


admin.site.register(Block, BlockAdmin)


class BlockInline(NestedStackedInline):
model = Block
sortable_field_name = "index"
Expand Down Expand Up @@ -277,19 +258,15 @@ class ExperimentAdmin(InlineActionsModelAdminMixin, NestedModelAdmin):
list_display = (
"name",
"slug_link",
"description_excerpt",
"remarks",
"dashboard",
"phases",
"active",
)
fields = [
"slug",
"active",
"theme_config",
"dashboard",
]
inline_actions = ["dashboard"]
inline_actions = ["experimenter_dashboard"]
form = ExperimentForm
inlines = [
ExperimentTranslatedContentInline,
Expand All @@ -313,23 +290,9 @@ def slug_link(self, obj):
f'<a href="{url}" target="_blank" rel="noopener noreferrer" title="Open {obj.slug} experiment group in new tab" >{obj.slug}&nbsp;<small>&#8599;</small></a>'
)

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

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

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

slug_link.short_description = "Slug"

def dashboard(self, request, obj, parent_obj=None):
def experimenter_dashboard(self, request, obj, parent_obj=None):
"""Open researchers dashboard for an experiment"""
all_blocks = obj.associated_blocks()
all_participants = obj.current_participants()
Expand All @@ -342,7 +305,7 @@ def dashboard(self, request, obj, parent_obj=None):
blocks = [
{
"id": block.id,
"name": block.name,
"slug": block.slug,
"started": len(all_sessions.filter(block=block)),
"finished": len(
all_sessions.filter(
Expand Down Expand Up @@ -437,7 +400,7 @@ def save_model(self, request, obj, form, change):
missing_language_flags = [get_flag_emoji(language) for language in missing_languages]
self.message_user(
request,
f"Block {block.name} does not have content in {', '.join(missing_language_flags)}",
f"Block {block.slug} does not have content in {', '.join(missing_language_flags)}",
level=messages.WARNING,
)

Expand Down Expand Up @@ -474,15 +437,8 @@ def blocks(self, obj):
if not blocks:
return "No blocks"

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


admin.site.register(Phase, PhaseAdmin)

return format_html(", ".join([block.slug for block in blocks]))

@admin.register(BlockTranslatedContent)
class BlockTranslatedContentAdmin(admin.ModelAdmin):
list_display = ["name", "block", "language"]
list_filter = ["language"]
Expand Down
32 changes: 0 additions & 32 deletions backend/experiment/fixtures/experiment.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"model": "experiment.Block",
"pk": 1,
"fields": {
"name": "DurationDiscrimination",
"slug": "ddi",
"active": true,
"rounds": 100,
"bonus_points": 0,
"rules": "DURATION_DISCRIMINATION",
Expand All @@ -25,9 +23,7 @@
"model": "experiment.Block",
"pk": 2,
"fields": {
"name": "DurationDiscriminationTone",
"slug": "ddit",
"active": true,
"rounds": 100,
"bonus_points": 0,
"rules": "DURATION_DISCRIMINATION_TONE",
Expand All @@ -40,9 +36,7 @@
"model": "experiment.Block",
"pk": 3,
"fields": {
"name": "BeatAlignment",
"slug": "bat",
"active": true,
"rounds": 17,
"bonus_points": 0,
"rules": "BEAT_ALIGNMENT",
Expand All @@ -55,9 +49,7 @@
"model": "experiment.Block",
"pk": 4,
"fields": {
"name": "HBAT-BIT",
"slug": "hbat_bit",
"active": true,
"rounds": 100,
"bonus_points": 0,
"rules": "H_BAT",
Expand All @@ -70,9 +62,7 @@
"model": "experiment.Block",
"pk": 5,
"fields": {
"name": "HBAT-BFIT",
"slug": "hbat_bfit",
"active": true,
"rounds": 100,
"bonus_points": 0,
"rules": "H_BAT_BFIT",
Expand All @@ -85,9 +75,7 @@
"model": "experiment.Block",
"pk": 6,
"fields": {
"name": "HBAT-BST",
"slug": "hbat_bst",
"active": true,
"rounds": 100,
"bonus_points": 0,
"rules": "BST",
Expand All @@ -100,9 +88,7 @@
"model": "experiment.Block",
"pk": 7,
"fields": {
"name": "Anisochrony",
"slug": "anis",
"active": true,
"rounds": 100,
"bonus_points": 0,
"rules": "ANISOCHRONY",
Expand All @@ -115,9 +101,7 @@
"model": "experiment.Block",
"pk": 8,
"fields": {
"name": "RhythmDiscrimination",
"slug": "rhdis",
"active": true,
"rounds": 40,
"bonus_points": 0,
"rules": "RHYTHM_DISCRIMINATION",
Expand All @@ -130,9 +114,7 @@
"model": "experiment.Block",
"pk": 9,
"fields": {
"name": "Rhythm Battery Final",
"slug": "rhythm_outro",
"active": true,
"rounds": 10,
"bonus_points": 0,
"rules": "RHYTHM_BATTERY_FINAL",
Expand All @@ -145,9 +127,7 @@
"model": "experiment.Block",
"pk": 10,
"fields": {
"name": "Rhythm Battery Intro",
"slug": "rhythm_intro",
"active": true,
"rounds": 10,
"bonus_points": 0,
"rules": "RHYTHM_BATTERY_INTRO",
Expand All @@ -160,9 +140,7 @@
"model": "experiment.Block",
"pk": 14,
"fields": {
"name": "Hooked-China",
"slug": "huang_2022",
"active": true,
"rounds": 30,
"bonus_points": 0,
"rules": "HUANG_2022",
Expand All @@ -177,9 +155,7 @@
"model": "experiment.Block",
"pk": 16,
"fields": {
"name": "Categorization",
"slug": "cat",
"active": true,
"rounds": 10,
"bonus_points": 0,
"rules": "CATEGORIZATION",
Expand All @@ -192,9 +168,7 @@
"model": "experiment.Block",
"pk": 17,
"fields": {
"name": "TuneTwins",
"slug": "tunetwins",
"active": true,
"rounds": 10,
"bonus_points": 0,
"rules": "MATCHING_PAIRS",
Expand All @@ -207,9 +181,7 @@
"model": "experiment.Block",
"pk": 18,
"fields": {
"name": "Hooked-Eurovision",
"slug": "eurovision_2021",
"active": true,
"rounds": 30,
"bonus_points": 0,
"rules": "EUROVISION_2020",
Expand All @@ -222,9 +194,7 @@
"model": "experiment.Block",
"pk": 19,
"fields": {
"name": "Hooked-Christmas",
"slug": "christmas_2020",
"active": true,
"rounds": 30,
"bonus_points": 0,
"rules": "KUIPER_2020",
Expand All @@ -237,9 +207,7 @@
"model": "experiment.Block",
"pk": 20,
"fields": {
"name": "ThatsMySong",
"slug": "thats_my_song",
"active": false,
"rounds": 10,
"bonus_points": 0,
"rules": "THATS_MY_SONG",
Expand Down
27 changes: 6 additions & 21 deletions backend/experiment/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
CheckboxSelectMultiple,
TextInput,
)
from django.contrib.admin.widgets import RelatedFieldWidgetWrapper

from experiment.models import BlockTranslatedContent, Experiment, Block, SocialMediaConfig, ExperimentTranslatedContent
from experiment.models import (
Experiment,
Block,
SocialMediaConfig,
ExperimentTranslatedContent,
)
from experiment.rules import BLOCK_RULES
from django.db.models.fields.related import ManyToManyRel


# session_keys for Export CSV
Expand Down Expand Up @@ -186,22 +189,11 @@ class Media:


class ExperimentForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ModelForm, self).__init__(*args, **kwargs)
self.fields["dashboard"].help_text = (
"This field will be deprecated in the nearby future. "
"Please use experiment phases for dashboard configuration. (see bottom of form). <br><br>"
'Legacy behavior: If you check "dashboard", the experiment will have a '
"dashboard that shows all or a subgroup of related blocks along "
"with a description, footer, and about page. If you leave it unchecked, "
"the experiment will redirect to the first block."
)

class Meta:
model = Experiment
fields = [
"slug",
"dashboard",
]

class Media:
Expand Down Expand Up @@ -270,21 +262,14 @@ class Meta:
model = Block
fields = [
"index",
"name",
"slug",
"active",
"rules",
"rounds",
"bonus_points",
"playlists",
]
help_texts = {
"description": "A short description of the block that will be displayed on the experiment page and as a meta description in search engines.",
"image": "An image that will be displayed on the experiment page and as a meta image in search engines.",
"consent": "Upload an HTML (.html) or MARKDOWN (.md) file with a text to ask a user its consent<br> \
for using the block data for this instance of the block.<br> \
This field will override any consent text loaded from the rules file. <br>\
HTML files also allow django template tags so that the text can be translated",
"slug": "The slug is used to identify the block in the URL so you can access it on the web as follows: app.amsterdammusiclab.nl/{slug} <br>\
It must be unique, lowercase and contain only letters, numbers, and hyphens. Nor can it start with any of the following reserved words: admin, server, block, participant, result, section, session, static.",
}
Expand Down
Loading

0 comments on commit 9c92383

Please sign in to comment.