Skip to content

Commit

Permalink
doors can now be ordered and button color changed, also some clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
cod3monk committed Aug 7, 2023
1 parent 0b5de9f commit cd78fdd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 30 deletions.
23 changes: 6 additions & 17 deletions src/door_commander/opa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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")


Expand Down
3 changes: 1 addition & 2 deletions src/door_commander/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def load_or_create_secret_key() -> str:
'[::1]',
'python',
'sesam.zam.haus',
# 'door-commander.betreiberverein.de',
]

# ================================================================
Expand Down Expand Up @@ -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")

# ================================================================
Expand Down
5 changes: 2 additions & 3 deletions src/door_commander/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,8 +27,8 @@
# path('clientipaddress/', include('cliaentipaddress.urls')),
# </LEGACY>
# path('admin/', admin.site.urls),
url(
r'^accounts/login/$',
path(
'accounts/login/',
LoginView.as_view(
template_name='admin/login.html',
extra_context={
Expand Down
17 changes: 14 additions & 3 deletions src/doors/admin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Original file line number Diff line number Diff line change
@@ -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),
),
]
11 changes: 11 additions & 0 deletions src/doors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',)
8 changes: 3 additions & 5 deletions src/jinja-templates/web_homepage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
-moz-appearance: none;
}

.open {
background-color: MediumSeaGreen;
}

.logout {
background-color: Tomato;
}
Expand Down Expand Up @@ -56,7 +52,9 @@ <h2>Hallo, {{ request.user.display_name }}!</h2>
{% if can_open_doors[door] %}
<form action="{{ url("open", kwargs=dict(door_id=door.id)) }}" method="post">
{{ csrf_input }}
<button type="submit" class="open">{{ door.display_name }} &ouml;ffnen!</button>
<button type="submit" style="color: {{door.text_color}}; background-color: {{door.button_color}}">
{{ door.display_name }} &ouml;ffnen!
</button>
</form>
{% endif %}
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions src/web_homepage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit cd78fdd

Please sign in to comment.