Projet

Général

Profil

0001-WIP-add-journal-application-20695.patch

Paul Marillonnet, 20 novembre 2018 20:29

Télécharger (12,1 ko)

Voir les différences:

Subject: [PATCH] WIP add journal application (#20695)

 MANIFEST.in                                   |  2 +
 setup.py                                      |  1 +
 src/authentic2/settings.py                    |  1 +
 src/authentic2_journal/__init__.py            |  9 ++++
 src/authentic2_journal/admin.py               |  0
 src/authentic2_journal/app_settings.py        | 22 ++++++++++
 src/authentic2_journal/apps.py                |  8 ++++
 src/authentic2_journal/event_logger.py        | 17 ++++++++
 src/authentic2_journal/kinds.py               | 22 ++++++++++
 src/authentic2_journal/migrations/__init__.py |  0
 src/authentic2_journal/models.py              | 28 ++++++++++++
 src/authentic2_journal/tables.py              | 12 ++++++
 .../authentic2_journal/user_events.html       | 10 +++++
 src/authentic2_journal/urls.py                | 10 +++++
 src/authentic2_journal/utils.py               | 43 +++++++++++++++++++
 src/authentic2_journal/views.py               | 22 ++++++++++
 16 files changed, 207 insertions(+)
 create mode 100644 src/authentic2_journal/__init__.py
 create mode 100644 src/authentic2_journal/admin.py
 create mode 100644 src/authentic2_journal/app_settings.py
 create mode 100644 src/authentic2_journal/apps.py
 create mode 100644 src/authentic2_journal/event_logger.py
 create mode 100644 src/authentic2_journal/kinds.py
 create mode 100644 src/authentic2_journal/migrations/__init__.py
 create mode 100644 src/authentic2_journal/models.py
 create mode 100644 src/authentic2_journal/tables.py
 create mode 100644 src/authentic2_journal/templates/authentic2_journal/user_events.html
 create mode 100644 src/authentic2_journal/urls.py
 create mode 100644 src/authentic2_journal/utils.py
 create mode 100644 src/authentic2_journal/views.py
MANIFEST.in
22 22
recursive-include src/authentic2_auth_saml/templates/authentic2_auth_saml *.html
23 23
recursive-include src/authentic2_auth_oidc/templates/authentic2_auth_oidc *.html
24 24
recursive-include src/authentic2_idp_oidc/templates/authentic2_idp_oidc *.html
25
recursive-include src/authentic2_journal/templates/authentic2_journal *.html
25 26

  
26 27
recursive-include src/authentic2/vendor/totp_js/js *.js
27 28
recursive-include src/authentic2/saml/fixtures *.json
......
41 42
recursive-include src/authentic2_auth_saml/locale *.po *.mo
42 43
recursive-include src/authentic2_auth_oidc/locale *.po *.mo
43 44
recursive-include src/authentic2_idp_oidc/locale *.po *.mo
45
recursive-include src/authentic2_journal/locale *.po *.mo
44 46

  
45 47
recursive-include src/authentic2 README  xrds.xml *.txt yadis.xrdf
46 48
recursive-include src/authentic2_provisionning_ldap/tests *.ldif
setup.py
165 165
              'authentic2-idp-cas = authentic2_idp_cas:Plugin',
166 166
              'authentic2-idp-oidc = authentic2_idp_oidc:Plugin',
167 167
              'authentic2-provisionning-ldap = authentic2_provisionning_ldap:Plugin',
168
              'authentic2-journal = authentic2_journal:Plugin',
168 169
          ],
169 170
      })
src/authentic2/settings.py
126 126
    'authentic2.disco_service',
127 127
    'authentic2.manager',
128 128
    'authentic2_provisionning_ldap',
129
    'authentic2_journal',
129 130
    'authentic2',
130 131
    'django_rbac',
131 132
    'authentic2.a2_rbac',
src/authentic2_journal/__init__.py
1
# -*- coding: utf-8 -*-
2
class Plugin(object):
3
    def get_before_urls(self):
4
        from django.conf.urls import include, url
5
        from . import urls
6
        return [url(r'^manage/', include(urls))]
7

  
8
    def get_apps(self):
9
        return [__name__]
src/authentic2_journal/app_settings.py
1
class AppSettings(object):
2
    __DEFAULTS = {
3
            'ENABLE': False,
4
    }
5

  
6
    def __init__(self, prefix):
7
        self.prefix = prefix
8

  
9
    def _setting(self, name, dflt):
10
        from django.conf import settings
11
        return getattr(settings, self.prefix + name, dflt)
12

  
13
    def __getattr__(self, name):
14
        if name not in self.__DEFAULTS:
15
            raise AttributeError(name)
16
        return self._setting(name, self.__DEFAULTS[name])
17

  
18

  
19
import sys
20
app_settings = AppSettings('A2_JOURNAL_')
21
app_settings.__name__ = __name__
22
sys.modules[__name__] = app_settings
src/authentic2_journal/apps.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

  
4
from django.apps import AppConfig
5

  
6

  
7
class Authentic2JournalConfig(AppConfig):
8
    name = 'authentic2_journal'
src/authentic2_journal/event_logger.py
1
# -*- coding: utf-8 -*-
2
from authentic2_journal.models import Line
3
from authentic2_journal import kinds
4
from authentic2_journal.utils import (new_line, new_action_reference,
5
        new_modification_reference)
