def post(self):
        user = users.get_current_user()
        if user:
            pass
        else:
            self.redirect(users.create_login_url(self.request.uri))


        msg_job_queue_name= DEFAULT_MESSAGING_JOB_NAME
        msg_job = MessagingJob(parent=messagingJob_key(msg_job_queue_name))

        msg_job.owner = users.get_current_user()

        msg_job.note = self.request.get('content')

        if self.request.get('date_type') == 'lunar':
            try:
                msg_job.date = LunarDate(int(self.request.get('year')),
                        int(self.request.get('month')), int(self.request.get('day'))).toSolarDate()
            except ValueError:
                self.redirect('/?msg=invalid_date')
                return
        else:
            try:
                msg_job.date = date(int(self.request.get('year')),
                        int(self.request.get('month')), int(self.request.get('day')))
                LunarDate.fromSolarDate(msg_job.date.year,
                        msg_job.date.month,
                        msg_job.date.day).toSolarDate()
            except ValueError:
                self.redirect('/?msg=invalid_date')
                return

        lunarDate=LunarDate.fromSolarDate(msg_job.date.year,msg_job.date.month,msg_job.date.day)
        msg_job.lunar_month = lunarDate.month
        msg_job.lunar_day = lunarDate.day

        base_messaging_jobs_query = MessagingJob.query(
            ancestor=messagingJob_key(DEFAULT_MESSAGING_JOB_NAME))
        messaging_jobs_query_by_user = base_messaging_jobs_query.filter(
            MessagingJob.owner == user)
        if messaging_jobs_query_by_user.count() >= MAX_ENTRIES_PER_USER:
            self.redirect('/?msg=exceed_user_quota')
            return

        messaging_jobs_query_by_date = base_messaging_jobs_query.filter(
                MessagingJob.lunar_month == msg_job.lunar_month).filter(
                MessagingJob.lunar_day == msg_job.lunar_day)
        if messaging_jobs_query_by_date.count() >= MAX_ENTRIES_PER_DAY:
            self.redirect('/?msg=exceed_daily_quota')
            return

        msg_job.put()

        self.redirect('/')
def UpdateSchema(cursor=None,num_updated=0):
    query = MessagingJob.query()

    results, cursor, more = query.fetch_page(BATCH_SIZE, start_cursor=cursor)

    to_put = []
    for p in results:
        # migration #1 - AppVersion 2 - adding lunar_month & lunar_day index
        if p.lunar_month is None and p.lunar_day is None:
            lunarDate=p.getLunarDate()
            p.lunar_month=lunarDate.month
            p.lunar_day=lunarDate.day
            logging.info(str(p.getLunarDate()) + p.note + " migrated")
        else:
            logging.info(str(p.getLunarDate()) + p.note + " does not have an empty lunar_month/lunar_date")
            continue

        to_put.append(p)

    if to_put:
        ndb.put_multi(to_put)
        num_updated += len(to_put)
        logging.debug('Put %d entities to Datastore for a total of %d',
                len(to_put),num_updated)
        if more:
            deferred.defer(
                    UpdateSchema, cursor, num_updated)
        else:
            logging.info("Updated schema <MessagingJob> with %d updates" % (
                num_updated))

    return
 def get(self):
     today_ld = LunarDate.today()
     q1 = MessagingJob.query(MessagingJob.lunar_month == today_ld.month, MessagingJob.lunar_day == today_ld.day)
     for job in q1.iter():
         mail.send_mail(
             sender=SENDER,
             to=job.owner.email(),
             subject="Lunar Calendar Reminder - " + job.note,
             body=EMAIL_BODY % (job.owner.nickname(), job.note, job.getNextRun()),
         )
    def get(self):
        user = users.get_current_user()
        if user:
            pass
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'
            
            template_values = {
                    'jobs': '',
                    'logout_url': url,
                    'logout_url_linktext': url_linktext,
                    'error': self.request.get('msg',''),
                    }

            template = JINJA_ENVIRONMENT.get_template('index.html')
            self.response.write(template.render(template_values))
            #self.redirect(users.create_login_url(self.request.uri))
            return

        messaging_jobs_query = MessagingJob.query(
            ancestor=messagingJob_key(DEFAULT_MESSAGING_JOB_NAME))
        messaging_jobs_query = messaging_jobs_query.filter(
            MessagingJob.owner == user).order(-MessagingJob.created_date)
        messaging_jobs = messaging_jobs_query.fetch(MAX_ENTRIES_PER_USER)

        logout_url = users.create_logout_url(self.request.uri)
        logout_url_linktext = 'Logout'

        template_values = {
                'jobs': messaging_jobs,
                'logout_url': logout_url,
                'logout_url_linktext': logout_url_linktext,
                'error': self.request.get('msg',''),
                }

        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render(template_values))