A
A
Alexey Belov2018-02-09 00:28:37
Django
Alexey Belov, 2018-02-09 00:28:37

API for registering operator activity logs?

URL: /client/<client_id>/log/operator
Method: POST
Data:
{
    "access_token": "TOKEN",
    "limit": 100,
    "sort": "asc",
    "start_date": "2018-01-01T00:00:00Z"
}
OR
{
    "access_token": "TOKEN",
    "limit": 100,
    "sort": "asc",
    "offset": "500"
}

Success Response:
{
    "result": "ok",
    "logs": [
        {
            "date": "2018-01-01T00:00:00Z",
            "text": "Log entry"
        },
        ...
    ]
}

Log entry in the format: "message. User: operator's first name last name."
I need to create a request to get operator activity logs. Which returns a formatted log of all user actions.
I have an activity application which has an OperatorLog model.
from django.db import models

from users.models import User
from .constants import ACTIVITY_ACTIONS


class OperatorLogManager(models.Manager):
    """Manager for operator logs"""

    def active(self):
        """Mark object as active"""
        return self.filter(is_delete=False)
    
    def get_by_user_id(self, id):
        return self.active().filter(pk=id)

    def get_by_client_id(self, client_uid):
        return self.active().filter(client_uid=client_uid)


class OperatorLog(models.Model):
    """Model for logs"""

    user        = models.ForeignKey(User, on_delete=models.CASCADE)
    created     = models.DateTimeField(auto_now_add=True)
    is_deleted  = models.BooleanField(default=False)
    action_type = models.CharField(max_length=32,
         сhoices=ACTIVITY_ACTIONS)
    message     = models.TextField(max_length=2048)
    client_uid  = models.CharField(max_length=50)
    bitrix_id   = models.PositiveIntegerField()

    # Operator Manager
    objects = OperatorLogManager()

    def delete(self, **kwargs):
        self.is_deleted = True
        self.save()

You need a public api (activity/api.py) to register events in other applications,
there is also a file with a list of activity types (constants.py), it must be available in other applications via import api.py, you need to display logs using serializer ( DRF).
Here is the log settings
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(asctime)s %(levelname)s %(message)s',
            'datefmt': '%b %d %H:%M:%S',
        },
    },
    'handlers': {
        # Mute
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        # Sends all messages to console
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
        'actions': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': root('logs/actions.log'),
            'formatter': 'verbose',
        },
        'errors': {
            'level': 'WARNING',
            'class': 'logging.FileHandler',
            'filename': root('logs/errors.log'),
            'formatter': 'verbose',
        },
        'debug_file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': root('logs/debug.log'),
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['errors'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'actions': {
            'handlers': ['actions', 'console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'db': {
            'handlers': ['errors', 'console'],
            'level': 'WARNING',
            'propagate': False,
        },
        'apps': {
            'handlers': ['debug_file', 'errors'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

constants.py (Sample Version)
from model_utils import Choices


ACTIVITY_ACTIONS = Choices(
    ('ACTION_REQ_PHONE_VERIFY', 'Request phone verification'),
    ('VALIDATE_PHONE', 'Request phone validation'),
    ('VALIDATE_EMAIL', 'Email validation'),
    ('SET_INTERNAL_VERIFY', 'Set result of Internal verification'),
    ('SET_BANK_VERIFY', 'Set result of Bank verification'),
    ('GET_CLIENT_DATA', 'Request client data'),
    ('GET_SENSITIVE_INFO', 'Request sensitive info'),
)

You need something like logs.api.register(user, message, flags) So that you can arrange it in key places. And the method itself has already saved as it should and where it should be.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-02-10
@dimonchik2013

what is the question? that does not work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question