コード例 #1
0
def like(request):
    post_id = request.POST.get('post_id', False)
    customer_id = request.POST.get('customer_id', False)
    post = Post.objects.get(id=post_id)
    customer = Customer.objects.get(id=customer_id)
    like = Like.objects.filter(post=post, customer=customer_id)
    if like.exists():
        like.delete()
        nlikes = len(Like.objects.filter(post_id=post_id))
        data = nlikes
        notifi2 = Notification.objects.filter(customer_id=customer_id,
                                              post_id=post_id,
                                              status=2)
        notifi2.delete()

    else:
        like2 = Like()
        like2.customer = customer
        like2.post = post
        like2.save()
        nlikes = len(Like.objects.filter(post_id=post_id))
        data = nlikes
        notifi = Notification()
        notifi.customer_id = post.customer.id
        notifi.createtime = timezone.now()
        notifi.customer_user = customer
        notifi.description = customer.fullname + " đã yêu thích bài viết của bạn..."
        notifi.post = post
        notifi.status = 2
        notifi.save()

    return HttpResponse(data)
コード例 #2
0
def comments(request, post_id, id_customer):
    post = get_object_or_404(Post, pk=post_id)
    customer = get_object_or_404(Customer, pk=id_customer)
    fullname = customer.fullname
    if request.method == 'POST' and request.POST['text']:
        cmt = request.POST['text']
        if len(cmt) <= 1:
            data = {
                'Erro': "There was an error logging you in. Please Try again"
            }
            return JsonResponse(data)
    if request.method == 'POST' and request.POST['text']:
        comment = Comment()
        comment.createtime = timezone.now()
        comment.updatetime = timezone.now()
        comment.post = post
        comment.text = request.POST['text']
        comment.customer = customer
        comment.save()
        if id_customer != post.customer.id:
            notifi = Notification()
            notifi.customer_id = post.customer.id
            notifi.customer_user = customer
            notifi.createtime = timezone.now()
            notifi.description = customer.fullname + " đã bình luận bài viết của bạn..."
            notifi.post = post
            notifi.status = 1
            notifi.save()
        newcoments = comment.text
        fullname = customer.fullname
        createtime = comment.createtime
        data = {
            'datacmt': newcoments,
            'dataname': fullname,
            'Erro': "",
            'createtime': str(createtime)
        }
        return JsonResponse(data)
    else:
        data = {'Erro': "There was an error logging you in. Please Try again"}
        return JsonResponse(data)
コード例 #3
0
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)