sobjects/services.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 12 Feb 2016 14:09:38 +0100
changeset 11109 c361260fe191
parent 10951 ef1cfc80d51c
permissions -rw-r--r--
Remove unused import in entity.py
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     3
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     5
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     9
# any later version.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    10
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    14
# details.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    15
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    18
"""Define server side service provided by cubicweb"""
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    19
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    20
import threading
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    21
10770
c3ed65f23181 [sobjects] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10662
diff changeset
    22
from six import text_type
c3ed65f23181 [sobjects] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10662
diff changeset
    23
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
    24
from yams.schema import role_name
10951
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
    25
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
    26
from cubicweb import ValidationError
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    27
from cubicweb.server import Service
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
    28
from cubicweb.predicates import match_user_groups, match_kwargs
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    29
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    30
class StatsService(Service):
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    31
    """Return a dictionary containing some statistics about the repository
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    32
    resources usage.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    33
    """
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    34
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    35
    __regid__  = 'repo_stats'
10034
7d0acf9cb92a [service] allow repo_stats for users
Julien Cristau <julien.cristau@logilab.fr>
parents: 9722
diff changeset
    36
    __select__ = match_user_groups('managers', 'users')
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    37
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    38
    def call(self):
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    39
        repo = self._cw.repo # Service are repo side only.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    40
        results = {}
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    41
        querier = repo.querier
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    42
        source = repo.system_source
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    43
        for size, maxsize, hits, misses, title in (
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    44
            (len(querier._rql_cache), repo.config['rql-cache-size'],
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    45
            querier.cache_hit, querier.cache_miss, 'rqlt_st'),
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    46
            (len(source._cache), repo.config['rql-cache-size'],
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    47
            source.cache_hit, source.cache_miss, 'sql'),
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    48
            ):
10309
35bf741203d6 [services] Prevent 'repo_stats' service from aggregating information
David Douard <david.douard@logilab.fr>
parents: 10034
diff changeset
    49
            results['%s_cache_size' % title] = {'size': size, 'maxsize': maxsize}
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    50
            results['%s_cache_hit' % title] = hits
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    51
            results['%s_cache_miss' % title] = misses
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    52
            results['%s_cache_hit_percent' % title] = (hits * 100) / (hits + misses)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    53
        results['type_source_cache_size'] = len(repo._type_source_cache)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    54
        results['extid_cache_size'] = len(repo._extid_cache)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    55
        results['sql_no_cache'] = repo.system_source.no_cache
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    56
        results['nb_open_sessions'] = len(repo._sessions)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    57
        results['nb_active_threads'] = threading.activeCount()
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    58
        looping_tasks = repo._tasks_manager._looping_tasks
10309
35bf741203d6 [services] Prevent 'repo_stats' service from aggregating information
David Douard <david.douard@logilab.fr>
parents: 10034
diff changeset
    59
        results['looping_tasks'] = [(t.name, t.interval) for t in looping_tasks]
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    60
        results['available_cnxsets'] = repo._cnxsets_pool.qsize()
10309
35bf741203d6 [services] Prevent 'repo_stats' service from aggregating information
David Douard <david.douard@logilab.fr>
parents: 10034
diff changeset
    61
        results['threads'] = [t.name for t in threading.enumerate()]
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    62
        return results
9035
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    63
10951
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
    64
9035
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    65
class GcStatsService(Service):
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    66
    """Return a dictionary containing some statistics about the repository
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    67
    resources usage.
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    68
    """
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    69
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    70
    __regid__  = 'repo_gc_stats'
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    71
    __select__ = match_user_groups('managers')
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    72
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    73
    def call(self, nmax=20):
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    74
        """Return a dictionary containing some statistics about the repository
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    75
        memory usage.
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    76
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    77
        This is a public method, not requiring a session id.
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    78
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    79
        nmax is the max number of (most) referenced object returned as
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    80
        the 'referenced' result
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    81
        """
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    82
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    83
        from cubicweb._gcdebug import gc_info
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    84
        from cubicweb.appobject import AppObject
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    85
        from cubicweb.rset import ResultSet
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    86
        from cubicweb.web.request import CubicWebRequestBase
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    87
        from rql.stmts import Union
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    88
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    89
        lookupclasses = (AppObject,
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    90
                         Union, ResultSet,
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    91
                         CubicWebRequestBase)
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    92
        try:
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    93
            from cubicweb.server.session import Session, InternalSession
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    94
            lookupclasses += (InternalSession, Session)
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    95
        except ImportError:
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    96
            pass  # no server part installed
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    97
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    98
        results = {}
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    99
        counters, ocounters, garbage = gc_info(lookupclasses,
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
   100
                                               viewreferrersclasses=())
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10330
diff changeset
   101
        values = sorted(counters.items(), key=lambda x: x[1], reverse=True)
9035
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
   102
        results['lookupclasses'] = values
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10330
diff changeset
   103
        values = sorted(ocounters.items(), key=lambda x: x[1], reverse=True)[:nmax]
9035
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
   104
        results['referenced'] = values
