Replacing task_view with task_type in models.Task

* task_view was bound to the view name, which was not consistent
   for filtering as there are several ways of creating a task.
 * Changed settings to also be used via that task_type field.

Change-Id: I4b8e84c6470ea538416842353737128cb7bb08fb
This commit is contained in:
adriant 2015-09-30 18:32:38 +13:00 committed by Dale Smith
parent 142e41568e
commit d4759d6734
8 changed files with 65 additions and 51 deletions

View File

@ -57,8 +57,8 @@ TOKEN_SUBMISSION_URL: http://192.168.122.160:8080/token/
# Additonal actions for views:
# - The order of the actions matters. These will run after the default action,
# in the given order.
TASKVIEW_SETTINGS:
CreateProject:
TASK_SETTINGS:
create_project:
actions:
- AddAdminToProject
- DefaultProjectResources
@ -80,7 +80,7 @@ TASKVIEW_SETTINGS:
reply: no-reply@example.com
template: completed.txt
html_template: completed.txt
InviteUser:
invite_user:
emails:
# To not send this email, set the value to null,
# or don't have the field there at all.
@ -95,7 +95,7 @@ TASKVIEW_SETTINGS:
reply: no-reply@example.com
template: completed.txt
html_template: completed.txt
ResetPassword:
reset_password:
emails:
token:
subject: Your Token
@ -107,16 +107,7 @@ TASKVIEW_SETTINGS:
reply: no-reply@example.com
template: completed.txt
html_template: completed.txt
UserList:
emails:
initial: null
token:
subject: Invitation to join the Catalyst Cloud(rego)
reply: no-reply@example.com
template: token.txt
html_template: token.txt
completed: null
EditUser:
edit_user:
emails:
completed:
subject: Task completed

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='task',
old_name='task_view',
new_name='task_type',
),
]

View File

@ -35,8 +35,8 @@ class Task(models.Model):
keystone_user = JSONField(default={})
project_id = models.CharField(max_length=200, db_index=True, null=True)
# which ActionView initiated this
task_view = models.CharField(max_length=200, db_index=True)
# type of the task, for easy grouping
task_type = models.CharField(max_length=200, db_index=True)
# Effectively a log of what the actions are doing.
action_notes = JSONField(default={})
@ -77,7 +77,7 @@ class Task(models.Model):
"keystone_user": self.keystone_user,
"project_id": self.project_id,
"actions": actions,
"task_view": self.task_view,
"task_type": self.task_type,
"action_notes": self.action_notes,
"cancelled": self.cancelled,
"approved": self.approved,

View File

@ -26,8 +26,7 @@ class UserList(tasks.InviteUser):
@utils.mod_or_owner
def get(self, request):
"""Get a list of all users who have been added to a tenant"""
class_conf = settings.TASKVIEW_SETTINGS.get(self.__class__.__name__,
{})
class_conf = settings.TASK_SETTINGS.get('edit_user', {})
filters = class_conf.get('filters', [])
user_list = []
id_manager = user_store.IdentityManager()
@ -59,13 +58,13 @@ class UserList(tasks.InviteUser):
# Get my active tasks for this project:
project_tasks = models.Task.objects.filter(
project_id=project_id,
task_view='UserList',
task_type="invite_user",
completed=0,
cancelled=0)
# get the actions for the related tasks
# NOTE(adriant): We should later check for the correct action type
# if this task_view ends up having more than one.
# if this task_type ends up having more than one.
registrations = []
for task in project_tasks:
registrations.extend(task.actions)
@ -87,6 +86,7 @@ class UserList(tasks.InviteUser):
class UserDetail(tasks.TaskView):
task_type = 'edit_user'
@utils.mod_or_owner
def get(self, request, user_id):
@ -125,7 +125,7 @@ class UserDetail(tasks.TaskView):
status=501)
project_tasks = models.Task.objects.filter(
project_id=project_id,
task_view='UserList',
task_type="invite_user",
completed=0,
cancelled=0)
for task in project_tasks:
@ -140,6 +140,7 @@ class UserDetail(tasks.TaskView):
class UserRoles(tasks.TaskView):
default_action = 'EditUserRoles'
task_type = 'edit_roles'
@utils.mod_or_owner
def get(self, request, user_id):
@ -206,6 +207,7 @@ class UserRoles(tasks.TaskView):
class RoleList(tasks.TaskView):
task_type = 'edit_roles'
@utils.mod_or_owner
def get(self, request):

View File

