def time_period_begin(self): if self.time_period_method == self.FIXED_DURATION: return localtime(timezone.now()) - self.time_period elif self.time_period_method == self.SINCE_MONDAY: now = localtime(timezone.now()) last_monday = now - datetime.timedelta(days=now.weekday()) last_monday = last_monday.replace(hour=0, minute=0, second=0, microsecond=0) # midnight return last_monday elif self.time_period_method == self.SINCE_1ST: now = localtime(timezone.now()) month_1st = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) return month_1st elif self.time_period_method == self.SINCE_JAN_1ST: now = localtime(timezone.now()) jan_1st = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) return jan_1st elif self.time_period_method == self.SINCE_TODAY_MIDNIGHT: now = localtime(timezone.now()) midnight = now.replace(hour=0, minute=0, second=0, microsecond=0) return midnight elif self.time_period_method == self.SINCE_4pm: now = localtime(timezone.now()) if now.hour >= 16: # kein Bier vor vier last4pm = now.replace(hour=16, minute=0, second=0, microsecond=0) else: yesterday = now - datetime.timedelta(days=1) last4pm = yesterday.replace(hour=16, minute=0, second=0, microsecond=0) return last4pm
def accept_it(self): self.status = 'accepted' self.save() if self.vhost.main_domain is None or \ self.vhost.main_domain.name == self.vhost.service.network_configuration.name: self.vhost.main_domain = self self.vhost.save() from apimws.ipreg import set_cname try: set_cname(self.name, self.vhost.service.network_configuration.name) except DomainNameDelegatedException: return self.reject_it("Domain delegated") from apimws.ansible import launch_ansible launch_ansible(self.vhost.service) now = datetime.now() # Check if the set_cname was executed before the DNS refresh of the current hour. # DNS refreshes happen at 53 minutes of each hour if now.minute > 55: # If it was executed after the DNS refresh of the current hour, send the email to the user when # the next refresh happens eta = now.replace(minute=54) + timedelta(hours=1) else: # If it was executed before the DNS refresh of the current hour, send the email to the user when # the refresh happens eta = now.replace(minute=54) from apimws.utils import domain_confirmation_user domain_confirmation_user.apply_async(args=[ self, ], eta=eta)
def list(self, request, *args, **kwargs): obj = self.get_object() now = datetime.datetime.now(pytz.utc) start_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) year = now.year if start_month.month == 12: year += 1 relative_month = dateutil.relativedelta.relativedelta(months=1) end_month = datetime.datetime(year, (start_month + relative_month).month, 1) - datetime.timedelta(days=1) end_month = end_month.replace(hour=23, minute=59, second=50, tzinfo=pytz.utc) schedule = tzcron.Schedule(obj.schedule.crontab, pytz.utc, start_month, end_month) return Response([s.isoformat() for s in schedule])
def post(self, request, *args, **kwargs): user = request.user team_info = json.loads(request.body.decode("utf-8")) validator = Validator(team_schema) if not validator.validate(team_info): return JsonResponse({'error': validator.errors}) survey_send_time = parser.parse(team_info['send_time']).replace(second=0, microsecond=0) summary_send_time = parser.parse(team_info['summary_time']).replace(second=0, microsecond=0) rule = recurrence.Rule(recurrence.WEEKLY, byday=team_info['days_of_week']) now = datetime.datetime.now() exdates = [] if survey_send_time < now: exdates.append(now.replace(hour=0, minute=0, second=0, microsecond=0)) rec = recurrence.Recurrence(rrules=[rule], exdates=exdates) try: team = Team.objects.create(admin=user, name=team_info['name']) except IntegrityError: return JsonResponse({'error': {"name": _("team with this name already exists")}}) Report.objects.create(team=team, recurrences=rec, survey_send_time=survey_send_time, summary_send_time=summary_send_time) team_dict = model_to_dict(team, exclude=['users']) team_dict['report'] = self.report_dict(team) return JsonResponse({'team': team_dict})
def time_now(): now = datetime.now() start = now.replace(hour=20, minute=00, second=00, microsecond=00) return start if start > now else start + timedelta(days=1)