def aggregate(self): cube = Cube() estimates = {} for expenditure in self.group.filter(source=Expenditure.SOURCE_JCT): for estimate in expenditure.estimate_set.all(): estimates[estimate.estimate_year] = estimate for year in estimates: estimate = estimates[year] if estimate.corporations_amount != None: cube.add( { 'year': estimate.estimate_year, 'source': expenditure.source, 'recipient': 'corporation' }, estimate.corporations_amount) if estimate.individuals_amount != None: cube.add( { 'year': estimate.estimate_year, 'source': expenditure.source, 'recipient': 'individual' }, estimate.individuals_amount) for expenditure in self.group.filter( source=Expenditure.SOURCE_TREASURY): for estimate in expenditure.estimate_set.all(): estimates[estimate.estimate_year] = estimate for year in estimates: estimate = estimates[year] if estimate.corporations_amount != None: cube.add( { 'year': estimate.estimate_year, 'source': expenditure.source, 'recipient': 'corporation' }, estimate.corporations_amount) if estimate.individuals_amount != None: cube.add( { 'year': estimate.estimate_year, 'source': expenditure.source, 'recipient': 'individual' }, estimate.individuals_amount) return cube
def calc_summary(self): self.groupsummary_set.all().delete() cube = Cube() current_expenditure_year = TE_CURRENT_YEAR for group in Group.objects.filter(parent=self): cube += group.calc_summary() for expenditure in self.expenditure_set.all(): if expenditure.analysis_year < current_expenditure_year: try: estimate = Estimate.objects.get( expenditure=expenditure, estimate_year=expenditure.analysis_year - 2) cube.add( { 'source': expenditure.source, 'analysis_year': expenditure.analysis_year, 'estimate_year': estimate.estimate_year, 'estimate': GroupSummary.ESTIMATE_CORPORATIONS }, { 'amount': estimate.corporations_amount, 'notes': estimate.corporations_notes }) cube.add( { 'source': expenditure.source, 'analysis_year': expenditure.analysis_year, 'estimate_year': estimate.estimate_year, 'estimate': GroupSummary.ESTIMATE_INDIVIDUALS }, { 'amount': estimate.individuals_amount, 'notes': estimate.individuals_notes }) except ObjectDoesNotExist: pass elif expenditure.analysis_year == current_expenditure_year: for year in range(current_expenditure_year - 2, current_expenditure_year + 5): try: estimate = Estimate.objects.get( expenditure=expenditure, estimate_year=year) cube.add( { 'source': expenditure.source, 'analysis_year': expenditure.analysis_year, 'estimate_year': estimate.estimate_year, 'estimate': GroupSummary.ESTIMATE_CORPORATIONS }, { 'amount': estimate.corporations_amount, 'notes': estimate.corporations_notes }) cube.add( { 'source': expenditure.source, 'analysis_year': expenditure.analysis_year, 'estimate_year': estimate.estimate_year, 'estimate': GroupSummary.ESTIMATE_INDIVIDUALS }, { 'amount': estimate.individuals_amount, 'notes': estimate.individuals_notes }) except ObjectDoesNotExist: pass for estimate_year in TE_YEARS: if cube.dimensions.has_key('estimate_year') and cube.dimensions[ 'estimate_year'].values.has_key(estimate_year): results_corp = cube.query(attributes={ 'estimate_year': estimate_year, 'estimate': GroupSummary.ESTIMATE_CORPORATIONS }, groups=['source']) results_indv = cube.query(attributes={ 'estimate_year': estimate_year, 'estimate': GroupSummary.ESTIMATE_INDIVIDUALS }, groups=['source']) results_comb = cube.query( attributes={'estimate_year': estimate_year}, groups=['source']) if cube.dimensions.has_key( 'source') and cube.dimensions['source'].values.has_key( Expenditure.SOURCE_JCT): create_summary_object(results_corp, self, estimate_year, Expenditure.SOURCE_JCT, GroupSummary.ESTIMATE_CORPORATIONS) create_summary_object(results_indv, self, estimate_year, Expenditure.SOURCE_JCT, GroupSummary.ESTIMATE_INDIVIDUALS) create_summary_object(results_comb, self, estimate_year, Expenditure.SOURCE_JCT, GroupSummary.ESTIMATE_COMBINED) if cube.dimensions.has_key( 'source') and cube.dimensions['source'].values.has_key( Expenditure.SOURCE_TREASURY): create_summary_object(results_corp, self, estimate_year, Expenditure.SOURCE_TREASURY, GroupSummary.ESTIMATE_CORPORATIONS) create_summary_object(results_indv, self, estimate_year, Expenditure.SOURCE_TREASURY, GroupSummary.ESTIMATE_INDIVIDUALS) create_summary_object(results_comb, self, estimate_year, Expenditure.SOURCE_TREASURY, GroupSummary.ESTIMATE_COMBINED) return cube
def calc_detail(self): self.groupdetail_set.all().delete() cube = Cube() for group in Group.objects.filter(parent=self): cube += group.calc_detail() for expenditure in self.expenditure_set.all(): for estimate in expenditure.estimate_set.all(): cube.add( { 'source': expenditure.source, 'analysis_year': expenditure.analysis_year, 'estimate_year': estimate.estimate_year, 'estimate': GroupSummary.ESTIMATE_CORPORATIONS }, { 'amount': estimate.corporations_amount, 'notes': estimate.corporations_notes, 'group': self, 'expenditure': expenditure }) cube.add( { 'source': expenditure.source, 'analysis_year': expenditure.analysis_year, 'estimate_year': estimate.estimate_year, 'estimate': GroupSummary.ESTIMATE_INDIVIDUALS }, { 'amount': estimate.individuals_amount, 'notes': estimate.individuals_notes, 'group': self, 'expenditure': expenditure }) for analysis_year in TE_YEARS: if cube.dimensions.has_key('analysis_year') and cube.dimensions[ 'analysis_year'].values.has_key(analysis_year): results_sources = cube.query( attributes={'analysis_year': analysis_year}, groups=['source']) if cube.dimensions.has_key( 'source') and cube.dimensions['source'].values.has_key( Expenditure.SOURCE_JCT): create_detail_report_object(results_sources, self, analysis_year, Expenditure.SOURCE_JCT) if cube.dimensions.has_key( 'source') and cube.dimensions['source'].values.has_key( Expenditure.SOURCE_TREASURY): create_detail_report_object(results_sources, self, analysis_year, Expenditure.SOURCE_TREASURY) for estimate_year in TE_YEARS: if cube.dimensions.has_key( 'estimate_year' ) and cube.dimensions['estimate_year'].values.has_key( estimate_year) and cube.dimensions.has_key( 'analysis_year') and cube.dimensions[ 'analysis_year'].values.has_key(analysis_year): results_corp = cube.query(attributes={ 'analysis_year': analysis_year, 'estimate_year': estimate_year, 'estimate': GroupSummary.ESTIMATE_CORPORATIONS }, groups=['source']) results_indv = cube.query(attributes={ 'analysis_year': analysis_year, 'estimate_year': estimate_year, 'estimate': GroupSummary.ESTIMATE_INDIVIDUALS }, groups=['source']) results_comb = cube.query(attributes={ 'analysis_year': analysis_year, 'estimate_year': estimate_year }, groups=['source']) if cube.dimensions.has_key('source') and cube.dimensions[ 'source'].values.has_key(Expenditure.SOURCE_JCT): create_detail_object( results_corp, self, estimate_year, analysis_year, Expenditure.SOURCE_JCT, GroupSummary.ESTIMATE_CORPORATIONS) create_detail_object(results_indv, self, estimate_year, analysis_year, Expenditure.SOURCE_JCT, GroupSummary.ESTIMATE_INDIVIDUALS) create_detail_object(results_comb, self, estimate_year, analysis_year, Expenditure.SOURCE_JCT, GroupSummary.ESTIMATE_COMBINED) if cube.dimensions.has_key('source') and cube.dimensions[ 'source'].values.has_key( Expenditure.SOURCE_TREASURY): create_detail_object( results_corp, self, estimate_year, analysis_year, Expenditure.SOURCE_TREASURY, GroupSummary.ESTIMATE_CORPORATIONS) create_detail_object(results_indv, self, estimate_year, analysis_year, Expenditure.SOURCE_TREASURY, GroupSummary.ESTIMATE_INDIVIDUALS) create_detail_object(results_comb, self, estimate_year, analysis_year, Expenditure.SOURCE_TREASURY, GroupSummary.ESTIMATE_COMBINED) return cube