Exemplo n.º 1
0
    def loop_begin_date(self, timezone=None):
        if not self.recurring:   # ignore for non-recurring Blitz
            return self.begin_date

        if timezone is None:
            timezone = current_tz()
            timezone.normalize(timezone_now()).date()

        period_begin = self.begin_date
        # loop through Blitz period to encompass today's date
        while period_begin + datetime.timedelta(days=7*self.num_weeks()) <= timezone.normalize(timezone_now()).date():
            period_begin += datetime.timedelta(days=7*self.num_weeks())

        return next_weekday(period_begin,0)
Exemplo n.º 2
0
def _make_naive(value, timezone):
    """
    Makes an aware datetime.datetime naive in a given time zone.
    """
    value = value.astimezone(timezone)
    if hasattr(timezone, 'normalize'):
        # available for pytz time zones
        value = timezone.normalize(value)
    return value.replace(tzinfo=None)
Exemplo n.º 3
0
def _make_naive(value, timezone):
    """
    Makes an aware datetime.datetime naive in a given time zone.
    """
    value = value.astimezone(timezone)
    if hasattr(timezone, 'normalize'):
        # available for pytz time zones
        value = timezone.normalize(value)
    return value.replace(tzinfo=None)
Exemplo n.º 4
0
def schedule_to_service_calendar(sched, agency_id):
    timezone = timezone_from_agency(sched, agency_id)

    day_start, day_end = day_bounds_from_sched(sched)

    startdate, enddate = [ datetime.datetime( *parse_date(x) ) for x in sched.GetDateRange() ]

    cal = ServiceCalendar()

    for currdate in iter_dates(startdate, enddate):
        local_dt = timezone.localize(currdate)

        service_ids = [x.encode("ascii") for x in get_service_ids( sched, currdate )]

        this_day_begins = timezone.normalize( local_dt + timedelta(seconds=day_start) )
        this_day_ends = timezone.normalize( local_dt + timedelta(seconds=day_end)  )

        cal.add_period( TimeHelpers.datetime_to_unix(this_day_begins), TimeHelpers.datetime_to_unix(this_day_ends), service_ids )

    return cal
Exemplo n.º 5
0
 def get_next_workout(self, timezone=None):
     """
     (date, day) tuple of next workout
     is today's workout even if already completed
     """
     if timezone is None:
         timezone = current_tz()
     today = timezone.normalize(timezone_now()).date()
     for workout_date, workout_plan_day in self.get_blitz().iterate_workouts():
         if workout_date >= today and not GymSession.objects.filter(client=self, workout_plan_day=workout_plan_day, is_logged=True).exists():
             return workout_date, workout_plan_day
     return None, None
Exemplo n.º 6
0
Arquivo: emails.py Projeto: ugik/Blitz
def usage_digest(days=0):
    from django.core.mail import EmailMultiAlternatives
    from django.utils.timezone import now as timezone_now, get_current_timezone as current_tz
    from pytz import timezone

    timezone = current_tz()
    startdate = date.today() - timedelta(days=days)

    enddate = date.today() - timedelta(days=0)
    trainers = Trainer.objects.filter(date_created__range=[startdate, enddate])
    members = BlitzMember.objects.filter(
        date_created__range=[startdate, enddate])

    # get clients with CC on file
    paying_clients = Client.objects.filter(~Q(balanced_account_uri=''))
    revenue = 0
    for payer in paying_clients:
        if payer.blitzmember_set.all()[0].price:
            revenue += float(payer.blitzmember_set.all()[0].price)

    revenue = float(revenue * 0.12)

    users = User.objects.all()
    login_users = []
    for user in users:
        if timezone.normalize(user.last_login).date() >= startdate:
            user.username = user_display_name(user)
            login_users.append(user)

    f = open('/etc/hosts', 'r')  # grab host and ip address
    lines = [line.strip() for line in f]
    f.close()

    template_html = 'usage_email.html'
    template_text = 'usage_email.txt'
    context = {
        'days': days + 1,
        'trainers': trainers,
        'login_users': login_users,
        'members': members,
        'revenue': revenue,
        'hosts': lines[0]
    }
    to_mail = ['*****@*****.**']
    from_mail = settings.DEFAULT_FROM_EMAIL
    subject = "Usage Digest"

    send_email(from_mail, to_mail, subject, template_text, template_html,
               context)
Exemplo n.º 7
0
 def get_missed_workouts(self, timezone=None, limit=None):
     if not self.get_blitz().workout_plan:
         return []
     if timezone is None:
         timezone = current_tz()
     today = timezone.normalize(timezone_now()).date()
     ret = []
     for workout_date, workout_plan_day in self.get_blitz().iterate_workouts():
         if workout_date < today and not GymSession.objects.filter(client=self, workout_plan_day=workout_plan_day, is_logged=True).exists() and not self.date_created > workout_date:
             ret.append( (workout_date, workout_plan_day) )
         elif workout_date >= today:
             break
     if not limit:
         return ret
     else:
         return ret[-limit:]   # return the most recent items in array
Exemplo n.º 8
0
 def days_since_checkin(self, date=None, timezone=None):
     if timezone is None:
         timezone = current_tz()
     today = timezone.normalize(timezone_now()).date()
     delta = today - self.date_created
     return delta.days
Exemplo n.º 9
0
 def days_since_begin(self, timezone=None):
     if timezone is None:
         timezone = current_tz()
     today = timezone.normalize(timezone_now()).date()
     delta = today - self.begin_date
     return delta.days
