Verified Commit 422dcafd authored by Štěpán Henek's avatar Štěpán Henek 🌩

contract_valid and registration_code info are obtained from the configuration backend

parent afa4d01f
......@@ -4,7 +4,7 @@ import bottle
from foris.config_app import prepare_config_app
from foris.wizard_app import prepare_wizard_app
from foris.state import info
from foris.state import current_state
app_map = {
"config": prepare_config_app,
......@@ -61,7 +61,7 @@ def main():
elif args.backend == "unix-socket":
from foris.backend.buses.unix_socket import UnixSocketSender
backend_instance = UnixSocketSender(args.backend_socket)
info.set_backend(args.backend, args.backend_socket, backend_instance)
current_state.set_backend(args.backend, args.backend_socket, backend_instance)
if args.routes:
# routes should be printed and we can safely exit
......
......@@ -26,10 +26,10 @@ import time
from functools import wraps
from foris.nuci import client, filters
from foris.nuci.helpers import write_uci_lang, contract_valid
from foris.nuci.helpers import write_uci_lang
from foris.caches import nuci_cache
from foris.utils import (
redirect_unauthenticated, is_safe_redirect, messages, login_required
redirect_unauthenticated, is_safe_redirect, messages, login_required, contract_valid
)
from foris.middleware.bottle_csrf import update_csrf_token, CSRFValidationError, CSRFPlugin
from foris.utils.routing import reverse
......@@ -39,6 +39,7 @@ from foris.utils.bottle_stuff import (
clear_lazy_cache,
disable_caching,
)
from foris.state import current_state
logger = logging.getLogger("foris.common")
......@@ -73,8 +74,7 @@ def login():
bottle.redirect(next)
# update contract status
client.update_contract_status()
nuci_cache.invalidate("foris.contract")
current_state.backend_instance.send("about", "update_contract_status", {})
else:
messages.error(_("The password you entered was not valid."))
......@@ -198,6 +198,7 @@ def require_contract_valid(valid=True):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if not (contract_valid() == valid):
raise bottle.HTTPError(403, "Contract validity mismatched.")
return func(*args, **kwargs)
......
......@@ -32,12 +32,12 @@ from foris.config_handlers import (
from foris.nuci import client
from foris.nuci.client import filters
from foris.nuci.exceptions import ConfigRestoreError
from foris.nuci.helpers import make_notification_title, contract_valid, get_wizard_progress
from foris.nuci.helpers import make_notification_title, get_wizard_progress
from foris.nuci.preprocessors import preproc_disabled_to_agreed
from foris.utils import login_required, messages, is_safe_redirect
from foris.utils import login_required, messages, is_safe_redirect, contract_valid
from foris.middleware.bottle_csrf import CSRFPlugin
from foris.utils.routing import reverse
from foris.state import info
from foris.state import current_state
logger = logging.getLogger(__name__)
......@@ -439,20 +439,17 @@ class AboutConfigPage(ConfigPageMixin):
@require_contract_valid(True)
def _action_registration_code(self):
return client.get_registration()
data = current_state.backend_instance.send("about", "get_registration_number", {})
return data["registration_number"]
def call_ajax_action(self, action):
if action == "registration_code":
regnum = self._action_registration_code()
if regnum:
data = regnum.value
else:
data = None
return dict(success=regnum is not None, data=data)
return dict(success=regnum is not False, data=regnum)
raise ValueError("Unknown AJAX action.")
def render(self, **kwargs):
data = info.backend_instance.send("about", "get", {})
data = current_state.backend_instance.send("about", "get", {})
data["firewall_status"]["seconds_ago"] = \
int(time.time() - data["firewall_status"]["last_check"])
data["firewall_status"]["datetime"] = \
......
......@@ -30,11 +30,11 @@ from foris.common import render_js_md5, init_common_app, init_default_app
from foris.middleware.sessions import SessionMiddleware
from foris.middleware.reporting import ReportingMiddleware
from foris.nuci import client
from foris.nuci.helpers import contract_valid, read_uci_lang
from foris.nuci.helpers import read_uci_lang
from foris.langs import DEFAULT_LANGUAGE
from foris.plugins import ForisPluginLoader
from foris.state import info
from foris.utils import messages
from foris.state import current_state
from foris.utils import messages, contract_valid
from foris.utils.translators import translations
from foris.utils.bottle_stuff import (
prepare_template_defaults,
......@@ -62,7 +62,7 @@ def prepare_config_app(args):
"""
# set app
info.set_app("config")
current_state.set_app("config")
# internationalization
i18n_defaults(bottle.SimpleTemplate, bottle.request)
......
......@@ -23,7 +23,7 @@ from foris.nuci.modules.uci_raw import (
Uci, Config, Section, Option, List, Value, parse_uci_bool, build_option_uci_tree,
)
from foris.utils.translators import gettext_dummy as gettext, _
from foris.state import info
from foris.state import current_state
from .base import BaseConfigHandler, logger
......@@ -196,7 +196,7 @@ class RegistrationCheckHandler(BaseConfigHandler):
)
def form_cb(data):
result = client.get_registration_status(data.get("email"), info.language)
result = client.get_registration_status(data.get("email"), current_state.language)
return "save_result", {
'success': result[0],
'response': result[1],
......
......@@ -23,8 +23,8 @@ from foris import validators
from foris.form import Checkbox, Textbox
from foris.nuci.filters import create_config_filter
from foris.nuci.helpers import contract_valid
from foris.nuci.modules.uci_raw import Uci, Config, Section, Option
from foris.utils import contract_valid
from foris.utils.translators import gettext_dummy as gettext, _
......
......@@ -22,7 +22,7 @@ from foris.nuci import client
from foris.nuci.filters import create_config_filter, wifi_filter
from foris.nuci.preprocessors import guest_network_enabled, generate_network_preprocessor
from foris.nuci.modules.uci_raw import Uci, Config, Section, Option, List, Value, parse_uci_bool
from foris.state import info
from foris.state import current_state
from foris.utils.routing import reverse
from foris.utils.translators import gettext_dummy as gettext, _
......@@ -71,7 +71,7 @@ class LanHandler(BaseConfigHandler):
nuci_path="uci.dhcp.lan.limit")\
.requires("dhcp_enabled", True)
if info.app == "config":
if current_state.app == "config":
guest_network_section = lan_form.add_section(
name="guest_network",
title=_("Guest network"),
......@@ -156,7 +156,7 @@ class LanHandler(BaseConfigHandler):
else:
dhcp.add(Option("ignore", "1"))
if info.app == "config":
if current_state.app == "config":
# qos data
qos = {'enabled': False}
if 'guest_network_shapping' in data and data['guest_network_shapping']:
......
......@@ -23,7 +23,7 @@ from foris.nuci.modules.uci_raw import Uci, Config, Section, Option
from foris.form import (
Hidden, Password, Textbox, Dropdown, Checkbox,
)
from foris.state import info
from foris.state import current_state
from foris.utils import (
tzinfo, localized_sorted
)
......@@ -120,7 +120,7 @@ class RegionHandler(BaseConfigHandler):
)
)
lang = info.language
lang = current_state.language
def construct_args(items, translation_function=_, key_getter=lambda x: x):
"""
......
......@@ -20,9 +20,9 @@ from foris import fapi, validators
from foris.caches import lazy_cache
from foris.form import Checkbox, Radio, RadioSingle, Number
from foris.nuci import client, filters
from foris.nuci.helpers import contract_valid
from foris.nuci.modules.uci_raw import Uci, Config, Section, Option, List, Value, parse_uci_bool
from foris.nuci.preprocessors import preproc_disabled_to_agreed
from foris.utils import contract_valid
from foris.utils.translators import gettext_dummy as gettext, _
from .base import BaseConfigHandler, logger
......
......@@ -18,7 +18,7 @@ import re
from .base import BaseConfigHandler
from foris import fapi, validators
from foris.state import info
from foris.state import current_state
from foris.nuci import filters
from foris.nuci.modules.uci_raw import Uci, Section, Config, Option, Value, List
from foris.form import (
......@@ -39,7 +39,7 @@ class WanHandler(BaseConfigHandler):
self.hide_no_wan = kwargs.pop("hide_no_wan", False)
super(WanHandler, self).__init__(*args, **kwargs)
self.wan_ifname = "eth2"
if info.device_customization == "omnia":
if current_state.device_customization == "omnia":
self.wan_ifname = "eth1"
def get_form(self):
......
......@@ -19,7 +19,7 @@ from foris import validators
from foris.form import Checkbox, Dropdown, Hidden, Password, Radio, Textbox, HorizontalLine
from foris.nuci import client, filters, preprocessors
from foris.nuci.modules.uci_raw import Uci, Config, Section, Option, parse_uci_bool
from foris.state import info
from foris.state import current_state
from foris.utils.routing import reverse
from foris.utils.translators import gettext_dummy as gettext, _
......@@ -191,7 +191,7 @@ class WifiHandler(BaseConfigHandler):
hint=HINTS['password']
).requires(prefixed_name("wifi_enabled"), True)
if info.app == "config":
if current_state.app == "config":
# Guest wi-fi part
guest_section = wifi_main.add_section(
name=prefixed_name("set_guest_wifi"),
......@@ -289,7 +289,7 @@ class WifiHandler(BaseConfigHandler):
wireless.add(iface)
device = Section("radio%s" % radio, "wifi-device")
wireless.add(device)
if info.app == "config":
if current_state.app == "config":
guest_iface = Section("guest_iface_%s" % radio, "wifi-iface")
wireless.add(guest_iface)
......@@ -299,7 +299,7 @@ class WifiHandler(BaseConfigHandler):
iface.add(Option("disabled", not wifi_enabled))
device.add(Option("disabled", not wifi_enabled))
if info.app == "config":
if current_state.app == "config":
guest_iface.add(Option("disabled", not wifi_enabled or not guest_enabled))
if wifi_enabled:
......@@ -323,7 +323,7 @@ class WifiHandler(BaseConfigHandler):
device.add(Option("channel", channel))
# setting guest wifi
if info.app == "config":
if current_state.app == "config":
if guest_enabled:
guest_iface.add(Option("device", "radio%s" % radio))
guest_iface.add(Option("mode", "ap"))
......
......@@ -16,15 +16,14 @@
from ncclient.operations import TimeoutExpiredError, RPCError
from foris.caches import nuci_cache
from foris.state import info
from foris.state import current_state
from foris.utils import WIZARD_NEXT_STEP_ALLOWED_KEY
from foris.utils.translators import _
from .client import get as nuci_get, edit_config as nuci_edit_config
from .filters import foris_config
from .modules.user_notify import Severity
from .modules.uci_raw import parse_uci_bool, Uci, Config, Section, Option
from .modules.uci_raw import Uci, Config, Section, Option
def make_notification_title(notification):
......@@ -49,24 +48,6 @@ def make_notification_title(notification):
)
def contract_valid():
"""Read whether the contract related with the current router is valid from uci
:return: whether the contract is still valid
"""
if info.device_customization == "omnia":
return False
data = nuci_cache.get("foris.contract", 60 * 60) # once per hour should be enought
valid = data.find_child("foris.contract.valid")
if not valid: # valid record
# valid record not found, assuming that the contract is still valid
return True
return parse_uci_bool(valid)
def read_uci_lang(default):
"""Read interface language saved in Uci config foris.settings.lang.
......@@ -78,7 +59,7 @@ def read_uci_lang(default):
lang = lang.value if lang else default
# Update info variable
info.update_lang(lang)
current_state.update_lang(lang)
return lang
......@@ -96,7 +77,7 @@ def write_uci_lang(lang):
server = Section("settings", "config")
foris.add(server)
server.add(Option("lang", lang))
if info.app == "config":
if current_state.app == "config":
# LuCI language (only in config app)
luci = Config("luci")
uci.add(luci)
......@@ -107,7 +88,7 @@ def write_uci_lang(lang):
nuci_edit_config(uci.get_xml())
# Update info variable
info.update_lang(lang)
current_state.update_lang(lang)
return True
except (RPCError, TimeoutExpiredError):
......
......@@ -23,7 +23,7 @@ from foris.langs import DEFAULT_LANGUAGE
logger = logging.getLogger("foris.state")
class ForisStateInfo(object):
class ForisState(object):
def __init__(self):
self.device_customization = DEVICE_CUSTOMIZATION
self.foris_version = version
......@@ -48,4 +48,4 @@ class ForisStateInfo(object):
return "%s (%s)" % (self.__class__, str(vars(self)))
info = ForisStateInfo()
current_state = ForisState()
......@@ -21,11 +21,10 @@ import bottle
from functools import wraps
import logging
from foris.state import info
from .routing import reverse
from . import messages
from .translators import _
from foris.state import current_state
logger = logging.getLogger("foris.utils")
......@@ -184,3 +183,24 @@ def localized_sorted(iterable, lang, cmp=None, key=None, reverse=False):
return map(safe_index, key(x))
return sorted(iterable, cmp, key_fn, reverse)
def contract_valid():
"""Read whether the contract related with the current router is valid
:return: whether the contract is still valid
"""
CONRACT_VALID = "valid"
CONRACT_UNKNOWN = "unknown"
if current_state.device_customization == "omnia":
return False
data = current_state.backend_instance.send("about", "get_contract_status", {})
if data["contract_status"] == CONRACT_VALID:
return True
if data["contract_status"] == CONRACT_UNKNOWN:
# Consider old contract valid for old turrises
return True
return False
......@@ -21,7 +21,7 @@ import bottle
from foris.caches import lazy_cache
from foris.langs import iso2to3, translation_names
from foris.middleware.bottle_csrf import get_csrf_token
from foris.state import info
from foris.state import current_state
from .routing import reverse, static as static_path
from .translators import translations, ugettext, ungettext
......@@ -35,7 +35,7 @@ def prepare_template_defaults():
bottle.SimpleTemplate.defaults['iso2to3'] = iso2to3
bottle.SimpleTemplate.defaults['ungettext'] = \
lambda singular, plural, n: ungettext(singular, plural, n)
bottle.SimpleTemplate.defaults['foris_info'] = info
bottle.SimpleTemplate.defaults['foris_info'] = current_state
# template defaults
# this is not really straight-forward, check for user_authenticated() (with brackets) in template,
......
......@@ -20,7 +20,7 @@ import gettext
import os
from foris.langs import translations
from foris.state import info
from foris.state import current_state
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
......@@ -32,8 +32,9 @@ translations = collections.OrderedDict(
for e in translations
)
ugettext = lambda x: translations[info.language].ugettext(x)
ungettext = lambda singular, plural, n: translations[info.language].ungettext(singular, plural, n)
ugettext = lambda x: translations[current_state.language].ugettext(x)
ungettext = lambda singular, plural, n: \
translations[current_state.language].ungettext(singular, plural, n)
gettext_dummy = lambda x: x
_ = ugettext
......@@ -24,13 +24,14 @@ from foris.config_handlers import base, lan, misc, updater, wan, wifi
from foris.nuci import client, filters
from foris.nuci.configurator import add_config_update, commit
from foris.nuci.helpers import (
contract_valid, mark_wizard_finished_session, allow_next_step_session, get_wizard_progress,
mark_wizard_finished_session, allow_next_step_session, get_wizard_progress,
make_notification_title,
)
from foris.nuci.modules.uci_raw import build_option_uci_tree
from foris.nuci.preprocessors import preproc_disabled_to_agreed
from foris.utils import (
login_required, messages, WIZARD_NEXT_STEP_ALLOWED_KEY, NUM_WIZARD_STEPS, is_safe_redirect
login_required, messages, WIZARD_NEXT_STEP_ALLOWED_KEY, NUM_WIZARD_STEPS, is_safe_redirect,
contract_valid
)
from foris.middleware.bottle_csrf import CSRFPlugin
from foris.utils.routing import reverse
......
......@@ -30,11 +30,11 @@ from foris.common import render_js_md5, init_common_app, init_default_app
from foris.middleware.sessions import SessionMiddleware
from foris.middleware.reporting import ReportingMiddleware
from foris.nuci import client
from foris.nuci.helpers import contract_valid, read_uci_lang
from foris.nuci.helpers import read_uci_lang
from foris.langs import DEFAULT_LANGUAGE
from foris.plugins import ForisPluginLoader
from foris.state import info
from foris.utils import messages
from foris.state import current_state
from foris.utils import messages, contract_valid
from foris.utils.translators import translations
from foris.utils.bottle_stuff import (
prepare_template_defaults,
......@@ -62,7 +62,7 @@ def prepare_wizard_app(args):
"""
# set app
info.set_app("wizard")
current_state.set_app("wizard")
# internationalization
i18n_defaults(bottle.SimpleTemplate, bottle.request)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment