Skip to content
Snippets Groups Projects
Commit 90c98604 authored by Stefano Alberto Russo's avatar Stefano Alberto Russo
Browse files

Moved to uuids for primary keys. Added support for deleting a contianer. Minor fixes.

parent e0a7d705
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,7 @@ class TaskStatuses(object):
#=========================
class Profile(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.OneToOneField(User, on_delete=models.CASCADE)
timezone = models.CharField('User Timezone', max_length=36, default='UTC')
authtoken = models.CharField('User auth token', max_length=36, blank=True, null=True)
......@@ -43,6 +44,7 @@ class Profile(models.Model):
#=========================
class LoginToken(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.OneToOneField(User, on_delete=models.CASCADE)
token = models.CharField('Login token', max_length=36)
......@@ -51,9 +53,9 @@ class LoginToken(models.Model):
# Tasks
#=========================
class Task(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, related_name='+', on_delete=models.CASCADE)
tid = models.CharField('Task ID', max_length=64, blank=False, null=False)
uuid = models.CharField('Task UUID', max_length=36, blank=False, null=False)
name = models.CharField('Task name', max_length=36, blank=False, null=False)
status = models.CharField('Task status', max_length=36, blank=True, null=True)
created = models.DateTimeField('Created on', default=timezone.now)
......@@ -105,7 +107,7 @@ class Task(models.Model):
@property
def short_uuid(self):
return self.uuid.split('-')[0]
return str(self.uuid).split('-')[0]
#=========================
......@@ -113,8 +115,9 @@ class Task(models.Model):
#=========================
class Container(models.Model):
#uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, related_name='+', on_delete=models.CASCADE, null=True)
# If a container has no user, it will be available to anyone. Can be created, edited and deleted only by admins.
image = models.CharField('Container image', max_length=255, blank=False, null=False)
type = models.CharField('Container type', max_length=36, blank=False, null=False)
......
......@@ -78,7 +78,7 @@
{% else %}
Ok, Container added. Go back to your <a href="/tasks">tasks list</a>.
Ok, Container added. Go back to your <a href="/containers">container list</a>.
{% endif %}
......
......@@ -8,7 +8,7 @@
<div class="container">
<div class="dashboard">
<div class="span8 offset2">
<h1>Containers List</h1>
<h1>Container List</h1>
<hr/>
......@@ -63,6 +63,11 @@
<td>{{ container.service_ports}}</td>
</tr>
<tr>
<td><b>Operations</b></td>
<td><a href="?action=delete&uuid={{ container.uuid }}">Delete</a></td>
</tr>
</table>
<br />
{% endfor %}
......
......@@ -46,15 +46,15 @@
<tr>
<td><b>Task container</b></td><td>
<select name="task_container_id" >
<select name="task_container_uuid" >
<!-- <option value="metadesktop" selected>Meta Desktop</option>
<option value="astroccok">Astrocook</option>
<option value="gadgetviewer">Gadget Viewer</option> -->
{% for container in data.platform_containers %}
<option value="{{container.id}}">{{container.image}} (platform)</option> -->
<option value="{{container.uuid}}">{{container.image}} (platform)</option> -->
{% endfor %}
{% for container in data.user_containers %}
<option value="{{container.id}}">{{container.image}} (user)</option> -->
<option value="{{container.uuid}}">{{container.image}} (user)</option> -->
{% endfor %}
</select>
......@@ -103,7 +103,7 @@
{% else %}
Ok, task created. Go back to your <a href="/tasks">tasks list</a>.
Ok, task created. Go back to your <a href="/tasks">task list</a>.
{% endif %}
......
......@@ -416,7 +416,6 @@ def tasks(request):
try:
if task.compute == 'local':
str_shortuuid = task.uuid.split('-')[0]
# Delete the Docker container
if standby_supported:
......@@ -578,16 +577,16 @@ def create_task(request):
if task_name:
# Task container
task_container_id = request.POST.get('task_container_id', None)
task_container_uuid = request.POST.get('task_container_uuid', None)
# Get the container object, first try as public and then as private
try:
task_container = Container.objects.get(id=task_container_id, user=None)
task_container = Container.objects.get(uuid=task_container_uuid, user=None)
except Container.DoesNotExist:
try:
task_container = Container.objects.get(id=task_container_id, user=request.user)
task_container = Container.objects.get(uuid=task_container_uuid, user=request.user)
except Container.DoesNotExist:
raise Exception('Consistency error, container with id "{}" does not exists or user "{}" does not have access rights'.format(task_container_id, request.user.email))
raise Exception('Consistency error, container with uuid "{}" does not exists or user "{}" does not have access rights'.format(task_container_uuid, request.user.email))
# Compute
task_compute = request.POST.get('task_compute', None)
......@@ -715,9 +714,32 @@ def containers(request):
data={}
data['user'] = request.user
data['profile'] = Profile.objects.get(user=request.user)
data['title'] = 'Add compute'
data['title'] = 'Containers'
data['name'] = request.POST.get('name',None)
# Get action if any
action = request.GET.get('action', None)
uuid = request.GET.get('uuid', None)
if action and uuid:
if action=='delete':
try:
# Get the task (raises if none available including no permission)
container = Container.objects.get(user=request.user, uuid=uuid)
# Delete
container.delete()
# Unset uuid to load the list again
uuid = None
except Exception as e:
data['error'] = 'Error in deleting the container'
logger.error('Error in deleting task with uuid="{}": "{}"'.format(uuid, e))
return render(request, 'error.html', {'data': data})
# Get containers configured on the platform, both private to this user and public
data['user_containers'] = Container.objects.filter(user=request.user)
data['platform_containers'] = Container.objects.filter(user=None)
......@@ -816,4 +838,3 @@ def add_compute(request):
return render(request, 'add_compute.html', {'data': data})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment