Example #1
0
def dgu_linked_user(user, maxlength=16):  # Overwrite h.linked_user
    from ckan import model
    from ckan.lib.base import h
    from ckanext.dgu.plugins_toolkit import c

    if user in [model.PSEUDO_USER__LOGGED_IN, model.PSEUDO_USER__VISITOR]:
        return user
    if not isinstance(user, model.User):
        user_name = unicode(user)
        user = model.User.get(user_name)
    if not user:
        # may be in the format "NHS North Staffordshire (uid 6107 )"
        match = re.match(".*\(uid (\d+)\s?\)", user_name)
        if match:
            drupal_user_id = match.groups()[0]
            user = model.User.get("user_d%s" % drupal_user_id)

    if c.is_an_official:
        # only officials can see the actual user name
        if user:
            publisher = ", ".join([group.title for group in user.get_groups("publisher")])

            display_name = "%s (%s)" % (user.fullname, publisher)
            link_text = truncate(user.fullname or user.name, length=maxlength)
            return h.link_to(link_text, h.url_for(controller="user", action="read", id=user.name))
        else:
            return truncate(user_name, length=maxlength)
    else:
        # joe public just gets a link to the user's publisher(s)
        import ckan.authz

        if user:
            groups = user.get_groups("publisher")
            if groups:
                return h.literal(
                    " ".join(
                        [
                            h.link_to(truncate(group.title, length=maxlength), "/publisher/%s" % group.name)
                            for group in groups
                        ]
                    )
                )
            elif ckan.authz.Authorizer().is_sysadmin(user):
                return "System Administrator"
            else:
                return "Staff"
        else:
            return "Staff"
Example #2
0
    def filter(self, stream):
        """
        Implements IGenshiStreamFilter.
        """
        routes = request.environ.get('pylons.routes_dict')

        # add a 'Todo' link to the menu bar
        menu_data = {'href': 
            h.link_to("Todo", h.url_for('todo_page'), 
                class_ = ('active' if c.controller == 'ckanext.todo.controller:TodoController' else ''))}

        stream = stream | Transformer('body//div[@id="mainmenu"]')\
            .append(HTML(html.MENU_CODE % menu_data))

        # if this is the read action of a package, show todo info
        if(routes.get('controller') == 'package' and
           routes.get('action') == 'read' and 
           c.pkg.id):
            user_id = controller.get_user_id(request.environ.get('REMOTE_USER')) or ""
            data = {'package': c.pkg.name,
                    'user_id': user_id}
            # add CSS style
            stream = stream | Transformer('head').append(HTML(html.HEAD_CODE))
            # add jquery and todo.js links
            stream = stream | Transformer('body').append(HTML(html.BODY_CODE % data))
            # add todo subsection
            stream = stream | Transformer('//div[@id="dataset"]')\
                .append(HTML(html.TODO_CODE))
        return stream
Example #3
0
def dgu_linked_user(user, maxlength=16):  # Overwrite h.linked_user
    from ckan import model
    from ckan.lib.base import h
    from ckanext.dgu.plugins_toolkit import c

    if user in [model.PSEUDO_USER__LOGGED_IN, model.PSEUDO_USER__VISITOR]:
        return user
    if not isinstance(user, model.User):
        user_name = unicode(user)
        user = model.User.get(user_name)
    if not user:
        # may be in the format "NHS North Staffordshire (uid 6107 )"
        match = re.match('.*\(uid (\d+)\s?\)', user_name)
        if match:
            drupal_user_id = match.groups()[0]
            user = model.User.get('user_d%s' % drupal_user_id)

    if (c.is_an_official):
        # only officials can see the actual user name
        if user:
            publisher = ', '.join([group.title for group in user.get_groups('publisher')])

            display_name = '%s (%s)' % (user.fullname, publisher)
            link_text = truncate(user.fullname or user.name, length=maxlength)
            return h.link_to(link_text,
                             h.url_for(controller='user', action='read', id=user.name))
        else:
            return truncate(user_name, length=maxlength)
    else:
        # joe public just gets a link to the user's publisher(s)
        import ckan.authz
        if user:
            groups = user.get_groups('publisher')
            if groups:
                return h.literal(' '.join([h.link_to(truncate(group.title, length=maxlength),
                                                     '/publisher/%s' % group.name) \
                                         for group in groups]))
            elif ckan.authz.Authorizer().is_sysadmin(user):
                return 'System Administrator'
            else:
                return 'Staff'
        else:
            return 'Staff'
