# 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
                   (self.request.get('sid'), self.request.get('phone')))
      account = twilio.Account(config.ACCOUNT_SID, config.ACCOUNT_TOKEN)
      sms = {
             'From' : config.CALLER_ID,
             'To' : self.request.get('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 SendSMSHandler

class AddUserHandler(webapp2.RequestHandler):
    def get(self):
      user_phone = self.request.get('phone')
      if( user_phone is None or user_phone == '' ):
          logging.error("Failed to create a new user. No phone number was provided")
          self.response.out.write('error. no phone number was provided - ?phone=')
      else:
          # prepend +1 on the number
          user_phone = '+1' + user_phone
          logging.info('Adding new user... %s' % user_phone)
          q = db.GqlQuery("select * from Caller where phone = :1", user_phone)
          new_user = q.get()
          if( new_user is None ):

# 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

                   (self.request.get('sid'), self.request.get('phone')))
      account = twilio.Account(config.ACCOUNT_SID, config.ACCOUNT_TOKEN)
      sms = {
             'From' : config.CALLER_ID,
             'To' : self.request.get('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 SendSMSHandler

class AddUserHandler(webapp2.RequestHandler):
    def get(self):
      user_phone = self.request.get('phone')
      if( user_phone is None or user_phone == '' ):
          logging.error("Failed to create a new user. No phone number was provided")
          self.response.out.write('error. no phone number was provided - ?phone=')
      else:
          # prepend +1 on the number
          user_phone = '+1' + user_phone
          logging.info('Adding new user... %s' % user_phone)
          q = db.GqlQuery("select * from Caller where phone = :1", user_phone)
          new_user = q.get()
          if( new_user is None ):

            new_user = Caller()