Implement notification rules and settings
This commit is contained in:
@@ -11,7 +11,6 @@
|
||||
import uuid
|
||||
|
||||
# from core import r
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.paginator import Paginator
|
||||
from django.db.models import QuerySet
|
||||
@@ -490,54 +489,6 @@ class ObjectDelete(RestrictedViewMixin, ObjectNameMixin, DeleteView):
|
||||
return response
|
||||
|
||||
|
||||
class SearchDenied:
|
||||
def __init__(self, key, value):
|
||||
self.key = key
|
||||
self.value = value
|
||||
|
||||
|
||||
class LookupDenied:
|
||||
def __init__(self, key, value):
|
||||
self.key = key
|
||||
self.value = value
|
||||
|
||||
|
||||
def remove_defaults(query_params):
|
||||
for field, value in list(query_params.items()):
|
||||
if field in settings.DRILLDOWN_DEFAULT_PARAMS:
|
||||
if value == settings.DRILLDOWN_DEFAULT_PARAMS[field]:
|
||||
del query_params[field]
|
||||
|
||||
|
||||
def add_defaults(query_params):
|
||||
for field, value in settings.DRILLDOWN_DEFAULT_PARAMS.items():
|
||||
if field not in query_params:
|
||||
query_params[field] = value
|
||||
|
||||
|
||||
def dedup_list(data, check_keys):
|
||||
"""
|
||||
Remove duplicate dictionaries from list.
|
||||
"""
|
||||
seen = set()
|
||||
out = []
|
||||
|
||||
dup_count = 0
|
||||
for x in data:
|
||||
dedupeKey = tuple(x[k] for k in check_keys if k in x)
|
||||
if dedupeKey in seen:
|
||||
dup_count += 1
|
||||
continue
|
||||
if dup_count > 0:
|
||||
out.append({"type": "control", "hidden": dup_count})
|
||||
dup_count = 0
|
||||
out.append(x)
|
||||
seen.add(dedupeKey)
|
||||
if dup_count > 0:
|
||||
out.append({"type": "control", "hidden": dup_count})
|
||||
return out
|
||||
|
||||
|
||||
# from random import randint
|
||||
# from timeit import timeit
|
||||
# entries = 10000
|
||||
|
||||
57
core/views/notifications.py
Normal file
57
core/views/notifications.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from core.forms import NotificationRuleForm, NotificationSettingsForm
|
||||
from core.models import NotificationRule, NotificationSettings
|
||||
|
||||
from core.views.helpers import ObjectCreate, ObjectList, ObjectUpdate, ObjectDelete
|
||||
|
||||
# Notifications - we create a new notification settings object if there isn't one
|
||||
# Hence, there is only an update view, not a create view.
|
||||
class NotificationsUpdate(LoginRequiredMixin, ObjectUpdate):
|
||||
model = NotificationSettings
|
||||
form_class = NotificationSettingsForm
|
||||
|
||||
page_title = "Update your notification settings"
|
||||
page_subtitle = (
|
||||
"At least the topic must be set if you want to receive notifications."
|
||||
)
|
||||
|
||||
submit_url_name = "notifications_update"
|
||||
submit_url_args = ["type"]
|
||||
|
||||
pk_required = False
|
||||
|
||||
hide_cancel = True
|
||||
|
||||
def get_object(self, **kwargs):
|
||||
notification_settings, _ = NotificationSettings.objects.get_or_create(
|
||||
user=self.request.user
|
||||
)
|
||||
return notification_settings
|
||||
|
||||
class RuleList(LoginRequiredMixin, ObjectList):
|
||||
list_template = "partials/rule-list.html"
|
||||
model = NotificationRule
|
||||
page_title = "List of notification rules."
|
||||
|
||||
list_url_name = "rules"
|
||||
list_url_args = ["type"]
|
||||
|
||||
submit_url_name = "rule_create"
|
||||
|
||||
|
||||
class RuleCreate(LoginRequiredMixin, ObjectCreate):
|
||||
model = NotificationRule
|
||||
form_class = NotificationRuleForm
|
||||
|
||||
submit_url_name = "rule_create"
|
||||
|
||||
|
||||
class RuleUpdate(LoginRequiredMixin, ObjectUpdate):
|
||||
model = NotificationRule
|
||||
form_class = NotificationRuleForm
|
||||
|
||||
submit_url_name = "rule_update"
|
||||
|
||||
|
||||
class RuleDelete(LoginRequiredMixin, ObjectDelete):
|
||||
model = NotificationRule
|
||||
@@ -10,6 +10,7 @@ from django_tables2 import SingleTableView
|
||||
from rest_framework.parsers import FormParser
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from core.db import add_defaults, remove_defaults
|
||||
from core.db.storage import db
|
||||
from core.lib.threshold import (
|
||||
annotate_num_chans,
|
||||
@@ -17,7 +18,6 @@ from core.lib.threshold import (
|
||||
get_chans,
|
||||
get_users,
|
||||
)
|
||||
from core.views import helpers
|
||||
from core.views.ui.tables import DrilldownTable
|
||||
|
||||
# from copy import deepcopy
|
||||
@@ -125,7 +125,7 @@ class DrilldownTableView(SingleTableView):
|
||||
# No query, this is a fresh page load
|
||||
# Don't try to search, since there's clearly nothing to do
|
||||
params_with_defaults = {}
|
||||
helpers.add_defaults(params_with_defaults)
|
||||
add_defaults(params_with_defaults)
|
||||
context = {
|
||||
"sizes": sizes,
|
||||
"params": params_with_defaults,
|
||||
@@ -194,12 +194,12 @@ class DrilldownTableView(SingleTableView):
|
||||
|
||||
# Add any default parameters to the context
|
||||
params_with_defaults = dict(query_params)
|
||||
helpers.add_defaults(params_with_defaults)
|
||||
add_defaults(params_with_defaults)
|
||||
context["params"] = params_with_defaults
|
||||
|
||||
# Remove anything that we or the user set to a default for
|
||||
# pretty URLs
|
||||
helpers.remove_defaults(query_params)
|
||||
remove_defaults(query_params)
|
||||
url_params = urllib.parse.urlencode(query_params)
|
||||
context["client_uri"] = url_params
|
||||
|
||||
|
||||
Reference in New Issue
Block a user