<commit_before>import calendar
import csv
import jinja2
import json
import logging
import os
import StringIO
import urllib2
import webapp2

from google.appengine.api import files, mail, memcache
from google.appengine.ext import db, deferred

import commands
import env
import model
import templates

class AdminDashboardHandler(webapp2.RequestHandler):
  def get(self):
    users = AdminDashboardHandler.get_missing_data_users()

    template = templates.GetTemplate('admin-dashboard.html')
    self.response.write(template.render({
      'missingUsers': [dict(email=user.email, amount=amt/100)
                       for user, amt in users],
      'totalMissing': sum(v for _, v in users)/100,
      'shardedCounterTotal': model.ShardedCounter.get_count('TOTAL-5'),
      'commands': AdminDashboardHandler.get_commands(),
    }))

  # Gets all the users with missing employer/occupation/targeting data
  # who gave at least $200 when we were on wordpress. If a user has
  # since updated their info, delete that user's record in the
  # MissingDataUsersSecondary model.
  #
  # Returns list of (User, amountCents) tuples.
  @staticmethod
  def get_missing_data_users():
    users = []
    for missing_user_secondary in model.MissingDataUsersSecondary.all():
      user = model.User.get_by_key_name(missing_user_secondary.email)

      # If they've added their info, delete them.
      if user.occupation and user.employer:
        db.delete(missing_user_secondary)
      else:
        # missing_user_secondary.amountCents never gets updated, but
        # that's okay, because it won't change unless the user makes a
        # new pledge, which will cause their info to be updated, so
        # we'll go down the other fork in this if.
        users.append((user, missing_user_secondary.amountCents))
    users.sort(key=lambda (_, amt): amt, reverse=True)
    return users

  @staticmethod
  def get_commands():
    return [dict(name=c.NAME, url='/admin/command/' + c.SHORT_NAME)
            for c in commands.COMMANDS if c.SHOW]


def generate_pledges_csv(file_name):
  """ Generates the pledges.csv file in a deferred way """

  csv_buffer = StringIO.StringIO()
  w = csv.writer(csv_buffer)
  w.writerow(['time', 'amount'])
  for pledge in model.WpPledge.all():
    w.writerow([str(pledge.donationTime), pledge.amountCents])
  for pledge in model.Pledge.all():
    w.writerow([str(pledge.donationTime), pledge.amountCents])
  with files.open(file_name, 'a') as f:
    f.write(csv_buffer.getvalue())
  csv_buffer.close()
  files.finalize(file_name)


class GeneratePledgesCsvHandler(webapp2.RequestHandler):
  def get(self):
    # Create a blobstore file, a deferred task, and redirect to the download
    # page.
    file_name = files.blobstore.create(
      mime_type='text/csv', _blobinfo_uploaded_filename="pledges.csv")
    deferred.defer(generate_pledges_csv, file_name,
                   _queue='generatePledgesCSV')
    self.redirect('/admin/files%s/pledges.csv' % file_name)


class PledgesCsvHandler(webapp2.RequestHandler):
  def get(self, file_name):
    """
    Serve the blobstore file if it exists, otherwise instruct the user to
    refresh later.
    """
    try:
      with files.open(file_name, 'r') as f:
        self.response.headers['Content-type'] = 'text/csv'
        for row in f.read():
          self.response.write(row)
    except:
      self.response.write('Working on it, refresh in a few minutes.')


class StretchHandler(webapp2.RequestHandler):
  def get(self):
    total = model.StretchCheckTotal.get()
    if total != 0:
      total = total/100
    template = templates.GetTemplate('stretch.html')
    self.response.write(template.render({'stretch': total}))

  def post(self):
    total = self.request.get("stretch")
    try:
      centsTotal = int(total) * 100
      model.StretchCheckTotal.update(centsTotal)

      # clear the cache so that it recalculates the next time
      model.ShardedCounter.clear('TOTAL-5')
      self.response.write('The Stretch total has been updated to: $' + str(total))
    except Exception as e:
      self.response.write('Sorry, something went wrong: ' + str(e))

def MakeCommandHandler(cmd_cls):<commit_msg>serve the csv file using a download handler<commit_after>import calendar
import csv
import jinja2
import json
import logging
import os
import StringIO
import urllib2
import webapp2

