Esempio n. 1
0
File: group.py Progetto: abulte/ckan
    def index(self):
        group_type = self._guess_group_type()

        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'for_view': True,
            'with_private': False
        }

        data_dict = {'all_fields': True}

        try:
            check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))

        results = get_action('group_list')(context, data_dict)

        c.page = Page(collection=results,
                      page=request.params.get('page', 1),
                      url=h.pager_url,
                      items_per_page=20)
        return render(self._index_template(group_type))
Esempio n. 2
0
    def organization_index(self):
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'for_view': True,
            'with_private': False
        }

        data_dict = {'all_fields': True}

        try:
            check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))

        # pass user info to context as needed to view private datasets of
        # orgs correctly
        if c.userobj:
            context['user_id'] = c.userobj.id
            context['user_is_admin'] = c.userobj.sysadmin

        results = get_action('organization_list')(context, data_dict)

        def org_key(org):
            title = org['title'].split(' | ')[-1 if c.language == 'fr' else 0]
            return normalize_strip_accents(title)

        results.sort(key=org_key)

        c.page = Page(collection=results,
                      page=request.params.get('page', 1),
                      url=h.pager_url,
                      items_per_page=1000)
        return render('organization/index.html')
Esempio n. 3
0
    def index(self):
        group_type = self._guess_group_type()

        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'for_view': True,
                   'with_private': False}

        data_dict = {'all_fields': True}

        try:
            self._check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))

        # pass user info to context as needed to view private datasets of
        # orgs correctly
        if c.userobj:
            context['user_id'] = c.userobj.id
            context['user_is_admin'] = c.userobj.sysadmin

        results = self._action('group_list')(context, data_dict)

        c.page = Page(
            collection=results,
            page=request.params.get('page', 1),
            url=h.pager_url,
            items_per_page=21
        )
        return render(self._index_template(group_type))
Esempio n. 4
0
 def activity(self):
     do_if_user_not_sysadmin()
     page = get_page_number(tk.request.params)
     total_rows = App.all().count()
     total_pages = ((total_rows - 1) / self.paginated_by + 1) or 1
     # redirect for delete page parameter reason
     if not 0 < page <= total_pages:
         redirect_to('apps_activity')
     apps_activity = App.all().order_by(App.created.desc()).offset(
         (page - 1) * self.paginated_by).limit(self.paginated_by)
     activity = [
         dict(id=i.id,
              url=i.get_absolute_url(),
              content=i.content,
              name=i.name,
              status=i.status,
              author_name=i.author.name,
              created=i.created) for i in apps_activity
     ]
     c.page = Page(collection=apps_activity,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     context = {
         'activity': sorted(activity,
                            key=itemgetter('created'),
                            reverse=True),
         'statuses': STATUSES,
     }
     return self.__render('apps_activity.html', context)
Esempio n. 5
0
    def index(self):
        group_type = request.path.strip('/').split('/')[0]
        if group_type == 'group':
            group_type = None

        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author
        }

        data_dict = {'all_fields': True}

        try:
            check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))

        results = get_action('group_list')(context, data_dict)

        c.page = Page(collection=results,
                      page=request.params.get('page', 1),
                      url=h.pager_url,
                      items_per_page=20)
        return render(self._index_template(group_type))
Esempio n. 6
0
    def index(self):
        from ckan.lib.helpers import Page

        if not self.authorizer.am_authorized(c, model.Action.SITE_READ,
                                             model.System):
            abort(401, _('Not authorized to see this page'))

        query = ckan.authz.Authorizer().authorized_query(
            c.user, model.AuthorizationGroup)
        query = query.options(eagerload_all('users'))
        c.page = Page(collection=query,
                      page=request.params.get('page', 1),
                      items_per_page=20)
        return render('authorization_group/index.html')
Esempio n. 7
0
 def index(self):
     
     if not self.authorizer.am_authorized(c, model.Action.SITE_READ, model.System):
         abort(401, _('Not authorized to see this page'))
     
     query = authz.Authorizer().authorized_query(c.user, model.Group)
     query = query.order_by(model.Group.name.asc())
     query = query.order_by(model.Group.title.asc())
     c.page = Page(
         collection=query,
         page=request.params.get('page', 1),
         items_per_page=20
     )
     return render('group/index.html')
