Changeset 6d4e11a


Ignore:
Timestamp:
Jan 19, 2010 6:37:06 PM (5 years ago)
Author:
olivier <>
Branches:
master, crem, crem2, dev, dev2, diadems, feature/breadcrumbs, feature/ts-0.5, feature/ts-0.5.4, feature/writecache, forma, generic, instru_search, lam, lam2, mapsv3, merge, nlivemulti, production, release/1.4.4, sabiod, security, server, social, storage, telecaster, test, video
Children:
dcb9160
Parents:
576e960
git-author:
olivier <> (19/01/2010 18:37:06)
git-committer:
olivier <> (19/01/2010 18:37:06)
Message:

fix crem models DOM export and add support for embedding foreign keys

Location:
telemeta
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • telemeta/models/crem.py

    r576e960 r6d4e11a  
    3636from django.db import models 
    3737import cremquery as query 
     38from xml.dom.minidom import getDOMImplementation 
    3839 
    3940class ModelCore(models.Model): 
     
    7576        return fields_list 
    7677 
    77     def get_dom_element_name(cls): 
     78    @classmethod 
     79    def get_dom_name(cls): 
    7880        "Convert the class name to a DOM element name" 
    7981        clsname = cls.__name__ 
    8082        return clsname[0].lower() + clsname[1:] 
    81     get_dom_element_name = classmethod(get_dom_element_name) 
     83 
     84    @staticmethod 
     85    def get_dom_field_name(field_name): 
     86        "Convert the class name to a DOM element name" 
     87        tokens = field_name.split('_') 
     88        name = tokens[0] 
     89        for t in tokens[1:]: 
     90            name += t[0].upper() + t[1:] 
     91        return name 
    8292 
    8393    def to_dom(self): 
    8494        "Return the DOM representation of this media object" 
    8595        impl = getDOMImplementation() 
    86         root = self.get_dom_element_name() 
     96        root = self.get_dom_name() 
    8797        doc = impl.createDocument(None, root, None) 
    8898        top = doc.documentElement 
    89         top.setAttribute("id", self.id) 
     99        top.setAttribute("id", str(self.id)) 
    90100        fields = self.to_dict() 
    91101        for name, value in fields.iteritems(): 
    92             element = doc.createElement(name) 
     102            element = doc.createElement(self.get_dom_field_name(name)) 
     103            if isinstance(value, models.Model): 
     104                element.setAttribute('key', str(value.pk)) 
    93105            value = unicode(value) 
    94106            element.appendChild(doc.createTextNode(value)) 
  • telemeta/tests/model_tests.py

    r576e960 r6d4e11a  
    255255        else: 
    256256            self.fail("No exception raised") 
    257          
     257        
     258    def testDomForeignKey(self): 
     259        doc = self.item_4.to_dom() 
     260        self.assertEquals(doc.getElementsByTagName('collection')[0].getAttribute('key'), str(self.persepolis.id)) 
     261 
Note: See TracChangeset for help on using the changeset viewer.