Skip to content

Commit

Permalink
Move signals to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Apr 11, 2024
1 parent 0bb0357 commit cec4da1
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 91 deletions.
14 changes: 6 additions & 8 deletions onadata/apps/api/viewsets/data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
nullify_exports_time_of_last_submission,
update_xform_submission_count_delete,
)
from onadata.apps.main.models import UserProfile
from onadata.apps.viewer.models.parsed_instance import (
_remove_from_mongo,
ParsedInstance,
)
from onadata.apps.viewer.models.parsed_instance import ParsedInstance
from onadata.apps.viewer.signals import remove_from_mongo
from onadata.libs.renderers import renderers
from onadata.libs.mixins.anonymous_user_public_forms_mixin import (
AnonymousUserPublicFormsMixin)
AnonymousUserPublicFormsMixin,
)
from onadata.apps.api.permissions import (
EnketoSubmissionEditPermissions,
EnketoSubmissionViewPermissions,
Expand Down Expand Up @@ -413,7 +411,7 @@ def bulk_delete(self, request, *args, **kwargs):
postgres_query, mongo_query = self.__build_db_queries(xform, request.data)

# Disconnect signals to speed-up bulk deletion
pre_delete.disconnect(_remove_from_mongo, sender=ParsedInstance)
pre_delete.disconnect(remove_from_mongo, sender=ParsedInstance)
post_delete.disconnect(
nullify_exports_time_of_last_submission, sender=Instance,
dispatch_uid='nullify_exports_time_of_last_submission',
Expand Down Expand Up @@ -445,7 +443,7 @@ def bulk_delete(self, request, *args, **kwargs):
)
finally:
# Pre_delete signal needs to be re-enabled for parsed instance
pre_delete.connect(_remove_from_mongo, sender=ParsedInstance)
pre_delete.connect(remove_from_mongo, sender=ParsedInstance)
post_delete.connect(
nullify_exports_time_of_last_submission,
sender=Instance,
Expand Down
16 changes: 0 additions & 16 deletions onadata/apps/logger/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
# coding: utf-8
from django.apps import AppConfig


class LoggerAppConfig(AppConfig):

name = 'onadata.apps.logger'

def ready(self):
# Makes sure all signal handlers are connected
from onadata.apps.logger import signals
# Monkey patch reversion package to insert real user in DB instead of
# system account superuser.
from kobo_service_account.utils import reversion_monkey_patch
reversion_monkey_patch()
super().ready()
16 changes: 16 additions & 0 deletions onadata/apps/logger/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# coding: utf-8
from django.apps import AppConfig


class LoggerAppConfig(AppConfig):

name = 'onadata.apps.logger'

def ready(self):
# Makes sure all signal handlers are connected
from onadata.apps.logger import signals
# Monkey patch reversion package to insert real user in DB instead of
# system account superuser.
from kobo_service_account.utils import reversion_monkey_patch
reversion_monkey_patch()
super().ready()
10 changes: 10 additions & 0 deletions onadata/apps/main/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# coding: utf-8
from django.apps import AppConfig


class MainConfig(AppConfig):
name = 'onadata.apps.main'

def ready(self):
from onadata.apps.main import signals
super().ready()
27 changes: 0 additions & 27 deletions onadata/apps/main/models/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.utils import timezone
from django.utils.translation import gettext_lazy
from guardian.conf import settings as guardian_settings
from guardian.shortcuts import get_perms_for_model, assign_perm
from rest_framework.authtoken.models import Token

from onadata.apps.logger.fields import LazyDefaultBooleanField
from onadata.apps.main.signals import set_api_permissions
from onadata.libs.utils.country_field import COUNTRIES
from onadata.libs.utils.gravatar import get_gravatar_img_link, gravatar_exists

Expand Down Expand Up @@ -63,27 +57,6 @@ class Meta:
)


def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)


post_save.connect(create_auth_token, sender=User, dispatch_uid='auth_token')

post_save.connect(set_api_permissions, sender=User,
dispatch_uid='set_api_permissions')


def set_object_permissions(sender, instance=None, created=False, **kwargs):
if created:
for perm in get_perms_for_model(UserProfile):
assign_perm(perm.codename, instance.user, instance)


post_save.connect(set_object_permissions, sender=UserProfile,
dispatch_uid='set_object_permissions')


