Пример #1
0
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('pimms.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('pimms.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('pimms.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')})
Пример #2
0
 def edit(self,request):
     ''' Provides a form to edit a component, and handle the posting of a form
     containing edits for a component, or a delete'''
     
     c=self.component
     logging.debug('Starting editing component %s'%c.id)
     
     if request.method=='POST':
         if 'deleteMe' in request.POST:
             if c.controlled:
                 logging.debug('Invalid delete POST to controlled component')
                 return HttpResponse('Invalid Request')
             else:
                 if len(c.components.all())<>0:
                     return HttpResponse('You need to delete child components first')
                 parent=Component.objects.filter(components=c)[0]
                 url=reverse('pimms.apps.qn.views.componentEdit',args=(self.centre_id,parent.id,))
                 c.delete()
                 return HttpResponseRedirect(url)
     
             
     # find my own urls ...
     urls={}
     urls['form']=self.url
     urls['refs']=reverse('pimms.apps.qn.views.assign',args=(self.centre_id,'reference',
                          'component',c.id,))
     urls['subcomp']=reverse('pimms.apps.qn.views.componentSub',args=(self.centre_id,c.id,))
     urls['coupling']=reverse('pimms.apps.qn.views.componentCup',args=(self.centre_id,c.id))
     urls['inputs']=reverse('pimms.apps.qn.views.componentInp',args=(self.centre_id,c.id))
     urls['text']=reverse('pimms.apps.qn.views.componentTxt',args=(self.centre_id,c.id))
     
     urls=commonURLs(c.model,urls)
     
     baseURL=reverse('pimms.apps.qn.views.componentAdd',args=(self.centre_id,))
     template='+EDID+'
     baseURL=baseURL.replace('add/','%s/edit'%template)
 
     # this is the automatic machinery ...
     refs=Reference.objects.filter(component__id=c.id)
     inps=ComponentInput.objects.filter(owner__id=c.id)
     
     postOK=True
     if request.method=="POST":
         pform=ParamGroupForm(c,request.POST,prefix='props')
         pform.newatt=1
         cform=ComponentForm(request.POST,prefix='gen',instance=c)
         if cform.is_valid():
             c=cform.save()
             c=RemoteUser(request,c)
             logging.debug('Saving component %s details (e.g. uri %s)'%(c.id,c.uri))
         else:
             logging.debug('Unable to save characteristics for component %s'%c.id)
             postOK=False
             logging.debug(cform.errors)
         ok=pform.save()
         if postOK: postOK=ok  # if not postok, ok value doesn't matter
     
     # We separate the response handling so we can do some navigation in the
     # meanwhile ...
     navtree=yuiTree2(c.id,baseURL,template=template)
     
     
     # Handle a request to copy responsible details downwards to subcomponents
     if request.method=='POST':
         if 'filterdown' in request.POST:
             c.filterdown()
         if 'filterdowngrid' in request.POST:
             c.filterdowngrid()
     
     #OK, we have three cases to handle:
     
     #FIXME; we'll need to put this in the right places with instances:
 
     if request.method=='POST':
         if postOK:
             #redirect, so repainting the page doesn't resubmit
             logging.debug('Finished handling post to %s'%c.id)
             return HttpResponseRedirect(urls['form'])
         else:
             pass
             # don't reset the forms so the user gets an error response.
     else:
         #get some forms
         cform=ComponentForm(instance=c, prefix='gen')
         
         pform=ParamGroupForm(c,prefix='props')
         pform.newatt=1
     
     if c.isModel:
         # We'd better decide what we want to say about couplings. Same 
         # code in simulation!
         cset=c.couplings(None)
     else: 
         cset=None
         
     # Need to check that the current top level model name is in agreement 
     # with the official DRS names and sound a warning if not (as opposed 
     # to raising a form error and halting further progress)
     if c.isModel:
         modname = c.abbrev
         if modname not in modelnames:
             warning = \
             """
             Warning: The model name used does not match an official cmip5
             model name. Although this does not stop you from proceeding it 
             will mean that you will not be able to export your metadata to 
             CMIP5. If you believe your model name is correct, please get in 
             touch as we update our list of accepted names only periodically   
             """
             
         else:
             warning=False
     else:
             warning=False
             
     # Text for notification panel to alert user of new questions and the 
     # background behind this
     if c.isModel:
         helppaneltitle = "New Questions added"
         helppaneltext = \
         """         
         
         These additional questions have been established by the Lead Authors 
         of IPCC AR5 Chap 9 on "Model evaluation" and in consultation with 
         the community to inform the model development process of CMIP5. These 
         will help document: <br/> 
         1) the model development path, <br/> 
         2) the tuning process, and  <br/>
         3) the conservation of integral properties. <br/> 
         
         It is expected that answers provided before July 15th 2012  will be 
         reflected in the second order draft of the AR5 report.
         
         """ 
     else:
         helppaneltitle = ''
         helppaneltext = ''
     
     
     logging.debug('Finished handling %s to component %s'%(request.method,c.id))
     return render_to_response('componentMain.html',
             {'c':c,'refs':refs,'inps':inps,
             'cform':cform,'pform':pform,
             'navtree':navtree.html,
             'urls':urls,
             'isRealm':c.isRealm,
             'isModel':c.isModel,
             'isParamGroup':c.isParamGroup,
             'cset':cset,
             'warning':warning,
             'helppaneltitle': helppaneltitle,
             'helppaneltext': helppaneltext,
             'tabs':tabs(request, self.centre_id, 'Model', self.component.model.id),
             'notAjax':not request.is_ajax()})