10310
b49761555ad6 [services] Fix 'repo_gc_stats' to return a list of unreachable objects' repr
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10309
diff changeset
   105
        results['unreachable'] = garbage
9035
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
   106
        return results
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   107
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   108
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   109
class RegisterUserService(Service):
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   110
    """check if a user with the given login exists, if not create it with the
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   111
    given password. This service is designed to be used for anonymous
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   112
    registration on public web sites.
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   113
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   114
    To use it, do:
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   115
     with self.appli.repo.internal_cnx() as cnx:
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   116
        cnx.call_service('register_user',
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   117
                         login=login,
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   118
                         password=password,
9690
2b506822ca54 [services] rename register_user kwargs to cwuserkwargs
Julien Cristau <julien.cristau@logilab.fr>
parents: 9689
diff changeset
   119
                         **cwuserkwargs)
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   120
    """
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   121
    __regid__ = 'register_user'
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   122
    __select__ = Service.__select__ & match_kwargs('login', 'password')
9689
9e4a3c8719a7 [services] don't hardcode 'users' group for register_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 9687
diff changeset
   123
    default_groups = ('users',)
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   124
9690
2b506822ca54 [services] rename register_user kwargs to cwuserkwargs
Julien Cristau <julien.cristau@logilab.fr>
parents: 9689
diff changeset
   125
    def call(self, login, password, email=None, groups=None, **cwuserkwargs):
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   126
        cnx = self._cw
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   127
        errmsg = cnx._('the value "%s" is already used, use another one')
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   128
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   129
        if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login},
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   130
                        build_descr=False)
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   131
            or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s',
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   132
                           {'login': login}, build_descr=False)):
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   133
            qname = role_name('login', 'subject')
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   134
            raise ValidationError(None, {qname: errmsg % login})
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   135
10770
c3ed65f23181 [sobjects] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10662
diff changeset
   136
        if isinstance(password, text_type):
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   137
            # password should *always* be utf8 encoded
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   138
            password = password.encode('UTF8')
9690
2b506822ca54 [services] rename register_user kwargs to cwuserkwargs
Julien Cristau <julien.cristau@logilab.fr>
parents: 9689
diff changeset
   139
        cwuserkwargs['login'] = login
2b506822ca54 [services] rename register_user kwargs to cwuserkwargs
Julien Cristau <julien.cristau@logilab.fr>
parents: 9689
diff changeset
   140
        cwuserkwargs['upassword'] = password
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   141
        # we have to create the user
9690
2b506822ca54 [services] rename register_user kwargs to cwuserkwargs
Julien Cristau <julien.cristau@logilab.fr>
parents: 9689
diff changeset
   142
        user = cnx.create_entity('CWUser', **cwuserkwargs)
9689
9e4a3c8719a7 [services] don't hardcode 'users' group for register_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 9687
diff changeset
   143
        if groups is None:
9e4a3c8719a7 [services] don't hardcode 'users' group for register_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 9687
diff changeset
   144
            groups = self.default_groups
9691
b9ca1551f14b [services] catch missing group early in register_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 9690
diff changeset
   145
        assert groups, "CWUsers must belong to at least one CWGroup"
9689
9e4a3c8719a7 [services] don't hardcode 'users' group for register_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 9687
diff changeset
   146
        group_names = ', '.join('%r' % group for group in groups)
9e4a3c8719a7 [services] don't hardcode 'users' group for register_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 9687
diff changeset
   147
        cnx.execute('SET X in_group G WHERE X eid %%(x)s, G name IN (%s)' % group_names,
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   148
                    {'x': user.eid})
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   149
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   150
        if email or '@' in login:
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   151
            d = {'login': login, 'email': email or login}
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   152
            if cnx.execute('EmailAddress X WHERE X address %(email)s', d,
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   153
                           build_descr=False):
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   154
                qname = role_name('address', 'subject')
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   155
                raise ValidationError(None, {qname: errmsg % d['email']})
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   156
            cnx.execute('INSERT EmailAddress X: X address %(email)s, '
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   157
                        'U primary_email X, U use_email X '
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9035
diff changeset
   158
                        'WHERE U login %(login)s', d, build_descr=False)
9722
c0ae36be84ee [services] register_user now returns the new CWUser
Julien Cristau <julien.cristau@logilab.fr>
parents: 9691
diff changeset
   159
c0ae36be84ee [services] register_user now returns the new CWUser
Julien Cristau <julien.cristau@logilab.fr>
parents: 9691
diff changeset
   160
        return user
10951
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   161
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   162
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   163
class SourceSynchronizationService(Service):
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   164
    """Force synchronization of a datafeed source"""
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   165
    __regid__ = 'source-sync'
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   166
    __select__ = Service.__select__ & match_user_groups('managers')
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   167
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   168
    def call(self, source_eid):
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   169
        source_entity = self._cw.entity_from_eid(source_eid)
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   170
        repo = self._cw.repo # Service are repo side only.
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   171
        with repo.internal_cnx() as cnx:
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   172
            source = repo.sources_by_uri[source_entity.name]
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   173
            source.pull_data(cnx)
ef1cfc80d51c action, view and service so managers can start source synchronization from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10770
diff changeset
   174