def get_context_data(self, **kwargs): context = super(QigongCycleView, self).get_context_data(**kwargs) today = period_models.today() end_date = period_models.today() + datetime.timedelta(days=14) user = self.request.user if user.birth_date: context['physical'] = _generate_cycles(user.birth_date, today, end_date, 23) context['emotional'] = _generate_cycles(user.birth_date, today, end_date, 28) context['intellectual'] = _generate_cycles(user.birth_date, today, end_date, 33) return context
def qigong_cycles(request): today = period_models.today() end_date = period_models.today() + datetime.timedelta(days=14) data = {} if request.user.birth_date: data = { 'physical': _generate_cycles(request.user.birth_date, today, end_date, 23), 'emotional': _generate_cycles(request.user.birth_date, today, end_date, 28), 'intellectual': _generate_cycles(request.user.birth_date, today, end_date, 33) } return JsonResponse(data)
def period_form(request, period_id=None): # e.g. /period_form/?timestamp=2015-08-19T08:31:24-07:00 user_timezone = pytz.timezone(request.user.timezone.zone) try: flow_event = period_models.FlowEvent.objects.get(pk=int(period_id)) flow_event.timestamp = flow_event.timestamp.astimezone(user_timezone) except (TypeError, period_models.FlowEvent.DoesNotExist): timestamp = request.GET.get('timestamp') try: timestamp = dateutil_parser.parse(timestamp) except AttributeError: timestamp = period_models.today().astimezone(user_timezone) flow_event = period_models.FlowEvent(timestamp=timestamp) if timestamp: yesterday = timestamp - datetime.timedelta(days=1) yesterday_start = yesterday.replace(hour=0, minute=0, second=0) yesterday_events = period_models.FlowEvent.objects.filter( timestamp__gte=yesterday_start, timestamp__lte=timestamp) if not yesterday_events.count(): flow_event.first_day = True flow_event.timestamp = flow_event.timestamp.replace(tzinfo=pytz.utc) form = period_forms.PeriodForm(instance=flow_event) data = { 'form': form, } return render_to_response('periods/period_form.html', data, context_instance=RequestContext(request))
def retrieve(self, request, *args, **kwargs): min_timestamp = request.query_params.get('min_timestamp') try: min_timestamp = datetime.datetime.strptime(min_timestamp, DATE_FORMAT) min_timestamp = pytz.timezone("US/Eastern").localize(min_timestamp) except TypeError: min_timestamp = period_models.today() instance = self.get_object() instance.set_start_date_and_day(min_timestamp) serializer = self.get_serializer(instance) return Response(serializer.data)
def get_timestamp(self): # e.g. ?timestamp=2015-08-19T08:31:24-07:00 timestamp = self.request.GET.get('timestamp') try: timestamp = parse_datetime(timestamp) except TypeError as e: print("Could not parse date: %s" % e) if not timestamp: timestamp = self.convert_to_user_timezone(period_models.today()) timestamp = self.set_to_utc(timestamp) return timestamp
def list(self, request, *args, **kwargs): # Only return a single statistics object, for the authenticated user min_timestamp = request.query_params.get('min_timestamp') try: min_timestamp = datetime.datetime.strptime(min_timestamp, DATE_FORMAT) min_timestamp = pytz.timezone("US/Eastern").localize(min_timestamp) except TypeError: min_timestamp = period_models.today() queryset = self.filter_queryset(self.get_queryset()) instance = queryset[0] instance.set_start_date_and_day(min_timestamp) serializer = self.get_serializer(instance) return Response(serializer.data)
def list(self, request, *args, **kwargs): # Only return a single statistics object, for the authenticated user min_timestamp = request.query_params.get('min_timestamp') try: min_timestamp = datetime.datetime.strptime( min_timestamp, settings.API_DATE_FORMAT) min_timestamp = pytz.timezone( request.user.timezone.zone).localize(min_timestamp) except TypeError: min_timestamp = period_models.today() queryset = self.filter_queryset(self.get_queryset()) instance = queryset[0] instance.set_start_date_and_day(min_timestamp) serializer = self.get_serializer(instance) return Response(serializer.data)
def handle(self, *args, **options): users = period_models.User.objects.filter( is_active=True, flow_events__isnull=False, statistics__isnull=False).exclude( send_emails=False).distinct() for user in users: today = period_models.today() upcoming_events = user.statistics.predicted_events if not upcoming_events: continue # The upcoming events are in date order, ovulation/period/ovulation/... expected_date = upcoming_events[1]['timestamp'] calendar_start_date = expected_date - datetime.timedelta(days=7) expected_in = (expected_date - today.date()).days expected_abs = abs(expected_in) if expected_abs == 1: day = 'day' else: day = 'days' context = Context({ 'full_name': user.get_full_name(), 'today': self._format_date(today), 'expected_in': expected_abs, 'day': day, 'expected_date': self._format_date(expected_date), 'calendar_start_date': self._format_date(calendar_start_date), 'admin_name': settings.ADMINS[0][0], 'full_domain': helpers.get_full_domain(), }) subject = '' if expected_in < 0: subject = "Period was expected %s %s ago" % (expected_abs, day) template_name = 'expected_ago' elif expected_in == 0: subject = "Period today!" template_name = 'expected_now' elif expected_in < 4: subject = "Period starting" template_name = 'expected_in' elif expected_in == user.luteal_phase_length: subject = "Ovulation today!" template_name = 'ovulating' if subject: plaintext = get_template('periods/email/%s.txt' % template_name) html = get_template('periods/email/%s.html' % template_name) email_sender.send(user, subject, plaintext.render(context), html.render(context))
def handle(self, *args, **options): users = period_models.User.objects.filter( is_active=True, flow_events__isnull=False, statistics__isnull=False).exclude( send_emails=False).distinct() for user in users: today = period_models.today() upcoming_events = user.statistics.predicted_events if not upcoming_events: continue # The upcoming events are in date order, ovulation/period/ovulation/... expected_date = upcoming_events[1]['timestamp'] calendar_start_date = expected_date - datetime.timedelta(days=7) expected_in = (expected_date - today.date()).days expected_abs = abs(expected_in) if expected_abs == 1: day = 'day' else: day = 'days' context = { 'full_name': user.get_full_name(), 'today': self._format_date(today), 'expected_in': expected_abs, 'day': day, 'expected_date': self._format_date(expected_date), 'calendar_start_date': self._format_date(calendar_start_date), 'admin_name': settings.ADMINS[0][0], 'full_domain': helpers.get_full_domain(), } subject = '' if expected_in < 0: subject = "Period was expected %s %s ago" % (expected_abs, day) template_name = 'expected_ago' elif expected_in == 0: subject = "Period today!" template_name = 'expected_now' elif expected_in < 4: subject = "Period starting" template_name = 'expected_in' elif expected_in == user.luteal_phase_length: subject = "Ovulation today!" template_name = 'ovulating' if subject: plaintext = get_template('periods/email/%s.txt' % template_name) html = get_template('periods/email/%s.html' % template_name) email_sender.send(user, subject, plaintext.render(context), html.render(context))
def handle(self, *args, **options): users = period_models.User.objects.filter( flow_events__isnull=False, statistics__isnull=False).exclude( send_emails=False).distinct() for user in users: expected_in = (user.statistics.average_cycle_length - user.statistics.current_cycle_length) expected_abs = abs(expected_in) today = period_models.today() expected_date = today + datetime.timedelta( days=expected_in) if expected_abs == 1: day = 'day' else: day = 'days' context = Context({ 'full_name': user.get_full_name(), 'today': self._format_date(today), 'expected_in': expected_abs, 'day': day, 'expected_date': self._format_date(expected_date), 'site_name': settings.ADMINS[0][0] }) subject = '' if expected_in < 0: subject = "Period was expected %s %s ago" % (expected_abs, day) template_name = 'expected_ago' elif expected_in == 0: subject = "Period today!" template_name = 'expected_now' elif expected_in < 4: subject = "Period expected in %s %s" % (expected_in, day) template_name = 'expected_in' elif expected_in == user.luteal_phase_length: subject = "Ovulation today!" template_name = 'ovulating' if subject: plaintext = get_template('email/%s.txt' % template_name) email_sender.send(user, subject, plaintext.render(context), None)