core: Remove AppTask Inheritance from Models

This commit is contained in:
Jens Langhammer 2018-11-28 14:01:46 +01:00
parent bc65fae4b2
commit 62ee5db752

View file

@ -8,7 +8,6 @@ from django.db import models
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from model_utils.managers import InheritanceManager from model_utils.managers import InheritanceManager
from passbook.core.celery import CELERY_APP
from passbook.lib.models import CreatedUpdatedModel, UUIDModel from passbook.lib.models import CreatedUpdatedModel, UUIDModel
LOGGER = getLogger(__name__) LOGGER = getLogger(__name__)
@ -32,13 +31,6 @@ class Provider(models.Model):
return getattr(self, 'name') return getattr(self, 'name')
return super().__str__() return super().__str__()
class TaskModel(CELERY_APP.Task, models.Model):
"""Django model which is also a celery task"""
class Meta:
abstract = True
class RuleModel(UUIDModel, CreatedUpdatedModel): class RuleModel(UUIDModel, CreatedUpdatedModel):
"""Base model which can have rules applied to it""" """Base model which can have rules applied to it"""
@ -99,7 +91,7 @@ class UserSourceConnection(CreatedUpdatedModel):
unique_together = (('user', 'source'),) unique_together = (('user', 'source'),)
@reversion.register() @reversion.register()
class Rule(TaskModel, UUIDModel, CreatedUpdatedModel): class Rule(UUIDModel, CreatedUpdatedModel):
"""Rules which specify if a user is authorized to use an Application. Can be overridden by """Rules which specify if a user is authorized to use an Application. Can be overridden by
other types to add other fields, more logic, etc.""" other types to add other fields, more logic, etc."""
@ -122,10 +114,6 @@ class Rule(TaskModel, UUIDModel, CreatedUpdatedModel):
return self.name return self.name
return "%s action %s" % (self.name, self.action) return "%s action %s" % (self.name, self.action)
def run(self, user_id: int) -> bool:
"""Celery wrapper for passes"""
return self.passes(User.objects.get(pk=user_id))
def passes(self, user: User) -> bool: def passes(self, user: User) -> bool:
"""Check if user instance passes this rule""" """Check if user instance passes this rule"""
raise NotImplementedError() raise NotImplementedError()
@ -152,7 +140,6 @@ class FieldMatcherRule(Rule):
match_action = models.CharField(max_length=50, choices=MATCHES) match_action = models.CharField(max_length=50, choices=MATCHES)
value = models.TextField() value = models.TextField()
name = 'passbook_core.FieldMatcherRule'
form = 'passbook.core.forms.rules.FieldMatcherRuleForm' form = 'passbook.core.forms.rules.FieldMatcherRuleForm'
def __str__(self): def __str__(self):
@ -214,7 +201,6 @@ class WebhookRule(Rule):
result_jsonpath = models.TextField() result_jsonpath = models.TextField()
result_json_value = models.TextField() result_json_value = models.TextField()
name = 'passbook_core.WebhookRule'
form = 'passbook.core.forms.rules.WebhookRuleForm' form = 'passbook.core.forms.rules.WebhookRuleForm'
def passes(self, user: User): def passes(self, user: User):
@ -225,7 +211,3 @@ class WebhookRule(Rule):
verbose_name = _('Webhook Rule') verbose_name = _('Webhook Rule')
verbose_name_plural = _('Webhook Rules') verbose_name_plural = _('Webhook Rules')
# Register tasks
for task_model in Rule.__subclasses__():
CELERY_APP.tasks.register(task_model)