Exemplo n.º 10
0
 def in_progress(self, timezone=None):
     if timezone is None:
         timezone = current_tz()
     return self.begin_date <= timezone.normalize(timezone_now()).date() < self.loop_begin_date() + datetime.timedelta(weeks=self.num_weeks() )
Exemplo n.º 11
0
 def current_day_index(self, timezone=None):
     if timezone is None:
         timezone = current_tz()
     return timezone.normalize(timezone_now()).date().weekday()
Exemplo n.º 12
0
 def current_week(self, timezone=None):
     if timezone is None:
         timezone = current_tz()
     return 1 + (timezone.normalize(timezone_now()).date() - self.loop_begin_date()).days / 7
Exemplo n.º 13
0
def convertDateToGoogleStr(timezone, d):
    dateStr = timezone.normalize(timezone.localize(d)).astimezone(tz.tzutc()).isoformat('T')
    return dateStr
Exemplo n.º 14
0
 def get_todays_workout(self, timezone=None):
     if timezone is None:
         timezone = current_tz()
     weekday_index = timezone.normalize(timezone_now()).date().weekday()
     return self.get_blitz().get_workout_for_day(self.get_blitz().current_week(), DAYS_OF_WEEK[weekday_index][0])
Exemplo n.º 15
0
Arquivo: emails.py Projeto: ugik/Blitz
def usage_trainer(trainer):
    from django.utils.timezone import now as timezone_now, get_current_timezone as current_tz
    from pytz import timezone
    LAGGARD_DAYS = 7

    timezone = current_tz()
    days = 7
    startdate = date.today() - timedelta(days=days)
    enddate = date.today() - timedelta(days=0)
    laggard = date.today() - timedelta(
        days=days + LAGGARD_DAYS)  # laggard users

    users = []
    clients = Client.objects.all()
    for client in clients:
        if not client.user.is_trainer and client.get_blitz(
        ) and client.get_blitz().trainer == trainer:
            users.append(client.user)

    if not users:
        return

    login_users = []
    laggard_users = []
    inactive_users = []

    for user in users:
        if timezone.normalize(user.last_login).date() >= startdate:
            user.username = user_display_name(
                user)  # username will hold the client/trainer name
            login_users.append(user)
    for user in users:
        # show the trainer users that are laggards but still 'active'
        if timezone.normalize(
                user.last_login).date() < startdate and timezone.normalize(
                    user.last_login).date() >= laggard:
            user.username = user_display_name(user)
            laggard_users.append(user)
    for user in users:
        # show the trainer users that are laggards but still 'active'
        if timezone.normalize(user.last_login).date() < laggard:
            user.username = user_display_name(user)
            inactive_users.append(user)

    if not login_users and not laggard_users:
        return

    template_html = 'usage_email.html'
    template_text = 'usage_email.txt'
    context = {
        'days': days,
        'trainer': trainer,
        'login_users': login_users,
        'laggard_users': laggard_users,
        'inactive_users': inactive_users
    }
    to_mail = [trainer.user.email]

    from_mail = settings.DEFAULT_FROM_EMAIL
    subject = "Usage Digest"

    send_email(from_mail, to_mail, subject, template_text, template_html,
               context)
Exemplo n.º 16
0
def spotter_usage(request):
    from django.utils.timezone import now as timezone_now, get_current_timezone as current_tz
    from pytz import timezone
    from base.tasks import usage_digest
    from django.db.models import Q

    if not request.user.is_staff:
        return redirect('home')

    # get clients with CC on file
    paying_clients = Client.objects.filter(~Q(balanced_account_uri=''))
    revenue = MRR = 0
    for payer in paying_clients:
        if payer.blitzmember_set:
            # recurring monthly charge
            if payer.get_blitz(
            ).price_model == "R" and payer.blitzmember_set.all()[0].price:
                MRR += float(payer.blitzmember_set.all()[0].price)
            # monthly charge for non-recurring blitz
            else:
                if payer.get_blitz().num_weeks(
                ) > 0 and payer.blitzmember_set.all()[0].price:
                    MRR += float(
                        payer.blitzmember_set.all()[0].price /
                        payer.blitzmember_set.all()[0].blitz.num_weeks() * 4)
            revenue += float(payer.blitzmember_set.all()[0].price)

    net = float(MRR * 0.12)
    revenue = float(revenue * 0.12)

    timezone = current_tz()
    if 'days' in request.GET:
        startdate = date.today() - timedelta(days=int(request.GET.get('days')))
        days = request.GET.get('days')
    else:
        days = 1
        startdate = date.today() - timedelta(days=days - 1)

    enddate = date.today() - timedelta(days=0)
    trainers = Trainer.objects.filter(date_created__range=[startdate, enddate])
    members = BlitzMember.objects.filter(
        date_created__range=[startdate, enddate])

    users = User.objects.all()
    login_users = []
    for user in users:
        if timezone.normalize(user.last_login).date() >= startdate:
            login_users.append(user)

    if 'email' in request.GET:
        usage_digest()

    return render(
        request, 'usage.html', {
            'days': days,
            'trainers': trainers,
            'login_users': login_users,
            'members': members,
            'revenue': revenue,
            'MRR': MRR,
            'net': net
        })
Exemplo n.º 17
0
 def to_utc(self, timezone, naive):
     return timezone.normalize(timezone.localize(naive)).astimezone(pytz.utc)