Skip to content

Commit

Permalink
Merge pull request #162 from N5GEH/development
Browse files Browse the repository at this point in the history
new release 2024-02-07
  • Loading branch information
djs0109 authored Feb 7, 2024
2 parents 15979e2 + fa6e582 commit a93dbab
Show file tree
Hide file tree
Showing 41 changed files with 35,672 additions and 103 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,17 @@ See [changelog](./docs/CHANGELOG.md) for detailed overview of changes.
## License

[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](LICENSE)

## Further project information

<a href="https://n5geh.de/"> <img alt="National 5G Energy Hub"
src="https://raw.githubusercontent.com/N5GEH/n5geh.platform/master/docs/logos/n5geh-logo.png" height="100"></a>

## Acknowledgments

We gratefully acknowledge the financial support of the Federal Ministry <br />
for Economic Affairs and Climate Action (BMWK), promotional references
03EN1030B and 03ET1561B.

<a href="https://www.bmwi.de/Navigation/EN/Home/home.html"> <img alt="BMWK"
src="https://raw.githubusercontent.com/N5GEH/n5geh.platform/master/docs/logos/BMWK-logo_en.png" height="100"> </a>
3 changes: 3 additions & 0 deletions app/Entirety/.env.EXAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ OIDC_TOKEN_ROLE_FIELD=roles
CB_URL=http://localhost:1026
IOTA_URL="http://localhost:4041"
QL_URL="http://localhost:8668"

# CSRF
CSRF_TRUSTED_ORIGINS=[]
3 changes: 2 additions & 1 deletion app/Entirety/devices/templates/devices/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ <h3 class="mt-5">Edit Service Group</h3>
{% endif %}
{% csrf_token %}
{% include 'accordion.html' %}
<button class="btn btn-primary rounded-pill" type="submit">Save</button>
<button class="btn btn-primary rounded-pill" type="submit" {% if view_only %}
disabled {% endif %}>Save</button>
</form>

{% endblock %}
24 changes: 15 additions & 9 deletions app/Entirety/devices/templates/devices/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ <h3>Devices</h3>
{% csrf_token %}
<div>
<button type="submit" class="btn btn-primary mb-1 ms-auto" data-toggle="tooltip" data-placement="bottom"
title="Create Device" value="True" name="Create">
title="Create Device" value="True" name="Create" {% if view_only %}
disabled {% endif %}>
<i class="bi bi-plus"></i>
</button>
<button type="submit" class="btn btn-primary mb-1 ms-auto" data-toggle="tooltip" data-placement="bottom"
title="Create Multiple Device" value="True" name="BatchCreate">
title="Create Multiple Device" value="True" name="BatchCreate" {% if view_only %}
disabled {% endif %}>
<i class="bi bi-file-earmark-plus"></i>
</button>
<button type="button" class="btn btn-danger mb-1" data-toggle="tooltip" data-placement="bottom"
title="Delete Device" data-bs-toggle="modal" data-bs-target="#deleteModal">
title="Delete Device" data-bs-toggle="modal" {% if not view_only %} data-bs-target="#deleteModal" {% endif %} {% if view_only %}
disabled {% endif %}>
<i class="bi bi-trash"></i>
</button>
<button type="submit" class="btn btn-secondary mb-1" data-toggle="tooltip" data-placement="bottom"
title="Edit Device" value="True" name="Edit">
<i class="bi bi-pen"></i>
title="Inspect Device" value="True" name="Edit">
<i class="bi bi-eye"></i>
</button>
</div>

Expand Down Expand Up @@ -169,16 +172,19 @@ <h3>Services Group</h3>
{% csrf_token %}
<div>
<button type="submit" class="btn btn-primary mb-1 ms-auto" data-toggle="tooltip" data-placement="bottom"
title="Create Service Group" value="True" name="Create_Group">
title="Create Service Group" value="True" name="Create_Group" {% if view_only %}
disabled {% endif %}>
<i class="bi bi-plus"></i>
</button>
<button type="button" class="btn btn-danger mb-1" data-toggle="tooltip" data-placement="bottom"
title="Delete Service Group" data-bs-toggle="modal" data-bs-target="#deleteModal_group">
title="Delete Service Group" data-bs-toggle="modal" {% if not view_only %}
data-bs-target="#deleteModal_group" {% endif %} {% if view_only %}
disabled {% endif %}>
<i class="bi bi-trash"></i>
</button>
<button type="submit" class="btn btn-secondary mb-1" data-toggle="tooltip" data-placement="bottom"
title="Edit Service Group" value="True" name="Edit_Group">
<i class="bi bi-pen"></i>
title="Inspect Service Group" value="True" name="Edit_Group">
<i class="bi bi-eye"></i>
</button>
</div>

