# 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
		# OK, this should never happen, but I'm not using the full uuid here
		# so lets just make sure...
		while Slug.query(Slug.slug == slug).get():
			slug = str(uuid.uuid4())[0:13].replace('-', '')		

		return slug

	def check_if_intro_email_sent_today(self, date):
		two_slugs = [s for s in Slug.query().fetch(2)]

		return len(two_slugs) == 1 and two_slugs[0].date == date

	def get_old_post(self, today):
		#Lets try to put in an old post...
		old_post = None
		old_type = ''

		#First try a year ago...
		if today.day == 29 and today.month == 2:
			old_post = None
		else:
			year_ago = datetime.date(today.year-1, today.month, today.day)
			old_post = Post.query(Post.date == year_ago).get()
			old_type = 'year'

		if not old_post:
			#lets try a month ago...
			last_day_of_last_month = datetime.date(today.year, today.month, 1) + datetime.timedelta(days=-1)
			if last_day_of_last_month.day >= today.day:
				month_ago = datetime.date(last_day_of_last_month.year, last_day_of_last_month.month, today.day)

# 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

		# OK, this should never happen, but I'm not using the full uuid here
		# so lets just make sure...
		while Slug.query(Slug.slug == slug).get():
			slug = str(uuid.uuid4())[0:13].replace('-', '')		

		return slug

	def check_if_intro_email_sent_today(self, date):
		two_slugs = [s for s in Slug.query().fetch(2)]

		return len(two_slugs) == 1 and two_slugs[0].date == date

	def get_old_post(self, today):
		#Lets try to put in an old post...
		old_post = None
		old_type = ''

		#First try a year ago...
		if today.day == 29 and today.month == 2:
			old_post = None
		else:
			year_ago = datetime.date(today.year-1, today.month, today.day)
			old_post = Post.query(Post.date == year_ago).get()
			old_type = 'year'

		if not old_post:
			#lets try a month ago...
			last_day_of_last_month = datetime.date(today.year, today.month, 1) + datetime.timedelta(days=-1)
			if last_day_of_last_month.day >= today.day:
				month_ago = datetime.date(last_day_of_last_month.year, last_day_of_last_month.month, today.day)

				old_post = Post.query(Post.date == month_ago).get()