def addsub(self,request): ''' Add a subcomponent to a parent, essentially, we create a subcomponent, and return it for editing ''' #we have instantiated self.component as the parent! #ok create a new component if request.method=='POST': u=atomuri() c=Component(scienceType='sub',centre=self.component.centre,uri=u,abbrev='new', contact=self.component.contact,author=self.component.author, funder=self.component.funder, model=self.component.model,realm=self.component.realm) r=c.save() p=ParamGroup() p.save() c.paramGroup.add(p) cg=ConstraintGroup(constraint='',parentGroup=p) cg.save() #print 'Return Value',r self.component.components.add(c) url=reverse('pimmsqn.apps.qn.views.componentEdit',args=(self.centre_id,c.id,)) logging.info('Created subcomponent %s in component %s (type "new")'%(c.id,self.component.id)) return HttpResponseRedirect(url) else: #would be better to post the create child to the parent url, not this artificial non restful way return HttpResponseBadRequest('Cannot use HTTP GET to this URL')
def makeEmptyGrid(self, centre, author=None, contact=None, funder=None, title='Grid Template', abbrev='Grid Template'): if author is None: author=self.joe if funder is None: funder=self.joe if contact is None: contact=self.joe grid=Grid(centre=centre,abbrev='',uri=atomuri(), author=author,contact=contact,funder=funder) grid.istopGrid=True grid.title=title grid.abbrev=abbrev grid.save() grid.topgrid=grid grid.save() self.top=grid logging.debug('Created empty top level grid %s'%grid) # now get a placeholder paramgroup and constraint group p=ParamGroup() p.save() grid.paramGroup.add(p) cg=ConstraintGroup(constraint='',parentGroup=p) cg.save()
def makeEmptyModel(self, centre, author=None, contact=None, funder=None, title='Model Template', abbrev='Model Template'): if author is None: author=self.joe if funder is None: funder=self.joe if contact is None: contact=self.joe component=Component(scienceType='model',centre=centre,abbrev='',uri=atomuri(), author=author,contact=contact,funder=funder) component.isModel=True component.isRealm=False component.title=title component.abbrev=abbrev component.save() component.model=component component.controlled=True component.save() self.top=component logging.debug('Created empty top level model %s'%component) # now get a placeholder paramgroup and constraint group p=ParamGroup() p.save() component.paramGroup.add(p) cg=ConstraintGroup(constraint='',parentGroup=p) cg.save()
def loadProperties(args): for arg in args: defn,values=properties[arg] v=Vocab(uri=atomuri(),name=arg,definition=defn) v.save() for r,d in values: rv=Term(vocab=v,name=r,definition=d) rv.save()
def loadvocab(key): ''' Used to load vocabularies ''' v=Vocab(uri=atomuri(),name=key) v.save() values=VocabList[key] for r in values: rv=Term(vocab=v,name=r) rv.save()
def setUp(self): try: self.centre=Centre.objects.get(abbrev="CMIP5") except: u=atomuri() c=Centre(abbrev='CMIP5',name='Dummy testing version', uri=u) logging.debug('Created dummy centre for testing') c.save() self.centre=c
def __handleParam(self,elem,pg,cg): ''' Add new parameter to cg, if cg none, create one in pg ''' if cg is None: cg=ConstraintGroup(constraint='',parentGroup=pg) cg.save() paramName=elem.attrib['name'] choiceType=elem.attrib['choice'] logging.debug('For %s found parameter %s with choice %s'%(self.item.attrib['name'],paramName,choiceType)) try: Param={'OR':OrParam,'XOR':XorParam,'keyboard':KeyBoardParam,'couple':ComponentInput}[choiceType] except KeyError: logging.info('ERROR: Ignoring parameter %s'%paramName) return if choiceType in ['OR','XOR']: #create and load vocabulary v=Vocab(uri=atomuri(),name=paramName+'Vocab') v.save() logging.debug('Created vocab %s'%v.name) co,info=None,None for item in elem: if item.tag=='value': # RF append any value definitions to the parameter definition assert len(item)==0 or len(item)==1, "Parse error: a value should have 0 or 1 children" if len(item)==1 : assert item[0].tag=='definition', "Parse error, the child of a value must be a definition element" defn+=" "+item.attrib['name']+": "+item[0].text # RF end append value=Term(vocab=v,name=item.attrib['name']) value.save() elif item.tag=='definition': defn=item.text else: logging.info('Found unexpected tag %s in %s'%(item.tag,paramName)) logging.debug('L %s %s '%(paramName,defn)) p=Param(name=paramName,constraint=cg,vocab=v,definition=defn,controlled=True) p.save() elif choiceType in ['keyboard']: defn,units='','' delem=elem.find('definition') if delem is not None: defn=delem.text numeric,units=self.__handleKeyboardValue(elem) p=Param(name=paramName,constraint=cg,definition=defn,units=units,numeric=numeric,controlled=True) p.save() elif choiceType in ['couple']: # we create an input requirement here and now ... ci=ComponentInput(owner=self.component,abbrev=paramName, description='Required by controlled vocabulary for %s'%self.component, realm=self.component.realm) ci.save() #and we have to create a coupling for it too cp=Coupling(component=self.component.model,targetInput=ci) cp.save() logging.info('Added component input %s for %s in %s'%(paramName,cg,pg)) return cg
def add(self, doSubs, model=None, realm=None): u=atomuri() component = Component( title='', scienceType=self.item.attrib['name'], abbrev=self.item.attrib['name'], uri=u,centre=self.centre,contact=self.author[0],email=self.author[1]) component.save() # we need a primary key value so we can add subcomponents later self.component=component # used to assign parameters ... if self.parent.tag != "component": logging.debug("Top-level component %s"%self.item.attrib['name']) self.topLevelID=component.id model=component elif component.scienceType in realms: realm=component component.model=model else: component.model=model # if realm doesn't exist, then somehow we've broken our controlled vocab # realm relationship. component.realm=realm if doSubs: ''' go ahead and process subelements, too ''' # 1. Component children of this parent for subchild in self.item: if subchild.tag == "component": logging.debug("Found child : %s"%subchild.tag) subComponentParser = ComponentParser(subchild, self.item, self.centre, self.author) # Handle child components of this one (True = recursive) child=subComponentParser.add(True,model=model,realm=realm) logging.debug("Adding sub-component %s to component %s"%(child.abbrev, component.abbrev)) component.components.add(child) #2. Parameter children of this parent ## FIXME, pretty sure we need to do something more sophisticated here: if subchild.tag == 'parameter': self.__handleParam(subchild) # 3. ComponentRef children of this parent # 4. ParameterRef children of this parent component.save() return component
def __handleParam(self,elem): ''' Handle parameters and add their properties to parent component ''' paramName=elem.attrib['name'] choiceType=elem.attrib['choice'] logging.debug('For %s found parameter %s with choice %s'%(self.item.attrib['name'],paramName,choiceType)) if choiceType in ['OR','XOR']: #create and load vocabulary v=Vocab(uri=atomuri(),name=paramName+'Vocab') v.save() logging.debug('Created vocab %s'%v.name) for item in elem: if item.tag=='value': value=Value(vocab=v,value=item.attrib['name']) value.save() logging.debug('Added %s to vocab %s'%(value.value,v.name)) else: logging.info('Found unexpected tag %s in %s'%(item.tag,paramName)) p=Param(name=paramName,component=self.component,ptype=choiceType,vocab=v) p.save() logging.info('Added parameter %s to component %s (%s)'%(paramName,self.component.abbrev,self.component.id))
def add(self,request): ''' Create a new simulation instance ''' # first see whether a model and platform have been created! # if not, we should return an error message .. c=self.centre p=c.platform_set.values() m=c.component_set.values() url=reverse('pimmsqn.apps.qn.views.centre',args=(self.centreid,)) if len(p)==0: ''' Require them to create a platform ''' message='You need to create a platform before creating a simulation' return render_to_response('error.html',{'message':message,'url':url}) elif len(m)==0: ''' Require them to create a model''' message='You need to create a model before creating a simulation' return render_to_response('error.html',{'message':message,'url':url}) url=reverse('pimmsqn.apps.qn.views.simulationAdd',args=(self.centreid,self.expid,)) u=atomuri() e=Experiment.objects.get(pk=self.expid) s=Simulation(uri=u,experiment=e,centre=self.centre) #grab the experiment duration if we can # there should be no more than one spatio temporal constraint, so let's # get that one. stcg=e.requirements.filter(ctype__name='SpatioTemporalConstraint') if len(stcg)<>1: logging.info('Experiment %s has no duration (%s)?'%(e,len(stcg))) else: stc=stcg[0].get_child_object() print 'duration',stc.requiredDuration s.duration=stc.requiredDuration label='Add' return self.__handle(request,s,e,url,label)
def platformEdit(request,centre_id,platform_id=None): ''' Handle platform editing ''' c=Centre.objects.get(id=centre_id) urls={} # start by getting a form ... if platform_id is None: urls['edit']=reverse('pimmsqn.apps.qn.views.platformEdit',args=(centre_id,)) if request.method=='GET': pform=MyPlatformForm(c) elif request.method=='POST': pform=MyPlatformForm(c,request.POST) p=None puri=atomuri() else: urls['edit']=reverse('pimmsqn.apps.qn.views.platformEdit',args=(centre_id,platform_id,)) p=Platform.objects.get(id=platform_id) puri=p.uri if request.method=='GET': pform=MyPlatformForm(c,instance=p) elif request.method=='POST': pform=MyPlatformForm(c,request.POST,instance=p) urls=commonURLs(p,urls) # now we've got a form, handle it if request.method=='POST': if pform.is_valid(): p=pform.save(commit=False) p.centre=c p.uri=puri p.save() return HttpResponseRedirect( reverse('pimmsqn.apps.qn.views.centre',args=(centre_id,))) return render_to_response('platform.html', {'pform':pform,'urls':urls,'p':p,'c':c, 'tabs':tabs(request,centre_id,'Platform')})
def clean_uri(self): ''' On creating a responsible party we need a uri, once we have one, it should stay the same ''' data=self.cleaned_data['uri'] if data == u'': data=atomuri() return data
def add(self, doSubs, isTop): u=atomuri() # add spaces before any capital letters to make the tree formatting look nicer name=self.item.attrib['name'] nameWithSpaces='' idx=0 for char in name : if idx>0 : # skip first string value as we do not want spaces at the beginning of a string if char.isupper() and name[idx-1].isalpha() : nameWithSpaces+=' ' nameWithSpaces+=char idx+=1 # check length if len(nameWithSpaces)> 29: logging.debug('TOOLONG: name %s will be abbreviated'%nameWithSpaces) nameWithSpaces=nameWithSpaces[0:29] if isTop <> True: grid = Grid(title='', abbrev=nameWithSpaces, isParamGroup=self.isParamGroup, uri=u, centre=self.grid.centre, contact=self.grid.contact, author=self.grid.author, funder=self.grid.funder) grid.save() # we need a primary key value so we can add subcomponents later self.grid=grid # used to assign parameters ... logging.debug('Handling Grid %s'%(grid.abbrev)) else: grid=self.grid if doSubs: #temporary for subchild in self.item: if subchild.tag == "component": logging.debug("Found child : %s"%subchild.tag) subGridParser = GridParser(subchild, self.grid) # Handle child components of this one (True = recursive) child=subGridParser.add(True, False) logging.debug("Adding sub-grid %s to K %s"%(child.abbrev, grid.abbrev)) grid.grids.add(child) elif subchild.tag == 'parametergroup': if subchild.attrib['componentView'] == 'False' or subchild.attrib['componentView'] == 'false': self.__handleParamGrp(subchild) else: # treating the componentView=true as a grid component subGridParser = GridParser(subchild, self.grid, isParamGroup=True) # Handle child grid components of this one (True = recursive) child=subGridParser.add(True,False) logging.debug("Adding sub-grid (parameter group) (paramgroup %s to grid %s)"%(child.abbrev, grid.abbrev)) grid.grids.add(child) elif subchild.tag == 'parameter': print logging.info('OOOOOOOPPPPPPs') self.__handleParam(subchild) else: logging.debug('Ignoring tag %s for %s'%(subchild.tag,self.grid)) grid.save() return grid
def add(self, doSubs, realm=None): u=atomuri() # add spaces before any capital letters to make the tree formatting look nicer name=self.item.attrib['name'] nameWithSpaces='' idx=0 for char in name : if idx>0 : # skip first string value as we do not want spaces at the beginning of a string if char.isupper() and name[idx-1].isalpha() : nameWithSpaces+=' ' nameWithSpaces+=char idx+=1 # check length if len(nameWithSpaces)> 29: logging.debug('TOOLONG: name %s will be abbreviated'%nameWithSpaces) nameWithSpaces=nameWithSpaces[0:29] component = Component( title='', scienceType=self.item.attrib['name'], abbrev=nameWithSpaces, controlled=True, isParamGroup=self.isParamGroup, uri=u, centre=self.model.centre, contact=self.model.contact, author=self.model.author, funder=self.model.funder) component.save() # we need a primary key value so we can add subcomponents later self.component=component # used to assign parameters ... logging.debug('Handling Component %s (%s)'%(component.abbrev,component.scienceType)) if component.scienceType in Realms: realm=component component.model=self.model component.isRealm=True component.realm=component else: component.model=self.model # if realm doesn't exist, then somehow we've broken our controlled vocab # realm relationship. component.realm=realm if doSubs: #temporary for subchild in self.item: if subchild.tag == "component": logging.debug("Found child : %s"%subchild.tag) subComponentParser = ComponentParser(subchild, self.model) # Handle child components of this one (True = recursive) child=subComponentParser.add(True,realm=realm) logging.debug("Adding sub-component %s to component %s (model %s, realm %s)"%(child.abbrev, component.abbrev,child.model,child.realm)) component.components.add(child) elif subchild.tag == 'parametergroup': if subchild.attrib['componentView'] == 'False' or subchild.attrib['componentView'] == 'false': self.__handleParamGrp(subchild) else: # treating the componentView=true as a component subComponentParser = ComponentParser(subchild, self.model, isParamGroup=True) # Handle child components of this one (True = recursive) child=subComponentParser.add(True,realm=realm) logging.debug("Adding sub-component (parameter group) (paramgroup %s to component %s (model %s, realm %s)"%(child.abbrev, component.abbrev,child.model,child.realm)) component.components.add(child) elif subchild.tag == 'parameter': print logging.info('OOOOOOOPPPPPPs') self.__handleParam(subchild) else: logging.debug('Ignoring tag %s for %s'%(subchild.tag,self.component)) component.save() return component