Пример #3
0
    def __handle(self, request, s, e, url, label):
        """
        This method handles the form itself for both the add and edit methods
        """

        logging.debug("entering simulation handle routine")

        if s.ensembleMembers > 1:
            eset = s.ensemble_set.all()
            assert (len(eset) == 1, "There can only be one ensemble set for %s" % s)
            members = eset[0].ensemblemember_set.all()
            ensemble = {"set": eset[0], "members": members}
        else:
            ensemble = None

        urls = {"url": url}
        if label == "Update":
            urls["ic"] = reverse(
                "pimms.apps.qn.views.assign", args=(self.centreid, "initialcondition", "simulation", s.id)
            )
            urls["bc"] = reverse("pimms.apps.qn.views.simulationCup", args=(self.centreid, s.id))
            urls["con"] = reverse("pimms.apps.qn.views.conformanceMain", args=(self.centreid, s.id))
            urls["ens"] = reverse("pimms.apps.qn.views.ensemble", args=(self.centreid, s.id))
            urls["mod"] = reverse("pimms.apps.qn.views.assign", args=(self.centreid, "modelmod", "simulation", s.id))
            urls = commonURLs(s, urls)
            # dont think we should be able to get to input mods from here ...
            # urls['ics']=reverse('pimms.apps.qn.views.assign',
            #         args=(self.centreid,'inputmod','simulation',s.id,))

        # A the moment we're only assuming one related simulation so we don't
        # have to deal with a formset
        rsims = s.related_from.all()
        if len(rsims):
            r = rsims[0]
        else:
            r = None

        if request.method == "POST":
            # do the simualation first ...
            simform = SimulationForm(request.POST, instance=s, prefix="sim")
            simform.specialise(self.centre)
            if simform.is_valid():
                simok = True
                if label == "Add":
                    oldmodel = None
                    olddrs = None
                    oldstartyear = None
                else:
                    oldmodel = s.numericalModel
                    olddrs = s.drsMember
                    oldstartyear = s.duration.startDate.year

                news = simform.save()
                logging.debug("model before %s, after %s" % (oldmodel, news.numericalModel))

                if news.numericalModel != oldmodel:
                    news.resetConformances()
                    news.resetCoupling()
                    news.updateDRS()

                logging.debug("drs before %s, after %s" % (olddrs, news.drsMember))

                # making sure the original sim has a drsoutput
                if s.drsOutput.all().count() == 0:
                    s.updateDRS()

                # update the drsouput if drsmember changes or new sim created
                if news.drsMember != olddrs:
                    news.updateDRS()

                # update the start year if necessary
                if news.duration.startDate.year != oldstartyear:
                    news.updateDRS()

            elif not simform.is_valid():
                simok = False
                logging.info("SIMFORM not valid [%s]" % simform.errors)
            relform = SimRelationshipForm(s, request.POST, instance=r, prefix="rel")

            if relform.is_valid():
                if simok:
                    r = relform.save()
                    return HttpResponseRedirect(news.edit_url())
            else:
                # if there is no sto, then we should delete this relationship instance and move on.
                pass

            # generate a drs string instance in DRS Output class

        else:
            relform = SimRelationshipForm(s, instance=r, prefix="rel")
            simform = SimulationForm(instance=s, prefix="sim")
            simform.specialise(self.centre)

        # work out what we want to say about couplings
        cset = []
        if label != "Add":
            cset = s.numericalModel.couplings(s)
        for i in cset:
            i.valid = (
                len(i.internalclosure_set.all()) + len(i.externalclosure_set.all()) > 0
            )  # need at least one closure

        # now work out what we want to say about conformances.
        cs = Conformance.objects.filter(simulation=s)

        return render_to_response(
            "simulation.html",
            {
                "s": s,
                "simform": simform,
                "urls": urls,
                "label": label,
                "exp": e,
                "cset": cset,
                "coset": cs,
                "ensemble": ensemble,
                "rform": relform,
                "tabs": tabs(request, self.centreid, "Simulation", s.id or 0),
            },
        )