Beispiel #1
0
class ModelField_Base(DBResource):

    # Fields
    required = Boolean_Field(title=MSG(u'Required'))
    multiple = Boolean_Field(title=MSG(u'Multiple'))
    tip = Text_Field(title=MSG(u'Tip'))

    def get_owner(self):
        return self.parent.get_owner()

    def set_value(self, name, value, language=None):
        proxy = super(ModelField_Base, self)
        has_changed = proxy.set_value(name, value, language)
        if has_changed:
            class_id = str(self.parent.abspath)
            self.database._resources_registry.pop(class_id, None)

        return has_changed

    def get_field_kw(self, field):
        return {
            'multiple': self.get_value('multiple'),
            'required': self.get_value('required'),
            'title': self.get_title(),
            'widget': field.widget(tip=self.get_value('tip'))
        }

    # Views
    _fields = ['title', 'required', 'multiple', 'tip']
    new_instance = AutoAdd(fields=_fields, automatic_resource_name=True)
    edit = AutoEdit(fields=_fields)
Beispiel #2
0
class ConfigMail(DBResource):

    class_id = 'config-mail'
    class_title = MSG(u'Email options')
    class_description = mail_description
    class_icon48 = '/ui/ikaaro/icons/48x48/mail.png'

    # Fields
    contacts = Select_Field(multiple=True,
                            datatype=ContactsOptions,
                            title=MSG(u'Select the contact accounts'),
                            has_empty_option=False)
    emails_from_addr = Text_Field(title=MSG(u'From header'))
    emails_reply_to = Boolean_Field(title=MSG(u'Reply to'), default=True)
    emails_signature = Textarea_Field(title=MSG(u'Signature'))

    # Views
    class_views = ['edit']
    edit = ConfigMail_Edit

    # Configuration
    config_name = 'mail'
    config_group = 'access'
Beispiel #3
0
class User(DBResource):

    class_id = 'user'
    class_version = '20081217'
    class_title = MSG(u'User')
    class_icon16 = '/ui/ikaaro/icons/16x16/user.png'
    class_icon48 = '/ui/ikaaro/icons/48x48/user.png'
    class_views = [
        'profile', 'edit_account', 'edit_preferences', 'edit_password',
        'edit_groups'
    ]

    # Fields
    firstname = Text_Field(multilingual=False,
                           indexed=True,
                           stored=True,
                           title=MSG(u'First Name'))
    lastname = Text_Field(multilingual=False,
                          indexed=True,
                          stored=True,
                          title=MSG(u'Last Name'))
    email = UserEmail_Field
    password = Password_Field(multiple=True)
    avatar = File_Field(title=MSG(u'Avatar'))
    user_language = Char_Field
    user_timezone = Char_Field
    user_state = UserState_Field
    groups = UserGroups_Field
    username = Char_Field(indexed=True, stored=True)  # Backwards compatibility

    # Remove some fields
    title = None
    description = None
    subject = None
    text = None

    ########################################################################
    # Indexing
    ########################################################################
    def get_catalog_values(self):
        values = super(User, self).get_catalog_values()

        # email domain
        email = self.get_value('email')
        if email and '@' in email:
            values['email_domain'] = email.split('@', 1)[1]

        # username (overrides default)
        values['username'] = self.get_login_name()

        # groups
        values['groups'] = self.get_value('groups')

        return values

    ########################################################################
    # API / Authentication
    ########################################################################
    def get_user_id(self):
        # Used by itools.web
        return str(self.name)

    def get_password(self):
        password = self.get_property('password')
        return password[-1] if password else None

    def get_auth_token(self):
        # Used by itools.web
        password = self.get_password()
        return password.value if password else None

    def authenticate(self, password):
        my_password = self.get_password()
        if my_password is None:
            return False
        algo = my_password.get_parameter('algo', 'sha1')
        salt = my_password.get_parameter('salt', '')

        password_hashed, salt = get_secure_hash(password, algo, salt)
        return password_hashed == my_password.value

    def _login(self, password, context):
        # We call this method '_login' to avoid a name clash with the login
        # view.

        if not self.authenticate(password):
            error = MSG_LOGIN_WRONG_NAME_OR_PASSWORD
        elif self.get_value('user_state') == 'inactive':
            error = ERROR(
                u'Your account has been canceled, contact the administrator '
                u' if you want to get access again.')
        else:
            error = None
            context.login(self)

        # To activate this feature set the lastlog field
        lastlog = self.get_field('lastlog')
        if lastlog:
            success = error is None
            self.set_value('lastlog', context.timestamp, success=success)

        # Ok
        return error

    def update_pending_key(self):
        state = self.get_property('user_state')
        if state.value == 'pending':
            # TODO Implement expiration
            return state.get_parameter('key')

        key = generate_password(30)
        self.set_value('user_state', 'pending', key=key)
        return key

    ########################################################################
    # API
    ########################################################################
    def get_owner(self):
        return str(self.abspath)

    def get_title(self, language=None):
        firstname = self.get_value('firstname')
        lastname = self.get_value('lastname')
        if firstname:
            if lastname:
                return '%s %s' % (firstname, lastname)
            return firstname
        if lastname:
            return lastname
        return self.get_login_name().decode('utf-8')

    login_name_property = 'email'

    def get_login_name(self):
        return self.get_value(self.login_name_property)

    def get_timezone(self):
        return self.get_value('user_timezone')

    #######################################################################
    # Views
    #######################################################################
    resend_confirmation = User_ResendConfirmation
    confirm_registration = User_ConfirmRegistration
    change_password_forgotten = User_ChangePasswordForgotten
    profile = User_Profile
    edit_account = User_EditAccount
    edit_preferences = User_EditPreferences
    edit_password = User_EditPassword
    edit_groups = AutoEdit(access='is_admin',
                           fields=['groups'],
                           title=MSG(u'Edit groups'))
