def notification_create(request): if request.method == 'POST': try: user_pk = request.POST.get('user_pk', None) market_id = request.POST.get('market_id', None) details = request.POST.get('details', None) description = request.POST.get('description', None) comment = request.POST.get('comment', None) kind = request.POST.get('kind', None) notification_type = int(request.POST.get('type', 0)) valid_types = [6, 7, 8, 10] if notification_type not in valid_types: return JsonResponse({'message': 'invalid type'}, status=400) user = request.user role = get_role(user) if role not in ['corporate', 'finance'] or (role in ['corporate', 'finance'] and kind == 'consultant'): receiver = get_user_model().objects.get(pk=user_pk) elif kind == 'market': market = Market.objects.get(pk=market_id, is_active=True) else: return JsonResponse({'message': 'invalid kind'}, status=400) if notification_type == 6: """ Direct notification """ details = "From {}".format(user.person.get_full_name()) user = get_user_model().objects.get(pk=user_pk) mail_context = { 'details': details, 'comment': comment } subject = 'New notification' send_mail_wrapper( subject, 'notifications/emails/new_notification_email.html', mail_context, [user.person.email]) elif notification_type == 7: post_id = request.POST['post_id'] post = Post.objects.get(pk=post_id) details = "From {} about the bulletin {} ".format( user.person.get_full_name(), post.title ) elif notification_type == 8: registration_id = request.POST['registration_id'] registration = Registration.objects.prefetch_related('costumer').get(pk=registration_id) details = "From {} about the {} registration notes".format( user.person.get_full_name(), registration.costumer.get_full_name() ) elif notification_type == 10: details = "From corporate" notification = Notification() notification.creator = request.user notification.type = notification_type notification.description = description notification.details = details notification.comment = comment notification.save() if role not in ['corporate', 'finance'] or (role in ['corporate', 'finance'] and kind == 'consultant'): notification_receiver = NotificationReceiver() notification_receiver.receiver = receiver notification_receiver.notification = notification notification_receiver.save() elif kind == 'market': groups = ['supervisors', 'free agents', 'consultants', 'new markets coordinator'] excluded_groups = Group.objects.exclude(name__in=groups) user_ids = get_user_model().objects.filter( groups__name__in=groups, is_active=True, consultantprofile__market=market ).exclude( groups__in=excluded_groups ).distinct().values_list('id', flat=True) bulk_receiver_creation(notification, user_ids) return JsonResponse({}, status=200) except get_user_model().DoesNotExist: return JsonResponse({'user': '******'}, status=400) except Exception as e: return JsonResponse({}, status=500)