Skip to content

Commit

Permalink
fix #1 and #2
Browse files Browse the repository at this point in the history
  • Loading branch information
hadpro24 committed Jun 18, 2021
1 parent d8b79e1 commit 03456c5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion forms_fieldset/templates/forms_fieldset/tabular.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h2 style="background-color: {{ color }}">{{ label }}</h2>
</tr>
{% endfor %}
<tr class="add-row" id="link-add-row"><td colspan="{{ inline_formset.extra|add:'1' }}">
<a href="#" id="add-info">{{ title_add }}</a></td></tr>
<a href="#" id="add-info">Ajout supplémentaire</a></td></tr>
</tbody>
</table>
</fieldset>
Expand Down
59 changes: 58 additions & 1 deletion forms_fieldset/templatetags/forms_fieldset.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
import logging

from django import template
from django.contrib.admin.helpers import AdminForm
from django.template.loader import render_to_string

from django.conf import settings

logger = logging.getLogger(__name__)

class FieldsetsNotSupported(Exception):
""" form field sets type error, it must be a list """
pass

class ArgumentNotSupported(Exception):
""" form field sets type error, it must be a list """
pass

register = template.Library()
@register.filter
def fieldset(form, color='#79AEC8'):
if not hasattr(form, 'fieldsets') or not isinstance(form.fieldsets, list):
raise FieldsetsNotSupported("your form does not contain 'fieldsets' or is not the correct type")
form_fieldset = AdminForm(form, list(form.fieldsets),{})
context = {
'form_fieldset': form_fieldset,
'color': color,
}
return render_to_string('forms_fieldset/fieldset.html', context)

@register.filter
def inline_fieldset(inline_formset, args):
if len(str(args).split(',')) != 2:
raise ArgumentNotSupported("The 'inline fieldset' argument \
does not have the correct argument. \
it must be a str (ex: '#000000, title inline form')")
color, label = args.split(',')
color = color or '#79AEC8'
label= label or 'Inline Form'
LANGEAGE_DATA = {
'en-en': 'Add another',
'en': 'Add another',
'EN-EN': 'Add another',
'EN': 'Add another',
'fr-fr': 'Ajout supplémentaire',
'fr': 'Ajout supplémentaire',
'FR-FR': 'Ajout supplémentaire',
'Fr': 'Ajout supplémentaire',
'de': 'Ajout supplémentaire',
}
context = {
'inline_formset': inline_formset,
'color': color,
'label': label,
'title_add': LANGEAGE_DATA.get(settings.LANGUAGE_CODE, 'Add another')
}
return render_to_string('forms_fieldset/tabular.html', context)

# old version
USED = False
@register.simple_tag
def fieldset(form, fieldsets, color='#79AEC8'):
logger.warning("""you are using the features of an old version, replace the tags with filters.
more information on this link: https://github.com/hadpro24/django-forms-fieldset """)
USED = True
if not isinstance(fieldsets, list):
raise FieldsetsNotSupported("form field sets type error, it must be a list")
form_fieldset = AdminForm(form, list(fieldsets),{})
Expand All @@ -21,6 +74,10 @@ def fieldset(form, fieldsets, color='#79AEC8'):

@register.simple_tag
def inline_fieldset(inline_formset, color='#79AEC8', label='Inline Form'):
if not USED:
print()
logger.error("""WARNING: you are using the features of an old version, replace the tags with filters.\nMore information on this link: https://github.com/hadpro24/django-forms-fieldset""")
print()
LANGEAGE_DATA = {
'en-en': 'Add another',
'en': 'Add another',
Expand All @@ -38,4 +95,4 @@ def inline_fieldset(inline_formset, color='#79AEC8', label='Inline Form'):
'label': label,
'title_add': LANGEAGE_DATA.get(settings.LANGUAGE_CODE, 'Add another')
}
return render_to_string('forms_fieldset/tabular.html', context)
return render_to_string('forms_fieldset/tabular.html', context)
3 changes: 0 additions & 3 deletions forms_fieldset/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.test import TestCase

# Create your tests here.

0 comments on commit 03456c5

Please sign in to comment.