Expand Down
54 changes: 36 additions & 18 deletions app/Entirety/devices/views/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.http import HttpRequest
import json
from entirety.utils import pop_data_from_session, add_data_to_session
from projects.mixins import ProjectContextMixin
from projects.mixins import ProjectContextMixin, ProjectContextAndViewOnlyMixin
import logging
from filip.models.ngsi_v2.iot import Device
from devices.forms import DeviceBasic, Attributes, Commands, DeviceBatchForm
Expand All @@ -22,7 +22,8 @@
get_device_by_id,
delete_device,
pattern_service_groups_filter,
pattern_devices_filter, post_devices,
pattern_devices_filter,
post_devices,
)
from devices.tables import DevicesTable, GroupsTable
from requests.exceptions import RequestException
Expand All @@ -32,7 +33,7 @@


# Devices list
class DeviceListView(ProjectContextMixin, MultiTableMixin, TemplateView):
class DeviceListView(ProjectContextAndViewOnlyMixin, MultiTableMixin, TemplateView):
# TemplateView.as_view() will render the template. Do not need to invoke render function
template_name = "devices/list.html"
# table_class = DevicesTable
Expand Down Expand Up @@ -84,10 +85,18 @@ def get_context_data(self, **kwargs):
context["project"] = self.project
if self.request.GET.get("search-pattern-groups", default=""):
context["to_servicegroup"] = True
context["view_only"] = (
True
if self.request.user in self.project.viewers.all()
and self.request.user not in self.project.maintainers.all()
and self.request.user not in self.project.users.all()
and self.request.user is not self.project.owner
else False
)
return context


class DeviceListSubmitView(ProjectContextMixin, View):
class DeviceListSubmitView(ProjectContextAndViewOnlyMixin, View):
# Redirect the request to corresponding view
def post(self, request, *args, **kwargs):
# press delete button
Expand Down Expand Up @@ -124,18 +133,17 @@ def post(self, request, *args, **kwargs):

# redirect to entity app
add_data_to_session(request, "entities", entities)
return redirect(
"projects:entities:delete",
project_id=self.project.uuid
)
return redirect("projects:entities:delete", project_id=self.project.uuid)

# press create button
elif request.POST.get("Create"):
return redirect("projects:devices:create", project_id=self.project.uuid)

# press batch create button
elif request.POST.get("BatchCreate"):
return redirect("projects:devices:batchcreate", project_id=self.project.uuid)
return redirect(
"projects:devices:batchcreate", project_id=self.project.uuid
)