Beispiel #4
0
class DBResource(Resource):

    class_version = '20071215'
    class_description = None
    class_icon16 = 'icons/16x16/resource.png'
    class_icon48 = 'icons/48x48/resource.png'
    class_views = []
    context_menus = []

    def __init__(self, metadata):
        self.metadata = metadata

    def __eq__(self, resource):
        if not isinstance(resource, DBResource):
            error = "cannot compare DBResource and %s" % type(resource)
            raise TypeError, error

        return self.abspath == resource.abspath

    def __ne__(self, node):
        return not self.__eq__(node)

    #######################################################################
    # API / Tree
    #######################################################################
    @property
    def database(self):
        return self.metadata.database

    @lazy
    def parent(self):
        abspath = self.abspath
        if len(abspath) == 0:
            return None
        return self.get_resource(abspath[:-1])

    @property
    def name(self):
        return self.abspath.get_name()

    def get_root(self):
        return self.get_resource('/')

    def get_pathto(self, resource):
        return self.abspath.get_pathto(resource.abspath)

    #######################################################################
    # API / Folderish
    #######################################################################
    __fixed_handlers__ = []  # Resources that cannot be removed

    @property
    def handler(self):
        cls = FolderHandler
        key = self.metadata.key[:-9]
        handler = self.database.get_handler(key, cls=cls, soft=True)
        if handler is None:
            handler = cls()
            self.database.push_phantom(key, handler)

        return handler

    def get_handlers(self):
        """Return all the handlers attached to this resource, except the
        metadata.
        """
        handlers = [self.handler]
        # Fields
        for name, field in self.get_fields():
            if issubclass(field, File_Field):
                value = field.get_value(self, name)
                if value is not None:
                    handlers.append(value)

        # Ok
        return handlers

    def _get_names(self):
        folder = self.handler
        return [
            x[:-9] for x in folder.get_handler_names() if x[-9:] == '.metadata'
        ]

    def get_names(self, path='.'):
        resource = self.get_resource(path)
        return resource._get_names()

    def get_resource(self, path, soft=False):
        if type(path) is not Path:
            path = Path(path)

        # 1. Get the metadata
        if path.is_absolute():
            abspath = path
        else:
            abspath = self.abspath.resolve2(path)

        return self.database.get_resource(abspath, soft=soft)

    def get_resources(self, path='.'):
        here = self.get_resource(path)
        for name in here._get_names():
            yield here.get_resource(name)

    def make_resource_name(self):
        max_id = -1
        for name in self.get_names():
            # Mixing explicit and automatically generated names is allowed
            try:
                id = int(name)
            except ValueError:
                continue
            if id > max_id:
                max_id = id

        return str(max_id + 1)

    def make_resource(self, name, cls, soft=False, **kw):
        # Automatic name
        if name is None:
            name = self.make_resource_name()

        # Make a resource somewhere else
        if '/' in name:
            path = dirname(name)
            name = basename(name)
            resource = self.get_resource(path)
            resource.make_resource(name, cls, soft=soft, **kw)
            return

        # Soft
        if soft is True:
            resource = self.get_resource(name, soft=True)
            if resource:
                return resource

        # Make the metadata
        metadata = Metadata(cls=cls)
        self.handler.set_handler('%s.metadata' % name, metadata)
        metadata.set_property('mtime', get_context().timestamp)
        # Initialize
        resource = self.get_resource(name)
        self.database.add_resource(resource)
        resource.init_resource(**kw)
        # Ok
        return resource

    def del_resource(self, name, soft=False, ref_action='restrict'):
        """ref_action allows to specify which action is done before deleting
        the resource.
        ref_action can take 2 values:
        - 'restrict' (default value): do an integrity check
        - 'force': do nothing
        """
        database = self.database
        resource = self.get_resource(name, soft=soft)
        if soft and resource is None:
            return

        # Referential action
        if ref_action == 'restrict':
            # Check referencial-integrity (FIXME Check sub-resources too)
            path = str(resource.abspath)
            query_base_path = get_base_path_query(path)
            query = AndQuery(PhraseQuery('links', path),
                             NotQuery(query_base_path))
            results = database.search(query)
            # A resource may have been updated in the same transaction,
            # so not yet reindexed: we need to check that the resource
            # really links.
            for referrer in results.get_resources():
                if path in referrer.get_links():
                    err = 'cannot delete, resource "%s" is referenced'
                    raise ConsistencyError, err % path
        elif ref_action == 'force':
            # Do not check referencial-integrity
            pass
        else:
            raise ValueError, 'Incorrect ref_action "%s"' % ref_action

        # Events, remove
        path = str(resource.abspath)
        database.remove_resource(resource)
        # Remove
        fs = database.fs
        for handler in resource.get_handlers():
            # Skip empty folders and phantoms
            if fs.exists(handler.key):
                database.del_handler(handler.key)
        self.handler.del_handler('%s.metadata' % name)
        # Clear cookie
        context = get_context()
        cut, paths = context.get_cookie('ikaaro_cp', datatype=CopyCookie)
        if path in paths:
            context.del_cookie('ikaaro_cp')

    def copy_resource(self, source, target):
        raise NotImplementedError

    def move_resource(self, source, target):
        raise NotImplementedError

    def traverse_resources(self):
        yield self
        for name in self._get_names():
            resource = self.get_resource(name)
            for x in resource.traverse_resources():
                yield x

    #######################################################################
    # API / Views
    #######################################################################
    def get_default_view_name(self):
        views = self.class_views
        if not views:
            return None
        context = get_context()
        for view_name in views:
            view = getattr(self, view_name, None)
            if context.is_access_allowed(self, view):
                return view_name
        return views[0]

    def get_view(self, name, query=None):
        # To define a default view, override this
        if name is None:
            name = self.get_default_view_name()
            if name is None:
                return None

        # Explicit view, defined by name
        view = getattr(self, name, None)
        if is_prototype(view, BaseView):
            context = get_context()
            view = view(resource=self, context=context)  # bind
            return view

        return None

    def get_context_menus(self):
        return self.context_menus

    ########################################################################
    # Properties
    ########################################################################
    def get_value(self, name, language=None):
        field = self.get_field(name)
        if field is None:
            return None
        return field.get_value(self, name, language)

    def set_value(self, name, value, language=None, **kw):
        field = self.get_field(name)
        if field.multilingual and language is None:
            raise ValueError, 'Field %s is multilingual' % name
        if field is None:
            raise ValueError, 'Field %s do not exist' % name
        return field.set_value(self, name, value, language, **kw)

    def get_value_title(self, name, language=None):
        field = self.get_field(name)
        if field is None:
            return None
        return field.get_value_title(self, name, language)

    def get_brain_value(self, name):
        brain = get_context().database.search(
            PhraseQuery('abspath', str(self.abspath))).get_documents()[0]
        return getattr(brain, name, None)

    def get_html_field_body_stream(self, name, language=None):
        """Utility method, returns the stream for the given html field.
        """
        # 1. Check it is an html-file field
        field = self.get_field(name)
        if not is_prototype(field, HTMLFile_Field):
            raise ValueError, 'expected html-file field'

        # 2. Get the handler
        handler = field.get_value(self, name, language)
        if not handler:
            handler = field.class_handler()

        # 3. Get the body
        body = handler.get_body()
        if not body:
            raise ValueError, 'html file does not have a body'
        return body.get_content_elements()

    def get_property(self, name, language=None):
        property = self.metadata.get_property(name, language=language)
        if property:
            return property

        field = self.get_field(name)
        if field is None:
            return None

        default = field.get_default()
        if field.multiple:
            return [Property(x) for x in default]
        return Property(default)

    # XXX Backwards compatibility
    set_property = set_value

    def get_page_title(self):
        return self.get_title()

    def init_resource(self, **kw):
        """Return a Metadata object with sensible default values.
        """
        # Ownership
        owner = self.get_field('owner')
        if owner:
            user = get_context().user
            if user:
                self.set_value('owner', str(user.abspath))

        # Keyword parameters
        for name, value in kw.items():
            field = self.get_field(name)
            if field is None:
                raise ValueError, 'undefined field "%s"' % name
            if type(value) is dict:
                for lang in value:
                    field._set_value(self, name, value[lang], lang)
            else:
                field._set_value(self, name, value)

    def load_handlers(self):
        self.get_handlers()

    ########################################################################
    # Fields
    ########################################################################
    mtime = Datetime_Field(indexed=True, stored=True, readonly=True)
    last_author = Char_Field(indexed=False, stored=True, readonly=True)
    title = Text_Field(indexed=True, stored=True, title=MSG(u'Title'))
    description = Textarea_Field(indexed=True,
                                 title=MSG(u'Description'),
                                 hidden_by_default=True)
    subject = Text_Field(indexed=True,
                         title=MSG(u'Keywords'),
                         hidden_by_default=True)
    share = Share_Field

    @property
    def is_content(self):
        return self.parent.is_content

    def has_property(self, name, language=None):
        return self.metadata.has_property(name, language=language)

    def del_property(self, name):
        if self.has_property(name):
            self.database.change_resource(self)
            self.metadata.del_property(name)

    ########################################################################
    # Versioning
    ########################################################################
    def get_files_to_archive(self, content=False):
        metadata = self.metadata.key
        if content is True:
            folder = self.handler.key
            return [metadata, folder]
        return [metadata]

    def get_revisions(self,
                      n=None,
                      content=False,
                      author_pattern=None,
                      grep_pattern=None):
        if self.parent is None and content is True:
            files = None
        else:
            files = self.get_files_to_archive(content)

        worktree = self.database.worktree
        return worktree.git_log(files, n, author_pattern, grep_pattern)

    def get_owner(self):
        return self.get_value('owner')

    def get_share(self):
        return self.get_value('share')

    ########################################################################
    # Indexing
    ########################################################################
    def to_text(self):
        """This function must return:
           1) An unicode text.
            or
           2) A dict in a multilingual context:
              {'fr': u'....',
               'en': u'....' ....}
        """
        raise NotImplementedError

    def get_catalog_values(self):
        values = {}

        # Step 1. Automatically index fields
        languages = self.get_root().get_value('website_languages')
        for name, field in self.get_fields():
            if not field.indexed and not field.stored:
                continue

            if field.multilingual:
                value = {}
                for language in languages:
                    value[language] = field.get_value(self, name, language)
                values[name] = value
            else:
                values[name] = field.get_value(self, name)

        # Step 2. Index non-metadata properties
        # Path related fields
        abspath = self.abspath
        values['abspath'] = str(abspath)
        n = len(abspath)
        values['abspath_depth'] = n
        if n:
            values['parent_paths'] = [str(abspath[:i]) for i in range(n)]

        values['name'] = self.name
        values['is_content'] = self.is_content

        # Class related fields
        values['format'] = self.metadata.format
        values['base_classes'] = []
        for cls in self.__class__.__mro__:
            class_id = getattr(cls, 'class_id', None)
            if class_id:
                values['base_classes'].append(class_id)

        # Links to other resources
        values['owner'] = self.get_owner()
        values['share'] = self.get_share()
        values['links'] = list(self.get_links())
        values['onchange_reindex'] = self.get_onchange_reindex()

        # Full text
        context = get_context()
        try:
            server = context.server
        except AttributeError:
            server = None
        if server is not None and server.index_text:
            try:
                values['text'] = self.to_text()
            except NotImplementedError:
                pass
            except Exception:
                log = 'Indexation failed: %s' % abspath
                log_warning(log, domain='ikaaro')

        # Time events
        reminder, payload = self.next_time_event()
        values['next_time_event'] = reminder
        values['next_time_event_payload'] = dumps(payload)

        # Ok
        return values

    def get_onchange_reindex(self):
        return None

    #######################################################################
    # Time events
    #######################################################################
    def next_time_event(self):
        return None, None

    def time_event(self, payload):
        raise NotImplementedError

    #######################################################################
    # API
    #######################################################################
    def rename_handlers(self, new_name):
        """Consider we want to rename this resource to the given 'new_name',
        return the old a new names for all the attached handlers (except the
        metadata).

        This method is required by the "move_resource" method.
        """
        langs = self.get_resource('/').get_value('website_languages')

        aux = [(self.name, new_name)]
        for field_name in self.fields:
            field = self.get_field(field_name)
            if field and issubclass(field, File_Field):
                old = '%s.%s' % (self.name, field_name)
                new = '%s.%s' % (new_name, field_name)
                if field.multilingual:
                    for language in langs:
                        aux.append(('%s.%s' % (old, language),
                                    '%s.%s' % (new, language)))
                else:
                    aux.append((old, new))

        return aux

    def _on_move_resource(self, source):
        """This method updates the links from/to other resources.  It is
        called when the resource has been moved and/or renamed.

        This method is called by 'Database._before_commit', the 'source'
        parameter is the place the resource has been moved from.
        """
        # (1) Update links to other resources
        self.update_incoming_links(Path(source))

        # (2) Update resources that link to me
        database = self.database
        target = self.abspath
        query = PhraseQuery('links', source)
        results = database.search(query).get_documents()
        for result in results:
            path = result.abspath
            path = database.resources_old2new.get(path, path)
            resource = self.get_resource(path)
            resource.update_links(source, target)

    def get_links(self):
        # Automatically from the fields
        languages = self.get_resource('/').get_value('website_languages')
        links = set()
        for field_name in self.fields:
            field = self.get_field(field_name)
            if field:
                field.get_links(links, self, field_name, languages)

        # Support for dynamic models
        class_id = self.metadata.format
        if class_id[0] == '/':
            links.add(class_id)

        # Ok
        return links

    def update_links(self, source, target):
        """The resource identified by 'source' is going to be moved to
        'target'.  Update our links to it.

        The parameters 'source' and 'target' are absolute 'Path' objects.
        """
        base = str(self.abspath)
        old_base = self.database.resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)
        languages = self.get_resource('/').get_value('website_languages')

        for field_name in self.fields:
            field = self.get_field(field_name)
            if field:
                field.update_links(self, field_name, source, target, languages,
                                   old_base, new_base)

        self.database.change_resource(self)

    def update_incoming_links(self, source):
        """Update the relative links coming out from this resource after it
        was moved, so they are not broken. The old path is in parameter. The
        new path is "self.abspath".
        """
        languages = self.get_resource('/').get_value('website_languages')
        for field_name in self.fields:
            field = self.get_field(field_name)
            if field:
                field.update_incoming_links(self, field_name, source,
                                            languages)

    ########################################################################
    # Upgrade
    ########################################################################
    def get_next_versions(self):
        cls_version = self.class_version
        obj_version = self.metadata.version
        # Set zero version if the resource does not have a version
        if obj_version is None:
            obj_version = '00000000'

        # Get all the version numbers
        versions = []
        for cls in self.__class__.mro():
            for name in cls.__dict__.keys():
                if not name.startswith('update_'):
                    continue
                kk, version = name.split('_', 1)
                if len(version) != 8:
                    continue
                try:
                    int(version)
                except ValueError:
                    continue
                if version > obj_version and version <= cls_version:
                    versions.append(version)

        versions.sort()
        return versions

    def update(self, version):
        # Action
        getattr(self, 'update_%s' % version)()

        # If the action removes the resource, we are done
        metadata = self.metadata
        if metadata.key is None:
            return

        # Update version
        metadata.set_changed()
        metadata.version = version

    #######################################################################
    # Icons
    #######################################################################
    @classmethod
    def get_class_icon(cls, size=16):
        icon = getattr(cls, 'class_icon%s' % size, None)
        if icon is None:
            return None
        return '/ui/%s' % icon

    @classmethod
    def get_resource_icon(cls, size=16):
        icon = getattr(cls, 'icon%s' % size, None)
        if icon is None:
            return cls.get_class_icon(size)
        return ';icon%s' % size

    def get_method_icon(self, view, size='16x16', **kw):
        icon = getattr(view, 'icon', None)
        if icon is None:
            return None
        if callable(icon):
            icon = icon(self, **kw)
        return '/ui/icons/%s/%s' % (size, icon)

    #######################################################################
    # User interface
    #######################################################################
    def get_views(self):
        context = get_context()
        for name in self.class_views:
            view_name = name.split('?')[0]
            view = self.get_view(view_name)
            if context.is_access_allowed(self, view):
                yield name, view

    def get_title(self, language=None):
        title = self.get_value('title', language=language)
        if title:
            return title
        # Fallback to the resource's name
        return unicode(self.name)

    def get_edit_languages(self, context):
        root = self.get_root()
        site_languages = root.get_value('website_languages')
        default = root.get_default_edit_languages()

        # Can not use context.query[] because edit_language is not necessarily
        # defined
        datatype = String(multiple=True, default=default)
        edit_languages = context.get_query_value('edit_language', datatype)
        edit_languages = [x for x in edit_languages if x in site_languages]

        return edit_languages if edit_languages else default

    #######################################################################
    # Cut & Paste Resources
    #######################################################################
    def can_paste(self, source):
        """Is the source resource can be pasted into myself.
        Question is "can I handle this type of resource?"
        """
        raise NotImplementedError

    def can_paste_into(self, target):
        """Can I be pasted into the given target.
        Question is "Is this container compatible with myself?"
        """
        # No restriction by default. Functional modules will want to keep
        # their specific resources for them.
        return True

    # Views
    new_instance = AutoAdd(fields=['title', 'location'])
    edit = AutoEdit(fields=['title', 'description', 'subject', 'share'])
    remove = DBResource_Remove
    get_file = DBResource_GetFile
    get_image = DBResource_GetImage
    # Login/Logout
    login = LoginView
    logout = LogoutView
    # Popups
    add_image = DBResource_AddImage
    add_link = DBResource_AddLink
    add_media = DBResource_AddMedia
    # Commit log
    commit_log = DBResource_CommitLog
    changes = DBResource_Changes
    # Links
    backlinks = DBResource_Backlinks
    links = DBResource_Links
    # External editor
    http_put = Put_View
    http_delete = Delete_View
    # Rest (web services)
    rest_login = Rest_Login
    rest_query = Rest_Query
    rest_create = Rest_Create
    rest_read = Rest_Read
    rest_update = Rest_Update
    rest_delete = Rest_Delete
    rest_schema = Rest_Schema
Beispiel #5
0
from itools.core import proto_lazy_property
from itools.datatypes import Enumerate, String, Unicode
from itools.gettext import MSG
from itools.web import get_context

# Import from ikaaro
from autoedit import AutoEdit
from autoform import TextWidget, RadioWidget, Widget
from config import Configuration
from fields import Field, Select_Field, Text_Field
from folder import Folder
from resource_ import DBResource
from utils import make_stl_template


captcha_field = Text_Field(multilingual=False)

###########################################################################
# ReCaptcha
###########################################################################

class RecaptchaWidget(Widget):

    title = MSG(u"Please enter the words below")
    public_key = None

    template = make_stl_template(
        """
          <input type="hidden" name="${name}" value="Check"/>
          <script type="text/javascript">
          var RecaptchaOptions = {