Esempio n. 8
0
    def index(self):
        from ckan.lib.helpers import Page
        try:
            context = {'model': model, 'user': c.user or c.author}
            check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))

        query = ckan.authz.Authorizer().authorized_query(
            c.user, model.AuthorizationGroup)
        query = query.options(eagerload_all('users'))
        c.page = Page(collection=query,
                      page=request.params.get('page', 1),
                      items_per_page=20)
        return render('authorization_group/index.html')
Esempio n. 9
0
    def index(self):
        page = get_page_number(tk.request.params)
        total_rows = Thread.all().count()
        total_pages = ((Thread.all().count() - 1) / self.paginated_by + 1) or 1
        if not 0 < page <= total_pages:
            # redirect for delete page parameter reason
            redirect_to('forum_index')
        thread_list = Thread.all().offset(
            (page - 1) * self.paginated_by).limit(self.paginated_by)

        c.page = Page(collection=thread_list,
                      page=page,
                      item_count=total_rows,
                      items_per_page=self.paginated_by)
        return self.__render('forum_index.html', {'thread_list': thread_list})
Esempio n. 10
0
    def read(self, id):
        try:
            context = {'model': model, 'user': c.user}
            c.source = get_action('harvest_source_show')(context, {'id': id})

            c.page = Page(collection=c.source['status']['packages'],
                          page=request.params.get('page', 1),
                          items_per_page=20,
                          url=pager_url)

            return render('source/read.html')
        except NotFound:
            abort(404, _('Harvest source not found'))
        except NotAuthorized, e:
            abort(401, self.not_auth_message)
Esempio n. 11
0
    def read(self, id):
        c.authorization_group = self._get_authgroup_by_name_or_id(id)
        if c.authorization_group is None:
            abort(404)
        auth_for_read = self.authorizer.am_authorized(c, model.Action.READ,
                                                      c.authorization_group)
        if not auth_for_read:
            abort(401, _('Not authorized to read %s') % id.encode('utf8'))

        import ckan.misc
        c.authorization_group_admins = self.authorizer.get_admins(
            c.authorization_group)

        c.page = Page(collection=c.authorization_group.users,
                      page=request.params.get('page', 1),
                      items_per_page=50)
        return render('authorization_group/read.html')
Esempio n. 12
0
 def thread_show(self, slug, id):
     thread = Thread.get_by_id(id=id)
     if not thread:
         abort(404)
     if not thread.active and (not c.userobj or not c.userobj.sysadmin):
         abort(404)
     form = CreatePostForm(tk.request.POST)
     if tk.request.POST:
         if c.userobj is None:
             tk.redirect_to(tk.url_for(controller='user', action='login'))
         if BannedUser.check_by_id(c.userobj):
             flash_error(tk._('You are banned'))
             tk.redirect_to(thread.get_absolute_url())
         if form.validate():
             post = tk.get_action('forum_create_post')(
                 {
                     'auth_user_obj': c.userobj
                 }, {
                     'thread_id': id,
                     'content': form.data['content']
                 })
             if post:
                 jobs.enqueue(
                     send_notifications_on_new_post,
                     [post, tk.request.environ.get('CKAN_LANG')])
                 flash_success(tk._('You successfully create comment'))
             else:
                 flash_error(tk._('Thread is closed for comments'))
             return tk.redirect_to(thread.get_absolute_url())
         else:
             flash_error(tk._('You have errors in form'))
     page = get_page_number(tk.request.params)
     total_rows = Post.filter_thread(thread.id).count()
     total_pages = (total_rows / self.paginated_by + 1) or 1
     if not 0 < page <= total_pages:
         # redirect for delete page parameter reason
         redirect_to('forum_index')
     posts_list = Post.filter_thread(thread.id).offset(
         (page - 1) * self.paginated_by).limit(self.paginated_by)
     c.page = Page(collection=posts_list,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     context = {'thread': thread, 'form': form, 'posts': posts_list}
     return self.__render('thread.html', context)
Esempio n. 13
0
 def board_show(self, slug):
     board = Board.get_by_slug(slug)
     if not board:
         abort(404)
     page = get_page_number(tk.request.params)
     total_rows = Thread.filter_board(board_slug=board.slug).count()
     total_pages = (total_rows / self.paginated_by + 1) or 1
     if not 0 < page <= total_pages:
         # redirect for delete page parameter reason
         redirect_to('forum_index')
     thread_list = Thread.filter_board(board_slug=board.slug).offset(
         (page - 1) * self.paginated_by).limit(self.paginated_by)
     c.page = Page(collection=thread_list,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     return self.__render('forum_index.html', {
         'board': board,
         'thread_list': thread_list
     })
Esempio n. 14
0
    def index(self):

        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author}

        data_dict = {'all_fields': True}

        try:
            check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))
        
        results = get_action('group_list')(context, data_dict)

        c.page = Page(
            collection=results,
            page=request.params.get('page', 1),
            url=h.pager_url,
            items_per_page=20
        )
        return render('group/index.html')
Esempio n. 15
0
    def index(self):
        page = get_page_number(tk.request.params)
        total_rows = App.all_active().count()
        total_pages = ((total_rows - 1) / self.paginated_by + 1) or 1
        # redirect for delete page parameter reason
        if not 0 < page <= total_pages:
            redirect_to('apps_index')

        apps_list = tk.get_action('apps_active_apps')(
            data_dict={
                "page": page,
                "paginated_by": self.paginated_by
            })
        c.page = Page(collection=apps_list,
                      page=page,
                      item_count=total_rows,
                      items_per_page=self.paginated_by)
        context = {
            'apps_list': apps_list,
        }
        log.debug('AppsController.index context: %s', context)
        return self.__render('apps_index.html', context)
Esempio n. 16
0
 def board_show(self, slug):
     board = Board.get_by_slug(slug)
     if not board:
         abort(404)
     page = get_page_number(tk.request.params)
     total_rows = App.filter_board(board_slug=board.slug).count()
     total_pages = (total_rows / self.paginated_by + 1) or 1
     # redirect for delete page parameter reason
     if not 0 < page <= total_pages:
         redirect_to('apps_index')
     apps_list = App.filter_board(board_slug=board.slug).offset(
         (page - 1) * self.paginated_by).limit(self.paginated_by)
     c.page = Page(collection=apps_list,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     context = {
         'board': board,
         'apps_list': apps_list,
     }
     log.debug('AppController.board_show context: %s', context)
     return self.__render('apps_index.html', context)
Esempio n. 17
0
 def activity(self):
     do_if_user_not_sysadmin()
     page = get_page_number(tk.request.params)
     total_rows = Thread.all().count()
     total_pages = ((total_rows - 1) / self.paginated_by + 1) or 1
     if not 0 < page <= total_pages:
         # redirect for delete page parameter reason
         redirect_to('forum_activity')
     thread_activity = Thread.all().order_by(Thread.created.desc())
     post_activity = Post.all().order_by(Post.created.desc())
     activity = [
         dict(id=i.id,
              url=i.get_absolute_url(),
              ban_url=tk.url_for('forum_thread_ban', id=i.id),
              type=tk._('Thread'),
              content=i.content,
              author_name=i.author.name,
              created=i.created) for i in thread_activity
     ]
     activity += [
         dict(id=i.id,
              url=i.get_absolute_url(),
              ban_url=tk.url_for('forum_post_ban', id=i.id),
              type=tk._('Post'),
              content=i.content,
              author_name=i.author.name,
              created=i.created) for i in post_activity
     ]
     activity = sorted(activity, key=itemgetter('created'), reverse=True)
     start_elem = int((page - 1) * self.paginated_by)
     end_elem = int(page * self.paginated_by)
     c.page = Page(collection=thread_activity,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     return self.__render('forum_activity.html',
                          {'activity': activity[start_elem:end_elem]})
Esempio n. 18
0
class GroupController(BaseController):

    ## hooks for subclasses 
    group_form = 'group/new_group_form.html'

    def _form_to_db_schema(self):
        return group_form_schema()

    def _db_to_form_schema(self):
        '''This is an interface to manipulate data from the database
        into a format suitable for the form (optional)'''

    def _setup_template_variables(self, context):
        c.is_sysadmin = Authorizer().is_sysadmin(c.user)

        ## This is messy as auths take domain object not data_dict
        group = context.get('group') or c.pkg
        if group:
            c.auth_for_change_state = Authorizer().am_authorized(
                c, model.Action.CHANGE_STATE, group)

    ## end hooks

    def __init__(self):
        BaseController.__init__(self)
        self.extensions = PluginImplementations(IGroupController)
    
    def index(self):
        
        if not self.authorizer.am_authorized(c, model.Action.SITE_READ, model.System):
            abort(401, _('Not authorized to see this page'))
        
        query = authz.Authorizer().authorized_query(c.user, model.Group)
        query = query.order_by(model.Group.name.asc())
        query = query.order_by(model.Group.title.asc())
        c.page = Page(
            collection=query,
            page=request.params.get('page', 1),
            items_per_page=20
        )
        return render('group/index.html')


    def read(self, id):
        c.group = model.Group.get(id)
        if c.group is None:
            abort(404)
        auth_for_read = self.authorizer.am_authorized(c, model.Action.READ, c.group)
        if not auth_for_read:
            abort(401, _('Not authorized to read %s') % id.encode('utf8'))
        
        import ckan.misc
        format = ckan.misc.MarkdownFormat()
        desc_formatted = format.to_html(c.group.description)
        try: 
            desc_formatted = genshi.HTML(desc_formatted)
        except genshi.ParseError, e:
            log.error('Could not print group description: %r Error: %r', c.group.description, e)
            desc_formatted = 'Error: Could not parse group description'
        c.group_description_formatted = desc_formatted
        c.group_admins = self.authorizer.get_admins(c.group)

        c.page = Page(
            collection=c.group.active_packages(),
            page=request.params.get('page', 1),
            items_per_page=50
        )
        for extension in self.extensions:
            extension.read(c.group)
        return render('group/read.html')
Esempio n. 19
0
                "Cannot render description")
            c.description_formatted = genshi.HTML(error_msg)

        try:
            desc_formatted = ckan.misc.MarkdownFormat().to_html(
                c.group.description)
            desc_formatted = genshi.HTML(desc_formatted)
        except genshi.ParseError, e:
            desc_formatted = 'Error: Could not parse group description'
        c.group_description_formatted = desc_formatted
        c.group_admins = self.authorizer.get_admins(c.group)

        results = get_action('group_package_show')(context, data_dict)

        c.page = Page(collection=results,
                      page=request.params.get('page', 1),
                      items_per_page=50)

        return render('group/read.html')

    def new(self, data=None, errors=None, error_summary=None):
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'extras_as_string': True,
            'schema': self._form_to_db_schema(),
            'save': 'save' in request.params
        }
        try:
            check_access('group_create', context)
