Exemple #1
0
    def set(self, code, value):
        """Set new value for given variable code"""
        from trionyx.trionyx.models import SystemVariable
        with CacheLock('variables-set', code):
            SystemVariable.objects.update_or_create(code=code,
                                                    defaults={
                                                        'value': value,
                                                    })

        utils.set_local_data('trionyx_variables', None)
        cache.delete(self.cache_key)
Exemple #2
0
def set_user(task_id, task, *args, **kwargs):
    """Set user to local data"""
    metadata = getattr(task.request, '__metadata__', {})
    user_id = metadata.get('trionyx_user_id')
    User = get_user_model()

    # Make sure local data is clean, before setting new data
    utils.clear_local_data()

    if user_id:
        try:
            user = User.objects.get(id=user_id)
            utils.set_local_data('user', user)
            logger.debug(f'set user: {user}')
        except User.DoesNotExist:
            pass
Exemple #3
0
    def get(self, code, default=None):
        """Get value for given variable code"""
        from trionyx.trionyx.models import SystemVariable

        variables = utils.get_local_data('trionyx_variables')

        if variables is None:
            variables = cache.get(self.cache_key)

        if variables is None:
            variables = {
                variable.code: variable.value
                for variable in SystemVariable.objects.all()
            }
            cache.set(self.cache_key, variables, timeout=60 * 60 * 24)

        utils.set_local_data('trionyx_variables', variables)

        return variables.get(code, default)
Exemple #4
0
    def get_attribute(self, user, code, default=None):
        """Get attribute for user"""
        cache_key = f'user-attributes-{user.id}'

        attributes = utils.get_local_data('trionyx_user_attributes')

        if attributes is None:
            attributes = cache.get(cache_key)

        if attributes is None:
            attributes = {
                attribute.code: attribute.value
                for attribute in self.filter(user=user)
            }
            cache.set(cache_key, attributes, timeout=60 * 60 * 24)

        if utils.get_local_data('trionyx_user_attributes') is None:
            utils.set_local_data('trionyx_user_attributes', attributes)

        return attributes.get(code, default)
Exemple #5
0
    def __call__(self, request):
        """Store request in local data"""
        utils.set_local_data('request', request)

        def streaming_content_wrapper(content):
            try:
                for chunk in content:
                    yield chunk
            finally:
                utils.clear_local_data()

        try:
            response = self.get_response(request)
        except Exception as e:
            utils.clear_local_data()
            raise e

        if response.streaming:
            response.streaming_content = streaming_content_wrapper(
                response.streaming_content)
        else:
            utils.clear_local_data()

        return response
Exemple #6
0
 def prepare_value(self, value):
     """Prepare value"""
     utils.set_local_data(f'choices-{self.field_id}', value)
     return super().prepare_value(value)
Exemple #7
0
    def set_attribute(self, user, code, value):
        """Set attribute for user"""
        utils.set_local_data('trionyx_user_attributes', None)
        cache.delete(f'user-attributes-{user.id}')

        self.update_or_create(user=user, code=code, defaults={'value': value})