# Path: data_model.py
# class PhoneLog(db.Model):
#   phone       = db.StringProperty()
#   to          = db.StringProperty(multiline=True)
#   date        = db.DateTimeProperty(auto_now_add=True)
#   body        = db.StringProperty(multiline=True,indexed=False)
#   smsID       = db.StringProperty(indexed=False)
#   outboundSMS = db.StringProperty(multiline=True,indexed=False)
# 
# Path: data_model.py
# class Caller(db.Model):
# 	# caller's phone number requests are sent from
# 	phone   = db.StringProperty(indexed=True)
#     # the SMB service number Caller has permission to use
# 	service = db.StringProperty()
# 	
# 	created = db.DateProperty(auto_now_add=True)
# 	expires = db.DateProperty()


import os
import logging
import webapp2
import twilio
import config
import paywall
from operator import itemgetter
from datetime import date
from datetime import timedelta
from google.appengine.api.labs.taskqueue import Task
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from data_model import PhoneLog
from data_model import Caller

      # create a page that provides a form for sending an SMS message
      path = os.path.join(os.path.dirname(__file__), 'views/outreach.html')
      self.response.out.write(template.render(path,template_values))

## end

class SendSMSOutreach(webapp2.RequestHandler):
    def post(self):
      callers = db.GqlQuery("SELECT * FROM Caller").fetch(limit=None)
      for friend in callers:
        logging.info("Send outreach SMS to %s" % friend.phone)
        account = twilio.Account(config.ACCOUNT_SID, config.ACCOUNT_TOKEN)
        sms = {
               'From' : config.CALLER_ID,
               'To'   : friend.phone,
               'Body' : self.request.get('text'),
               }
        try:
            account.request('/%s/Accounts/%s/SMS/Messages' % (config.API_VERSION, config.ACCOUNT_SID),
                            'POST', sms)
        except Exception, e:
            logging.error("Twilio REST error: %s" % e)


      self.response.out.write('success');

## end

class AdminHandler(webapp2.RequestHandler):

# Path: data_model.py
# class PhoneLog(db.Model):
#   phone       = db.StringProperty()
#   to          = db.StringProperty(multiline=True)
#   date        = db.DateTimeProperty(auto_now_add=True)
#   body        = db.StringProperty(multiline=True,indexed=False)
#   smsID       = db.StringProperty(indexed=False)
#   outboundSMS = db.StringProperty(multiline=True,indexed=False)
# 
# Path: data_model.py
# class Caller(db.Model):
# 	# caller's phone number requests are sent from
# 	phone   = db.StringProperty(indexed=True)
#     # the SMB service number Caller has permission to use
# 	service = db.StringProperty()
# 	
# 	created = db.DateProperty(auto_now_add=True)
# 	expires = db.DateProperty()


# Path: admin.py
import os
import logging
import webapp2
import twilio
import config
import paywall
from operator import itemgetter
from datetime import date
from datetime import timedelta
from google.appengine.api.labs.taskqueue import Task
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from data_model import PhoneLog
from data_model import Caller


      # create a page that provides a form for sending an SMS message
      path = os.path.join(os.path.dirname(__file__), 'views/outreach.html')
      self.response.out.write(template.render(path,template_values))

## end

class SendSMSOutreach(webapp2.RequestHandler):
    def post(self):
      callers = db.GqlQuery("SELECT * FROM Caller").fetch(limit=None)
      for friend in callers:
        logging.info("Send outreach SMS to %s" % friend.phone)
        account = twilio.Account(config.ACCOUNT_SID, config.ACCOUNT_TOKEN)
        sms = {
               'From' : config.CALLER_ID,
               'To'   : friend.phone,
               'Body' : self.request.get('text'),
               }
        try:
            account.request('/%s/Accounts/%s/SMS/Messages' % (config.API_VERSION, config.ACCOUNT_SID),
                            'POST', sms)
        except Exception, e:
            logging.error("Twilio REST error: %s" % e)


      self.response.out.write('success');

## end

class AdminHandler(webapp2.RequestHandler):

    def get(self):