def _run(self): # Scheduled cron job to update analytics for all settings accounts every hour all_settings = models.Settings.query() for s in all_settings: ## Update one_week_history last_week = datetime.today() - timedelta(days=7) # Get donations made in the last week donations = models.Donation.gql( "WHERE settings = :s AND donation_date > :last_week ORDER BY donation_date DESC", s=s.key, last_week=last_week) donation_count = 0 total_money = tools.toDecimal(0) for d in donations: # Counting total money total_money += d.amount_donated # Counting number of donations donation_count += 1 one_week_history = [donation_count, str(total_money)] s.one_week_history = json.dumps(one_week_history) ##################################################################################################### ## Update one_month_history last_week = datetime.today() - timedelta(days=30) # Get donations made in the last week donations = models.Donation.gql( "WHERE settings = :s AND donation_date > :last_week ORDER BY donation_date DESC", s=s.key, last_week=last_week) one_month_history = [["Date", "Amount Donated ($)"]] donations_dict = {} for d in donations: d_date = tools.convertTime(d.donation_date) day = str(d_date.month).zfill(2) + "/" + str(d_date.day).zfill(2) if day in donations_dict: donations_dict[day] += d.amount_donated else: donations_dict[day] = d.amount_donated for date in sorted(donations_dict.iterkeys()): one_month_history.append([date, float(donations_dict[date])]) s.one_month_history = json.dumps(one_month_history) s.put()
def get(self): try: mode = self.request.get("m") donation_key = self.request.get("id") if mode == "" or donation_key == "": # Throw an error if you don't have those two pieces of info raise Exception("Don't know mode or donation key.") d = tools.getKey(donation_key).get() date = tools.convertTime(d.donation_date).strftime("%B %d, %Y") s = d.settings.get() if d.individual: individual_name = d.individual.get().name elif d.team: individual_name = d.team.get().name else: individual_name = None template_variables = {"s": s, "d": d, "c": d.contact, "date": date, "individual_name": individual_name} if mode == "w": template_location = "pages/letters/thanks_live.html" elif mode == "p": template_location = "pages/letters/thanks_print.html" elif mode == "e": who = "http://ghidonations.appspot.com" template_variables["see_url"] = d.confirmation.see_url(who) template_variables["print_url"] = d.confirmation.print_url(who) template_location = "pages/letters/thanks_email.html" self.response.write( template.render(template_location, template_variables)) except: # If there's a malformed URL, give a 500 error self.error(500) self.response.write( template.render('pages/letters/thankyou_error.html', {}))
def formatted_creation_date(self): return tools.convertTime(self.creation_date).strftime("%b %d, %Y")