# press edit button
elif request.POST.get("Edit"):
Expand Down Expand Up @@ -180,11 +188,10 @@ def post(self, request, *args, **kwargs):
if form.is_valid():
devices_json = json.loads(self.request.POST.get("device_json"))
try:
devices = [Device(**device_dict) for device_dict in devices_json["devices"]]
post_devices(
devices,
project=self.project
)
devices = [
Device(**device_dict) for device_dict in devices_json["devices"]
]
post_devices(devices, project=self.project)
logger.info(
"Devices created by "
+ str(
Expand Down Expand Up @@ -254,7 +261,9 @@ def post(self, request: HttpRequest, **kwargs):
if "DUPLICATE_DEVICE_ID" in e.response.content.decode("utf-8"):
device_id = device.device_id
add_data_to_session(request, "search-pattern", device_id)
return redirect("projects:devices:list", project_id=self.project.uuid)
return redirect(
"projects:devices:list", project_id=self.project.uuid
)
logger.error(
str(
self.request.user.first_name
Expand Down Expand Up @@ -283,7 +292,7 @@ def post(self, request: HttpRequest, **kwargs):


# Edit devices
class DeviceEditView(ProjectContextMixin, TemplateView):
class DeviceEditView(ProjectContextAndViewOnlyMixin, TemplateView):
def get(self, request: HttpRequest, *args, **kwargs):
context = super(DeviceEditView, self).get_context_data()

Expand Down Expand Up @@ -319,7 +328,14 @@ def get(self, request: HttpRequest, *args, **kwargs):
commands = Commands(initial=device_dict["commands"], prefix=prefix_commands)
else:
commands = Commands(prefix=prefix_commands)

context["view_only"] = (
True
if self.request.user in self.project.viewers.all()
and self.request.user not in self.project.maintainers.all()
and self.request.user not in self.project.users.all()
and self.request.user is not self.project.owner
else False
)
context = {
"basic_info": basic_info,
"attributes": attributes,
Expand Down Expand Up @@ -404,7 +420,9 @@ def get(self, request: HttpRequest, *args, **kwargs):
for device_id in devices_id:
try:
delete_device(
project=self.project, device_id=device_id, delete_entity=delete_entity
project=self.project,
device_id=device_id,
delete_entity=delete_entity,
)
logger.info(
"Device deleted by "
Expand Down
19 changes: 14 additions & 5 deletions app/Entirety/devices/views/servicegroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.http import HttpRequest
import json
from entirety.utils import add_data_to_session, pop_data_from_session
from projects.mixins import ProjectContextMixin
from projects.mixins import ProjectContextMixin, ProjectContextAndViewOnlyMixin
from devices.forms import ServiceGroupBasic, Attributes, Commands
from devices.utils import (
prefix_attributes,
Expand All @@ -25,7 +25,7 @@
logger = logging.getLogger(__name__)


class ServiceGroupListSubmitView(ProjectContextMixin, View):
class ServiceGroupListSubmitView(ProjectContextAndViewOnlyMixin, View):
# Redirect the request to corresponding view
def post(self, request, *args, **kwargs):
# press delete button
Expand Down Expand Up @@ -137,7 +137,7 @@ def post(self, request: HttpRequest, **kwargs):


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

Expand Down Expand Up @@ -169,7 +169,14 @@ def get(self, request: HttpRequest, *args, **kwargs):
)
else:
attributes = Attributes(prefix=prefix_attributes)

context["view_only"] = (
True
if self.request.user in self.project.viewers.all()
and self.request.user not in self.project.maintainers.all()
and self.request.user not in self.project.users.all()
and self.request.user is not self.project.owner
else False
)
context = {
"basic_info": basic_info,
"attributes": attributes,
Expand Down Expand Up @@ -245,7 +252,9 @@ def get(self, request: HttpRequest, *args, **kwargs):
for service in services:
resource, apikey = service.split(";")
try:
delete_service_group(project=self.project, resource=resource, apikey=apikey)
delete_service_group(
project=self.project, resource=resource, apikey=apikey
)
logger.info(
"Service group deleted by "
+ str(
Expand Down
9 changes: 9 additions & 0 deletions app/Entirety/entirety/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Config:


class Databases(DatabaseSettings):

ps = PostgresSettings()
default: DatabaseDsn = Field(
default=f"postgres://{ps.DATABASE_USER}:{ps.DATABASE_PASSWORD}@{ps.DATABASE_HOST}:{ps.DATABASE_PORT}/postgres"
Expand Down Expand Up @@ -81,6 +82,7 @@ class AppLoadSettings(BaseSettings):
ENTITIES_LOAD: bool = Field(default=True, env="ENTITIES_LOAD")
DEVICES_LOAD: bool = Field(default=True, env="DEVICES_LOAD")
NOTIFICATIONS_LOAD: bool = Field(default=True, env="NOTIFICATIONS_LOAD")
SEMANTICS_LOAD: bool = Field(default=True, env="SEMANTICS_LOAD")

class Config:
case_sensitive = False
Expand Down Expand Up @@ -120,6 +122,7 @@ class Settings(PydanticSettings):

CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"


CRISPY_TEMPLATE_PACK = "bootstrap5"

MIDDLEWARE: List[str] = [
Expand Down Expand Up @@ -308,6 +311,9 @@ def secret_key_not_empty(cls, v) -> str:

IOTA_URL: AnyUrl = Field(default="http://localhost:4041", env="IOTA_URL")

# CSRF
CSRF_TRUSTED_ORIGINS: list = Field(default=[], env="CSRF_TRUSTED_ORIGINS ")

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES: Databases = Field({})
Expand Down Expand Up @@ -374,6 +380,9 @@ def secret_key_not_empty(cls, v) -> str:
INSTALLED_APPS.append("devices")
if APP_LOAD.NOTIFICATIONS_LOAD is True:
INSTALLED_APPS.append("subscriptions")
if APP_LOAD.SEMANTICS_LOAD is True:

INSTALLED_APPS.append("semantics")

class Config:
case_sensitive = False
Expand Down
Loading

0 comments on commit a93dbab

Please sign in to comment.