Skip to content

Commit

Permalink
Merge pull request #509 from hyanwong/image-adding
Browse files Browse the repository at this point in the history
Fix some donor_list issues
  • Loading branch information
hyanwong committed Feb 6, 2022
2 parents 7b51aed + 1e032b2 commit 1addda7
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 77 deletions.
79 changes: 59 additions & 20 deletions controllers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
sponsorship_restrict_contact, sponsor_renew_request_logic,
sponsorship_config, sponsorable_children_query)

from usernames import usernames_associated_to_email
from usernames import usernames_associated_to_email, donor_name_for_username

from partners import partner_identifiers_for_reservation_name

Expand Down Expand Up @@ -1068,50 +1068,89 @@ def donor_list():
grouped_img_src = "GROUP_CONCAT(if(`user_nondefault_image`,`verified_preferred_image_src`,NULL))"
grouped_img_src_id = "GROUP_CONCAT(if(`user_nondefault_image`,`verified_preferred_image_src_id`,NULL))"
grouped_otts = "GROUP_CONCAT(`OTT_ID`)"
grouped_donor_names = "GROUP_CONCAT(`verified_donor_name`)"
sum_paid = "COALESCE(SUM(`user_paid`),0)"
n_leaves = "COUNT(1)"
groupby = "IFNULL(verified_donor_name,id)"
groupby = "username"
limitby=(page*items_per_page,(page+1)*items_per_page+1)
curr_rows = db(
((db.reservations.user_donor_hide == None) | (db.reservations.user_donor_hide != True)) &
(db.reservations.verified_time != None)
donor_rows = []
max_groupby = 75 # The max number of sponsorships per username
for r in db(
((db.reservations.user_donor_hide == None) | (db.reservations.user_donor_hide == False)) &
(db.reservations.verified_time != None) &
(db.reservations.username != None)
).select(
grouped_img_src,
grouped_img_src_id,
grouped_otts,
grouped_otts,
grouped_donor_names,
sum_paid,
n_leaves,
db.reservations.verified_donor_name,
#the following fields are only of use for single displayed donors
db.reservations.name,
db.reservations.user_nondefault_image,
db.reservations.username,
#the following fields are only of use for single displayed donors
db.reservations.verified_kind,
db.reservations.verified_name,
db.reservations.verified_more_info,
groupby=groupby, orderby=sum_paid + " DESC, verified_time, reserve_time",
limitby=limitby)
for r in curr_rows:
limitby=limitby,
):
# Only show max 75 sponsored species, to avoid clogging page and also because of
# a low default group_concat_max_len which will restrict the number of otts anyway
# (note, the number shown may be < 75 as ones without images are not thumbnailed)
r['otts'] = [int(ott) for i, ott in enumerate(r[grouped_otts].split(",")) if i < 75]
names_for = [r['otts'][0] for r in curr_rows if r[n_leaves]==1] #only get names etc for unary sponsors
_, donor_name = donor_name_for_username(r.reservations.username)
if donor_name:
num_sponsorships = r[n_leaves]
ott_enum = enumerate(r[grouped_otts].split(","))
img_src_enum = enumerate((r[grouped_img_src] or '').split(","))
img_src_id_enum = enumerate((r[grouped_img_src_id] or '').split(","))
donor_rows.append({
"donor_name": donor_name,
"use_otts": [int(ott) for i, ott in ott_enum if i < max_groupby],
"num_sponsorships": num_sponsorships,
"img_srcs": [x for i, x in img_src_enum if i < max_groupby],
"img_src_ids": [x for i, x in img_src_id_enum if i < max_groupby],
"sum_paid": r[sum_paid],
"verified_kind": r.reservations.verified_kind if num_sponsorships == 1 else None,
"verified_name": r.reservations.verified_name if num_sponsorships == 1 else None,
"verified_more_info": r.reservations.verified_name if num_sponsorships == 1 else None,
})
names_for = [r['use_otts'][0] for r in donor_rows if r["num_sponsorships"] == 1]
html_names = nice_name_from_otts(names_for, html=True, leaf_only=True, first_upper=True, break_line=2)
otts = [ott for r in curr_rows for ott in r['otts']]
otts = [ott for r in donor_rows for ott in r['use_otts']]
#store the default image info (e.g. to get thumbnails, attribute correctly etc)
default_images = {r.ott:r for r in db(db.images_by_ott.ott.belongs(otts) & (db.images_by_ott.overall_best_any==1)).select(db.images_by_ott.ott, db.images_by_ott.src, db.images_by_ott.src_id, db.images_by_ott.rights, db.images_by_ott.licence, orderby=~db.images_by_ott.src)}
images = {
r.ott:r
for r in db(
db.images_by_ott.ott.belongs(otts) & (db.images_by_ott.overall_best_any==1)
).select(
db.images_by_ott.ott,
db.images_by_ott.src,
db.images_by_ott.src_id,
db.images_by_ott.rights,
db.images_by_ott.licence,
orderby=~db.images_by_ott.src
)
}
#also look at the nondefault images if present
user_images = {}
for r in curr_rows:
for img_src, img_src_id in zip(
(r[grouped_img_src] or '').split(","), (r[grouped_img_src_id] or '').split(",")):
for r in donor_rows:
for ott, img_src, img_src_id in zip(r["use_otts"], r["img_srcs"], r["img_src_ids"]):
if img_src is not None and img_src_id is not None:
row = db((db.images_by_ott.src == img_src) & (db.images_by_ott.src_id == img_src_id)).select(
db.images_by_ott.ott, db.images_by_ott.src, db.images_by_ott.src_id,
db.images_by_ott.rights, db.images_by_ott.licence).first()
if row:
user_images[row.ott] = row
return dict(rows=curr_rows, n_col_name=n_leaves, otts_col_name='otts', paid_col_name=sum_paid, page=page, items_per_page=items_per_page, vars=request.vars, html_names=html_names, user_images=user_images, default_images=default_images)
images[row.ott] = row
return dict(
donor_rows=donor_rows,
images=images,
page=page,
items_per_page=items_per_page,
vars=request.vars,
html_names=html_names,
cutoffs=[1000000,1000,150,0], # define gold, silver, and bronze sponsors
)



