def get_field_list(self, obj): from django.utils.datastructures import SortedDict form = self.form(instance=obj) opts = obj._meta fields = SortedDict() objs = SortedDict() for field in self._fields: _obj = {} if not field.name in self.exclude: data = { 'name': field.name, 'is_link': False, 'value': getattr(obj,field.name), 'link': None, 'secure': False, } if isinstance(field.widget, forms.PasswordInput): data['secure'] = True if isinstance(field, ForeignKey): data['is_link'] = True data['link'] = u'/mds/web/{model}/{uuid}/'.format( model=field.model, uuid=unicode(getattr(obj,field.name) )) elif isinstance(field, FileField): data['is_link'] = True data['link'] = u'/mds/media/{path}'.format( path=unicode(getattr(obj,field.name) )) _obj[field.name] = data objs.append(_obj) return tuple(fields)
def _parse_sig(sig, arg_names, validate=False): """ Parses signatures into a ``SortedDict`` of paramName => type. Numerically-indexed arguments that do not correspond to an argument name in python (ie: it takes a variable number of arguments) will be keyed as the stringified version of it's index. sig the signature to be parsed arg_names a list of argument names extracted from python source Returns a tuple of (method name, types dict, return type) """ d = SIG_RE.match(sig) if not d: raise ValueError('Invalid method signature %s' % sig) d = d.groupdict() ret = [(n, Any) for n in arg_names] if 'args_sig' in d and type( d['args_sig']) is str and d['args_sig'].strip(): for i, arg in enumerate(d['args_sig'].strip().split(',')): _type_checking_available(sig, validate) if '=' in arg: if not type(ret) is SortedDict: ret = SortedDict(ret) dk = KWARG_RE.match(arg) if not dk: raise ValueError('Could not parse arg type %s in %s' % (arg, sig)) dk = dk.groupdict() if not sum([(k in dk and type(dk[k]) is str and bool(dk[k].strip())) for k in ('arg_name', 'arg_type')]): raise ValueError('Invalid kwarg value %s in %s' % (arg, sig)) ret[dk['arg_name']] = _eval_arg_type(dk['arg_type'], None, arg, sig) else: if type(ret) is SortedDict: raise ValueError('Positional arguments must occur ' 'before keyword arguments in %s' % sig) if len(ret) < i + 1: ret.append((str(i), _eval_arg_type(arg, None, arg, sig))) else: ret[i] = (ret[i][0], _eval_arg_type(arg, None, arg, sig)) if not type(ret) is SortedDict: ret = SortedDict(ret) return (d['method_name'], ret, (_eval_arg_type( d['return_sig'], Any, 'return', sig) if d['return_sig'] else Any))
def _parse_sig(sig, arg_names, validate=False): """ Parses signatures into a ``SortedDict`` of paramName => type. Numerically-indexed arguments that do not correspond to an argument name in python (ie: it takes a variable number of arguments) will be keyed as the stringified version of it's index. sig the signature to be parsed arg_names a list of argument names extracted from python source Returns a tuple of (method name, types dict, return type) """ d = SIG_RE.match(sig) if not d: raise ValueError('Invalid method signature %s' % sig) d = d.groupdict() ret = [(n, Any) for n in arg_names] if 'args_sig' in d and type(d['args_sig']) is str and d['args_sig'].strip(): for i, arg in enumerate(d['args_sig'].strip().split(',')): _type_checking_available(sig, validate) if '=' in arg: if not type(ret) is SortedDict: ret = SortedDict(ret) dk = KWARG_RE.match(arg) if not dk: raise ValueError('Could not parse arg type %s in %s' % (arg, sig)) dk = dk.groupdict() if not sum([(k in dk and type(dk[k]) is str and bool(dk[k].strip())) for k in ('arg_name', 'arg_type')]): raise ValueError('Invalid kwarg value %s in %s' % (arg, sig)) ret[dk['arg_name']] = _eval_arg_type(dk['arg_type'], None, arg, sig) else: if type(ret) is SortedDict: raise ValueError('Positional arguments must occur ' 'before keyword arguments in %s' % sig) if len(ret) < i + 1: ret.append((str(i), _eval_arg_type(arg, None, arg, sig))) else: ret[i] = (ret[i][0], _eval_arg_type(arg, None, arg, sig)) if not type(ret) is SortedDict: ret = SortedDict(ret) return (d['method_name'], ret, (_eval_arg_type(d['return_sig'], Any, 'return', sig) if d['return_sig'] else Any))
def aggregate_report(cls, scope = None,location_type = None, location_name = None, startmonth_id = None, endmonth_id = None, group = None): # entries in the scope of the webuser entries = scope.entries() webuser_location = scope.location # filter only confirmed entries if webuser_location.type.name.lower() == "zone": entries = filter(lambda entries: entries.confirmed_by_woreda == True, entries) elif webuser_location.type.name.lower() == "region": entries = filter(lambda entries: entries.confirmed_by_zone == True, entries) elif webuser_location.type.name.lower() == "federal": entries = filter(lambda entries: entries.confirmed_by_region == True, entries) elif webuser_location.type.name.lower() == "woreda": # show all the entries entries = entries else: entries = filter(lambda entries: entries.confirmed_by_region == True, entries) if location_type != "" and location_name != "": # Get all locations in the specified location loc_selected = HealthPost.objects.filter( name = location_name, type__name = location_type)[0] descendent_location = loc_selected.descendants(include_self = True) descendent_location_code = [des_loc.code for des_loc in descendent_location] entries = filter(lambda entries: entries.supply_place.location.code in descendent_location_code, entries) # if time range is given, filter the entries based on the range ## if startmonth_id != "" and startmonth_id is not None: ## # Get the period range from the given date from ReportPeriod ## startmonth_start_date = ReportPeriod.objects.filter(id = startmonth_id)[0].start_date ## if endmonth_id != "": ## #endmonth_start_date = ReportPeriod.objects.filter(id = endmonth_id)[0].start_date ## endmonth_end_date = ReportPeriod.objects.filter(id = endmonth_id)[0].end_date ## entries = filter(lambda entries: startmonth_start_date <= entries.time and ## entries.time <= endmonth_end_date, entries) ## else: ## startmonth_end_date = ReportPeriod.objects.filter(id = startmonth_id)[0].end_date ## entries = filter(lambda entries: startmonth_start_date <= entries.time and ## entries.time <= startmonth_end_date , entries) if startmonth_id != "" and startmonth_id is not None: if endmonth_id != "" and endmonth_id is not None: entries = filter(lambda entries: int(startmonth_id) <= entries.report_period.id and entries.report_period.id <= int(endmonth_id), entries) else: entries = filter(lambda entries: entries.report_period.id == int(startmonth_id), entries) # if group by option is given, it will aggregate the report if group is not None: results = SortedDict() # locations in the specified group location locations = HealthPost.list_by_location(group) for key in ['quantity', 'consumption', 'balance']: results[key] = None # First filter the entries based on the above parameter and scope # if type cast is possible, we can simply cast the entries list to # queryset to apply aggregate function entry_ids = [entry.id for entry in entries] entries_filtered = Entry.objects.filter(id__in = entry_ids, supply_place__location__in = locations) agg_queryset = entries_filtered.aggregate(quantity = Sum('quantity'), consumption = Sum('consumption'), balance = Sum('balance')) for key, value in agg_queryset.items(): results[key] = value results['complete'] = True return results else: results = [] if len(entries) == 0: return results for entry in entries: result = SortedDict() result['supply_place'] = entry.supply_place #result['item_name'] = entry.supply_place.supply.name result['quantity'] = entry.quantity result['consumption'] = entry.consumption result['balance'] = entry.balance result['Reported_by'] = "%s %s" % (entry.rutf_reporter.first_name, entry.rutf_reporter.last_name) result['month'] = "%s %s" % (entry.report_period.month,entry.report_period.year) # it lists entries. each entry is complete #result['complete'] = True results.append(result) return results