def pledges(): user_pledges = db((db.users_pledged.user_id == auth.user.id) & (db.pledges.id == db.users_pledged.pledge_id) & (db.projects.id == db.pledges.project_id) & (db.project_status.id == db.projects.status_id) ).select() additional_pledges = dict() for (index, pledge) in enumerate(user_pledges): if pledge.projects.include_lower_pledges: funding = pledge.pledges.value # Add lower pledges when Bootable has stacked pledges additional_pledges[pledge.projects.id] = db((db.pledges.project_id == db.projects.id) & (db.projects.id == pledge.projects.id) & (db.pledges.value <= funding) ).select(db.pledges.description) pledge.pledges.value = custom_functions.format_pounds(pledge.pledges.value) pledge.projects.funding_goal = custom_functions.format_pounds(pledge.projects.funding_goal) pledge.projects.total_funded = custom_functions.format_pounds(get_project_funding(pledge.projects.id)) return dict( pledges = user_pledges, additional_pledges = additional_pledges )
def projects(): user_projects = db((db.projects.owner_id == auth.user.id) & (db.projects.status_id == db.project_status.id)).select() for project in user_projects: if project.project_status.name == 'open': project['formatted_goal'] = custom_functions.format_pounds(project.projects.funding_goal) project['formatted_funding'] = custom_functions.format_pounds(get_project_funding(project.projects.id)) return dict( projects = user_projects )
def submit_pledge(): if not request.vars.pledge_id: return dict() added = db.users_pledged.insert(pledge_id = request.vars.pledge_id, user_id = auth.user.id) stack_status = db((db.projects.id == db.pledges.project_id) & (db.pledges.id == request.vars.pledge_id) ).select(db.projects.include_lower_pledges).first() value_submitted = db(db.pledges.id == request.vars.pledge_id).select(db.pledges.value).first().as_dict() if stack_status.include_lower_pledges: project_id = get_project_id(request.vars.pledge_id) all_pledges = db((db.pledges.id == request.vars.pledge_id) & (db.pledges.value <= value_submitted['value'])).select().as_dict() else: all_pledges = db(db.pledges.id == request.vars.pledge_id).select().as_dict() highest_pledge = custom_functions.format_pounds(value_submitted['value']) return json.dumps( dict( highest_pledge = highest_pledge, all_pledges = all_pledges ) )
def editor(): project_id = request.vars.project_id project = db(db.projects.id == project_id).select().first() project_status = db(db.project_status.id == project.status_id).select(db.project_status.name).first().as_dict() project.status = project_status['name'] if (auth.user.id != project.owner_id): # Redirect to error if current user isn't the Bootable Manager redirect(URL('errors', 'generic.html', vars=dict(error_details = 'no_permission'))) elif project.status == 'open': # Prevent editing an open Bootable return dict( editable = False ) else: categories = db().select(db.project_categories.ALL) pledges = db(db.pledges.project_id == project_id).select(orderby=db.pledges.value) for pledge in pledges: # Convert each pledge value into pounds pledge.value = custom_functions.format_pounds(pledge.value) return dict( editable = True, project = project, categories = categories, pledges = pledges )
def view(): if not(request.vars.has_key('project_id')): redirect(URL('default', 'index.html')) project = db((db.projects.id == request.vars.project_id) & (db.projects.category_id == db.project_categories.id) & (db.projects.status_id == db.project_status.id) ).select().first() if project.project_status.name != 'open': # Only Bootables open for pledges are to allowed to be viewed redirect(URL('errors', 'generic.html', vars=dict(error_details='project_unavailable'))) # Calculate currently funded amount current_funding = get_project_funding(request.vars.project_id); current_formatted = custom_functions.format_pounds(current_funding); # Get funding goal and calculate the percent funded so far required_funding = db(db.projects.id == request.vars.project_id).select(db.projects.funding_goal).first().as_dict() funding_goal = required_funding['funding_goal'] funding_percent = int((float(current_funding)/float(funding_goal)) * 100) # Get project pledges and users who have purchased them pledges = db(db.pledges.project_id == request.vars.project_id).select() pledgers = db((db.users_pledged.user_id == db.auth_user.id) & (db.pledges.id == db.users_pledged.pledge_id) & (db.pledges.project_id == request.vars.project_id) ).select( db.auth_user.first_name, db.auth_user.email, db.auth_user.id, db.pledges.value, db.pledges.id) # Find out if this project has stackable pledges # If so, get all rewards up to and including each pledger's pledge value # If not, just get the pledge purchased stack_status = db((db.projects.id == request.vars.project_id)).select(db.projects.include_lower_pledges).first().as_dict() pledger_rewards = {} if stack_status['include_lower_pledges']: for pledger in pledgers: project_id = get_project_id(pledger.pledges.id) rewards = db((db.pledges.project_id == project_id) & (db.pledges.value <= pledger.pledges.value) ).select() pledger_rewards[pledger.auth_user.id] = rewards else: for pledger in pledgers: rewards = db((db.pledges.id == pledger.pledges.id) & (db.pledges.value == pledger.pledges.value)).select() pledger_rewards[pledger.auth_user.id] = rewards # Format pledge values for display in for each pledger and each plege associated with this Bootable for pledger in pledgers: pledger.pledges.value = custom_functions.format_pounds(pledger.pledges.value) for pledge in pledges: pledge.value = custom_functions.format_pounds(pledge.value) has_pledged = user_has_pledged(request.vars.project_id) pledge_made = dict() user_rewards = dict() # Work out user's rewards if they have pledged to the project if has_pledged: pledge_made = db((db.users_pledged.user_id == auth.user.id) & (db.pledges.project_id == request.vars.project_id) & (db.pledges.id == db.users_pledged.pledge_id) ).select( db.pledges.value, db.users_pledged.pledge_id).first().as_dict() # Stacked means user gets rewards up to and including chosen pledge # Otherwise, just get the reward from the one chosen if stack_status['include_lower_pledges']: user_rewards = db((db.pledges.id == pledge_made['users_pledged']['pledge_id']) & (db.pledges.value <= pledge_made['pledges']['value']) ).select() else: user_rewards = db((db.pledges.id == pledge_made['users_pledged']['pledge_id'])).select() pledge_made['pledges']['value'] = custom_functions.format_pounds(pledge_made['pledges']['value']) formatted_goal = custom_functions.format_pounds(project['projects']['funding_goal']) return dict( project = project, pledges = pledges, pledgers = pledgers, pledger_rewards = pledger_rewards, actual_funding = current_formatted, formatted_goal = formatted_goal, funding_percent = funding_percent, has_pledged = has_pledged, pledge_made = pledge_made, user_rewards = user_rewards, rewards_stack = stack_status['include_lower_pledges'] )