Esempio n. 1
0
    def get(self, field, language, state='active'):
        '''Return a translation for the given pair (field, language).
        '''
        assert isinstance(field, Field)
        verifyObject(IFieldContext, field.context)

        key = type(self)._key(field)
        language = check_language(language)

        # Lookup for a translation on this key
        
        cond = dict(
            package_id = self._package_id,
            source_language = self._source_language,
            key = key,
            language = language)
        if state and (state != '*'):
            cond['state'] = state
        r1, value = None, None
        q = model.Session.query(ext_model.PackageTranslation).filter_by(**cond)
        try:
            r1 = q.one()
        except orm.exc.NoResultFound as ex:
            pass
        else:
            value = r1.value
        
        if value:
            return field.bind(FieldContext(key=field.context.key, value=value))
        return None
Esempio n. 2
0
    def _to_xsd_type(self, type_prefix):
        xsd_uri = self.nsmap['xs']

        # Create an xs:complexType element

        # Note: This type-name will not be prefixed with type_prefix, as it is
        # computed from the object's type-name (and we want to re-use it).

        tname = self.typename
        e = Element(QName(xsd_uri, 'complexType'), attrib={'name': tname})
        e1 = SubElement(e, QName(xsd_uri, 'all'))

        # Create an xs:element for each field

        ys_prefix = type_prefix + '_' + self.typename
        tdefs = {}
        for k, yf in self.obj.iter_fields(exclude_properties=True):
            yv = yf.get(self.obj)
            yf1 = yf.bind(FieldContext(key=k, value=yv))
            ys = serializer_for_field(yf1)
            ys.target_namespace = self.target_namespace
            ye, ye_tdefs = ys.to_xsd(type_prefix=ys_prefix)
            e1.append(ye)
            tdefs.update(ye_tdefs)

        tdefs[tname] = e
        return ('target:' + tname, tdefs)
Esempio n. 3
0
 def render_item_template():
     yf = field.value_type.bind(FieldContext(key='{{key}}', value=None))
     yd = self.get_item_template_vars(key=None)
     return {
         'variables': ['key', 'title'],
         'markup': to_c14n_markup(
             markup_for_field(qa, yf, name_prefix=qname, data=yd),
             with_comments=False)
     }
Esempio n. 4
0
 def render_item(i, y):
     yf = field.value_type.bind(FieldContext(key=i, value=y))
     ye = error_dict.get(i) if error_dict else None
     yd = self.get_item_template_vars(index=i)
     return {
         'index': i,
         'markup': markup_for_field(
             qa, yf, errors=ye, name_prefix=qname, data=yd)
     }
Esempio n. 5
0
    def _to_xml(self, obj, e):
        assert isinstance(obj, type(self.obj))

        for k, yf in obj.iter_fields(exclude_properties=True):
            yv = yf.get(obj)
            if yv is None:
                continue
            yf1 = yf.bind(FieldContext(key=k, value=yv))
            ys = serializer_for_field(yf1)
            ys.target_namespace = self.target_namespace
            e.append(ys.to_xml(yv))
Esempio n. 6
0
 def render_item(k, y):
     yf = field.value_type.bind(FieldContext(key=k, value=y))
     term = terms.get(k)
     ye = error_dict.get(k) if error_dict else None
     yd = self.get_item_template_vars(key=k, term=term)
     return {
         'key': k,
         'key_term': term,
         'markup': markup_for_field(
             qa, yf, errors=ye, name_prefix=qname, data=yd),
     }
Esempio n. 7
0
    def _from_xml(self, e):
        schema = self.obj.get_schema()
        factory = type(self.obj)
        obj = factory()

        # Fixme: Why dont we directly update self.obj?

        for p in e:
            k = QName(p.tag).localname
            k = inflection.underscore(k)
            yf = schema.get(k)
            assert yf, 'Expected a field named %s at schema %s' % (k, schema)
            yf1 = yf.bind(FieldContext(key=k, value=yf.get(self.obj)))
            ys = serializer_for_field(yf1)
            ys.target_namespace = self.target_namespace
            yf.set(obj, ys.from_xml(p))

        return obj
Esempio n. 8
0
 def iter_fields(self, language, state='active'):
     '''Iterate on field translations for a given language.
    
     Generate tuples of (<state>, <bound-field>)
     ''' 
     
     cond = {
         'package_id': self._package_id, 
         'source_language': self._source_language,
         'language': check_language(language),
     }
     if state and (state != '*'):
         cond['state'] = state 
     
     uf = TextField()       
     q = model.Session.query(ext_model.PackageTranslation).filter_by(**cond)
     for r in q.all():
         yield r.state, uf.bind(FieldContext(key=r.key, value=r.value))
Esempio n. 9
0
    def translate(self, field, language, value, state='active'):
        '''Add or update translation for a given pair (field, language).
        '''
        assert isinstance(field, Field)
        verifyObject(IFieldContext, field.context)
        
        key = type(self)._key(field)
        language = check_language(language)

        value = unicode(value)
        if not value:
            raise ValueError('value: Missing')
        
        # Insert or update a translation

        cond = dict(
            package_id = self._package_id,
            source_language = self._source_language,
            key = key,
            language = language)
        q = model.Session.query(ext_model.PackageTranslation).filter_by(**cond)
        r1 = None
        try:
            r1 = q.one()
        except orm.exc.NoResultFound as ex:
            # Insert
            r1 = ext_model.PackageTranslation(**cond)
            model.Session.add(r1)
        
        r1.value = value
        r1.state = state
        
        if not self._defer_commit:
            model.Session.commit()
        
        return field.bind(FieldContext(key=field.context.key, value=value))