Commit 964ee65a authored by Florent Chehab's avatar Florent Chehab
Browse files

bug removed and generalization of model config

parent 7427179e
Pipeline #26852 passed with stages
in 2 minutes and 17 seconds
...@@ -53,8 +53,10 @@ router.register( ...@@ -53,8 +53,10 @@ router.register(
urlpatterns += [url(r'^api/', include(router.urls))] urlpatterns += [url(r'^api/', include(router.urls))]
for model in ALL_MODELS: for model in ALL_MODELS:
if model.model_config is None: for key in model.model_config:
raise Exception("You forgot to set the moderation_level variable in the model {}".format(str(model))) val = model.model_config[key]
if val is None:
raise Exception("You forgot to set the {} config variable in the model {}".format(key, str(model)))
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
for viewset in ALL_VIEWSETS: for viewset in ALL_VIEWSETS:
......
...@@ -24,4 +24,6 @@ class MyModel(models.Model): ...@@ -24,4 +24,6 @@ class MyModel(models.Model):
class Meta: class Meta:
abstract = True abstract = True
model_config = None model_config = {
"moderation_level": None,
}
...@@ -50,7 +50,7 @@ class MyModelSerializer(serializers.ModelSerializer): ...@@ -50,7 +50,7 @@ class MyModelSerializer(serializers.ModelSerializer):
model = MyModel model = MyModel
def moderation_required(self): def moderation_required(self):
moderation_level = self.Meta.model.model_config moderation_level = self.Meta.model.model_config['moderation_level']
user = self.get_user_in_request() user = self.get_user_in_request()
if moderation_level == 0: if moderation_level == 0:
......
import yaml from general.api import get_api_config
from os.path import join, realpath, dirname
def get_model_config(model): def get_model_config(model):
current_dir = dirname(realpath(__file__)) api_config = get_api_config()
api_file_loc = join(current_dir, '../../general/api/api_config.yml')
with open(api_file_loc, "r") as file: for obj in api_config:
api_config = yaml.load(file.read()) if obj['model'] == model:
for obj in api_config: return obj
if obj['model'] == model:
try:
return obj["moderation_level"]
except KeyError:
return 2
raise Exception("Model not found in API configuraiton, cannot process !") raise Exception("Model not found in API configuraiton, cannot process !")
...@@ -11,7 +11,7 @@ def get_api_config(): ...@@ -11,7 +11,7 @@ def get_api_config():
DEFAULT_SETTINGS = { DEFAULT_SETTINGS = {
"ignore_in_admin": False, "ignore_in_admin": False,
"requires_testing": False, "requires_testing": False,
"moderation_lebel": 2 "moderation_level": 2
} }
for obj in api_config: for obj in api_config:
......
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