Skip to content

Commit

Permalink
Merge pull request #114 from usnistgov/4.1.0.dev
Browse files Browse the repository at this point in the history
4.1.0.dev
  • Loading branch information
rptmat57 authored Jun 15, 2022
2 parents 5335106 + ad825a2 commit 16c208d
Show file tree
Hide file tree
Showing 123 changed files with 29,279 additions and 5,920 deletions.
2 changes: 2 additions & 0 deletions Dockerfile.splash_pad
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ RUN rm --recursive --force /nemo

RUN mkdir /nemo
RUN mkdir /nemo/media
RUN mkdir /nemo/media/tool_images
WORKDIR /nemo

COPY resources/icons/* /nemo/media/
COPY resources/people/* /nemo/media/
COPY resources/sounds/* /nemo/media/
COPY resources/images/tool_images/* /nemo/media/tool_images/
COPY resources/images/* /nemo/media/
COPY resources/emails/* /nemo/media/
COPY resources/splash_pad_rates.json /nemo/media/rates.json
Expand Down
8 changes: 6 additions & 2 deletions NEMO/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import sys

from django.apps import AppConfig


def init_admin_site():
from NEMO.views.customization import get_customization
from NEMO.views.customization import ApplicationCustomization
from django.contrib import admin
# customize the site
site_title = get_customization("site_title", raise_exception=False)
site_title = ApplicationCustomization.get("site_title", raise_exception=False)
admin.site.site_header = site_title
admin.site.site_title = site_title
admin.site.index_title = "Detailed administration"
Expand All @@ -20,6 +22,8 @@ class NEMOConfig(AppConfig):
name = "NEMO"

def ready(self):
if 'migrate' or 'makemigrations' in sys.argv:
return
from django.apps import apps
if apps.is_installed("django.contrib.admin"):
init_admin_site()
Expand Down
6 changes: 3 additions & 3 deletions NEMO/apps/area_access/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from NEMO.models import (BadgeReader, Door, PhysicalAccessLog, PhysicalAccessType, Project, UsageEvent, User)
from NEMO.views.area_access import log_in_user_to_area, log_out_user
from NEMO.views.customization import get_customization
from NEMO.views.customization import ApplicationCustomization, InterlockCustomization
from NEMO.views.policy import check_billing_to_project, check_policy_to_enter_any_area, check_policy_to_enter_this_area
from NEMO.views.tool_control import interlock_bypass_allowed

Expand Down Expand Up @@ -73,7 +73,7 @@ def login_to_area(request, door_id):
log.time = timezone.now()
log.result = PhysicalAccessType.DENY # Assume the user does not have access

facility_name = get_customization("facility_name")
facility_name = ApplicationCustomization.get("facility_name")

# Check policy for entering an area
try:
Expand Down Expand Up @@ -289,7 +289,7 @@ def open_door(request, door_id):


def interlock_error(action: str = None, user: User = None, bypass_allowed: bool = None):
error_message = get_customization('door_interlock_failure_message')
error_message = InterlockCustomization.get('door_interlock_failure_message')
bypass_allowed = interlock_bypass_allowed(user) if bypass_allowed is None else bypass_allowed
dictionary = {
"message": linebreaksbr(error_message),
Expand Down
2 changes: 1 addition & 1 deletion NEMO/apps/kiosk/templates/kiosk/tool_information.html
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ <h2>

update_stop_button();
$('body').on('question-group-changed', update_stop_button);
$('body').on('change keyup', '#tool_control input[required], #tool_control select[required], #tool_control textarea[required], #downtime', update_stop_button);
$('body').on('change keyup', '#tool_control input, #tool_control select, #tool_control textarea, #downtime', update_stop_button);
$('#downtime').numpad({'readonly': false, 'hidePlusMinusButton': true, 'hideDecimalButton': true});
$('#delayed_logoff_help').popover();
revert(60);
Expand Down
24 changes: 12 additions & 12 deletions NEMO/apps/kiosk/templates/kiosk/tool_reservation.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ <h4>When would you like to reserve the {{ tool }}?</h4>
</form>
<div style="height:350px"></div>
<script>
var unavailable_times = [];
let unavailable_times = [];
{% for item in tool_reservation_times %}
unavailable_times.push([{{ item.start|date:"U" }},{{ item.end|date:"U" }}]);
{% endfor %}
var date_picker = $('#date').pickadate({format: "dddd, mmmm d", formatSubmit: "yyyy-mm-dd", firstDay: 1, hiddenName: true, onSet: refresh_times});
var start_time_picker = $('#start').pickatime({interval: 15, formatSubmit: "H:i", hiddenName: true, formatLabel: format_label});
var end_time_picker = $('#end').pickatime({interval: 15, formatSubmit: "H:i", hiddenName: true, formatLabel: format_label});
let date_picker = $('#date').pickadate({format: "dddd, mmmm d", formatSubmit: "yyyy-mm-dd", firstDay: 1, hiddenName: true, onSet: refresh_times});
let start_time_picker = $('#start').pickatime({interval: 15, formatSubmit: "H:i", hiddenName: true, formatLabel: format_label});
let end_time_picker = $('#end').pickatime({interval: 15, formatSubmit: "H:i", hiddenName: true, formatLabel: format_label});
// set initial date
if ('{{ date }}') {
if ('{{ date|default_if_none:'' }}') {
date_picker.pickadate('picker').set('select', '{{ date }}', {format: 'yyyy-mm-dd'})
}
function refresh_times() {
Expand All @@ -71,13 +71,13 @@ <h4>When would you like to reserve the {{ tool }}?</h4>
}
function format_label(time) {
if (date_picker.pickadate('picker').get('select') && unavailable_times.length > 0) {
var date_selected = date_picker.pickadate('picker').get('select').pick; // selected date in milliseconds
var time_selected = time.pick * 60 * 1000; // time in milliseconds
var date_time_selected = (date_selected + time_selected)/1000; // back to seconds to compare with python timestamp
for (var i=0 ; i < unavailable_times.length; i++) {
var times = unavailable_times[i];
var start = times[0];
var end = times[1];
let date_selected = date_picker.pickadate('picker').get('select').pick; // selected date in milliseconds
let time_selected = time.pick * 60 * 1000; // time in milliseconds
let date_time_selected = (date_selected + time_selected)/1000; // back to seconds to compare with python timestamp
for (let i=0 ; i < unavailable_times.length; i++) {
let times = unavailable_times[i];
let start = times[0];
let end = times[1];
if (date_time_selected >= start && date_time_selected < end) {
return '<sp!an>h:i A</sp!an> <sm!all> !alre!ad!y re!serve!d</sm!all>';
}
Expand Down
Empty file added NEMO/apps/sensors/__init__.py
Empty file.
229 changes: 229 additions & 0 deletions NEMO/apps/sensors/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
from copy import deepcopy

from django import forms
from django.contrib import admin, messages
from django.contrib.admin import register
from django.contrib.admin.decorators import display
from django.contrib.admin.utils import display_for_value
from django.urls import reverse
from django.utils.safestring import mark_safe

from NEMO.apps.sensors.models import (
Sensor,
SensorAlertEmail,
SensorAlertLog,
SensorCard,
SensorCardCategory,
SensorCategory,
SensorData,
)


def duplicate_sensor_configuration(model_admin, request, queryset):
for sensor in queryset:
original_name = sensor.name
new_name = "Copy of " + sensor.name
try:
existing_sensor = Sensor.objects.filter(name=new_name)
if existing_sensor.exists():
messages.error(
request,
mark_safe(
f'There is already a copy of {original_name} as <a href="{reverse("admin:sensors_sensor_change", args=[existing_sensor.first().id])}">{new_name}</a>. Change the copy\'s name and try again'
),
)
continue
else:
new_sensor: Sensor = deepcopy(sensor)
new_sensor.name = new_name
new_sensor.read_frequency = 0
new_sensor.id = None
new_sensor.pk = None
new_sensor.save()
messages.success(
request,
mark_safe(
f'A duplicate of {original_name} has been made as <a href="{reverse("admin:sensors_sensor_change", args=[new_sensor.id])}">{new_sensor.name}</a>'
),
)
except Exception as error:
messages.error(
request, f"{original_name} could not be duplicated because of the following error: {str(error)}"
)


def read_selected_sensors(model_admin, request, queryset):
for sensor in queryset:
try:
response = sensor.read_data(raise_exception=True)
if isinstance(response, SensorData):
messages.success(request, f"{sensor} data read: {response.value}")
elif isinstance(response, str):
messages.warning(request, response)
except Exception as error:
messages.error(request, f"{sensor} data could not be read due to the following error: {str(error)}")


class SensorCardAdminForm(forms.ModelForm):
class Meta:
model = SensorCard
widgets = {"password": forms.PasswordInput(render_value=True)}
fields = "__all__"

def clean(self):
if any(self.errors):
return
cleaned_data = super().clean()
category = cleaned_data["category"]
from NEMO.apps.sensors import sensors

sensors.get(category).clean_sensor_card(self)
return cleaned_data


@register(SensorCard)
class SensorCardAdmin(admin.ModelAdmin):
form = SensorCardAdminForm
list_display = ("name", "enabled", "server", "port", "category")


class SensorAdminForm(forms.ModelForm):
class Meta:
model = Sensor
fields = "__all__"

def clean(self):
if any(self.errors):
return
cleaned_data = super().clean()

card = (
self.cleaned_data["sensor_card"]
if "sensor_card" in self.cleaned_data
else self.cleaned_data["interlock_card"]
)
if card:
category = card.category
from NEMO.apps.sensors import sensors

sensors.get(category).clean_sensor(self)
return cleaned_data


class SensorCategoryAdminForm(forms.ModelForm):
class Meta:
model = SensorCategory
fields = "__all__"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
children_ids = [child.id for child in self.instance.all_children()]
self.fields["parent"].queryset = SensorCategory.objects.exclude(id__in=[self.instance.pk, *children_ids])


@register(SensorCategory)
class SensorCategoryAdmin(admin.ModelAdmin):
form = SensorCategoryAdminForm
list_display = ("name", "get_parent", "get_children")

@display(ordering="children", description="Children")
def get_children(self, category: SensorCategory) -> str:
return mark_safe(
", ".join(
[
f'<a href="{reverse("admin:sensors_sensorcategory_change", args=[child.id])}">{child.name}</a>'
for child in category.children.all()
]
)
)

@display(ordering="parent", description="Parent")
def get_parent(self, category: SensorCategory) -> str:
if not category.parent:
return ""
return mark_safe(
f'<a href="{reverse("admin:sensors_sensorcategory_change", args=[category.parent.id])}">{category.parent.name}</a>'
)

def formfield_for_foreignkey(self, db_field, request, **kwargs):
""" Filter list of potential parents """
if db_field.name == "parent":
kwargs["queryset"] = SensorCategory.objects.filter()
return super().formfield_for_foreignkey(db_field, request, **kwargs)


@register(Sensor)
class SensorAdmin(admin.ModelAdmin):
form = SensorAdminForm
list_display = (
"id",
"name",
"visible",
"card",
"get_card_enabled",
"sensor_category",
"unit_id",
"read_address",
"number_of_values",
"get_read_frequency",
"get_last_read",
"get_last_read_at",
)
actions = [duplicate_sensor_configuration, read_selected_sensors]

@display(boolean=True, ordering="sensor_card__enabled", description="Card Enabled")
def get_card_enabled(self, obj: Sensor):
return obj.card.enabled

@display(description="Last read")
def get_last_read(self, obj: Sensor):
last_data_point = obj.last_data_point()
return last_data_point.value if last_data_point else ""

@display(description="Last read at")
def get_last_read_at(self, obj: Sensor):
last_data_point = obj.last_data_point()
return last_data_point.created_date if last_data_point else ""

@display(ordering="read_frequency", description="Read frequency")
def get_read_frequency(self, obj: Sensor):
return obj.read_frequency if obj.read_frequency != 0 else display_for_value(False, "", boolean=True)


@register(SensorCardCategory)
class SensorCardCategoryAdmin(admin.ModelAdmin):
list_display = ("name", "key")


@register(SensorData)
class SensorDataAdmin(admin.ModelAdmin):
list_display = ("created_date", "sensor", "value", "get_display_value")
date_hierarchy = "created_date"
list_filter = ("sensor", "sensor__sensor_category")

@display(ordering="sensor__data_prefix", description="Display value")
def get_display_value(self, obj: SensorData):
return obj.display_value()


@register(SensorAlertEmail)
class SensorAlertEmailAdmin(admin.ModelAdmin):
list_display = ("sensor", "enabled", "trigger_condition", "trigger_no_data", "additional_emails", "triggered_on")
readonly_fields = ("triggered_on",)


@register(SensorAlertLog)
class SensorAlertLogAdmin(admin.ModelAdmin):
list_display = ["id", "time", "sensor", "reset", "value"]
list_filter = ["sensor", "value", "reset"]
date_hierarchy = "time"

def has_delete_permission(self, request, obj=None):
return False

def has_add_permission(self, request):
return False

def has_change_permission(self, request, obj=None):
return False
15 changes: 15 additions & 0 deletions NEMO/apps/sensors/customizations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.core.validators import validate_email

from NEMO.decorators import customization
from NEMO.views.customization import CustomizationBase


@customization(key="sensors", title="Sensor Data")
class SensorCustomization(CustomizationBase):
variables = {"sensor_default_daterange": "", "sensor_default_refresh_rate": "0", "sensor_alert_emails": ""}

def validate(self, name, value):
if name == "sensor_alert_emails":
recipients = tuple([e for e in value.split(",") if e])
for email in recipients:
validate_email(email)
Loading

0 comments on commit 16c208d

Please sign in to comment.