def vacaciones(request): obj = Objeto.objects.all() id_obj_t = {} id_obj_f = {} for objeto in obj: logs_obj = Log.objects.filter(output = objeto) horas_par_t = [] horas_par_f = [] for i in range(len(logs_obj)): for j in range(len(logs_obj)): ta = True if logs_obj[i].hora.hour == logs_obj[j].hora.hour and logs_obj[i].status == logs_obj[j].status and j != i: if logs_obj[i].status: for t in horas_par_t: if t.hora.hour == logs_obj[i].hora.hour: ta = False if ta: horas_par_t.append(logs_obj[i]) else: for f in horas_par_f: if f.hora.hour == logs_obj[i].hora.hour: ta = False if ta: horas_par_f.append(logs_obj[i]) id_obj_t.update({objeto.id:horas_par_t}) id_obj_f.update({objeto.id:horas_par_f}) print id_obj_t print id_obj_f context = RequestContext(request) for i in id_obj_t: for j in range(len(id_obj_t[i])): vaca_cron = CrontabSchedule() vaca_cron.minute = id_obj_f[i][j].hora.minute vaca_cron.hour = id_obj_f[i][j].hora.hour vaca_cron.save() vaca_periodic = PeriodicTask() vaca_periodic.name = 'Vacaciones ' + str(id_obj_f[i][j].output.id) vaca_periodic.task = "module_1.tasks.on" vaca_periodic.crontab = vaca_cron vaca_periodic.args = "[ " +str(id_obj_f[i][j].output.pin)+ " ]" vaca_periodic.save() for i in id_obj_f: for j in range(len(id_obj_f[i])): vaca_cron = CrontabSchedule() vaca_cron.minute = id_obj_f[i][j].hora.minute vaca_cron.hour = id_obj_f[i][j].hora.hour vaca_cron.save() vaca_periodic = PeriodicTask() vaca_periodic.name = 'Vacaciones ' + str(id_obj_f[i][j].output.id) vaca_periodic.task = "module_1.tasks.off" vaca_periodic.crontab = vaca_cron vaca_periodic.args = "[ " +str(id_obj_f[i][j].output.pin)+ " ]" vaca_periodic.save() luces = Luz.objects.all() rule = Regla.objects.all() return render_to_response('luzauto.html',{'luz':luces, 'rules':rule},context)
def post(self, *args, **kwargs): seconds = self.request.POST.get('seconds') if seconds: # 几秒后执行任务 # 重复任务 int_sche = IntervalSchedule.objects.filter(every=seconds, period='seconds') if int_sche: int_sche = int_sche[0] else: int_sche = IntervalSchedule() int_sche.every = int(seconds) int_sche.period = 'seconds' int_sche.save() # 定时任务(也可以是重复的,不能精确到秒) # cron_sche = CrontabSchedule() # cron_sche.minute = 1 # cron_sche.hour = 1 # cron_sche.day_of_week = '0,1,2' # cron_sche.day_of_month = '0,1,2' # cron_sche.month_of_year = '0,1,2' # cron_sche.save() pt = PeriodicTask() pt.name = u'每隔 %sS 执行一次任务 %s' % (seconds, str(uuid.uuid1())) # name 是唯一的,故后缀了uuid pt.task = 'demo_celery.tasks.add' pt.interval = int_sche # interval 和 schedule 这两个属性只能选一个 # pt.crontab = cron_sche # pt.expires = datetime.datetime.now() + datetime.timedelta(days=1) pt.save() return HttpResponse('添加成功') else: res = add.delay(2, 3) return HttpResponse(HttpResponse(res.result))
def add_task(request, nums): if request.method == 'GET': logging.info(nums) if nums: # 几秒后执行任务 # 重复任务 int_sche = IntervalSchedule.objects.filter(every=nums, period='seconds') if int_sche: int_sche = int_sche[0] else: int_sche = IntervalSchedule() int_sche.every = nums int_sche.period = 'seconds' int_sche.save() # 定时任务(也可以是重复的,不能精确到秒) # cron_sche = CrontabSchedule() # cron_sche.minute = 1 # cron_sche.hour = 1 # cron_sche.day_of_week = '0,1,2' # cron_sche.day_of_month = '0,1,2' # cron_sche.month_of_year = '0,1,2' # cron_sche.save() pt = PeriodicTask() pt.name = u'每隔 %sS 执行一次任务 %s' % (nums, str(uuid.uuid1())) # name 是唯一的,故后缀了uuid pt.task = 'celery_demo.tasks.add' pt.interval= int_sche # interval 和 schedule 这两个属性只能选一个 # pt.crontab = cron_sche # pt.expires = datetime.datetime.now() + datetime.timedelta(days=1) pt.save() else: res = add.delay(2, 3) return HttpResponse(HttpResponse(res.result))
def deactivate_task(uid, days=14): """ uid:user.id days: 默认两周后关闭账户 """ from djcelery.models import CrontabSchedule, PeriodicTask from django.contrib.auth.models import User import datetime now = datetime.datetime.today() deadline = now + datetime.timedelta(minutes=3) task_date = CrontabSchedule() task_date.minute = deadline.timetuple().tm_min task_date.hour = deadline.timetuple().tm_hour task_date.day_of_month = deadline.timetuple().tm_mday task_date.month_of_year = deadline.timetuple().tm_mon try: from django.db import transaction with transaction.atomic(): task_date.save() except: return False user = User.objects.get(pk=uid) name = "Deactivate_User_%s" % uid new_task = PeriodicTask(name=name) new_task.name = name new_task.task = 'templates.tasks.deactivate_tempuser' new_task.crontab = task_date new_task.args = "[%s]" % uid new_task.enabled = True try: from django.db import transaction with transaction.atomic(): new_task.save() except: return False return True
def add_rule(request): context = RequestContext(request) id= request.POST.get('id') lista_permitidos = Usuario.objects.filter(permisos_luces=id) if lista_permitidos.__str__() != "[]": for permitido in lista_permitidos: if permitido.user.id == request.user.id: if request.method=='POST': print "hola" luz = Luz.objects.get(id = request.POST['id']) dias = eval(request.POST['days']) print "0" print dias,"-",request.POST['days'] hora = eval(request.POST['hours']) print "1" regla = Regla() print "2" regla.relacion = luz print "3" regla.pin = luz.pin print "4" if request.POST['status'] == "false": regla.status=False print request.POST['status'] else: regla.status=True print "true" print "4" days_t = [] for i in dias: if i=="Lunes": print "Lun" regla.lun = True days_t.append(1) elif i=="Martes": print "Mar" regla.mar = True days_t.append(2) elif i=="Miercoles": print "Mie" regla.mie = True days_t.append(3) elif i=="Jueves": print "Jue" regla.jue = True days_t.append(4) elif i=="Viernes": print "Vie" regla.vie = True days_t.append(5) elif i=="Sabado": print "Sab" regla.sab = True days_t.append(6) elif i=="Domingo": print "Dom" regla.dom = True days_t.append(0) regla.from_hour = hora[0] f_h = str(hora[0]) print "from" regla.to_hour = hora[1] t_h = str(hora[1]) print "to" regla.save() print "save" cron = CrontabSchedule() cron.minute = f_h[-2:] cron.hour = f_h[:2] print days_t aux="" for i in range(len(days_t)): if i == len(days_t)-1: aux += str(days_t[i]) else: aux += str(days_t[i])+"," days_t = aux print days_t cron.day_of_week = days_t cron.save() print "cron save" regla = Regla.objects.latest('id') cronT = CrontabSchedule.objects.latest('id') print "regla y cronT" periodic = PeriodicTask() print "P_t" na = str(regla.id)+"_1" print "na" periodic.name = na print "name" if regla.status: periodic.task = "module_1.tasks.on" else: periodic.task = "module_1.tasks.off" print "status" periodic.crontab = cronT print "cron= p_t" periodic.args = "[ " +str(regla.pin)+ " ]" print "arg" periodic.save() print "periodic save" cron = CrontabSchedule() cron.minute = t_h[-2:] cron.hour = t_h[:2] print days_t aux="" for i in range(len(days_t)): if i == len(days_t)-1: aux += str(days_t[i]) else: aux += str(days_t[i])+"," days_t = aux print days_t cron.day_of_week = days_t cron.save() print "cron save" cronT = CrontabSchedule.objects.latest('id') print "regla y cronT" periodic = PeriodicTask() print "P_t" na = str(regla.id)+"_2" print "na" periodic.name = na print "name" if regla.status: periodic.task = "module_1.tasks.off" else: periodic.task = "module_1.tasks.on" print "status" periodic.crontab = cronT print "cron= p_t" periodic.args = "[ " +str(regla.pin)+ " ]" print "arg" periodic.save() print "periodic save" rule = Regla.objects.all() return render_to_response('tab.html',{'rules':rule},context) rule = Regla.objects.all() response = render_to_response('tab.html',{'rules':rule},context) response.status_code = 202 return response