def __init__(self, mymodel, *args, **kwargs): try: numorder = kwargs['num'] del kwargs['num'] except KeyError: numorder = 5 try: directionWidget = kwargs['dwidget'] del kwargs['dwidget'] except KeyError: directionWidget = forms.RadioSelect forms.Form.__init__(self, *args, **kwargs) self.model = mymodel sortingfields = [] for x in modelFields(mymodel): if maskField(x): pass elif ordinalField(self.model, x): if x.verbose_name != "": sortingfields.append(x) if len(sortingfields) > 1: datachoices = (tuple((None, 'None') for x in [1]) + tuple((x.name, x.verbose_name) for x in sortingfields)) for order in range(1, numorder + 1): self.fields['order' + str(order)] = forms.ChoiceField(choices=datachoices, initial=datachoices[0][0], required=True) self.fields['direction' + str(order)] = forms.ChoiceField(choices=(('ASC', 'Ascending'), ('DESC', 'Descending')), widget=directionWidget, initial='ASC', required=True)
def __init__(self, mymodel, *args, **kwargs): try: numorder = kwargs['num'] del kwargs['num'] except KeyError: numorder = 5 try: directionWidget = kwargs['dwidget'] del kwargs['dwidget'] except KeyError: directionWidget = forms.RadioSelect forms.Form.__init__(self, *args, **kwargs) self.model = mymodel sortingfields = [] for x in modelFields(mymodel): if maskField(x): pass elif ordinalField(self.model, x): if x.verbose_name != "": sortingfields.append(x) if len(sortingfields) > 1: datachoices = (tuple((None, 'None') for x in [1]) + tuple( (x.name, x.verbose_name) for x in sortingfields)) for order in range(1, numorder + 1): self.fields['order' + str(order)] = forms.ChoiceField( choices=datachoices, initial=datachoices[0][0], required=True) self.fields['direction' + str(order)] = forms.ChoiceField( choices=(('ASC', 'Ascending'), ('DESC', 'Descending')), widget=directionWidget, initial='ASC', required=True)
def __init__(self, mymodel, *args, **kwargs): enumerableFields = kwargs.pop('enumerableFields', None) forms.Form.__init__(self, *args, **kwargs) self.model = mymodel for field in modelFields(mymodel): self.fields.update(editFormFields(mymodel, field, enumerableFields))
def __init__(self, mymodel, *args, **kwargs): enumerableFields = kwargs.pop('enumerableFields', None) queryGenerator = kwargs.pop('queryGenerator', None) forms.Form.__init__(self, *args, **kwargs) self.model = mymodel for field in modelFields(mymodel): self.fields.update(searchFormFields(mymodel, field, enumerableFields, queryGenerator=queryGenerator))
def as_table(self, expert=False): output = [] fieldmap = dict([(f.name, f) for f in modelFields(self.model)]) print(self.model) for ffield in self.fields: if ffield.endswith('_operator'): basename = ffield[:-(len('_operator'))] loname = basename + '_lo' hiname = basename + '_hi' ## this code is pretty confused, need to clean up try: ## virtual field branch mfield = fieldmap[basename] except KeyError: ## foreign key branch mfield = fieldmap[basename.split('__')[0]] ofield = forms.forms.BoundField(self, self.fields[ffield], ffield) if not expert: ofield = ofield.as_hidden() if (loname in self.fields) and (hiname in self.fields): fieldlo = forms.forms.BoundField(self, self.fields[loname], loname) fieldhi = forms.forms.BoundField(self, self.fields[hiname], hiname) row = (u'<tr><td style="text-align:right;"><label for="%(fieldid)s">%(label)s</label></td><td>%(ofield)s</td>' % {'label': unicode(mfield.verbose_name), #'label': unicode(basename), 'ofield': unicode(ofield), 'fieldid': unicode(fieldlo.auto_id) }) row = (row + u'<td>%(fieldlo)s</td><td><label for="%(fieldhi_id)s">up to</label></td><td>%(fieldhi)s</td>' % {'fieldlo': unicode(fieldlo), 'fieldhi': unicode(fieldhi), 'fieldhi_id': unicode(fieldhi.auto_id)}) else: bfield = forms.forms.BoundField(self, self.fields[basename], basename) row = (u'<tr><td style="text-align:right;"><label for="%(fieldid)s">%(label)s</label></td><td>%(ofield)s</td>' % {'label': unicode(mfield.verbose_name), # 'label': unicode(basename), 'ofield': unicode(ofield), # 'fieldid': unicode(ofield.auto_id[:-9]) 'fieldid': unicode(bfield.auto_id) }) row = (row + u'<td colspan=3>%(field)s</td>' % {'field': unicode(bfield)}) if row is not None: row = row + u'</tr>' output.append(row) return mark_safe(u'\n'.join(output))
def __init__(self, mymodel, *args, **kwargs): enumerableFields = kwargs.pop('enumerableFields', None) queryGenerator = kwargs.pop('queryGenerator', None) forms.Form.__init__(self, *args, **kwargs) self.model = mymodel for field in modelFields(mymodel): self.fields.update( searchFormFields(mymodel, field, enumerableFields, queryGenerator=queryGenerator))
def toFieldName(model): """ """ ## the to_field is a difficult problem ## it controls what the options are to be selected ## ideally that would be the same as what the user sees ## but that is __unicode__(). So potentially there is ## a mismatch. ## If the __unicode__ is just a particular field, we can ## make it work. Below it is hardcoded if the model has ## a field "name" (cheesy), otherwise defaults to pk try: to_field_name = settingsForModel(settings.XGDS_DATA_TO_FIELD_NAME, model)[0] except (AttributeError, IndexError): to_field_name = None if not to_field_name: fieldnames = [x.name for x in modelFields(model)] if 'name' in fieldnames: return 'name' else: to_field_name = None # pk(field.rel.to).name return to_field_name
hibound = nextPercentile(model, fld, loend, 'gt') return [lobound, hibound] if __name__ == "__main__": if len(sys.argv) <= 1: print("Usage: DataStatistics.py moduleName [modelName] [statistic]") quit() try: models = [resolveModel(sys.argv[1], sys.argv[2])] except IndexError: models = getModels(sys.argv[1]) try: statistic = sys.argv[3] except IndexError: statistic = 'count' for m in models: if not isAbstract(m): if statistic == 'count': print(m, getStatistic(m, '', 'count', m.objects.all().count)) elif statistic == 'StdDev': for f in modelFields(m): if isNumeric(m, f): fn = lambda: m.objects.all().aggregate(StdDev(f.name) ).values()[0] print(m, f, getStatistic(m, f.name, 'StdDev', fn))
def as_table(self, expert=False): output = [] fieldmap = dict([(f.name, f) for f in modelFields(self.model)]) print(self.model) for ffield in self.fields: if ffield.endswith('_operator'): basename = ffield[:-(len('_operator'))] loname = basename + '_lo' hiname = basename + '_hi' ## this code is pretty confused, need to clean up try: ## virtual field branch mfield = fieldmap[basename] except KeyError: ## foreign key branch mfield = fieldmap[basename.split('__')[0]] ofield = forms.forms.BoundField(self, self.fields[ffield], ffield) if not expert: ofield = ofield.as_hidden() if (loname in self.fields) and (hiname in self.fields): fieldlo = forms.forms.BoundField(self, self.fields[loname], loname) fieldhi = forms.forms.BoundField(self, self.fields[hiname], hiname) row = ( u'<tr><td style="text-align:right;"><label for="%(fieldid)s">%(label)s</label></td><td>%(ofield)s</td>' % { 'label': unicode(mfield.verbose_name), #'label': unicode(basename), 'ofield': unicode(ofield), 'fieldid': unicode(fieldlo.auto_id) }) row = ( row + u'<td>%(fieldlo)s</td><td><label for="%(fieldhi_id)s">up to</label></td><td>%(fieldhi)s</td>' % { 'fieldlo': unicode(fieldlo), 'fieldhi': unicode(fieldhi), 'fieldhi_id': unicode(fieldhi.auto_id) }) else: bfield = forms.forms.BoundField(self, self.fields[basename], basename) row = ( u'<tr><td style="text-align:right;"><label for="%(fieldid)s">%(label)s</label></td><td>%(ofield)s</td>' % { 'label': unicode(mfield.verbose_name), # 'label': unicode(basename), 'ofield': unicode(ofield), # 'fieldid': unicode(ofield.auto_id[:-9]) 'fieldid': unicode(bfield.auto_id) }) row = (row + u'<td colspan=3>%(field)s</td>' % { 'field': unicode(bfield) }) if row is not None: row = row + u'</tr>' output.append(row) return mark_safe(u'\n'.join(output))
else: hibound = nextPercentile(model, fld, loend, 'gt') return [lobound, hibound] if __name__ == "__main__": if len(sys.argv) <= 1: print("Usage: DataStatistics.py moduleName [modelName] [statistic]") quit() try: models = [resolveModel(sys.argv[1], sys.argv[2])] except IndexError: models = getModels(sys.argv[1]) try: statistic = sys.argv[3] except IndexError: statistic = 'count' for m in models: if not isAbstract(m): if statistic == 'count': print(m, getStatistic(m, '', 'count', m.objects.all().count)) elif statistic == 'StdDev': for f in modelFields(m): if isNumeric(m, f): fn = lambda: m.objects.all().aggregate(StdDev(f.name)).values()[0] print(m, f, getStatistic(m, f.name, 'StdDev', fn))