def run_at_end_of_day(): last_eod_time = datetime.now() while True: now = datetime.now() if now.minute != last_eod_time.minute: # FIXME: payoff every minute, not day logger.info('END OF DAY') employees = Employee.objects.all() for emp in employees: if emp.wallet > 0: with transaction.atomic(): logger.info('payoff for employee {}, amount: {}'.format(emp, emp.wallet)) tr = Transaction.objects.create(account_id=emp, minus=emp.wallet, description='Payoff for ' + str(now), kind=TransactionKind.DAILY_PAYMENT) tr.save() emp.wallet = 0 emp.save() send_event(producer_transactions, 'transactions', registry, 1, DailyPaymentCompleted(account_public_id=emp.public_id, amount=tr.minus, ts=datetime.now())) last_eod_time = now time.sleep(10)
def assign_tasks(request): if request.method == 'POST': employee_list = Employee.objects.all() open_tasks = Task.objects.filter(status=TaskStatus.OPEN) with transaction.atomic(): for task in open_tasks: task.assignee = random.choice(employee_list) task.save() send_event(producer, 'tasks', registry, 1, TaskAssigned(task_public_id=str(task.public_id), assignee_public_id=str(task.assignee.public_id))) return HttpResponseRedirect(reverse('pjira:index')) else: return render(request, 'pjira/assign_tasks.html')
def TaskCreatedHandler(event, producer, topic, schema_registry, version): cost_assign = random.randint(10, 20) cost_close = random.randint(20, 40) with transaction.atomic(): task = Task.objects.create(public_id=event.task_public_id, description=event.description, cost_assign=cost_assign, cost_close=cost_close) task.save() send_event(producer, topic, schema_registry, version, TaskCostAssigned(task_public_id=str(task.public_id), description=task.description, cost_assign=cost_assign, cost_close=cost_close))
def TaskClosedHandler(event, producer, topic, schema_registry, version): with transaction.atomic(): emp = Employee.objects.get(public_id=event.assignee_public_id) task = Task.objects.get(public_id=event.task_public_id) tr = Transaction.objects.create(account_id=emp, plus=task.cost_close, description='Close of task ' + task.description, kind=TransactionKind.TASK_CLOSED) tr.save() emp.wallet += tr.plus emp.save() send_event(producer, topic, schema_registry, version, TransactionCreated(account_public_id=str(emp.public_id), task_public_id=str(task.public_id), kind=TransactionKind.TASK_CLOSED, ts = datetime.now()))
def add_task(request): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = AddTaskForm(request.POST) # check whether it's valid: if form.is_valid(): try: with transaction.atomic(): new_task = Task.objects.create(description=form.cleaned_data['description']) new_task.save() send_event(producer, 'tasks', registry, 1, TaskCreated(task_public_id=str(new_task.public_id), description=new_task.description)) except Exception as e: error_message = str(e) return render(request, 'pjira/add_task.html', {'form': form, 'error_message': error_message}) return HttpResponseRedirect(reverse('pjira:index')) else: form = AddTaskForm() return render(request, 'pjira/add_task.html', {'form': form})
def create_employee(name, email, password, roles, phone_number, slack_id): emp_list = Employee.objects.filter(email=email) if len(emp_list) != 0: raise ValueError('Email already registered') with transaction.atomic(): emp = Employee.objects.create(name=name, email=email, password=password, roles=roles, phone_number=phone_number, slack_id=slack_id) emp.save() eventV2 = AccountCreatedv2(account_public_id=str(emp.public_id), name=emp.name, email=emp.email, roles=emp.roles, phone_number=emp.phone_number, slack_id=emp.slack_id) send_event(auth_service_producer, 'accounts', registry, 2, eventV2)
def close_task(request, task_id): if request.method == 'POST': try: task = Task.objects.get(pk=task_id) except Task.DoesNotExist: raise Http404("Task does not exist") if task.assignee is None: return HttpResponseServerError('Cannot close not assigned task') decoded = jwt.decode(request.COOKIES['jwt'], settings.SECRET_KEY, algorithms=[settings.JWT_ALGO]) my_id = decoded['id'] if my_id != str(task.assignee.public_id): return HttpResponseServerError('You cannot close task not assigned to you') with transaction.atomic(): task.status = TaskStatus.CLOSED task.save() send_event(producer, 'tasks', registry, 1, TaskClosed(task_public_id=str(task.public_id), assignee_public_id=str(task.assignee.public_id))) return HttpResponseRedirect(reverse('pjira:index')) return HttpResponseServerError("Wrong method")
def update_employee_by_email(email, name, password, roles, phone_number, slack_id): try: acc = Employee.objects.get(email=email) except Employee.DoesNotExist: raise Http404("Account does not exist") with transaction.atomic(): acc.name = name acc.password = password acc.roles = roles acc.phone_number = phone_number acc.slack_id = slack_id acc.save() eventV2 = AccountChangedv2(account_public_id=str(acc.public_id), name=acc.name, email=acc.email, roles=acc.roles, phone_number=acc.phone_number, slack_id=acc.slack_id) send_event(auth_service_producer, 'accounts', registry, 2, eventV2)