Created
April 22, 2014 11:47
-
-
Save ssbb/11175637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def build(self): | |
rows = [] | |
def force_number(val): | |
try: | |
return float(val) | |
except: | |
return 0 | |
users = WaterUser.objects.values( | |
'id', 'guiv', 'name', 'inn') | |
for user in users: | |
withdrawn = dict(self.base_qs1.filter( | |
journal_document__water_user=user.get('id') | |
).values_list('water_object__full_name').annotate( | |
Sum('year_volume'))) | |
drop = dict(self.base_qs2.filter( | |
journal_document__water_user=user.get('id') | |
).values_list('water_object__full_name').annotate( | |
Sum('year_allocated'))) | |
keys = set(withdrawn.keys() + drop.keys()) | |
user_rows = {k: { | |
'drop': drop.get(k, ''), | |
'withdrawn': withdrawn.get(k, '')} for k in keys} | |
user_cells = [ | |
user.get('guiv'), | |
user.get('name'), | |
user.get('inn'), | |
] | |
index = 0 | |
for wo, volume in user_rows.items(): | |
if index > 0: | |
user_cells = [Cell('', colspan=3)] | |
vol_withdrawn = self.format_number(volume.get('withdrawn')) | |
vol_drop = self.format_number(volume.get('drop')) | |
if (force_number(vol_withdrawn) >= self.minimum_withdrawal) or \ | |
(force_number(vol_drop) >= self.minimum_drop): | |
rows.append(user_cells + [ | |
vol_withdrawn, | |
vol_drop, | |
wo if volume.get('withdrawn') else '', | |
wo if volume.get('drop') else '', | |
]) | |
index += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment