Skip to content

Commit

Permalink
Merge branch 'feature/cleanup-round-getters' of https://github.com/Am…
Browse files Browse the repository at this point in the history
…sterdam-Music-Lab/MUSCLE into feature/cleanup-round-getters
  • Loading branch information
BeritJanssen committed Aug 9, 2024
2 parents 9286a2e + 7966867 commit 94fc748
Show file tree
Hide file tree
Showing 25 changed files with 1,195 additions and 867 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Run Backend Tests
run: sudo docker-compose --env-file .env-github-actions run server bash -c "coverage run manage.py test"
run: sudo docker compose --env-file .env-github-actions run server bash -c "coverage run manage.py test"
- name: Generate Backend Coverage Report (Inline)
run: sudo docker-compose --env-file .env-github-actions run server bash -c "coverage report --show-missing"
run: sudo docker compose --env-file .env-github-actions run server bash -c "coverage report --show-missing"

# Generate coverage badge (only for main and develop branches)
- name: Generate Backend Coverage Report (XML) and Badge
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
run: |
sudo docker-compose --env-file .env-github-actions run server bash -c "coverage xml"
sudo docker-compose --env-file .env-github-actions run server bash -c "genbadge coverage -i coverage.xml -o coverage-backend-badge-new.svg -n \"Backend Code Coverage\""
sudo docker compose --env-file .env-github-actions run server bash -c "coverage xml"
sudo docker compose --env-file .env-github-actions run server bash -c "genbadge coverage -i coverage.xml -o coverage-backend-badge-new.svg -n \"Backend Code Coverage\""
# Push coverage badge to separate branch (only for main and develop branches)
- name: Push Backend Coverage Badge to separate branch
continue-on-error: true
Expand Down Expand Up @@ -66,4 +66,4 @@ jobs:
- uses: actions/checkout@v3
- name: Lint Backend
continue-on-error: false
run: sudo docker-compose --env-file .env-github-actions run server bash -c "ruff check"
run: sudo docker compose --env-file .env-github-actions run server bash -c "ruff check"
8 changes: 4 additions & 4 deletions .github/workflows/ci-frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Run Frontend Tests
run: sudo docker-compose --env-file .env-github-actions run client yarn test:ci
run: sudo docker compose --env-file .env-github-actions run client yarn test:ci

frontend-coverage-badge:
name: Generate Frontend Coverage Badge
Expand All @@ -33,8 +33,8 @@ jobs:
- uses: actions/checkout@v3
- name: Generate Frontend Coverage Report (XML) and Badge
run: |
sudo docker-compose --env-file .env-github-actions run client yarn test:ci
sudo docker-compose --env-file .env-github-actions run client yarn coverage-badges -s public/coverage/coverage-summary.json -o public/coverage/coverage-frontend-badge-new.svg --label 'Frontend Code Coverage'
sudo docker compose --env-file .env-github-actions run client yarn test:ci
sudo docker compose --env-file .env-github-actions run client yarn coverage-badges -s public/coverage/coverage-summary.json -o public/coverage/coverage-frontend-badge-new.svg --label 'Frontend Code Coverage'
- name: Push Frontend Coverage Badge to separate branch
continue-on-error: true
run: |
Expand Down Expand Up @@ -65,4 +65,4 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: sudo docker-compose --env-file .env-github-actions run client yarn lint
- run: sudo docker compose --env-file .env-github-actions run client yarn lint
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

0 comments on commit 94fc748

Please sign in to comment.