Example #4
0
    def filter(self, stream):
        """
        Required to implement IGenshiStreamFilter.
        """
        routes = request.environ.get('pylons.routes_dict')

        # get any notifications
        #
        # TODO: the query called by this function needs to be speeded up before it can be called on every
        #       page load
        #
        # if c.user:
        #     notification_data = {
        #             'url': h.url_for(controller='ckanext.moderatededits.controller:ModeratedEditsController', 
        #                 action='has_pending', user=c.user
        #             )
        #     }
        #     stream = stream | Transformer('body').append(HTML(html.NOTIFICATIONS % notification_data))

        # if this is the edit action of a package, call the javascript init function
        controllers = ['package', 'ckanext.datacatalogs.controller:DataCatalogsController']
        if(routes.get('controller') in controllers and routes.get('action') == 'edit' and 
           c.pkg.id):
            if routes.get('controller') == 'package':
                data = {'package_name': c.pkg.name,
                        'revision_list_url': h.url_for(controller='package', action='history_ajax',
                                                       id=c.pkg.id),
                        'revision_data_url': h.url_for(controller='package', action='read_ajax')}
            else:
                data = {'package_name': c.pkg.name,
                        'revision_list_url': h.url_for(controller='ckanext.datacatalogs.controller:DataCatalogsController', 
                                                       action='history_ajax',
                                                       id=c.pkg.id),
                        'revision_data_url': h.url_for(controller='ckanext.datacatalogs.controller:DataCatalogsController', 
                                                       action='read_ajax')}

            # add CSS style
            stream = stream | Transformer('head').append(HTML(html.HEAD))
            # add javascript links
            stream = stream | Transformer('body').append(HTML(html.BODY % data))
            # add revision/moderator info boxes
            stream = stream | Transformer('body//div[@class="package"]/h2[1]')\
                .after(HTML(html.REVISION_INFO))
            # add revision list widget
            stream = stream | Transformer('body//div[@id="sidebar"]/ul')\
                .append(HTML(html.REVISION_LIST))

        # if this is the read action of a user page, show packages with pending edits
        elif(routes.get('controller') == 'user' and
             routes.get('action') == 'read' and 
             c.user):
            moderated_packages = controller.get_moderated_packages(c)
            mod_html = '<div class="moderation"><h3>Package Moderation</h3><ul>'
            # mod_html += '<li><strong>Number of packages moderated:</strong> ' +\
            #     str(len(moderated_packages))
            # mod_html += '</li>'
            if moderated_packages:
                # mod_html += '<li><strong>Moderated packages:</strong> '
                # for i, pkg in enumerate(moderated_packages):
                #     mod_html += str(h.link_to(pkg.name, h.url_for(
                #         controller='ckanext.datacatalogs.controller:DataCatalogsController', 
                #         action='read', id=pkg.name
                #     ))) 
                #     if i < len(moderated_packages) - 1:
                #         mod_html += ', '
                # mod_html += '</li>'
                pending = [pkg for pkg in moderated_packages \
                    if not bool(pkg.latest_related_revision.approved_timestamp)]
                mod_html += '<a name="num_pending"></a>'
                # mod_html += '<li><strong>Number of pending changes:</strong> '
                # mod_html += str(len(pending)) + '</li>'
                if pending:
                    mod_html += '<li><strong>Pending changes:</strong> '
                    for n, p in enumerate(pending):
                        mod_html += str(h.link_to(p.name, h.url_for(
                            controller='package', action='edit', id=p.name
                        ))) 
                        if n < len(pending) - 1:
                            mod_html += ', '
            mod_html += '</ul></div>'
            stream = stream | Transformer('body//div[@id="sidebar"]').append(HTML(mod_html))
        return stream
Example #5
0
    def filter(self, stream):
        """
        Required to implement IGenshiStreamFilter.

        If this is the package controller with the read action:
        Adds HTML to the document HEAD, the package heading and
        the bottom of the BODY to create the follow/unfollow button
        and the follower count button.

        If this is the user controller with the read action:
        Adds HTML to the BODY to display the currently followed
        packages, if any exist.
        """
        routes = request.environ.get('pylons.routes_dict')

        # if this is the read action of a package, show follower info
        if(routes.get('controller') == 'package' and
           routes.get('action') == 'read' and 
           c.pkg.id):
            # pass data to the javascript file that creates the
            # follower count and follow/unfollow buttons
            user_id = controller.get_user_id(request.environ.get('REMOTE_USER')) or ""
            data = {'package_id': c.pkg.id,
                    'package_name': c.pkg.name,
                    'user_id': user_id}
            # add CSS styles for follower HTML
            stream = stream | Transformer('head').append(HTML(html.HEAD_CODE))
            # add jquery and follower.js links
            stream = stream | Transformer('body')\
                .append(HTML(html.BODY_CODE % data))
            # add the follower DIV to the package title, after the
            # RSS 'subscribe' link
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .append(HTML(html.FOLLOWER_CODE))
            # add the follower error DIV after the H2 tag
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .after(HTML(html.ERROR_CODE))

        # if this is the read action of a user page, show packages being followed
        if(routes.get('controller') == 'user' and
           routes.get('action') == 'read' and 
           c.user):
            user_id = controller.get_user_id(c.user)
            packages = controller.packages_followed_by(user_id)
            if packages:
                packages_html = ""
                for package_number, package in enumerate(packages):
                    # add a link to the package page
                    package_name = controller.get_package_name(package)
                    packages_html += \
                        h.link_to(package_name, 
                                  h.url_for(controller='package', action='read', 
                                            id=package_name))
                    # add comma and space if this isn't the last package in the
                    # list
                    if package_number < len(packages) - 1:
                        packages_html += ", "
                packages_followed = {'packages_followed': packages_html}
                stream = stream | Transformer('body//div[@class="activity"]//ul')\
                    .append(HTML(html.PACKAGES_FOLLOWED_CODE % packages_followed))

        return stream
Example #6
0
    def filter(self, stream):
        """
        Required to implement IGenshiStreamFilter.

        If this is the package controller with the read action:
        Adds HTML to the document HEAD, the package heading and
        the bottom of the BODY to create the follow/unfollow button
        and the follower count button.

        If this is the user controller with the read action:
        Adds HTML to the BODY to display the currently followed
        packages, if any exist.
        """
        routes = request.environ.get('pylons.routes_dict')

        # if this is the read action of a package, show follower info
        if (routes.get('controller') == 'package'
                and routes.get('action') == 'read' and c.pkg.id):
            # pass data to the javascript file that creates the
            # follower count and follow/unfollow buttons
            user_id = controller.get_user_id(
                request.environ.get('REMOTE_USER')) or ""
            data = {
                'package_id': c.pkg.id,
                'package_name': c.pkg.name,
                'user_id': user_id
            }
            # add CSS styles for follower HTML
            stream = stream | Transformer('head').append(HTML(html.HEAD_CODE))
            # add jquery and follower.js links
            stream = stream | Transformer('body')\
                .append(HTML(html.BODY_CODE % data))
            # add the follower DIV to the package title, after the
            # RSS 'subscribe' link
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .append(HTML(html.FOLLOWER_CODE))
            # add the follower error DIV after the H2 tag
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .after(HTML(html.ERROR_CODE))

        # if this is the read action of a user page, show packages being followed
        if (routes.get('controller') == 'user'
                and routes.get('action') == 'read' and c.user):
            user_id = controller.get_user_id(c.user)
            packages = controller.packages_followed_by(user_id)
            if packages:
                packages_html = ""
                for package_number, package in enumerate(packages):
                    # add a link to the package page
                    package_name = controller.get_package_name(package)
                    packages_html += \
                        h.link_to(package_name,
                                  h.url_for(controller='package', action='read',
                                            id=package_name))
                    # add comma and space if this isn't the last package in the
                    # list
                    if package_number < len(packages) - 1:
                        packages_html += ", "
                packages_followed = {'packages_followed': packages_html}
                stream = stream | Transformer('body//div[@class="activity"]//ul')\
                    .append(HTML(html.PACKAGES_FOLLOWED_CODE % packages_followed))

        return stream