def get_anonymous_user_instance(User):
"""
Force `AnonymousUser` to be saved with `pk` == `ANONYMOUS_USER_ID`
Expand Down
26 changes: 24 additions & 2 deletions onadata/apps/main/signals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# coding: utf-8
from django.db.models.signals import post_save
from django.dispatch import receiver
from guardian.shortcuts import assign_perm, get_perms_for_model
from rest_framework.authtoken.models import Token

from django.contrib.auth.models import User
from onadata.apps.main.models.user_profile import UserProfile
from onadata.libs.utils.user_auth import set_api_permissions_for_user


@receiver(post_save, sender=User, dispatch_uid='set_api_permissions')
def set_api_permissions(sender, instance=None, created=False, **kwargs):
from onadata.libs.utils.user_auth import set_api_permissions_for_user
if created:
set_api_permissions_for_user(instance)


@receiver(post_save, sender=User)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)


@receiver(post_save, sender=UserProfile, dispatch_uid='set_object_permissions')
def set_object_permissions(sender, instance=None, created=False, **kwargs):
if created:
for perm in get_perms_for_model(UserProfile):
assign_perm(perm.codename, instance.user, instance)
7 changes: 4 additions & 3 deletions onadata/apps/restservice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@


class RestServiceConfig(AppConfig):
name = "onadata.apps.restservice"
verbose_name = "restservice"
name = 'onadata.apps.restservice'
verbose_name = 'restservice'

def ready(self):
# Register RestService signals
import onadata.apps.restservice.signals
from onadata.apps.restservice import signals
super().ready()
10 changes: 10 additions & 0 deletions onadata/apps/viewer/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# coding: utf-8
from django.apps import AppConfig


class ViewerConfig(AppConfig):
name = 'onadata.apps.viewer'

def ready(self):
from onadata.apps.viewer import signals
super().ready()
16 changes: 4 additions & 12 deletions onadata/apps/viewer/models/data_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from xml.dom import Node

from django.db import models
from django.db.models.signals import post_save
from django.utils.encoding import smart_str
from guardian.shortcuts import assign_perm, get_perms_for_model
from pyxform import SurveyElementBuilder
from pyxform.builder import create_survey_from_xls
from pyxform.question import Question
Expand All @@ -17,8 +15,10 @@
from onadata.apps.logger.xform_instance_parser import clean_and_parse_xml
from onadata.apps.api.mongo_helper import MongoHelper
from onadata.libs.utils.common_tags import UUID, SUBMISSION_TIME, TAGS, NOTES
from onadata.libs.utils.export_tools import question_types_to_exclude,\
DictOrganizer
from onadata.libs.utils.export_tools import (
question_types_to_exclude,
DictOrganizer,
)
from onadata.libs.utils.model_tools import queryset_iterator, set_uuid


Expand Down Expand Up @@ -425,11 +425,3 @@ def _mark_start_time_boolean(self):
def get_survey_elements_of_type(self, element_type):
return [e for e in self.get_survey_elements()
if e.type == element_type]


def set_object_permissions(sender, instance=None, created=False, **kwargs):
if created:
for perm in get_perms_for_model(XForm):
assign_perm(perm.codename, instance.user, instance)
post_save.connect(set_object_permissions, sender=DataDictionary,
dispatch_uid='xform_object_permissions')
10 changes: 0 additions & 10 deletions onadata/apps/viewer/models/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

from django.core.files.storage import default_storage
from django.db import models
from django.db.models.signals import post_delete
from django.utils.translation import gettext as t

from onadata.apps.logger.models import XForm


def export_delete_callback(sender, **kwargs):
export = kwargs['instance']
if export.filepath and default_storage.exists(export.filepath):
default_storage.delete(export.filepath)


class Export(models.Model):
class ExportTypeError(Exception):
def __str__(self):
Expand Down Expand Up @@ -177,6 +170,3 @@ def exports_outdated(cls, xform, export_type):
def is_filename_unique(cls, xform, filename):
return Export.objects.filter(
xform=xform, filename=filename).count() == 0


post_delete.connect(export_delete_callback, sender=Export)
10 changes: 1 addition & 9 deletions onadata/apps/viewer/models/parsed_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dateutil import parser
from django.conf import settings
from django.db import models
from django.db.models.signals import pre_delete

from django.utils.translation import gettext as t
from pymongo.errors import PyMongoError

Expand Down Expand Up @@ -411,11 +411,3 @@ def _get_attachments_from_instance(instance):
attachments.append(attachment)

return attachments


def _remove_from_mongo(sender, **kwargs):
instance_id = kwargs.get('instance').instance.id
xform_instances.delete_one({'_id': instance_id})


pre_delete.connect(_remove_from_mongo, sender=ParsedInstance)
30 changes: 30 additions & 0 deletions onadata/apps/viewer/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.conf import settings
from django.core.files.storage import default_storage
from django.db.models.signals import post_delete, post_save, pre_delete
from django.dispatch import receiver
from guardian.shortcuts import assign_perm, get_perms_for_model

from onadata.apps.logger.models import XForm
from onadata.apps.viewer.models.data_dictionary import DataDictionary
from onadata.apps.viewer.models.export import Export
from onadata.apps.viewer.models.parsed_instance import ParsedInstance


@receiver(post_delete, sender=Export)
def export_delete_callback(sender, **kwargs):
export = kwargs['instance']
if export.filepath and default_storage.exists(export.filepath):
default_storage.delete(export.filepath)


@receiver(post_save, sender=DataDictionary, dispatch_uid='xform_object_permissions')
def set_object_permissions(sender, instance=None, created=False, **kwargs):
if created:
for perm in get_perms_for_model(XForm):
assign_perm(perm.codename, instance.user, instance)


@receiver(pre_delete, sender=ParsedInstance)
def remove_from_mongo(sender, **kwargs):
instance_id = kwargs.get('instance').instance.id
settings.MONGO_DB.instances.delete_one({'_id': instance_id})
8 changes: 4 additions & 4 deletions onadata/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ def skip_suspicious_operations(record):
'django_digest',
'corsheaders',
'oauth2_provider',
'onadata.apps.logger.LoggerAppConfig',
'onadata.apps.logger.app.LoggerAppConfig',
'rest_framework',
'rest_framework.authtoken',
'taggit',
'readonly',
'onadata.apps.viewer',
'onadata.apps.main',
'onadata.apps.restservice',
'onadata.apps.viewer.app.ViewerConfig',
'onadata.apps.main.app.MainConfig',
'onadata.apps.restservice.app.RestServiceConfig',
'onadata.apps.api',
'guardian',
'onadata.libs',
Expand Down

0 comments on commit cec4da1

Please sign in to comment.