Expand Down
10 changes: 6 additions & 4 deletions modules/sponsorship.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
child_leaf_query, nice_name_from_otts
)


from .partners import partner_definitions, partner_identifiers_for_reservation_name

"""HMAC expiry in seconds, NB: This is when they're rotated, so an HMAC will be valid for 2xHMAC_EXPIRY"""
Expand Down Expand Up @@ -291,6 +290,8 @@ def reservation_confirm_payment(basket_code, total_paid_pence, basket_fields):

# Fetch latest asking price
ott_price_pence = db(db.ordered_leaves.ott==r.OTT_ID).select(db.ordered_leaves.price).first().price
if ott_price_pence is None:
ott_price_pence = float('inf')

# Fetch any previous row
prev_row = db(db.expired_reservations.id == r.prev_reservation_id).select().first() if r.prev_reservation_id else None
Expand Down Expand Up @@ -373,17 +374,18 @@ def reservation_confirm_payment(basket_code, total_paid_pence, basket_fields):
# Multiple partners, we can't represent that, so flag with NaN
fields_to_update['partner_percentage'] = float('nan')

price_float = None if ott_price_pence == float('inf') else (ott_price_pence / 100)
# Update DB entry with recalculated asking price
fields_to_update['asking_price'] = ott_price_pence / 100
fields_to_update['asking_price'] = price_float

if remaining_paid_pence >= ott_price_pence:
# Can pay for this node, so do so.
remaining_paid_pence -= ott_price_pence
# NB: Strictly speaking user_paid is "What they promised to pay", and should
# have been set before the paypal trip. But with a basket of items we don't divvy up
# their donation until now.
fields_to_update['user_paid'] = ott_price_pence / 100
fields_to_update['verified_paid'] = '{:.2f}'.format(ott_price_pence / 100)
fields_to_update['user_paid'] = price_float
fields_to_update['verified_paid'] = None if price_float is None else '{:.2f}'.format(price_float)
else:
# Can't pay for this node, but make all other changes
fields_to_update['user_paid'] = None
Expand Down
27 changes: 25 additions & 2 deletions modules/usernames.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,35 @@ def find_username(target_row, return_otts=False, allocate_species_name=False):
return None, None


def donor_name_for_username(username, include_hidden=False):
"""Return a verified_donor_title and verified_donor_name for the username"""
db = current.db
if include_hidden:
query = (db.reservations.username == username)
else:
query = (
(db.reservations.username == username)
# Need to check both False (0) and None (NULL) in SQL
& ((db.reservations.user_donor_hide == False) | (db.reservations.user_donor_hide == None))
)
for r in db(query).select(
db.reservations.verified_donor_title,
db.reservations.verified_donor_name,
orderby=~db.reservations.verified_time,
):
if r.verified_donor_name:
return r.verified_donor_title, r.verified_donor_name
return None, None

def email_for_username(username):
"""Return the most up to date e-mail address for a given username"""
db = current.db

pp_e_mail = None
for r in db(db.reservations.username == username).iterselect(orderby=~db.reservations.verified_time):
for r in db(db.reservations.username == username).iterselect(
db.reservations.e_mail,
db.reservations.PP_e_mail,
orderby=~db.reservations.verified_time
):
if r.e_mail:
return r.e_mail
if r.PP_e_mail and not pp_e_mail:
Expand Down
Binary file modified static/FinalOutputs/AllLife_full_tree.phy.gz
Binary file not shown.
12 changes: 8 additions & 4 deletions tests/unit/test_controllers_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import unittest
from inspect import getmembers, isfunction, getfullargspec

import applications.OZtree.controllers.default as default
from applications.OZtree.tests.unit import util

from gluon.globals import Request, Session
from gluon.http import HTTP # This is the error code

import applications.OZtree.controllers.default as default
from applications.OZtree.tests.unit import util
import img

def dummy(*args, **kwargs):
"Pass-through function used e.g. to replace built-in 'redirect'"
pass
Expand All @@ -35,7 +36,10 @@ def setUp(self):
default.T = current.globalenv['T']
default.HTTP = current.globalenv['HTTP']
default.sponsor_suggestion_flags = current.globalenv['sponsor_suggestion_flags']
default.thumb_base_url = current.globalenv['myconf'].take('images.url_base')
try:
default.thumb_base_url = current.globalenv['myconf'].take('images.url_base')
except:
default.thumb_base_url = "//" + img.local_path
default.redirect = dummy
# Define all the globals in _OZglobals

Expand Down
Loading

0 comments on commit 1addda7

Please sign in to comment.