Skip to content

Allow your staff team to change configurations

deby edited this page Nov 24, 2021 · 16 revisions

↑ Parent: Advanced tutorials

← Previous: Roles and permissions

Most of the website can be configured via Website settings. But some configurations might change more often than developers want to deploy new versions to production.

For this reason, MagiCircles provides "Staff configurations" which are stored in the database and can be configured via the website itself.

ℹ️ Staff configurations are then cached and updated every 24 hours via generated settings.

  1. Enable StaffConfiguration collection in ${PROJECT}/magicollections.py:

    from magi.magicollections import StaffConfigurationCollection as _StaffConfigurationCollection
    
    class StaffConfigurationCollection(_StaffConfigurationCollection):
        enabled = True
  2. The first time you enable staff configurations, on your local or in production, as well as whenever a new language gets added to the website, the database can be populated using:

python manage.py populate_staffconfigurations

Add your own configuration variables

  1. Add the new configuration via the web interface, both on your local and in production.

    • The key is what's used within the code. It's recommended but not enforced to use lowercase snake case strings (foo_bar).

    • The name is a verbose name for staff members. It can give details about where it's used.

    • Is long, Is markdown and Is boolean are just a way to display the right form fields when staff will fill the value.

  2. Use it in Python:

from django.conf import settings as django_settings

# Not per language:
print django_settings.STAFF_CONFIGURATIONS.get('max_level', 0)

# Per language:
print django_settings.STAFF_CONFIGURATIONS.get('team_introduction', {}).get(request.LANGUAGE_CODE, 'default')
  1. Use it in a template:
{# Not per language: #}

{% if staff_configurations.max_level %}
<span>{{ staff_configurations.max_level }}</span>
{% endif %}

{# Per language: #}

{% if staff_configurations.team_introduction and staff_configurations.team_introduction|getattribute:LANGUAGE_CODE %}
<p>{{ staff_configurations.team_introduction|getattribute:LANGUAGE_CODE }}</p>
{% endif %}

You can also create a script to add the staff configurations like so:

${PROJECT}/management/commands/populate_staffconfigurations_${PROJECT}.py:

from django.core.management.base import BaseCommand, CommandError
from magi.utils import LANGUAGES_DICT
from magi.management.commands.populate_staffconfigurations import create

class Command(BaseCommand):
    can_import_settings = True

    def handle(self, *args, **options):

        ##################################
        # Translatable

        for language in LANGUAGES_DICT.keys():
            create({
                'key': 'card_skill_template',
                'verbose_key': 'Template of card skills, must contain {percent} and {bonus}',
                'is_markdown': True,
                'is_long': True,
                'i_language': language,
            })

        ##################################
        # Non translatable

        create({
            'key': 'max_song_length',
            'verbose_key': 'Maximum length of a song, in seconds',
        })
        create({
            'key': 'allow_emojis_card_tags',
            'verbose_key': 'Are emojis allowed in cards tags?',
            'is_boolean': True,
        })

On your local or in production, when this script gets added, whenever it gets updated to add new configurations, as well as whenever a new language gets added to the website, you'll need to populate the database like so:

python manage.py populate_staffconfigurations_${PROJECT}

I. Introduction

II. Tutorials

  1. Collections
    1. MagiModel
    2. MagiCollection
    3. MagiForm
    4. MagiFiltersForm
    5. MagiFields
  2. Single pages
  3. Configuring the navbar

III. References

IV. Utils

V. Advanced tutorials

VI. More

Clone this wiki locally