def _build_object_properties(self, object_dn: str, mappings: QuerySet, **kwargs) -> dict[str, dict[Any, Any]]: properties = {"attributes": {}} for mapping in mappings.all().select_subclasses(): if not isinstance(mapping, LDAPPropertyMapping): continue mapping: LDAPPropertyMapping try: value = mapping.evaluate(user=None, request=None, ldap=kwargs, dn=object_dn) if value is None: continue object_field = mapping.object_field if object_field.startswith("attributes."): # Because returning a list might desired, we can't # rely on self._flatten here. Instead, just save the result as-is properties["attributes"][object_field.replace( "attributes.", "")] = value else: properties[object_field] = self._flatten(value) except PropertyMappingExpressionException as exc: self._logger.warning("Mapping failed to evaluate", exc=exc, mapping=mapping) continue if self._source.object_uniqueness_field in kwargs: properties["attributes"][LDAP_UNIQUENESS] = self._flatten( kwargs.get(self._source.object_uniqueness_field)) properties["attributes"][LDAP_DISTINGUISHED_NAME] = object_dn return properties
def queryset(self, request, queryset: QuerySet) -> QuerySet: """ Returns the filtered queryset based on the value provided in the query string and retrievable via `self.value()`. """ if self.value() is None: return self.get_default_queryset(queryset) if self.value() == self.no_filter_value: return queryset.all() return self.get_filtered_queryset(queryset)
def send_webpush_notification(students: QuerySet, message: dict): """Send a notification on devices, to several students, with one message, by creating a celery task in background. Parameters ---------- students : QuerySet<Student> A queryset of student objects message : dict A message to send, as a dict, with the structure of the "options" parameter of a notification: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification Please note that title must be placed in the same dict as options in our case, for simplicity. You can also use the key "hidden": if true, you can send a notification to a user but it won't be shown to him (useful for incrementing the badge for example). Returns ------- task_launched : bool Return True if the celery task has been created successfully, else False """ # we first convert the queryset of student to a list, because we cannot # pass a queryset for an async function with celery student_ids = list(students.all().values_list('id', flat=True)) # then we try to launch the celery task for sending notifications in async # mode, so as to continue the process without waiting we have send all try: send_webpush_notification_task.delay(student_ids, message) except kombu.exceptions.OperationalError as err: # if the celery task does not work, send notifications in normal mode logger.warning( "WARNING: cannot send notifications through celery. " + "Celery might not be configured on this machine.\n" + "Error code: " + str(err) ) send_webpush_notification_task(student_ids, message)
def ReportsPermit(self, request, queryset:QuerySet): queryset.update(permit=True) for Report in queryset.all(): Report.informants.is_active = False; Report.informants.save() pass;
def api_department_list(request): if request.method == 'GET': _page = request.GET.get('_page', 0) _limit = request.GET.get('_limit', 10) _sort = request.GET.get('_sort') _order = request.GET.get('_order') dept_no__contains = request.GET.get('dept_no__contains') dept_name__contains = request.GET.get('dept_name__contains') form = DepartmentListParamsForm( dict(_page=_page, _limit=_limit, _order=_order, _sort=_sort, dept_no__contains=dept_no__contains, dept_name__contains=dept_name__contains)) if form.is_valid(): params = form.cleaned_data _page = params.get('_page') _limit = params.get('_limit') _sort = params.get('_sort') _order = params.get('_order') dept_no__contains = params.get('dept_no__contains') dept_name__contains = params.get('dept_name__contains') offset = _page * _limit end = offset + _limit qs = QuerySet(Department) if dept_no__contains: qs = qs.filter(dept_no__contains=dept_no__contains) if dept_name__contains: qs = qs.filter(dept_name__contains=dept_name__contains) count = qs.all().count() if _sort and _order: if _order == 'asc': qs = qs.order_by(f'{_sort}') else: qs = qs.order_by(f'-{_sort}') departments = qs.all()[offset:end] return JsonResponse(serialize_queryset(departments, exclude=['employees']), safe=False, headers={'X-TOTAL-COUNT': count}) else: return JsonResponse({'error': 'validate error'}, status=400) elif request.method == 'POST': dept_no = request.POST.get('dept_no') dept_name = request.POST.get('dept_name') dept = dict(dept_no=dept_no, dept_name=dept_name) form = DepartmentForm(dept) if form.is_valid(): try: dept = Department.objects.create(dept_no=dept_no, dept_name=dept_name) return JsonResponse( serialize_model(dept, exclude=['employees'])) except IntegrityError: return JsonResponse({'error': 'integrity error'}, status=400) else: return JsonResponse({'error': 'validate error'}, status=400)