kpsul/kfet/migrations/0051_delete_settings.py
Aurélien Delobelle 85caa6b058 Use django-djconfig for kfet app.
Old configuration(/settings), based on Settings model, system is
deleted: SettingsForm, Settings.
New system use `django-djconfig` module.

- `kfet.config` module provides `kfet_config` to access configuration concerning
kfet app.
- Views, forms, models, etc now use this object to retrieve conf values.
- Views no longer add config values to context, instead templates use
  `kfet_config` provided by a new context_processor.
- Enhance list and update views of settings.
- Fix: settings can directly be used without having to visit a specific
  page...

Misc
- Delete some py2/3 imports
- Delete unused imports in kfet.models and kfet.views
- Some PEP8 compliance
2017-04-03 20:32:16 +02:00

53 lines
1.5 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
from kfet.forms import KFetConfigForm
def adapt_settings(apps, schema_editor):
Settings = apps.get_model('kfet', 'Settings')
db_alias = schema_editor.connection.alias
obj = Settings.objects.using(db_alias)
cfg = {}
def try_get(new, old, type_field):
try:
value = getattr(obj.get(name=old), type_field)
cfg[new] = value
except Settings.DoesNotExist:
pass
try_get('kfet_subvention_cof', 'SUBVENTION_COF', 'value_decimal')
try_get('kfet_addcost_amount', 'ADDCOST_AMOUNT', 'value_decimal')
try_get('kfet_addcost_for', 'ADDCOST_FOR', 'value_account')
try_get('kfet_overdraft_duration', 'OVERDRAFT_DURATION', 'value_duration')
try_get('kfet_overdraft_amount', 'OVERDRAFT_AMOUNT', 'value_decimal')
try_get('kfet_cancel_duration', 'CANCEL_DURATION', 'value_duration')
cfg_form = KFetConfigForm(initial=cfg)
if cfg_form.is_valid():
cfg_form.save()
class Migration(migrations.Migration):
dependencies = [
('kfet', '0050_remove_checkout'),
('djconfig', '0001_initial'),
]
operations = [
migrations.RunPython(adapt_settings),
migrations.RemoveField(
model_name='settings',
name='value_account',
),
migrations.DeleteModel(
name='Settings',
),
]