Package madrona :: Package analysistools :: Module models
[hide private]

Source Code for Module madrona.analysistools.models

  1  from django.contrib.gis.db import models 
  2  from django.conf import settings 
  3  from madrona.features.models import Feature, FeatureForm 
  4  from madrona.common.utils import get_class 
  5  from madrona.features import register 
  6  from django.contrib.gis.geos import GEOSGeometry  
  7  from madrona.common.utils import asKml 
  8  import os 
9 10 -class Analysis(Feature):
11 """ 12 Abstract Feature model representing the inputs and outputs 13 of an analysis or modeling run 14 """ 15 16 @property
17 - def kml(self):
18 # Note: if you want network links, return None here and 19 # use kml_full instead (kml_done wrapped in a full kml Document) 20 if self.done: 21 return self.kml_done 22 else: 23 return self.kml_working
24 25 @property
26 - def kml_done(self):
27 """ 28 Translate the model outputs to KML Placemark or Folder 29 """ 30 return """ 31 <Placemark id="%s"> 32 <visibility>0</visibility> 33 <name>%s</name> 34 </Placemark> 35 """ % (self.uid, self.name)
36 37 @property
38 - def kml_working(self):
39 """ 40 Translate the model outputs to KML Placemark or Folder 41 """ 42 return """ 43 <Placemark id="%s"> 44 <visibility>0</visibility> 45 <name>%s (Working...)</name> 46 </Placemark> 47 """ % (self.uid, self.name)
48 49 @property
50 - def kml_style(self):
51 return ""
52 53 @classmethod
54 - def input_fields(klass):
55 return [f for f in klass._meta.fields if f.attname.startswith('input_')]
56 57 @classmethod
58 - def input_manytomany_fields(klass):
59 return [f for f in klass._meta.many_to_many if f.attname.startswith('input_')]
60 61 @property
62 - def inputs(self):
63 """ 64 Returns a dict of all input parameters 65 """ 66 odict = {} 67 for f in self.input_fields(): 68 odict[f.verbose_name] = self._get_FIELD_display(f) 69 return odict
70 71 @classmethod
72 - def output_fields(klass):
73 return [f for f in klass._meta.fields if f.attname.startswith('output_')]
74 75 @property
76 - def outputs(self):
77 """ 78 Returns a dict of all output parameters 79 If processing is incomplete, values will be None 80 """ 81 odict = {} 82 for f in self.output_fields(): 83 odict[f.verbose_name] = self._get_FIELD_display(f) 84 return odict
85 86 @property
87 - def status_html(self):
88 if self.done: 89 return "<p>All done</p>" 90 else: 91 return "<p>Not done yet; output fields are still blank"
92 93 @property
94 - def progress(self):
95 """ 96 How many sub-tasks completed out of a total 97 e.g. (3,6) means 3 out of 6 pieces are complete so progress bar can show 50% 98 """ 99 return (1,1)
100 101 @property
102 - def done(self):
103 """ 104 If it's asynchronously processed, this is the definitive 105 property to determine if processing is completed. 106 """ 107 # For now just check that the outputs are not None 108 for of in self.outputs.keys(): 109 if self.outputs[of] is None: 110 return False 111 return True
112
113 - def run(self):
114 """ 115 Method to execute the model. 116 Passes all input parameters to the analysis backend, 117 takes the results and stores in the model output fields. 118 """ 119 pass
120
121 - def clear_output_fields(self):
122 """ 123 Reset button; Sets all output fields to None 124 """ 125 for f in self.output_fields(): 126 self.__dict__[f.attname] = None
127 128 ''' 129 Note on keyword args rerun and form: these are extracted from kwargs so that they will not cause an unexpected 130 keyword argument error during call to super.save 131 Note on rerun: When set to false no output fields will be cleared and the run method will not be called 132 Note on form: This is passed from feature.views update and create methods. In the case of m2m fields this needs to 133 be called after super.save. Since it also needs to be called before self.run, it will be passed from here (in kwargs) 134 to the superclass (Feature) save method. 135 (rather than its previous location in feature views update and create (after save has completed)) 136 '''
137 - def save(self, rerun=True, *args, **kwargs):
138 if rerun: 139 self.clear_output_fields() # get rid of old outputs 140 super(Analysis, self).save(*args, **kwargs) # have to save first so it has a pk 141 self.run() 142 super(Analysis, self).save(*args, **kwargs) 143 else: 144 super(Analysis, self).save(*args, **kwargs) # have to save first so it has a pk
145
146 - class Meta:
147 abstract = True
148