diff --git a/plinth/kvstore.py b/plinth/kvstore.py index 864b288ce..829cfd8ad 100644 --- a/plinth/kvstore.py +++ b/plinth/kvstore.py @@ -40,3 +40,8 @@ def set(key, value): # pylint: disable-msg=W0622 """Store the value of a key""" store = KVStore(key=key, value=value) store.save() + + +def delete(key): + """Delete a key""" + return KVStore.objects.get(key=key).delete() diff --git a/plinth/migrations/0003_merge_firstboot_completed_fields.py b/plinth/migrations/0003_merge_firstboot_completed_fields.py new file mode 100644 index 000000000..28f1c41b8 --- /dev/null +++ b/plinth/migrations/0003_merge_firstboot_completed_fields.py @@ -0,0 +1,82 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +Remove the deprecated KVStore entries 'setup_state' and 'firstboot_state', +and only use the new entry 'firstboot_completed' instead. +""" + +from __future__ import unicode_literals + +from django.db import migrations +from plinth.models import KVStore + + +def merge_firstboot_finished_fields(apps, schema_editor): + """ + Merge 'setup_state' and 'firstboot_state' into 'firstboot_completed'. + + 'firstboot_completed' is the most accurate name for now, and by combining + the fields we do not have to deal with legacy states/fields anymore. + """ + # Get and remove 'firstboot_state' + firstboot_state = 0 + try: + _object = KVStore.objects.get(key='firstboot_state') + except KVStore.DoesNotExist: + pass + else: + firstboot_state = _object.value + _object.delete() + + # Get and remove 'setup_state' + setup_state = 0 + try: + _object = KVStore.objects.get(key='setup_state') + except KVStore.DoesNotExist: + pass + else: + setup_state = _object.value + _object.delete() + + # Get current 'firstboot_completed' + firstboot_completed = False + try: + _object = KVStore.objects.get(key='firstboot_completed') + except KVStore.DoesNotExist: + pass + else: + firstboot_completed = _object.value + + # Set new 'firstboot_completed' if needed + new_firstboot_completed = bool(firstboot_completed or setup_state or + firstboot_state) + if new_firstboot_completed and not firstboot_completed: + obj, created = KVStore.objects.get_or_create(key='firstboot_completed') + obj.value = 1 + obj.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('plinth', '0002_modulestore'), + ] + + operations = [ + migrations.RunPython(merge_firstboot_finished_fields), + ] diff --git a/plinth/modules/first_boot/__init__.py b/plinth/modules/first_boot/__init__.py index b490e1b97..76b7c419e 100644 --- a/plinth/modules/first_boot/__init__.py +++ b/plinth/modules/first_boot/__init__.py @@ -115,10 +115,7 @@ def is_completed(): global _is_completed if _is_completed is None: - # TODO - # Rename setup_state to 'firstboot_completed', - # taking care of the existing kvstore variable name. - _is_completed = kvstore.get_default('setup_state', 0) + _is_completed = kvstore.get_default('firstboot_completed', 0) return bool(_is_completed) @@ -129,4 +126,4 @@ def set_completed(): global _is_completed _is_completed = True - kvstore.set('setup_state', 1) + kvstore.set('firstboot_completed', 1) diff --git a/plinth/modules/first_boot/middleware.py b/plinth/modules/first_boot/middleware.py index 9e47fad00..7ba2f3282 100644 --- a/plinth/modules/first_boot/middleware.py +++ b/plinth/modules/first_boot/middleware.py @@ -25,7 +25,6 @@ from django.urls import reverse from django.conf import settings import logging -from plinth import kvstore from plinth.modules import first_boot LOGGER = logging.getLogger(__name__) @@ -49,14 +48,6 @@ class FirstBootMiddleware(object): return firstboot_completed = first_boot.is_completed() - - # Migrate from old settings variable - if not firstboot_completed: - old_state = kvstore.get_default('firstboot_state', 0) - if old_state == 10: - firstboot_completed = True - first_boot.set_completed() - user_requests_firstboot = first_boot.is_firstboot_url(request.path) # Redirect to first boot if requesting normal page and first diff --git a/plinth/modules/first_boot/templatetags/firstboot_extras.py b/plinth/modules/first_boot/templatetags/firstboot_extras.py index 4b2c81d95..71a1db65d 100644 --- a/plinth/modules/first_boot/templatetags/firstboot_extras.py +++ b/plinth/modules/first_boot/templatetags/firstboot_extras.py @@ -22,7 +22,6 @@ Template tags for first boot module. from django import template from plinth.modules import first_boot -from plinth import kvstore register = template.Library()