from google.appengine.api import files, mail, memcache
from google.appengine.ext import blobstore, db, deferred
from google.appengine.ext.webapp import blobstore_handlers

import commands
import env
import model
import templates

class AdminDashboardHandler(webapp2.RequestHandler):
  def get(self):
    users = AdminDashboardHandler.get_missing_data_users()

    template = templates.GetTemplate('admin-dashboard.html')
    self.response.write(template.render({
      'missingUsers': [dict(email=user.email, amount=amt/100)
                       for user, amt in users],
      'totalMissing': sum(v for _, v in users)/100,
      'shardedCounterTotal': model.ShardedCounter.get_count('TOTAL-5'),
      'commands': AdminDashboardHandler.get_commands(),
    }))

  # Gets all the users with missing employer/occupation/targeting data
  # who gave at least $200 when we were on wordpress. If a user has
  # since updated their info, delete that user's record in the
  # MissingDataUsersSecondary model.
  #
  # Returns list of (User, amountCents) tuples.
  @staticmethod
  def get_missing_data_users():
    users = []
    for missing_user_secondary in model.MissingDataUsersSecondary.all():
      user = model.User.get_by_key_name(missing_user_secondary.email)

      # If they've added their info, delete them.
      if user.occupation and user.employer:
        db.delete(missing_user_secondary)
      else:
        # missing_user_secondary.amountCents never gets updated, but
        # that's okay, because it won't change unless the user makes a
        # new pledge, which will cause their info to be updated, so
        # we'll go down the other fork in this if.
        users.append((user, missing_user_secondary.amountCents))
    users.sort(key=lambda (_, amt): amt, reverse=True)
    return users

  @staticmethod
  def get_commands():
    return [dict(name=c.NAME, url='/admin/command/' + c.SHORT_NAME)
            for c in commands.COMMANDS if c.SHOW]


def generate_pledges_csv(file_name):
  """ Generates the pledges.csv file in a deferred way """

  csv_buffer = StringIO.StringIO()
  w = csv.writer(csv_buffer)
  w.writerow(['time', 'amount'])
  for pledge in model.WpPledge.all():
    w.writerow([str(pledge.donationTime), pledge.amountCents])
  for pledge in model.Pledge.all():
    w.writerow([str(pledge.donationTime), pledge.amountCents])
  with files.open(file_name, 'a') as f:
    f.write(csv_buffer.getvalue())
  csv_buffer.close()
  files.finalize(file_name)


class GeneratePledgesCsvHandler(webapp2.RequestHandler):
  def get(self):
    # Create a blobstore file, a deferred task, and redirect to the download
    # page.
    file_name = files.blobstore.create(
      mime_type='text/csv', _blobinfo_uploaded_filename="pledges.csv")
    deferred.defer(generate_pledges_csv, file_name,
                   _queue='generatePledgesCSV')
    self.redirect('/admin/files%s/pledges.csv' % file_name)


class PledgesCsvHandler(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, file_name):
    """
    Serve the blobstore file if it exists, otherwise instruct the user to
    refresh later.
    """
    try:
      blob_key = files.blobstore.get_blob_key(file_name)
      blob_info = blobstore.BlobInfo.get(blob_key)
      self.send_blob(blob_info)
    except:
      self.response.write('Working on it, refresh in a few minutes.')


class StretchHandler(webapp2.RequestHandler):
  def get(self):
    total = model.StretchCheckTotal.get()
    if total != 0:
      total = total/100
    template = templates.GetTemplate('stretch.html')
    self.response.write(template.render({'stretch': total}))

  def post(self):
    total = self.request.get("stretch")
    try:
      centsTotal = int(total) * 100
      model.StretchCheckTotal.update(centsTotal)

      # clear the cache so that it recalculates the next time
      model.ShardedCounter.clear('TOTAL-5')
      self.response.write('The Stretch total has been updated to: $' + str(total))
    except Exception as e:
      self.response.write('Sorry, something went wrong: ' + str(e))

def MakeCommandHandler(cmd_cls):
{'fraction_non_alphanumeric': 0.10255500354861603, 'fraction_numerical': 0.005796072864916016, 'mean_word_length': 3.6379594075699395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5126785', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2566, 'n_words': 686}