# Path: models/slug.py
# class Slug(ndb.Model):
# 	slug = ndb.StringProperty()
# 	date = ndb.DateProperty()
# 	created = ndb.DateTimeProperty(auto_now_add=True)
# 
# Path: models/post.py
# class Post(ndb.Model):
# 	text = ndb.TextProperty()
# 	date = ndb.DateProperty()
# 	source = ndb.StringProperty(choices=['empty', 'ohlife','email','web','mixed'], default='empty')
# 	created = ndb.DateTimeProperty(auto_now_add=True)
# 	updated = ndb.DateTimeProperty(auto_now=True)
# 	images=ndb.StringProperty(repeated=True)
# 	has_images = ndb.BooleanProperty(default=False)
# 
# 	seperator = '-------------------------'
# 	
# 	def date_string(self):
# 		return self.date.strftime('%Y-%m-%d')
# 
# 	@classmethod
# 	def min_date(cls):
# 		post = Post.query().order(Post.date).get()
# 		if post:
# 			return post.date
# 		return None
# 
# 	@classmethod
# 	def max_date(cls):
# 		post = Post.query().order(-Post.date).get()
# 		if post:
# 			return post.date
# 		return None		
# 
# Path: models/settings.py
# class Settings(ndb.Model):
# 	email_address = ndb.StringProperty(required=True)
# 	timezone = ndb.StringProperty(default="America/Los_Angeles")
# 	email_hour = ndb.IntegerProperty(default=20)
# 	include_old_post_in_entry = ndb.BooleanProperty(default=True)
# 	dropbox_access_token = ndb.StringProperty(required=False)
# 	dropbox_last_backup = ndb.DateTimeProperty(required=False)
# 	blobstore_migration_done=ndb.BooleanProperty(default=False)
# 	
# 	@classmethod
#  	def get(cls):
#  		settings = cls.query().get() or Settings()
# 
#  		if not settings.email_address:
#  			#If the user is logged in we'll save his email address...
#  			user = users.get_current_user()
#  			if user:
#  				settings.email_address = user.email()
#  				settings.put()
# 
#  		return settings
# 
# Path: models/timezones.py

# 
# Path: models/postcounter.py
# class PostCounter(ndb.Model):
# 	count = ndb.IntegerProperty(indexed=False, default=0)
# 	months = ndb.StructuredProperty(Month, repeated=True)
# 
# 	def increment(self, year, month, put=True):
# 		m = self._get_month(year, month)
# 		m.count += 1
# 		self.count = sum(m.count for m in self.months)
# 		if put:
# 			self.put()
# 
# 	def decrement(self, year, month, put=True):
# 		m = self._get_month(year, month)
# 		m.count -= 1
# 		self.count = sum(m.count for m in self.months)
# 		if put:
# 			self.put()
# 
# 	def _get_month(self, year, month):
# 		for m in self.months:
# 			if m.year == year and m.month == month:
# 				return m
# 
# 		m = Month(year=year, month=month, count=0)
# 		self.months.append(m)
# 		return m
# 
# 
# 	@classmethod
#  	def get(cls):
#  		counter = cls.query().get()
# 
#  		if not counter:
#  			counter = PostCounter(count=0)
#  			counter.put()
# 
#  		return counter
# 
# Path: errorhandling.py
# def log_error(subject, message, *args):
# 	if args:
# 		try:
# 			message = message % args
# 		except:
# 			pass
# 
# 	logging.error(subject + ' : ' + message)
# 
# 	subject = 'MyLife Error: ' + subject
# 	app_id = app_identity.get_application_id()
# 	sender = "MyLife Errors <errors@%s.appspotmail.com>" % app_id
# 	try:
# 		to = Settings.get().email_address
# 		mail.check_email_valid(to, 'To')
# 		mail.send_mail(sender, to, subject, message)
# 	except:
# 		mail.send_mail_to_admins(sender, subject, message)


import traceback, random
import datetime, uuid, re, logging, os
from google.appengine.api import mail
from models.slug import Slug
from models.post import Post
from models.settings import Settings
from models.timezones import timezones
from models.postcounter import PostCounter
from google.appengine.api import app_identity
from errorhandling import log_error
class DailyMail():
	
	def send(self, is_intro_email=False, force_send=False, date=None):

		try:
			now = datetime.datetime.now()
			settings = Settings.get()

			if is_intro_email:
				current_time = now
				logging.info('Sending intro email to %s' % settings.email_address)
			else:
				current_time, id, name, offset = self.get_time_in_timezone(settings)

				if current_time.hour != settings.email_hour and not force_send:
					logging.info('Current time for %s is %s, not sending email now, will send at %02d:00' % (name, current_time, settings.email_hour))
					return


			if date and force_send:
				today = date #Allow overriding this stuff
			else:
				today = current_time.date()
			
			if self.check_if_intro_email_sent_today(today) and not force_send:
				logging.info('Already sent the intro email today, skipping this email for now')	
				return	


			#Check if we've already sent an email