6

  
7

  
8
def register(user, ou, content=None):
9
    line = new_line(user=user, obj=ou, kind=kinds.ActionRegister, content=content)
10
    new_reference(line, user)
11
    new_reference(line, ou)
12

  
13

  
14
def create(user, obj, content=None):
15
    line = new_line(user, ou, kinds.ModificationCreate, content)
16
    new_reference(line, user)
17
    new_reference(line, obj)
src/authentic2_journal/kinds.py
1
# -*- coding: utf-8 -*-
2
from django.db import models
3

  
4

  
5
class BaseEvent(models.Model):
6
    pass
7

  
8

  
9
class BaseAction(BaseEvent):
10
    pass
11

  
12

  
13
class BaseModification(BaseEvent):
14
    pass
15

  
16

  
17
class ActionRegister(BaseAction):
18
    pass
19

  
20

  
21
class ModificationCreate(BaseModification):
22
    pass
src/authentic2_journal/models.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

  
4
from django.db import models
5
try:
6
    from django.contrib.contenttypes.fields import GenericForeignKey
7
except ImportError:
8
    from django.contrib.contenttypes.generic import GenericForeignKey
9
from django.contrib.contenttypes.models import ContentType
10
from django.utils.translation import ugettext_lazy as _
11
from jsonfield import JSONField
12

  
13
from authentic2_journal import kinds
14

  
15

  
16
class Line(models.Model):
17
    timestamp = models.DateTimeField(auto_now=True, verbose_name=_('timestamp'))
18
    kind = models.ForeignKey(kinds.BaseEvent)
19
    content = JSONField(blank=True,null=True, verbose_name=_('content'))
20

  
21

  
22
class Reference(models.Model):
23
    timestamp = models.DateTimeField(auto_now=True, verbose_name=_('timestamp'))
24
    line = models.ForeignKey(Line)
25
    representation = models.TextField(max_length=64)
26
    target_ct = models.ForeignKey(ContentType)
27
    target_id = models.IntegerField(default=0)
28
    target = GenericForeignKey('target_ct', 'target_id')
src/authentic2_journal/tables.py
1
# -*- coding: utf-8 -*-
2
import django_tables2 as tables
3
from authentic2_journal.models import Reference
4
from django.utils.translation import ugettext_lazy as _
5

  
6

  
7
class UserEventsTable(tables.Table):
8
    class Meta:
9
        attrs = {'class': 'main'}
10
        model = Reference
11
        exclude = ['line', 'target_id', 'target_ct']
12
        empty_text = _('None')
src/authentic2_journal/templates/authentic2_journal/user_events.html
1
{% extends "authentic2/manager/base.html" %}
2
{% load i18n staticfiles django_tables2 %}
3

  
4
{% block page_title %}
5
{% trans "User events journal" %}
6
{% endblock %}
7

  
8
{% block content %}
9
  {% render_table table "authentic2/manager/table.html" %}
10
{% endblock %}
src/authentic2_journal/urls.py
1
from django.conf.urls import url
2

  
3
from . import views
4

  
5

  
6
urlpatterns = [
7
    url('^users/(?P<pk>\d+)/journal/$',
8
        views.user_journal,
9
        name='a2-journal-user'),
10
]
src/authentic2_journal/utils.py
1
# -*- coding: utf-8 -*-
2
import logging
3

  
4
from authentic2_journal import kinds
5
from authentic2_journal.models import Line, Reference
6
from django.contrib.contenttypes.models import ContentType
7
from django.core.exceptions import ObjectDoesNotExist
8

  
9

  
10

  
11
def new_line(user, obj, kind=kinds.BaseEvent, extra={}):
12
    try:
13
        line = Line.objects.create(
14
                kind=kind,
15
                content={
16
                    'email': user.email,
17
                    'obj': str(obj),
18
                    'extra': extra})
19
    except:
20
        logger.error("Couldn't create event journal entry for user %s on object %s" % (
21
                user, obj))
22
    else:
23
        return line
24

  
25

  
26
def new_reference(line, obj):
27
    logger = logging.getLogger(__name__)
28
    try:
29
        ct = ContentType.objects.get_for_model(obj)
30
    except ObjectDoesNotExist:
31
        logger.error('ContentType retrieval from model failed for object %s' % obj)
32
        logger.warning('No reference will be created for line %s' % line)
33
    else:
34
        try:
35
            reference = Reference.objects.create(
36
                    line=line,
37
                    target_ct=ct,
38
                    target_id=obj.pk)
39
        except:
40
            logger.error("Couldn't create event reference for line %s and object %s" % (
41
                    line, obj))
42
        else:
43
            return reference
src/authentic2_journal/views.py
1
# -*- coding: utf-8 -*-
2
from authentic2.manager.views import SimpleSubTableView
3
from authentic2_journal.tables import UserEventsTable
4
from authentic2_journal.models import Reference
5
from django.contrib.auth import get_user_model
6
from django.contrib.contenttypes.models import ContentType
7

  
8

  
9
class UserJournal(SimpleSubTableView):
10
    model = get_user_model()
11
    table_class = UserEventsTable
12
    template_name = 'authentic2_journal/user_events.html'
13
    permissions = ['custom_user.view_user']
14
    filter_table_by_perm = False
15

  
16
    def get_table_queryset(self):
17
        return Reference.objects.filter(
18
                target_id=self.object.pk,
19
                target_ct=ContentType.objects.get_for_model(self.object))
20

  
21

  
22
user_journal = UserJournal.as_view()
0
-