Skip to content

Commit

Permalink
Use fileField for consent
Browse files Browse the repository at this point in the history
  • Loading branch information
Evert-R committed Feb 19, 2024
1 parent 47509f6 commit e6e9c5c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
27 changes: 18 additions & 9 deletions backend/experiment/actions/consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
from django.template import Template, Context
from django_markup.markup import formatter

def get_render_format(url):
"""
Detect markdown file by file extention
"""
url_length = len(url)
if url[(url_length-2):url_length] == 'md':
return 'MARKDOWN'
return 'HTML'

class Consent(BaseAction): # pylint: disable=too-few-public-methods
"""
Provide data for a view that ask consent for using the experiment data
- text: Has priority over 'url'
- text: Uploaded file via experiment.consent (fileField)
- title: The title to be displayed
- confirm: The text on the confirm button
- deny: The text on the deny button
- url: If no text is provided the url will be used to load a template (HTML or MARKDOWN)
- render_format: (autodetected when reading from a file)
- render_format: (autodetected from the file extention)
'HTML': (default) Allowed tags: html, django template language
'MARKDOWN': Allowed tags: Markdown language
Expand All @@ -34,16 +43,16 @@ class Consent(BaseAction): # pylint: disable=too-few-public-methods

def __init__(self, text, title='Informed consent', confirm='I agree', deny='Stop', url='', render_format='HTML'):
# Determine which text to use
# from field: experiment.consent (prio-1)
# Uploaded consent via file field: experiment.consent (High priority)
if text!='':
dry_text = text
# from template file (prio-2)
with text.open('r') as f:
dry_text = f.read()
render_format = get_render_format(text.url)
# Template file via url (Low priority)
elif url!='':
dry_text = render_to_string(url)
url_length = len(url)
if url[(url_length-2):url_length] == 'md':
render_format = 'MARKDOWN'
# use efault text (prio-3)
render_format = get_render_format(url)
# use default text
else:
dry_text = self.default_text
# render text fot the consent component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 3.2.23 on 2024-02-06 09:10
# Generated by Django 3.2.24 on 2024-02-15 13:30

from django.db import migrations, models
import experiment.models


class Migration(migrations.Migration):
Expand All @@ -17,6 +18,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='experiment',
name='consent',
field=models.TextField(blank=True, default=''),
field=models.FileField(blank=True, default='', upload_to=experiment.models.consent_upload_path),
),
]
6 changes: 5 additions & 1 deletion backend/experiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def __str__(self):
class Meta:
verbose_name_plural = "Experiment Series"

def consent_upload_path(instance, filename):
"""Generate path to save audio based on playlist.name"""
folder_name = instance.slug
return 'consent/{0}/{1}'.format(folder_name, filename)

class Experiment(models.Model):
"""Root entity for configuring experiments"""
Expand All @@ -50,7 +54,7 @@ class Experiment(models.Model):
blank=True,
default=get_default_question_keys
)
consent = models.TextField(blank=True, default='')
consent = models.FileField(upload_to=consent_upload_path, blank=True, default='')

class Meta:
ordering = ['name']
Expand Down

0 comments on commit e6e9c5c

Please sign in to comment.