@ -42,8 +42,7 @@ class TaskView(APIViewWithLogger):
The get method will return a json listing the actions this
view will run, and the data fields that those actions require.
"""
class_conf = settings.TASKVIEW_SETTINGS.get(self.__class__.__name__,
{})
class_conf = settings.TASK_SETTINGS.get(self.task_type, {})
actions = [self.default_action, ]
@ -69,8 +68,7 @@ class TaskView(APIViewWithLogger):
function on all the actions.
"""
class_conf = settings.TASKVIEW_SETTINGS.get(self.__class__.__name__,
{})
class_conf = settings.TASK_SETTINGS.get(self.task_type, {})
actions = [self.default_action, ]
@ -110,11 +108,11 @@ class TaskView(APIViewWithLogger):
task = Task.objects.create(
ip_address=ip_address, keystone_user=keystone_user,
project_id=keystone_user['project_id'],
task_view=self.__class__.__name__)
task_type=self.task_type)
except KeyError:
task = Task.objects.create(
ip_address=ip_address, keystone_user=keystone_user,
task_view=self.__class__.__name__)
task_type=self.task_type)
task.save()
for i, action in enumerate(action_list):
@ -216,8 +214,7 @@ class TaskView(APIViewWithLogger):
if need_token:
token = create_token(task)
try:
class_conf = settings.TASKVIEW_SETTINGS[
self.__class__.__name__]
class_conf = settings.TASK_SETTINGS[self.task_type]
# will throw a key error if the token template has not
# been specified
@ -279,8 +276,8 @@ class TaskView(APIViewWithLogger):
task.save()
# Sending confirmation email:
class_conf = settings.TASKVIEW_SETTINGS.get(
self.__class__.__name__, {})
class_conf = settings.TASK_SETTINGS.get(
self.task_type, {})
email_conf = class_conf.get(
'emails', {}).get('completed', None)
send_email(task, email_conf)
@ -293,6 +290,8 @@ class TaskView(APIViewWithLogger):
class CreateProject(TaskView):
task_type = "create_project"
default_action = "NewProject"
def post(self, request, format=None):
@ -324,6 +323,8 @@ class CreateProject(TaskView):
class InviteUser(TaskView):
task_type = "invite_user"
default_action = 'NewUser'
@utils.mod_or_owner
@ -336,10 +337,8 @@ class InviteUser(TaskView):
Invites a user to the current tenant.
This endpoint requires either Admin access or the
request to come from a project_owner.
request to come from a project_owner|project_mod.
As such this Task is considered pre-approved.
Runs process_actions, then does the approve step and
post_approve validation, and creates a Token if valid.
"""
self.logger.info("(%s) - New AttachUser request." % timezone.now())
@ -347,6 +346,9 @@ class InviteUser(TaskView):
if 'project_id' not in request.data or request.data['project_id'] is None:
request.data['project_id'] = request.keystone_user['project_id']
# TODO: First check if the user already exists or is pending
# We should not allow duplicate invites.
processed = self.process_actions(request)
errors = processed.get('errors', None)
@ -363,6 +365,8 @@ class InviteUser(TaskView):
class ResetPassword(TaskView):
task_type = "reset_password"
default_action = 'ResetUser'
def post(self, request, format=None):
@ -386,12 +390,13 @@ class ResetPassword(TaskView):
class EditUser(TaskView):
task_type = "edit_user"
default_action = 'EditUser'
@utils.mod_or_owner
def get(self, request):
class_conf = settings.TASKVIEW_SETTINGS.get(self.__class__.__name__,
{})
class_conf = settings.TASK_SETTINGS.get(self.task_type, {})
actions = [self.default_action, ]

View File

@ -311,8 +311,7 @@ class TaskDetail(APIViewWithLogger):
if need_token:
token = create_token(task)
try:
class_conf = settings.TASKVIEW_SETTINGS[
task.task_view]
class_conf = settings.TASK_SETTINGS[task.task_type]
# will throw a key error if the token template has not
# been specified
@ -369,8 +368,7 @@ class TaskDetail(APIViewWithLogger):
task.save()
# Sending confirmation email:
class_conf = settings.TASKVIEW_SETTINGS.get(
task.task_view, {})
class_conf = settings.TASK_SETTINGS.get(task.task_type, {})
email_conf = class_conf.get(
'emails', {}).get('completed', None)
send_email(task, email_conf)
@ -472,8 +470,7 @@ class TokenList(APIViewWithLogger):
token = create_token(task)
try:
class_conf = settings.TASKVIEW_SETTINGS[
task.task_view]
class_conf = settings.TASK_SETTINGS[task.task_type]
# will throw a key error if the token template has not
# been specified
@ -646,8 +643,8 @@ class TokenDetail(APIViewWithLogger):
token.delete()
# Sending confirmation email:
class_conf = settings.TASKVIEW_SETTINGS.get(
token.task.task_view, {})
class_conf = settings.TASK_SETTINGS.get(
token.task.task_type, {})
email_conf = class_conf.get(
'emails', {}).get('completed', None)
send_email(token.task, email_conf)

View File

@ -145,7 +145,7 @@ TOKEN_SUBMISSION_URL = CONFIG['TOKEN_SUBMISSION_URL']
# Additonal actions for views:
# - The order of the actions matters. These will run after the default action,
# in the given order.
TASKVIEW_SETTINGS = CONFIG['TASKVIEW_SETTINGS']
TASK_SETTINGS = CONFIG['TASK_SETTINGS']
ACTION_SETTINGS = CONFIG['ACTION_SETTINGS']

View File

@ -73,8 +73,8 @@ TOKEN_SUBMISSION_URL = 'http://localhost:8080/token/'
# Additonal actions for views:
# - The order of the actions matters. These will run after the default action,
# in the given order.
TASKVIEW_SETTINGS = {
'InviteUser': {
TASK_SETTINGS = {
'invite_user': {
'emails': {
'token': {
'reply': 'no-reply@example.com',
@ -91,7 +91,7 @@ TASKVIEW_SETTINGS = {
}
}
},
'CreateProject': {
'create_project': {
'emails': {
'token': {
'reply': 'no-reply@example.com',
@ -117,7 +117,7 @@ TASKVIEW_SETTINGS = {
'DefaultProjectResources'
]
},
'ResetPassword': {
'reset_password': {
'emails': {
'token': {
'reply': 'no-reply@example.com',
@ -160,7 +160,7 @@ conf_dict = {
"USERNAME_IS_EMAIL": USERNAME_IS_EMAIL,
"KEYSTONE": KEYSTONE,
"DEFAULT_REGION": DEFAULT_REGION,
"TASKVIEW_SETTINGS": TASKVIEW_SETTINGS,
"TASK_SETTINGS": TASK_SETTINGS,
"ACTION_SETTINGS": ACTION_SETTINGS,
"TOKEN_SUBMISSION_URL": TOKEN_SUBMISSION_URL
}