def record_status(processname, message=None, percent_done=None): try: proc_rec = Process.objects.get(name=processname) except Process.DoesNotExist: proc_rec = Process(name=processname) if message: proc_rec.statustext = message if percent_done: proc_rec.percentdone = percent_done proc_rec.save()
def record_status(processname, message=None, percent_done=None): '''Record process feedback so we can display it during long-running operations''' try: proc_rec = Process.objects.get(name=processname) except Process.DoesNotExist: proc_rec = Process(name=processname) if message: proc_rec.statustext = message if percent_done: proc_rec.percentdone = percent_done proc_rec.save()
def post(self, request, format=None): date = request.DATA.get('date') try: activity = Activity.objects.get(name=request.DATA.get('activity')) except Activity.DoesNotExist: activity = Activity(name=request.DATA.get('activity'), average_price=0) activity.save() p = Process(date=request.DATA.get('date'), activity=activity, average_price=0) p.save() consumer_list = json.loads(request.DATA.get('list')) user_to_be_saved = [] total = 0 try: for num, i in enumerate(consumer_list): name = i.get('name') prepay = i.get('prepay') consume = i.get('consume') #check name user = User.objects.get(name=name) p.participants.add(user) #user money user.remain += prepay - consume user_to_be_saved.append(user) total += consume #send email except: print sys.exc_info() p.delete() return Response(status=404) #caculate the average price of process p.average_price =total/(num +1) #caculate the average price of activity if p.average_price == 0: activity.average_price = p.average_price else: activity.average_price = (p.average_price + activity.average_price)/2 p.save() for i in user_to_be_saved: i.save() return Response({"detail":"success"} ,status=200)
def run(request): if request.method == 'POST': print 'got run request for makecatalogs' # remove records for exited processes Process.objects.filter(name='makecatalogs', exited=True).delete() while True: # Loop until there are no more running processes processes = Process.objects.filter(name='makecatalogs', exited=False) if not processes: break # clean up any processes no longer in the process table for process in processes: if not pid_exists(process.pid): process.delete() processes = Process.objects.filter(name='makecatalogs', exited=False) if not processes: break time.sleep(1) proc = subprocess.Popen([MAKECATALOGS, REPO_DIR], stdout=subprocess.PIPE, stderr=subprocess.PIPE) record = Process(name='makecatalogs') record.pid = proc.pid record.save() while True: output = proc.stdout.readline().decode('utf-8').rstrip('\n') if output: record.statustext = output.rstrip('\n') record.save() if proc.poll() != None: break record.statustext = 'Done' record.exited = True record.exitcode = proc.returncode record.save() return HttpResponse(json.dumps('done'), content_type='application/json') return HttpResponse(json.dumps('must be a POST request'), content_type='application/json')
def run(request): if request.method == 'POST': print 'got run request for makecatalogs' # remove records for exited processes Process.objects.filter(name='makecatalogs', exited=True).delete() while True: # Loop until there are no more running processes processes = Process.objects.filter(name='makecatalogs', exited=False) if not processes: break # clean up any processes no longer in the process table for process in processes: if not pid_exists(process.pid): process.delete() processes = Process.objects.filter(name='makecatalogs', exited=False) if not processes: break time.sleep(1) proc = subprocess.Popen( [MAKECATALOGS, REPO_DIR], stdout=subprocess.PIPE, stderr=subprocess.PIPE) record = Process(name='makecatalogs') record.pid = proc.pid record.save() while True: output = proc.stdout.readline().decode('utf-8').rstrip('\n') if output: record.statustext = output.rstrip('\n') record.save() if proc.poll() != None: break record.statustext = 'Done' record.exited = True record.exitcode = proc.returncode record.save() return HttpResponse(json.dumps('done'), content_type='application/json') return HttpResponse(json.dumps('must be a POST request'), content_type='application/json')