Example #1
0
 def _form_message(self, container, key, msg):
     if msg:
         msg = to_string(msg)
         if key in container:
             container[key].append(msg)
         else:
             container[key] = [msg]
Example #2
0
 def get_json(self, key):
     value = self.get(key)
     if value is not None:
         try:
             return json.loads(to_string(value))
         except Exception:
             self.app.logger.warning('Could not convert to JSON: %s', value)
Example #3
0
 def get_json(self, key):
     value = self.get(key)
     if value is not None:
         try:
             return json.loads(to_string(value))
         except Exception:
             self.app.logger.warning('Could not convert to JSON: %s',
                                     value)
Example #4
0
    def _unwind(self):
        if hasattr(self, '_forms'):
            return
        related_form = self.related_form
        if related_form is None:
            raise ValueError('Related form not specified')
        self.prefix = '{0}_{1}_'.format(related_form.prefix or '', self.name)
        errors = self._errors = {}
        forms = self._forms = []
        is_bound = self.is_bound
        nf = '{0}{1}'.format(self.prefix, self.NUMBER_OF_FORMS_CODE)
        instances = []
        if is_bound:
            if nf not in related_form.rawdata:
                raise ValidationError(
                    'Could not find number of "{0}" forms'.format(self.name))
            num_forms = int(related_form.rawdata[nf])
        else:
            related = related_form.instance
            num_forms = 0
            if related is not None and related.id:
                if self.instances_from_related:
                    instances = self.instances_from_related(related)
                else:
                    instances = self.mapper.filter(
                        **{self.related_name: related})
                instances = list(instances)
                num_forms = self.extra_length + len(instances)
            num_forms = max(num_forms, self.initial_length)
        self.num_forms = HiddenInput(name=nf, value=num_forms)

        for idx, instance in zip_longest(range(num_forms), instances):
            f = self.get_form(self.prefix, idx, instance)
            if f is not None:
                forms.append(f)
                errors.update(f.errors)

        if is_bound and not errors and self.clean:
            try:
                self.clean(self)
            except ValidationError as e:
                self.form.add_error(to_string(e))
Example #5
0
    def _unwind(self):
        if hasattr(self, '_forms'):
            return
        related_form = self.related_form
        if related_form is None:
            raise ValueError('Related form not specified')
        self.prefix = '{0}_{1}_'.format(related_form.prefix or '', self.name)
        errors = self._errors = {}
        forms = self._forms = []
        is_bound = self.is_bound
        nf = '{0}{1}'.format(self.prefix, self.NUMBER_OF_FORMS_CODE)
        instances = []
        if is_bound:
            if nf not in related_form.rawdata:
                raise ValidationError(
                    'Could not find number of "{0}" forms'.format(self.name))
            num_forms = int(related_form.rawdata[nf])
        else:
            related = related_form.instance
            num_forms = 0
            if related is not None and related.id:
                if self.instances_from_related:
                    instances = self.instances_from_related(related)
                else:
                    instances = self.mapper.filter(
                        **{self.related_name: related})
                instances = list(instances)
                num_forms = self.extra_length + len(instances)
            num_forms = max(num_forms, self.initial_length)
        self.num_forms = HiddenInput(name=nf, value=num_forms)

        for idx, instance in zip_longest(range(num_forms), instances):
            f = self.get_form(self.prefix, idx, instance)
            if f is not None:
                forms.append(f)
                errors.update(f.errors)

        if is_bound and not errors and self.clean:
            try:
                self.clean(self)
            except ValidationError as e:
                self.form.add_error(to_string(e))
Example #6
0
 def decode(self, msg):
     try:
         return json.loads(to_string(msg))
     except Exception as exc:
         raise ProtocolError('Invalid JSON') from exc
Example #7
0
 def decode(self, msg):
     try:
         return json.loads(to_string(msg))
     except Exception as exc:
         raise ProtocolError('Invalid JSON') from exc
Example #8
0
def _gen_query(query_string, encoding):
    # keep_blank_values=True
    for key, value in parse_qsl((query_string or ''), True):
        yield (to_string(key, encoding, errors='replace'),
               to_string(value, encoding, errors='replace'))
Example #9
0
 def load(cls, data, method=None):
     method = method or 'json'
     if method == 'json':
         return cls(**json.loads(to_string(data)))
     else:
         raise ImproperlyConfigured('Unknown serialisation "%s"' % method)
Example #10
0
def _gen_query(query_string, encoding):
    # keep_blank_values=True
    for key, value in parse_qsl((query_string or ''), True):
        yield (to_string(key, encoding, errors='replace'),
               to_string(value, encoding, errors='replace'))
Example #11
0
 def decode(self, message):
     return to_string(message)
Example #12
0
 def home_page(self, request):
     data = open(os.path.join(CHAT_DIR, 'chat.html')).read()
     request.response.content_type = 'text/html'
     request.response.content = to_string(data % request.environ)
     return request.response
Example #13
0
 def __lt__(self, other):
     if isinstance(other, self.__class__):
         return to_string(self) < to_string(other)
     else:
         raise TypeError('Cannot compare {0} with {1}'.format(self, other))
Example #14
0
 def decode(self, message):
     return to_string(message)
Example #15
0
 def home_page(self, request):
     data = open(os.path.join(CHAT_DIR, 'chat.html')).read()
     request.response.content_type = 'text/html'
     request.response.content = to_string(data % request.environ)
     return request.response