import inspect import os from importlib import import_module from pathlib import Path from django.core.checks import Error, Warning, register from otree import common from otree.api import BasePlayer, BaseGroup, BaseSubsession, Currency, WaitPage, Page from otree.common import _get_all_configs class AppCheckHelper: """Basically a wrapper around the AppConfig """ def __init__(self, app_config, errors): self.app_config = app_config self.errors = errors def add_error(self, title, numeric_id: int, **kwargs): issue_id = 'otree.E' + str(numeric_id).zfill(3) kwargs.setdefault('obj', self.app_config.label) return self.errors.append(Error(title, id=issue_id, **kwargs)) def add_warning(self, title, numeric_id: int, **kwargs): kwargs.setdefault('obj', self.app_config.label) issue_id = 'otree.W' + str(numeric_id).zfill(3) return self.errors.append(Warning(title, id=issue_id, **kwargs)) # Helper meythods def get_path(self, name): return os.path.join(self.app_config.path, name) def get_rel_path(self, name): basepath = os.getcwd() return os.path.relpath(name, basepath) def get_module(self, name): return import_module(self.app_config.name + '.' + name) def get_template_names(self): path = self.get_path('templates') template_names = [] for root, dirs, files in os.walk(path): for filename in [f for f in files if f.endswith('.html')]: template_names.append(os.path.join(root, filename)) return template_names def module_exists(self, module): try: self.get_module(module) return True except ImportError as e: return False def class_exists(self, module, name): module = self.get_module(module) cls = getattr(module, name, None) return inspect.isclass(cls) def files(helper: AppCheckHelper, **kwargs): # don't check views.py because it might be pages.py for fn in ['models.py']: if not os.path.isfile(helper.get_path(fn)): helper.add_error('No "%s" file found in app folder' % fn, numeric_id=102) templates_dir = Path(helper.get_path('templates')) app_label = helper.app_config.label if templates_dir.is_dir(): # check for files in templates/, but not in templates/