Пример #1
0
    def filter_pattern_config(self):
        config = {
            'tags': self.data.get('available_tags', []) or []
        }
        form = self.get_form()
        config['query'] = {}
        for attr in self.query_attrs:
            if form.get(attr):
                config['query'][attr] = form.get(attr)
        if ('Subject' in config['query'] and
                isinstance(config['query']['Subject'], basestring)):
            config['query']['Subject'] = [config['query']['Subject']]

        config['display_type'] = self.data.get('display_type', None)

        out = '{}'
        try:
            config['ajaxResults'] = {
                'url': self.this_url,
                'selector': '#query-results-%s' % self.id or ''
            }
            out = json.dumps(config)
        except UnicodeDecodeError:
            try:
                # try to gracefully smooth over any unicode errors
                out = json.dumps(config, ensure_ascii=False)
            except UnicodeDecodeError:
                # It still didn't work. Let's just return an empty object
                pass
        return out
Пример #2
0
 def this_url(self):
     url = '{}/@@{}/{}'.format(self.context.absolute_url(), self.__name__,
                               self.id or '')
     params = {}
     form = self.get_form()
     for attr in self.query_attrs:
         if form.get(attr):
             params[attr] = form.get(attr)
     if len(params) > 0:
         url += '?' + urlencode(params)
     return url
Пример #3
0
    def results(self):
        catalog = getToolByName(self.context, 'portal_catalog')

        # there is a special case with Subject queries...
        # subject queries are OR, so if they are in the original query
        # and have a further filter with a different Subject, we need to
        # do some manual filtering. This can be potentially slow....
        # it's an edge case, so hopefully it's okay...
        query = self.get_query()
        form = self.get_form()
        subject_filter = None
        for attr in self.query_attrs:
            if form.get(attr):
                val = _list(form.get(attr))
                if attr == 'Subject':
                    if attr in query and len(
                            set(val) & set(_query_val(query[attr]))) > 0:
                        # matches here when subject is in the original query
                        # so we're trying to turn this into an AND query with manual
                        # filtering later
                        subject_filter = val[0]
                    else:
                        query[attr] = val
                else:
                    query[attr] = val[0]

        if query.get('sort_on', '') not in catalog._catalog.indexes:
            query['sort_on'] = 'effective'

        result = catalog(**query)
        if subject_filter is not None:
            # special case where we have to further filter...
            result = [
                item for item in result
                if item.Subject and subject_filter in item.Subject
            ]
        try:
            form = self.get_form()
            page = int(form.get('page', 1)) - 1
        except:
            page = 0
        page = max(page, 0)
        start = page * self.limit
        end = start + self.limit
        return {
            'total': len(result),
            'page': page + 1,
            'items': result[start:end]
        }
Пример #4
0
 def filter_pattern_config(self):
     config = {'tags': self.data.get('available_tags', []) or []}
     form = self.get_form()
     config['query'] = {}
     for attr in self.query_attrs:
         if form.get(attr):
             config['query'][attr] = form.get(attr)
     if ('Subject' in config['query']
             and isinstance(config['query']['Subject'], basestring)):
         config['query']['Subject'] = [config['query']['Subject']]
     config['ajaxResults'] = {
         'url': self.this_url,
         'selector': '#query-results-%s' % self.id or ''
     }
     return json.dumps(config)
Пример #5
0
 def this_url(self):
     url = '{}/@@{}/{}'.format(
         self.context.absolute_url(), self.__name__, self.id or ''
     )
     params = {}
     form = self.get_form()
     for attr in self.query_attrs:
         if form.get(attr):
             val = form.get(attr)
             if isinstance(val, list):
                 val = [unidecode(v) for v in val]
             else:
                 val = unidecode(val)
             params[attr] = val
     if len(params) > 0:
         url += '?' + urlencode(params)
     return url
 def get_from_form(form, fieldname):
     value = form.get(fieldname, None)
     if value:
         if isinstance(value, list):
             return value[0]
         if isinstance(value, str):
             return value
     return None
Пример #7
0
    def results(self):
        catalog = getToolByName(self.context, 'portal_catalog')

        # there is a special case with Subject queries...
        # subject queries are OR, so if they are in the original query
        # and have a further filter with a different Subject, we need to
        # do some manual filtering. This can be potentially slow....
        # it's an edge case, so hopefully it's okay...
        query = self.get_query()
        form = self.get_form()
        subject_filter = None
        for attr in self.query_attrs:
            if form.get(attr):
                val = form.get(attr)
                if attr in query and val not in query[attr]:
                    subject_filter = val
                else:
                    query[attr] = val

        result = catalog(**query)
        if subject_filter is not None:
            # special case where we have to further filter...
            result = [item for item in result
                      if item.Subject and subject_filter in item.Subject]
        try:
            form = self.get_form()
            page = int(form.get('page', 1)) - 1
        except:
            page = 0
        page = max(page, 0)
        start = page * self.limit
        end = start + self.limit
        return {
            'total': len(result),
            'page': page + 1,
            'items': result[start:end]
        }
Пример #8
0
 def _updateForm(self, form):
     INVALID_OPTION = _(u'Invalid option')
     options = form.get('options', '')
     if isinstance(options, list):
         self.errors.append(INVALID_OPTION)
     elif isinstance(options, str):
         if not options.isdigit():
             self.errors.append(INVALID_OPTION)
         else:
             options = int(options)
     if not self.errors:
         # Let's vote
         try:
             self.context.setVote(options, self.request)
             self.messages.append(_(u'Thanks for your vote'))
             # We do this to avoid redirecting anonymous user as
             # we just sent them the cookie
             self._has_voted = True
         except Unauthorized:
             self.errors.append(_(u'You are not authorized to vote'))