Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _revert_task_from_audit(task_audit):
task = Task.objects.get(id=task_audit['task']['id'])
task.status = task_audit['reverted_status']
task.save()
for assignment_audit in task_audit['assignments']:
_revert_assignment_from_audit(assignment_audit)
if command_type in ('submit', 'accept'):
iteration_status = Iteration.Status.REQUESTED_REVIEW
elif command_type == 'reject':
iteration_status = Iteration.Status.PROVIDED_REVIEW
else:
raise BadRequest('Illegal command')
try:
submit_task(assignment_information['task_id'],
assignment_information['task_data'],
iteration_status,
worker)
return {}
except TaskStatusError:
raise BadRequest('Task already completed')
except Task.DoesNotExist:
raise BadRequest('No task for given id')
except IllegalTaskSubmission as e:
raise BadRequest(e)
except TaskAssignmentError as e:
raise BadRequest(e)
Raises:
orchestra.core.errors.AssignmentPolicyError:
Machine steps cannot be included in an assignment policy.
"""
if related_steps is None:
raise AssignmentPolicyError('No related steps given')
workflow_version = task.step.workflow_version
for step_slug in related_steps:
step = workflow_version.steps.get(slug=step_slug)
if not step.is_human:
raise AssignmentPolicyError('Machine step should not be '
'member of assignment policy')
related_tasks = (
Task.objects
.filter(step__slug__in=related_steps, project=task.project)
.select_related('step'))
for related_task in related_tasks:
entry_level_assignment = assignment_history(related_task).first()
if entry_level_assignment and entry_level_assignment.worker:
try:
return assign_task(entry_level_assignment.worker.id, task.id)
except WorkerCertificationError:
# Task could not be assigned to related worker, try with
# another related worker
logger.warning('Tried to assign worker %s to step %s, for '
'which they are not certified',
entry_level_assignment.worker.id,
task.step.slug, exc_info=True)
except Exception:
logger.warning('Unable to assign task.', exc_info=True)
Information about the specified task assignment.
"""
reviewer_task_assignment = (
TaskAssignment.objects.filter(
task=task_assignment.task)
.order_by('-assignment_counter')[0])
worker = task_assignment.worker
worker_info = {attr: getattr(getattr(worker, 'user', None), attr, None)
for attr in ('username', 'first_name', 'last_name')}
return {
'assignment_id': task_assignment.id,
'task': {
'data': task_assignment.in_progress_task_data,
'status': (dict(Task.STATUS_CHOICES)
[task_assignment.task.status])
},
'worker': worker_info,
'status': (dict(TaskAssignment.STATUS_CHOICES)
[task_assignment.status]),
'is_reviewer': (
task_assignment.id == reviewer_task_assignment.id and
task_assignment.assignment_counter > 0),
'is_read_only': (
task_assignment.status != TaskAssignment.Status.PROCESSING),
}
def warn_staffing_team_about_unstaffed_tasks():
max_unstaffed_datetime = (
timezone.now() - settings.ORCHESTRA_STAFFBOT_STAFFING_MIN_TIME)
# Get all requests without winners
task_values = (
Task.objects.all()
.filter(start_datetime__lt=max_unstaffed_datetime)
.exclude(staffing_requests__inquiries__responses__is_winner=True)
.exclude(status=Task.Status.COMPLETE)
.exclude(status=Task.Status.ABORTED)
.exclude(staffing_requests__isnull=True)
.exclude(staffing_requests__inquiries__isnull=True)
.order_by('-start_datetime')
.values('staffing_requests__required_role_counter', 'id'))
for task_value in task_values:
required_role_counter = task_value[
'staffing_requests__required_role_counter']
request = (
StaffBotRequest.objects.filter(
task=task_value['id'],
required_role_counter=required_role_counter)
return worker.assignments.filter(task__project=project_id).exists()
elif request.method == 'POST':
# Create calls have a task ID
task_id = request.data.get('task')
todo_id = request.data.get('todo')
try:
if task_id:
task = Task.objects.get(id=task_id)
elif todo_id:
task = Todo.objects.get(id=todo_id).task
else:
task = None
return task and \
worker.assignments.filter(
task__project=task.project).exists()
except (Task.DoesNotExist, Todo.DoesNotExist):
return False
return False
def execute(project_id, step_slug):
project = Project.objects.get(id=project_id)
step = Step.objects.get(slug=step_slug,
workflow_version=project.workflow_version)
task = Task.objects.get(project=project,
step=step)
# Run machine function
if step.is_human:
raise MachineExecutionError('Step worker type is not machine')
if task.status == Task.Status.COMPLETE:
raise MachineExecutionError('Task assignment already completed')
# Machine tasks are only assigned to one worker/machine,
# so they should only have one task assignment,
# and should never be submitted for review.
with transaction.atomic():
# Uniqueness constraint on assignnment_counter and task prevents
# concurrent creation of more than one assignment
task_assignment, created = TaskAssignment.objects.get_or_create(
assignment_counter=0,
task=task,
defaults={
'status': TaskAssignment.Status.PROCESSING,
'in_progress_task_data': {}})
if created:
def _notify_slack_status_change(task, current_worker, slack_api_key,
slack_channel, with_slack_link=True,
with_user_mention=False):
slack = OrchestraSlackService(slack_api_key)
slack_statuses = {
Task.Status.PROCESSING: 'Task has been picked up by a worker.',
Task.Status.PENDING_REVIEW: 'Task is awaiting review.',
Task.Status.REVIEWING: 'Task is under review.',
Task.Status.POST_REVIEW_PROCESSING: 'Task was returned by reviewer.',
Task.Status.COMPLETE: 'Task has been completed.',
Task.Status.ABORTED: 'Task has been aborted.',
}
slack_username = getattr(current_worker, 'slack_username', None)
worker_string = current_worker.user.username if current_worker else None
if current_worker and slack_username and with_user_mention:
user_id = slack.users.get_user_id(slack_username)
worker_string += ' (<@{}|{}>)'.format(user_id, slack_username)
slack_message = ('*{}*\n'
'>>>'
'Current worker: {}'
'{}').format(slack_statuses[task.status],
worker_string,
_task_information(
task, with_slack_link=with_slack_link))
slack.chat.post_message(slack_channel, slack_message)
from orchestra.models import Project
from orchestra.models import Step
from orchestra.models import Task
from orchestra.models import TaskAssignment
from orchestra.models import TimeEntry
from orchestra.models import Worker
from orchestra.models import WorkerCertification
from orchestra.models import Workflow
from orchestra.models import WorkflowVersion
admin.site.register(Certification)
admin.site.register(Iteration)
admin.site.register(Project)
admin.site.register(PayRate)
admin.site.register(Step)
admin.site.register(Task)
admin.site.register(TaskAssignment)
admin.site.register(Worker)
admin.site.register(WorkerCertification)
admin.site.register(Workflow)
admin.site.register(WorkflowVersion)
@admin.register(TimeEntry)
class TimeEntryAdmin(admin.ModelAdmin):
list_display = ('id', 'date', 'worker', 'time_worked', 'assignment')
search_fields = ('id', 'worker')
admin.site.site_header = 'Orchestra'
admin.site.site_title = 'Orchestra'
admin.site.index_title = 'Orchestra'