Firstboot, KVStore: merge old firstboot state fields

- use a migration to merge the old 'firstboot_state' and 'setup_state'
  fields into 'firstboot_completed' which is a more accurate name
- introduce kvstore.delete()
This commit is contained in:
fonfon 2016-12-27 12:10:14 +01:00 committed by James Valleroy
parent 3af82ab14e
commit e1aa78d25a
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
5 changed files with 89 additions and 15 deletions

View File

@ -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()

View File

@ -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 <http://www.gnu.org/licenses/>.
#
"""
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),
]

View File

@ -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)

View File

@ -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

View File

@ -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()