# Path: models/slug.py
# class Slug(ndb.Model):
# 	slug = ndb.StringProperty()
# 	date = ndb.DateProperty()
# 	created = ndb.DateTimeProperty(auto_now_add=True)
# 
# Path: models/post.py
# class Post(ndb.Model):
# 	text = ndb.TextProperty()
# 	date = ndb.DateProperty()
# 	source = ndb.StringProperty(choices=['empty', 'ohlife','email','web','mixed'], default='empty')
# 	created = ndb.DateTimeProperty(auto_now_add=True)
# 	updated = ndb.DateTimeProperty(auto_now=True)
# 	images=ndb.StringProperty(repeated=True)
# 	has_images = ndb.BooleanProperty(default=False)
# 
# 	seperator = '-------------------------'
# 	
# 	def date_string(self):
# 		return self.date.strftime('%Y-%m-%d')
# 
# 	@classmethod
# 	def min_date(cls):
# 		post = Post.query().order(Post.date).get()
# 		if post:
# 			return post.date
# 		return None
# 
# 	@classmethod
# 	def max_date(cls):
# 		post = Post.query().order(-Post.date).get()
# 		if post:
# 			return post.date
# 		return None		
# 
# Path: models/settings.py
# class Settings(ndb.Model):
# 	email_address = ndb.StringProperty(required=True)
# 	timezone = ndb.StringProperty(default="America/Los_Angeles")
# 	email_hour = ndb.IntegerProperty(default=20)
# 	include_old_post_in_entry = ndb.BooleanProperty(default=True)
# 	dropbox_access_token = ndb.StringProperty(required=False)
# 	dropbox_last_backup = ndb.DateTimeProperty(required=False)
# 	blobstore_migration_done=ndb.BooleanProperty(default=False)
# 	
# 	@classmethod
#  	def get(cls):
#  		settings = cls.query().get() or Settings()
# 
#  		if not settings.email_address:
#  			#If the user is logged in we'll save his email address...
#  			user = users.get_current_user()
#  			if user:
#  				settings.email_address = user.email()
#  				settings.put()
# 
#  		return settings
# 
# Path: models/timezones.py

# 
# Path: models/postcounter.py
# class PostCounter(ndb.Model):
# 	count = ndb.IntegerProperty(indexed=False, default=0)
# 	months = ndb.StructuredProperty(Month, repeated=True)
# 
# 	def increment(self, year, month, put=True):
# 		m = self._get_month(year, month)
# 		m.count += 1
# 		self.count = sum(m.count for m in self.months)
# 		if put:
# 			self.put()
# 
# 	def decrement(self, year, month, put=True):
# 		m = self._get_month(year, month)
# 		m.count -= 1
# 		self.count = sum(m.count for m in self.months)
# 		if put:
# 			self.put()
# 
# 	def _get_month(self, year, month):
# 		for m in self.months:
# 			if m.year == year and m.month == month:
# 				return m
# 
# 		m = Month(year=year, month=month, count=0)
# 		self.months.append(m)
# 		return m
# 
# 
# 	@classmethod
#  	def get(cls):
#  		counter = cls.query().get()
# 
#  		if not counter:
#  			counter = PostCounter(count=0)
#  			counter.put()
# 
#  		return counter
# 
# Path: errorhandling.py
# def log_error(subject, message, *args):
# 	if args:
# 		try:
# 			message = message % args
# 		except:
# 			pass
# 
# 	logging.error(subject + ' : ' + message)
# 
# 	subject = 'MyLife Error: ' + subject
# 	app_id = app_identity.get_application_id()
# 	sender = "MyLife Errors <errors@%s.appspotmail.com>" % app_id
# 	try:
# 		to = Settings.get().email_address
# 		mail.check_email_valid(to, 'To')
# 		mail.send_mail(sender, to, subject, message)
# 	except:
# 		mail.send_mail_to_admins(sender, subject, message)


# Path: models/dailymail.py
import traceback, random
import datetime, uuid, re, logging, os
from google.appengine.api import mail
from models.slug import Slug
from models.post import Post
from models.settings import Settings
from models.timezones import timezones
from models.postcounter import PostCounter
from google.appengine.api import app_identity
from errorhandling import log_error

class DailyMail():
	
	def send(self, is_intro_email=False, force_send=False, date=None):

		try:
			now = datetime.datetime.now()
			settings = Settings.get()

			if is_intro_email:
				current_time = now
				logging.info('Sending intro email to %s' % settings.email_address)
			else:
				current_time, id, name, offset = self.get_time_in_timezone(settings)

				if current_time.hour != settings.email_hour and not force_send:
					logging.info('Current time for %s is %s, not sending email now, will send at %02d:00' % (name, current_time, settings.email_hour))
					return


			if date and force_send:
				today = date #Allow overriding this stuff
			else:
				today = current_time.date()
			
			if self.check_if_intro_email_sent_today(today) and not force_send:
				logging.info('Already sent the intro email today, skipping this email for now')	
				return	


			#Check if we've already sent an email

			slug = Slug.query(Slug.date == today).get()