diff --git a/src/door_commander/opa.py b/src/door_commander/opa.py index dcd9598..e01e4ad 100644 --- a/src/door_commander/opa.py +++ b/src/door_commander/opa.py @@ -38,24 +38,13 @@ def get_auth_header(): def get_allowed_result(path, function, key="allow"): - try: - if get_data_result(path, function)[key] is True: - return True - else: - return False - except: - raise Exception("Auth check failed") - + return get_data_result(path, function)[key] is True def check_allowed(path, function): - try: - if get_data_result(path, function)["allow"] is True: - return - else: - raise Exception("Unauthorized") # TODO 401/403 - except: - raise Exception("Auth check failed") - + if get_data_result(path, function)["allow"] is True: + return + else: + raise Exception("Unauthorized") # TODO 401/403 def get_data_result(path, function): """ @@ -84,7 +73,7 @@ def get_data_result(path, function): # log.setLevel(logging.DEBUG) log.debug("Return authorization result %s", ic.format(path, input, result)) return result['result'] - except: + except Exception as e: raise Exception("Auth check failed") diff --git a/src/door_commander/settings.py b/src/door_commander/settings.py index affbd89..b83509a 100644 --- a/src/door_commander/settings.py +++ b/src/door_commander/settings.py @@ -130,7 +130,6 @@ def load_or_create_secret_key() -> str: '[::1]', 'python', 'sesam.zam.haus', - # 'door-commander.betreiberverein.de', ] # ================================================================ @@ -222,7 +221,7 @@ def load_or_create_secret_key() -> str: # Authz Microservice # ================================================================ -OPA_BEARER_TOKEN = os.getenv("OPA_BEARER_TOKEN") +OPA_BEARER_TOKEN = os.getenv("OPA_BEARER_TOKEN") or "" OPA_URL = os.getenv("OPA_URL") # ================================================================ diff --git a/src/door_commander/urls.py b/src/door_commander/urls.py index 295e91b..8a552f6 100644 --- a/src/door_commander/urls.py +++ b/src/door_commander/urls.py @@ -14,7 +14,6 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.conf import settings -from django.conf.urls import url from django.contrib import admin from django.urls import path, include @@ -28,8 +27,8 @@ # path('clientipaddress/', include('cliaentipaddress.urls')), # # path('admin/', admin.site.urls), - url( - r'^accounts/login/$', + path( + 'accounts/login/', LoginView.as_view( template_name='admin/login.html', extra_context={ diff --git a/src/doors/admin.py b/src/doors/admin.py index 54c89c2..6fa4f36 100644 --- a/src/doors/admin.py +++ b/src/doors/admin.py @@ -1,13 +1,26 @@ from uuid import uuid4 from django.contrib import admin +from django.forms import ModelForm +from django.forms.widgets import TextInput -# Register your models here. from doors.models import Door from doors import door_names_publisher +class DoorForm(ModelForm): + class Meta: + model = Door + fields = "__all__" + widgets = { + "text_color": TextInput(attrs={"type": "color"}), + "button_color": TextInput(attrs={"type": "color"}), + } + + +@admin.register(Door) class DoorAdmin(admin.ModelAdmin): + form = DoorForm list_display = (Door.mqtt_id.field.name, Door.display_name.field.name) def get_form(self, request, obj=None, **kwargs): @@ -19,5 +32,3 @@ def save_model(self, request, obj, form, change): super(DoorAdmin, self).save_model(request, obj, form, change) door_names_publisher.publish_door_name(obj) - -admin.site.register(Door, DoorAdmin) diff --git a/src/doors/migrations/0003_door_button_color_door_order_door_text_color.py b/src/doors/migrations/0003_door_button_color_door_order_door_text_color.py new file mode 100644 index 0000000..ffcef3a --- /dev/null +++ b/src/doors/migrations/0003_door_button_color_door_order_door_text_color.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2.4 on 2023-08-07 15:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('doors', '0002_alter_door_options'), + ] + + operations = [ + migrations.AddField( + model_name='door', + name='button_color', + field=models.CharField(help_text='HTML hex color code for button', max_length=7, default='#60b177', null=True), + ), + migrations.AddField( + model_name='door', + name='order', + field=models.IntegerField(default=0, help_text='Order of appearance for door buttons. Lower is higher up.'), + preserve_default=False, + ), + migrations.AddField( + model_name='door', + name='text_color', + field=models.CharField(help_text='HTML hex color code for text on button', max_length=7, default='#ffffff', null=True), + ), + ] diff --git a/src/doors/models.py b/src/doors/models.py index 18b2d28..8464563 100644 --- a/src/doors/models.py +++ b/src/doors/models.py @@ -14,8 +14,19 @@ class Door(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) mqtt_id = models.CharField(max_length=256, unique=True, db_index=True) display_name = models.TextField() + order = models.IntegerField(help_text="Order of appearance for door buttons. Lower is higher up.", default=42) + text_color = models.CharField( + max_length=7, + default="#ffffff", + null=True, + help_text="HTML hex color code for text on button") + button_color = models.CharField( + max_length=7, + default="#60b177", + help_text="HTML hex color code for button") class Meta: permissions = [ (_PERMISSION_OPEN_DOOR, "Can open any door"), (_PERMISSION_LOCATION_OVERRIDE, "Can open doors from anywhere"), ] + ordering = ('order',) diff --git a/src/jinja-templates/web_homepage/index.html b/src/jinja-templates/web_homepage/index.html index 6393247..43b13c2 100644 --- a/src/jinja-templates/web_homepage/index.html +++ b/src/jinja-templates/web_homepage/index.html @@ -23,10 +23,6 @@ -moz-appearance: none; } - .open { - background-color: MediumSeaGreen; - } - .logout { background-color: Tomato; } @@ -56,7 +52,9 @@

Hallo, {{ request.user.display_name }}!

{% if can_open_doors[door] %}
{{ csrf_input }} - +
{% endif %} {% endfor %} diff --git a/src/web_homepage/views.py b/src/web_homepage/views.py index 813ee2b..9f3b243 100644 --- a/src/web_homepage/views.py +++ b/src/web_homepage/views.py @@ -28,6 +28,7 @@ def home(request): user_doors = list(door for door in Door.objects.all() if check_can_view_door(request, door)) + user_doors.sort(key=lambda d: d.order) #has_allowed_location, allowed_location_reason = check_has_allowed_location(request) doors_status = fetch_status() can_open_doors = {door: check_can_open_door(request, door) for door in user_doors}