Skip to content

Commit

Permalink
feat: redirection of service group tab
Browse files Browse the repository at this point in the history
  • Loading branch information
djs0109 committed Nov 7, 2022
1 parent 8b82f25 commit 74d82ac
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
31 changes: 24 additions & 7 deletions app/Entirety/devices/templates/devices/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,30 @@
{#</div>#}

<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="tab" href="#devices">Devices</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" href="#groups">Services Group</a>
</li>
{% if to_servicegroup %}
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" href="#devices">Devices</a>
</li>
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="tab" href="#groups">Services Group</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="tab" href="#devices">Devices</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" href="#groups">Services Group</a>
</li>
{% endif %}
</ul>

<div class="tab-content">
<br>
{% if to_servicegroup %}
<div class="tab-pane container fade" id="devices">
{% else %}
<div class="tab-pane container active" id="devices">
{% endif %}
<h3>Devices</h3>
<form action="" method="GET" >
{% csrf_token %}
Expand Down Expand Up @@ -126,7 +139,11 @@ <h5 class="modal-title" id="deleteModalAdvLabel">Delete Device</h5>
</form>
</div>

{% if to_servicegroup %}
<div class="tab-pane container active" id="groups">
{% else %}
<div class="tab-pane container fade" id="groups">
{% endif %}
<h3>Services Group</h3>
<form action="" method="GET" >
{% csrf_token %}
Expand Down Expand Up @@ -175,7 +192,7 @@ <h5 class="modal-title" id="deleteModalLabel">Delete Service Group</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure you want to delete this device?
Are you sure you want to delete this service group?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
Expand Down
14 changes: 11 additions & 3 deletions app/Entirety/devices/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ def get_service_groups(project: Project):

def get_service_group_by_apikey(project: Project, **kwargs):
"""
Get service groups by apikey in current project
Get service groups by apikey in current project by apikey and resource
Args:
project: dict
apikey: str
apikey: see iota_client.get_group
resource: see iota_client.get_group
Returns:
filip.models.ngsi_v2.iot.ServiceGroup
"""
Expand Down Expand Up @@ -327,3 +327,11 @@ def delete_service_group(project: Project, **kwargs):
),
) as iota_client:
iota_client.delete_group(**kwargs)


def add_group_to_session(request):
"""
Add to_servicegroup = True to session
"""
request.session["to_servicegroup"] = True
return request
4 changes: 4 additions & 0 deletions app/Entirety/devices/views/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def get_tables(self):
# add context to html
def get_context_data(self, **kwargs):
context = super(DeviceListView, self).get_context_data(**kwargs)
if self.request.session.get("to_servicegroup"):
context["to_servicegroup"] = True
else:
context["to_servicegroup"] = False
context["project"] = self.project
context["table_devices"] = DeviceListView.get_tables(self)[0]
context["table_groups"] = DeviceListView.get_tables(self)[1]
Expand Down
27 changes: 13 additions & 14 deletions app/Entirety/devices/views/servicegroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
from projects.mixins import ProjectContextMixin
from devices.forms import ServiceGroupBasic, Attributes, Commands
from devices.utils import (
# get_servicegroups,
# post_servicegroup,
# update_servicegroup,
prefix_attributes,
prefix_commands,
get_service_group_by_apikey,
parse_request_data,
build_service_group,
post_service_group,
update_service_group,
delete_service_group
# build_servicegroup,
# get_servicegroup_by_id,
# delete_servicegroup,
# pattern_servicegroups_filter,
delete_service_group,
add_group_to_session
)
from devices.tables import GroupsTable
from requests.exceptions import RequestException
Expand All @@ -34,9 +28,10 @@ def post(self, request, *args, **kwargs):
if request.POST.get("Delete_Group"):
if not request.POST.get("selection"):
messages.error(request, "Please select one service group")
add_group_to_session(request)
return redirect("projects:devices:list", project_id=self.project.uuid)
else:
# use session to cache the selected servicegroups
# use session to cache the selected service group
request.session["resource"], request.session["apikey"] = request.POST.get("selection").split(";")
return redirect("projects:devices:delete_group", project_id=self.project.uuid)

Expand All @@ -48,13 +43,14 @@ def post(self, request, *args, **kwargs):
elif request.POST.get("Edit_Group"):
if not request.POST.get("selection"):
messages.error(request, "Please select one service group")
add_group_to_session(request)
return redirect("projects:devices:list", project_id=self.project.uuid)
else:
request.session["resource"], request.session["apikey"] = request.POST.get("selection").split(";")
return redirect("projects:devices:edit_group", project_id=self.project.uuid)


# Create servicegroups
# Create service group
class ServiceGroupCreateView(ProjectContextMixin, TemplateView):
def get(self, request, *args, **kwargs):
basic_info = ServiceGroupBasic(initial={"resource": "/iot/json"})
Expand Down Expand Up @@ -86,6 +82,7 @@ def post(self, request: HttpRequest, **kwargs):
data_attributes=data_attributes
)
post_service_group(service_group, project=self.project)
add_group_to_session(request)
return redirect("projects:devices:list", project_id=self.project.uuid)
# handel the error from server
except RequestException as e:
Expand All @@ -105,12 +102,12 @@ def post(self, request: HttpRequest, **kwargs):
return render(request, "devices/detail.html", context)


# Edit servicegroups
# Edit service group
class ServiceGroupEditView(ProjectContextMixin, TemplateView):
def get(self, request: HttpRequest, *args, **kwargs):
context = super(ServiceGroupEditView, self).get_context_data()

# get the selected servicegroups from session
# get the selected service group from session
resource = request.session.get("resource")
apikey = request.session.get("apikey")
service_group = get_service_group_by_apikey(project=self.project, apikey=apikey, resource=resource)
Expand Down Expand Up @@ -154,6 +151,7 @@ def post(self, request: HttpRequest, **kwargs):
data_attributes=data_attributes
)
update_service_group(service_group, project=self.project)
add_group_to_session(request)
return redirect("projects:devices:list", project_id=self.project.uuid)
except RequestException as e:
messages.error(request, e.response.content.decode("utf-8"))
Expand All @@ -172,7 +170,7 @@ def post(self, request: HttpRequest, **kwargs):
# Delete ServiceGroup
class ServiceGroupDeleteView(ProjectContextMixin, View):
def get(self, request: HttpRequest, *args, **kwargs):
# get the selected servicegroups from session
# get the selected service group from session
resource = request.session.get("resource")
apikey = request.session.get("apikey")

Expand All @@ -184,5 +182,6 @@ def get(self, request: HttpRequest, *args, **kwargs):
except RequestException as e:
messages.error(request, e.response.content.decode("utf-8"))

# if success, redirect to servicegroups list view
# if success, redirect to service group list view
add_group_to_session(request)
return redirect("projects:devices:list", project_id=self.project.uuid)

0 comments on commit 74d82ac

Please sign in to comment.