Esempio n. 20
0
            c.description_formatted = genshi.HTML(error_msg)

        try:
            desc_formatted = ckan.misc.MarkdownFormat().to_html(
                c.group.description)
            desc_formatted = genshi.HTML(desc_formatted)
        except genshi.ParseError, e:
            desc_formatted = 'Error: Could not parse group description'
        c.group_description_formatted = desc_formatted
        c.group_admins = self.authorizer.get_admins(c.group)

        context['return_query'] = True
        results = get_action('group_package_show')(context, data_dict)

        c.page = Page(collection=results,
                      page=request.params.get('page', 1),
                      url=h.pager_url,
                      items_per_page=30)

        result = []
        for pkg_rev in c.page.items:
            result.append(package_dictize(pkg_rev, context))
        c.page.items = result

        return render('group/read.html')

    def new(self, data=None, errors=None, error_summary=None):
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'extras_as_string': True,
Esempio n. 21
0
 def list(self):
     format = request.params.get('format', '')
     if format == 'atom':
         # Generate and return Atom 1.0 document.
         from webhelpers.feedgenerator import Atom1Feed
         feed = Atom1Feed(
             title=_(u'CKAN Repository Revision History'),
             link=h.url_for(controller='revision', action='list', id=''),
             description=_(u'Recent changes to the CKAN repository.'),
             language=unicode(get_lang()),
         )
         # TODO: make this configurable?
         # we do not want the system to fall over!
         maxresults = 200
         try:
             dayHorizon = int(request.params.get('days', 5))
         except:
             dayHorizon = 5
         ourtimedelta = timedelta(days=-dayHorizon)
         since_when = datetime.now() + ourtimedelta
         revision_query = model.repo.history()
         revision_query = revision_query.filter(
             model.Revision.timestamp >= since_when).filter(
                 model.Revision.id != None)
         revision_query = revision_query.limit(maxresults)
         for revision in revision_query:
             package_indications = []
             revision_changes = model.repo.list_changes(revision)
             resource_revisions = revision_changes[model.Resource]
             resource_group_revisions = \
                 revision_changes[model.ResourceGroup]
             package_extra_revisions = revision_changes[model.PackageExtra]
             for package in revision.packages:
                 if not package:
                     # package is None sometimes - I don't know why,
                     # but in the meantime while that is fixed,
                     # avoid an exception here
                     continue
                 number = len(package.all_revisions)
                 package_revision = None
                 count = 0
                 for pr in package.all_revisions:
                     count += 1
                     if pr.revision.id == revision.id:
                         package_revision = pr
                         break
                 if package_revision and package_revision.state == \
                         model.State.DELETED:
                     transition = 'deleted'
                 elif package_revision and count == number:
                     transition = 'created'
                 else:
                     transition = 'updated'
                     for resource_revision in resource_revisions:
                         if resource_revision.continuity.resource_group.\
                                 package_id == package.id:
                             transition += ':resources'
                             break
                     for resource_group_revision in \
                             resource_group_revisions:
                         if resource_group_revision.package_id == \
                                 package.id:
                             transition += ':resource_group'
                             break
                     for package_extra_revision in package_extra_revisions:
                         if package_extra_revision.package_id == \
                                 package.id:
                             if package_extra_revision.key == \
                                     'date_updated':
                                 transition += ':date_updated'
                                 break
                 indication = "%s:%s" % (package.name, transition)
                 package_indications.append(indication)
             pkgs = u'[%s]' % ' '.join(package_indications)
             item_title = u'r%s ' % (revision.id)
             item_title += pkgs
             if revision.message:
                 item_title += ': %s' % (revision.message or '')
             item_link = h.url_for(action='read', id=revision.id)
             item_description = _('Datasets affected: %s.\n') % pkgs
             item_description += '%s' % (revision.message or '')
             item_author_name = revision.author
             item_pubdate = revision.timestamp
             feed.add_item(
                 title=item_title,
                 link=item_link,
                 description=item_description,
                 author_name=item_author_name,
                 pubdate=item_pubdate,
             )
         feed.content_type = 'application/atom+xml'
         return feed.writeString('utf-8')
     else:
         query = model.Session.query(model.Revision)
         c.page = Page(collection=query,
                       page=request.params.get('page', 1),
                       url=h.pager_url,
                       items_per_page=20)
         return render('revision/list.html')
Esempio n. 22
0
    def importexcel(self):
        user = model.User.get('admin')
        context = {'model': model, 'user': user, 'session': model.Session}
        log.fatal(c.user)

        self._setup_template_variables(context)
        group = c.cdo_group
        groupharvester_id = 0
        con = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'schema': self._groupform_to_db_schema(group.type),
            'for_view': True
        }
        data_dict = {'id': group.id}

        try:
            c.group_dict = get_action('group_show')(con, data_dict)
            c.group = con['group']
            for e in c.group_dict['extras']:
                if e['key'] == u'harvester':
                    groupharvester_id = e['value']
                    pass
        except NotFound:
            abort(404, _('Group not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read group %s') % id)
        ##insert code to handle different groups
        #group = 'stadt-wien'
        groupharvester_id = groupharvester_id.replace('\"', '')
        if groupharvester_id == 0:
            abort(404, _('Harvester not properly configured'))

        if request.params.get('sent'):
            c.sent = request.params.get('sent')
            tempfile = request.POST['file']
            filepath = '/importFiles/' + group.name + '/'
            ensure_dir(filepath)
            filepath = filepath + '/import.xls'
            perm_file = open(filepath, 'wb')
            shutil.copyfileobj(tempfile.file, perm_file)
            with open(filepath, "r") as myfile:
                os.chmod(filepath, 0777)
            #context = {'model':model, 'user':c.user or c.author, 'session':model.Session}
            try:
                get_action('harvest_job_create')(context, {
                    'source_id': groupharvester_id
                })
            except Exception:
                pass
            h.flash_success(
                _(u'Der Import wurde angestossen und wird innerhalb der nächsten 15 Minuten erledigt'
                  ))
        else:
            c.sent = 0
        try:
            c.source = get_action('harvest_source_show')(
                context, {
                    'id': groupharvester_id
                })

            c.page = Page(collection=c.source['status']['packages'],
                          page=request.params.get('page', 1),
                          items_per_page=20,
                          url=pager_url)
        except NotFound:
            abort(404, _('Harvest source not found'))
        return render('home/import.html')