Пример #1
0
 def _notify_users(self, level, verb, threshold):
     """ creates notifications for users """
     opts = dict(actor=self,
                 level=level,
                 verb=verb,
                 action_object=threshold)
     if self.content_object is None:
         opts['actor'] = self
         target_org = None
     else:
         opts['target'] = self.content_object
         target_org = getattr(opts['target'], 'organization_id', None)
     self._set_extra_notification_opts(opts)
     # retrieve superusers
     where = Q(is_superuser=True)
     # if target_org is specified, retrieve also
     # staff users that are member of the org
     if target_org:
         where = (where | (Q(is_staff=True)
                           & Q(openwisp_users_organization=target_org)))
     # only retrieve users which have the receive flag active
     where = where & Q(notificationuser__receive=True)
     # perform query
     qs = User.objects.select_related('notificationuser') \
                      .order_by('date_joined') \
                      .filter(where)
     for user in qs:
         n = Notification(**opts)
         n.recipient = user
         n.full_clean()
         n.save()
Пример #2
0
def add_notification(request):
	if request.method == "POST":
		form_data = JSONMiddleware()
		form_data.process_request(request)
		notification_instance = Notification()
		# if the user has failed to enter required form data return Key Error as error and status 500
		try:
			notification_obj = NotificationType.objects.get(id=request.POST['notification_type'])
			notification_instance.notification_type = notification_obj
			notification_instance.notification_title = request.POST['notification_name']
			notification_instance.notification_subject = request.POST['subject']
			notification_instance.notification_body = request.POST['body']
			print notification_instance
		except (ValueError, KeyError):
			return HttpResponse(json.dumps({'error':'Key Error'}), status=500)
		# if the user input data fails validation return Validation Error as error and status 500
		try:
			notification_instance.full_clean()
			notification_instance.save()
			notification_contacts = request.POST.getlist('notification_contacts')
			notification_instance.notification_contacts.add(*notification_contacts)
		except ValidationError as error:
			return HttpResponse(json.dumps({'error':'Validation Error'}), status=500)
		return redirect('notification')