Commit e7c08ebc by Estók Dániel

Modifieed client-side fornt-end.

parent b5ff1ab1
Pipeline #20 failed with stage
in 0 seconds
......@@ -52,6 +52,31 @@ $(function () {
return false;
});
// Setty modal dialog (creating service)
$('.setty-create').click(function(e) {
$.ajax({
type: 'GET',
url: $(this).prop('href'),
success: function(data) {
$('body').append(data);
var modal = $('#confirmation-modal');
modal.modal('show');
modal.on('hidden.bs.modal', function() {
modal.remove();
});
// Handling create button event
$("#setty-create-button").click(function() {
return true;
});
}
});
return false;
});
$('.template-choose').click(function(e) {
$.ajax({
type: 'GET',
......
......@@ -13,7 +13,7 @@
<a href="{% url 'setty.views.service-detail' pk=s.pk %}" class="list-group-item
{% if forloop.last and templates|length < 5 %} list-group-item-last{% endif %}">
<span class="index-template-list-name">
{{ s.name }}
<i class="fa fa-server"></i>&nbsp;{{ s.name }}
</span>
<small class="text-muted index-template-list-system">{{ s.status }}</small>
<div data-href="{% url 'setty.views.service-start' pk=s.pk %}" class="pull-right">
......@@ -54,7 +54,7 @@
<a href="{% url 'setty.views.service-list' %}" class="btn btn-primary btn-xs">
<i class="fa fa-chevron-circle-right"></i> {% trans "list" %}
</a>
<a href="{% url 'setty.views.service-create' %}" class="btn btn-success btn-xs">
<a href="{% url 'setty.views.service-create' %}" class="btn btn-success btn-xs setty-create">
<i class="fa fa-plus-circle"></i> {% trans "new" %}
</a>
</div>
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('setty', '0010_auto_20160126_1258'),
]
operations = [
migrations.RenameField(
model_name='element',
old_name='anchors',
new_name='anchor_number',
),
migrations.RenameField(
model_name='element',
old_name='pos_x',
new_name='pos_top',
),
migrations.RenameField(
model_name='element',
old_name='pos_y',
new_name='position_left',
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('setty', '0011_auto_20160308_1432'),
]
operations = [
migrations.RenameField(
model_name='element',
old_name='pos_top',
new_name='position_top',
),
]
......@@ -48,9 +48,9 @@ class Element(Model):
service = models.ForeignKey(Service, on_delete=models.CASCADE)
parameters = models.TextField()
display_id = models.TextField()
pos_x = models.FloatField() # Stores a rate.
pos_y = models.FloatField() # Stores concrete position.
anchors = models.PositiveSmallIntegerField()
position_left = models.FloatField()
position_top = models.FloatField()
anchor_number = models.PositiveSmallIntegerField()
def __unicode__(self):
return "%s (%s)" % (self.service.name, self.display_id)
......
{% load i18n %}
<p class="text-muted">
{% trans "Services lets you to create virtual machines or services graphically." %}
</p>
<form method="post" action="{% url 'setty.views.service-create' %}">
{% csrf_token %}
<div class="row">
<div class="col-xs-12 text-left">
<label>{% trans 'Name' %}</label>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<input name="serviceName" class="form-control" type="text" />
</div>
&nbsp;
<div class="col-xs-12 text-left">
<input type="submit" class="btn btn-primary" value="{% trans 'Create' %}" />
</div>
</div>
</form>
<!DOCTYPE html>
{% extends "dashboard/base.html" %}
{% load staticfiles %}
{% load i18n %}
{% block title-page %}{% trans 'Setty' %}{% endblock %}
{% block content %}
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link type="text/css" rel="stylesheet" href="{% static 'setty/style.css' %}">
<div class="row" id="workspace">
<div class="col-md-3">
<div class="panel panel-default initHidden" id="informationPanel">
<div class="panel-heading text-center">
<div class="row">
<div class="col-xs-10 text-left">
<h3 class="no-margin"><i class="fa fa-info"></i>&nbsp;{% trans 'Information' %}</h3>
</div>
<div class="col-xs-2">
<button class="btn btn-danger btn-xs" id="closeInfoPanel">
<i class="fa fa-times"></i>
</button>
</div>
</div>
</div>
<div class="panel-body" id="informationContainer">
</div>
<div class="panel-footer">
<label></label>
</div>
</div>
<div class="panel panel-default text-center" id="dragPanel">
<div class="panel-heading">
<div class="row">
<div class="col-xs-12 text-left">
<h3 class="no-margin"><i class="fa fa-outdent"></i> {% trans 'Elements' %}</h3>
</div>
</div>
</div>
<div class="panel-heading text-center">
<div class="row" id="searchElementTemplate">
<div class="col-md-12">
<input type="text" class="form-control" id="searchElementTemplateInput" placeholder="{% trans 'Search' %}"/>
</div>
&nbsp;
<div class="col-md-12">
<button class="btn btn-primary btn-xs btn-block" id="dragContainerScrollUp">
<i class="fa fa-chevron-up"></i>
</button>
</div>
</div>
</div>
<div class="panel-body container-fluid" id="dragContainer">
{% for element in elementTemplateList %}
<div class="col-md-12 col-sm-4" id="elementTemplatePanel">
<div class="panel panel-default">
<div class="panel-heading">
<div class="row text-center">
<div class="col-xs-9 text-center">
<label class="no-margin">{{ element.name }}</label>
</div>
<div class="col-xs-3 text-right">
<button class="btn btn-primary btn-xs elementTemplateInfo" element="{{ element.id }}"><i class="fa fa-info"></i></button>
</div>
</div>
</div>
<div class="panel-body">
<img class="elementTemplate" type="{% for target in element.compatibles.all %}{{ target.id }},{% endfor %}"
id="{{ element.id }}" desc="{{ element.description }}"
src="{% static element.logo %}" alt="{{ element.name }}"
ondragstart="return false;"/>
</div>
</div>
</div>
{% endfor %}
</div>
<div class="panel-footer">
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary btn-xs btn-block" id="dragContainerScrollDown"><i class="fa fa-chevron-down"></i></button>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading text-center">
<div class="row">
<div class="col-xs-2 text-left">
<button class="btn btn-info btn-xs" id="undoMovement" title="{% trans 'Undo' %}"><i class="fa fa-undo"></i></button>
</div>
<div class="col-xs-2 text-left">
<button class="btn btn-info btn-xs" id="redoMovement" title="{% trans 'Redo' %}"><i class="fa fa-repeat"></i></button>
</div>
<div class="col-xs-4">
<h3 class="no-margin" id="serviceName">Service</h3>
</div>
<div class="col-xs-2 text-right">
<button class="btn btn-info btn-xs" id="clearService" title="{% trans 'Clear workspace' %}"><i class="fa fa-eraser"></i></button>
</div>
<div class="col-xs-2 text-right">
<button class="btn btn-success btn-xs" id="saveService" title="{% trans 'Save workspace' %}"><i class="fa fa-floppy-o"></i></button>
</div>
</div>
</div>
<div class="panel-body" id="dropContainer" oncontextmenu="return false;"></div>
<div class="panel-footer no-margin text-left">
<div class="row">
<div class="col-xs-2 text-left">
<label class="no-margin" id="serviceStatus"></label>
</div>
<div class="col-xs-2 col-xs-push-8 text-right">
<button class="btn btn-danger btn-xs" id="deteleService" title="{% trans 'Delete service' %}" data-toggle="modal" data-target="#deleteServiceDialog"><i class="fa fa-trash-o"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div id="deleteServiceDialog" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title"><i class="fa fa-trash-o"></i> {% trans 'Deleting service' %}</h4>
</div>
<div class="modal-body">
<p>{% trans 'Are you sure you want to delete this service?' %}</p>
</div>
<div class="modal-footer">
<div class="row">
<div class="col-xs-2">
<button type="button" class="btn btn-primary btn-md" data-dismiss="modal">{% trans 'Close' %}</button>
</div>
<div class="col-xs-2 col-xs-push-8">
<form method="post" action="{% url 'setty.views.service-delete' actualId %}">
{% csrf_token %}
<input type="submit" class="btn btn-danger btn-md" value="{% trans 'Delete' %}" />
</form>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
......@@ -30,9 +30,6 @@ urlpatterns = [
name='setty.views.service-start'),
url(r'^stop/(?P<pk>\d+)$',
views.StopView.as_view(),
name='setty.views.service-start'),
url(r'^stop/(?P<pk>\d+)$',
views.StartView.as_view(),
name='setty.views.service-stop'),
url(r'^list/$',
views.ListView.as_view(),
......
......@@ -19,9 +19,11 @@ from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.http import JsonResponse
from django.shortcuts import redirect
from braces.views import LoginRequiredMixin
from django.views.generic import TemplateView, DeleteView, CreateView
from django.views.generic import TemplateView, DeleteView
from .models import Element, ElementTemplate, ElementConnection, Service
from django.utils.translation import ugettext as _
import json
......@@ -31,35 +33,28 @@ class DetailView(LoginRequiredMixin, TemplateView):
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['elementTemplateList'] = ElementTemplate.objects.all()
context['actualId'] = kwargs['pk']
# context['elementCategoryList'] = ...
return context
def post(self, request, *args, **kwargs):
if self.request.POST.get('event') == "saveService":
data = json.loads(self.request.POST.get('data'))
service_name = data['serviceName']
if 'pk' in kwargs:
service = Service.objects.get(id=kwargs['pk'])
service.name = service_name
service.save()
Element.objects.filter(service=service).delete()
service = Service.objects.get(id=kwargs['pk'])
service.name = data['serviceName']
service.save()
else:
service = Service(
name=service_name,
user=self.request.user
)
service.save()
Element.objects.filter(service=service).delete()
for element in data['elements']:
elementObject = Element(
service=service,
parameters=element['parameters'],
display_id=element['displayId'],
pos_x=element['posX'],
pos_y=element['posY'],
anchors=element['anchors']
position_left=element['positionLeft'],
position_top=element['positionTop'],
anchor_number=element['anchorNumber']
)
elementObject.save()
......@@ -102,9 +97,9 @@ class DetailView(LoginRequiredMixin, TemplateView):
elements.append({
'parameters': item.parameters,
'displayId': item.display_id,
'posX': item.pos_x,
'posY': item.pos_y,
'anchors': item.anchors})
'positionLeft': item.position_left,
'positionTop': item.position_top,
'anchorNumber': item.anchor_number})
for item in elementConnectionList:
elementConnections.append({
......@@ -126,8 +121,37 @@ class DeleteView(LoginRequiredMixin, DeleteView):
success_url = reverse_lazy("dashboard.index")
class CreateView(LoginRequiredMixin, CreateView):
pass
class CreateView(LoginRequiredMixin, TemplateView):
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/_modal.html']
else:
return ['dashboard/nojs-wrapper.html']
def get_context_data(self, *args, **kwargs):
context = super(CreateView, self).get_context_data(*args, **kwargs)
context.update({
'box_title': _('Create service'),
'ajax_title': True,
'template': "setty/create-service.html",
})
return context
def post(self, request, *args, **kwargs):
service_name = self.request.POST.get('serviceName')
if not service_name:
service_name="Noname"
service = Service(
name=service_name,
user=self.request.user
)
service.save()
return redirect('setty.views.service-detail', pk=service.pk)
class StartView(LoginRequiredMixin, TemplateView):
......
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.http import JsonResponse
from braces.views import LoginRequiredMixin
from django.views.generic import TemplateView, DeleteView
from .models import Element, ElementTemplate, ElementConnection, Service
import json
class DetailView(LoginRequiredMixin, TemplateView):
taaemplate_name = "setty/index.html"
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['elementTemplateList'] = ElementTemplate.objects.all()
return context
def post(self, request, *args, **kwargs):
if self.request.POST.get('event') == "saveService":
data = json.loads(self.request.POST.get('data'))
service_name = data['serviceName']
if 'pk' in kwargs:
service = Service.objects.get(id=kwargs['pk'])
service.name = service_name
service.save()
Element.objects.filter(service=service).delete()
else:
service = Service(
name=service_name,
user=self.request.user
)
service.save()
for element in data['elements']:
elementObject = Element(
service=service,
parameters=element['parameters'],
display_id=element['displayId'],
pos_x=element['posX'],
pos_y=element['posY'],
anchors=element['anchors']
)
elementObject.save()
for elementConnection in data['elementConnections']:
sourceId = elementConnection['sourceId']
targetId = elementConnection['targetId']
sourceEndpoint = elementConnection['sourceEndpoint']
targetEndpoint = elementConnection['targetEndpoint']
connectionParameters = elementConnection['parameters']
targetObject = Element.objects.get(
display_id=targetId,
service=service)
sourceObject = Element.objects.get(
display_id=sourceId,
service=service)
connectionObject = ElementConnection(
target=targetObject,
source=sourceObject,
target_endpoint=targetEndpoint,
source_endpoint=sourceEndpoint,
parameters=connectionParameters
)
connectionObject.save()
return JsonResponse({'serviceName': service.name})
elif self.request.POST.get('event') == "loadService":
service = Service.objects.get(id=kwargs['pk'])
elementList = Element.objects.filter(service=service)
elementConnectionList = ElementConnection.objects.filter(
Q(target__in=elementList) | Q(source__in=elementList))
elements = []
elementConnections = []
for item in elementList:
elements.append({
'parameters': item.parameters,
'displayId': item.display_id,
'posX': item.pos_x,
'posY': item.pos_y,
'anchors': item.anchors})
for item in elementConnectionList:
elementConnections.append({
'targetEndpoint': item.target_endpoint,
'sourceEndpoint': item.source_endpoint,
'parameters': item.parameters})
return JsonResponse(
{'elements': elements,
'elementConnections': elementConnections,
'serviceName': service.name})
else:
raise PermissionDenied
class DeleteView(LoginRequiredMixin, DeleteView):
model = Service
success_url = reverse_lazy("dashboard.index")
class CreateView(LoginRequiredMixin, TemplateView):
pass
class StartView(LoginRequiredMixin, TemplateView):
pass
class StopView(LoginRequiredMixin, TemplateView):
pass
class ListView(LoginRequiredMixin, TemplateView):
pass
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.http import JsonResponse
from braces.views import LoginRequiredMixin
from django.views.generic import TemplateView, DeleteView
from .models import Element, ElementTemplate, ElementConnection, Service
import json
class DetailView(LoginRequiredMixin, TemplateView):
template_name = "setty/index.html"
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['elementTemplateList'] = ElementTemplate.objects.all()
return context
def post(self, request, *args, **kwargs):
if self.request.POST.get('event') == "saveService":
data = json.loads(self.request.POST.get('data'))
service_name = data['serviceName']
if 'pk' in kwargs:
service = Service.objects.get(id=kwargs['pk'])
service.name = service_name
service.save()
Element.objects.filter(service=service).delete()
else:
service = Service(
name=service_name,
user=self.request.user
)
service.save()
for element in data['elements']:
elementObject = Element(
service=service,
parameters=element['parameters'],
display_id=element['displayId'],
pos_x=element['posX'],
pos_y=element['posY'],
anchors=element['anchors']
)
elementObject.save()
for elementConnection in data['elementConnections']:
sourceId = elementConnection['sourceId']
targetId = elementConnection['targetId']
sourceEndpoint = elementConnection['sourceEndpoint']
targetEndpoint = elementConnection['targetEndpoint']
connectionParameters = elementConnection['parameters']
targetObject = Element.objects.get(
display_id=targetId,
service=service)
sourceObject = Element.objects.get(
display_id=sourceId,
service=service)
connectionObject = ElementConnection(
target=targetObject,
source=sourceObject,
target_endpoint=targetEndpoint,
source_endpoint=sourceEndpoint,
parameters=connectionParameters
)
connectionObject.save()
return JsonResponse({'serviceName': service.name})
elif self.request.POST.get('event') == "loadService":
service = Service.objects.get(id=kwargs['pk'])
elementList = Element.objects.filter(service=service)
elementConnectionList = ElementConnection.objects.filter(
Q(target__in=elementList) | Q(source__in=elementList))
elements = []
elementConnections = []
for item in elementList:
elements.append({
'parameters': item.parameters,
'displayId': item.display_id,
'posX': item.pos_x,
'posY': item.pos_y,
'anchors': item.anchors})
for item in elementConnectionList:
elementConnections.append({
'targetEndpoint': item.target_endpoint,
'sourceEndpoint': item.source_endpoint,
'parameters': item.parameters})
return JsonResponse(
{'elements': elements,
'elementConnections': elementConnections,
'serviceName': service.name})
else:
raise PermissionDenied
class DeleteView(LoginRequiredMixin, DeleteView):
model = Service
success_url = reverse_lazy("dashboard.index")
class CreateView(LoginRequiredMixin, TemplateView):
template_name = "setty/create-service.html"
def get_context_data(self, *args, **kwargs):
context = super(hoose, self).get_context_data(*args, **kwargs) context.update({
'box_title': _('Choose template'),
'ajax_title': True,
'template': "setty/create-service.html",
'templates': "",
})
return context
class StartView(LoginRequiredMixin, TemplateView):
pass
class StopView(LoginRequiredMixin, TemplateView):
pass
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.http import JsonResponse
from braces.views import LoginRequiredMixin
from django.views.generic import TemplateView, DeleteView
from .models import Element, ElementTemplate, ElementConnection, Service
from django.utils.translation import ugettext as _
import json
class DetailView(LoginRequiredMixin, TemplateView):
template_name = "setty/index.html"
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['elementTemplateList'] = ElementTemplate.objects.all()
return context
def post(self, request, *args, **kwargs):
if self.request.POST.get('event') == "saveService":
data = json.loads(self.request.POST.get('data'))
service_name = data['serviceName']
if 'pk' in kwargs:
service = Service.objects.get(id=kwargs['pk'])
service.name = service_name
service.save()
Element.objects.filter(service=service).delete()
else:
service = Service(
name=service_name,
user=self.request.user
)
service.save()
for element in data['elements']:
elementObject = Element(
service=service,
parameters=element['parameters'],
display_id=element['displayId'],
pos_x=element['posX'],
pos_y=element['posY'],
anchors=element['anchors']
)
elementObject.save()
for elementConnection in data['elementConnections']:
sourceId = elementConnection['sourceId']
targetId = elementConnection['targetId']
sourceEndpoint = elementConnection['sourceEndpoint']
targetEndpoint = elementConnection['targetEndpoint']
connectionParameters = elementConnection['parameters']
targetObject = Element.objects.get(
display_id=targetId,
service=service)
sourceObject = Element.objects.get(
display_id=sourceId,
service=service)
connectionObject = ElementConnection(
target=targetObject,
source=sourceObject,
target_endpoint=targetEndpoint,
source_endpoint=sourceEndpoint,
parameters=connectionParameters
)
connectionObject.save()
return JsonResponse({'serviceName': service.name})
elif self.request.POST.get('event') == "loadService":
service = Service.objects.get(id=kwargs['pk'])
elementList = Element.objects.filter(service=service)
elementConnectionList = ElementConnection.objects.filter(
Q(target__in=elementList) | Q(source__in=elementList))
elements = []
elementConnections = []
for item in elementList:
elements.append({
'parameters': item.parameters,
'displayId': item.display_id,
'posX': item.pos_x,
'posY': item.pos_y,
'anchors': item.anchors})
for item in elementConnectionList:
elementConnections.append({
'targetEndpoint': item.target_endpoint,
'sourceEndpoint': item.source_endpoint,
'parameters': item.parameters})
return JsonResponse(
{'elements': elements,
'elementConnections': elementConnections,
'serviceName': service.name})
else:
raise PermissionDenied
class DeleteView(LoginRequiredMixin, DeleteView):
model = Service
success_url = reverse_lazy("dashboard.index")
class CreateView(LoginRequiredMixin, TemplateView):
template_name = "setty/create-service.html"
def get_template_names(self):
return ['dashboard/_modal.html']
def get_context_data(self, *args, **kwargs):
context = super(CreateView, self).get_context_data(*args, **kwargs)
context.update({
'box_title': _('Create service'),
'ajax_title': True,
'template': "setty/create-service.html",
'templates': "",
})
return context
class StartView(LoginRequiredMixin, TemplateView):
pass
class StopView(LoginRequiredMixin, TemplateView):
pass
class ListView(LoginRequiredMixin, TemplateView):
pass
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.http import JsonResponse
from braces.views import LoginRequiredMixin
from django.views.generic import TemplateView, DeleteView
from .models import Element, ElementTemplate, ElementConnection, Service
from django.utils.translation import ugettext as _
import json
class DetailView(LoginRequiredMixin, TemplateView):
template_name = "setty/index.html"
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['elementTemplateList'] = ElementTemplate.objects.all()
return context
def post(self, request, *args, **kwargs):
if self.request.POST.get('event') == "saveService":
data = json.loads(self.request.POST.get('data'))
service_name = data['serviceName']
if 'pk' in kwargs:
service = Service.objects.get(id=kwargs['pk'])
service.name = service_name
service.save()
Element.objects.filter(service=service).delete()
else:
service = Service(
name=service_name,
user=self.request.user
)
service.save()
for element in data['elements']:
elementObject = Element(
service=service,
parameters=element['parameters'],
display_id=element['displayId'],
pos_x=element['posX'],
pos_y=element['posY'],
anchors=element['anchors']
)
elementObject.save()
for elementConnection in data['elementConnections']:
sourceId = elementConnection['sourceId']
targetId = elementConnection['targetId']
sourceEndpoint = elementConnection['sourceEndpoint']
targetEndpoint = elementConnection['targetEndpoint']
connectionParameters = elementConnection['parameters']
targetObject = Element.objects.get(
display_id=targetId,
service=service)
sourceObject = Element.objects.get(
display_id=sourceId,
service=service)
connectionObject = ElementConnection(
target=targetObject,
source=sourceObject,
target_endpoint=targetEndpoint,
source_endpoint=sourceEndpoint,
parameters=connectionParameters
)
connectionObject.save()
return JsonResponse({'serviceName': service.name})
elif self.request.POST.get('event') == "loadService":
service = Service.objects.get(id=kwargs['pk'])
elementList = Element.objects.filter(service=service)
elementConnectionList = ElementConnection.objects.filter(
Q(target__in=elementList) | Q(source__in=elementList))
elements = []
elementConnections = []
for item in elementList:
elements.append({
'parameters': item.parameters,
'displayId': item.display_id,
'posX': item.pos_x,
'posY': item.pos_y,
'anchors': item.anchors})
for item in elementConnectionList:
elementConnections.append({
'targetEndpoint': item.target_endpoint,
'sourceEndpoint': item.source_endpoint,
'parameters': item.parameters})
return JsonResponse(
{'elements': elements,
'elementConnections': elementConnections,
'serviceName': service.name})
else:
raise PermissionDenied
class DeleteView(LoginRequiredMixin, DeleteView):
model = Service
success_url = reverse_lazy("dashboard.index")
class CreateView(LoginRequiredMixin, TemplateView):
def get_template_names(self):
return ['dashboard/_modal.html']
def get_context_data(self, *args, **kwargs):
context = super(CreateView, self).get_context_data(*args, **kwargs)
context.update({
'box_title': _('Create service'),
'ajax_title': True,
'template': "setty/create-service.html",
'templates': "",
})
return context
class StartView(LoginRequiredMixin, TemplateView):
pass
class StopView(LoginRequiredMixin, TemplateView):
pass
class ListView(LoginRequiredMixin, TemplateView):
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment