Ejemplo n.º 1
0
def push_message(request, object_id):
    """
    Its Ajax posted message
    """
    try:
        if not request.user.is_authenticated():
            raise AccessError
        msg = Message()
        msg.ip = request.META['REMOTE_ADDR']
        msg.user_agent = request.META['HTTP_USER_AGENT']
        msg.subject = request.POST.get('message_subject') or None
        msg.body = request.POST.get('message_body') or None
        msg.sender = request.user
        msg.recipient = get_user_model().objects.get(id=object_id)
        msg.sent_at = now()
        msg.save()
        try:
            avatar_id = request.user.avatar.pk
        except:
            avatar_id = False
        message_date = msg.sent_at.strftime(settings.COMMENT_DATE_FORMAT)
        payload = {'success': True, 'id': msg.pk, 'username': msg.sender.get_name,
                   'username_url': msg.sender.get_absolute_url(),
                   'message_subject': msg.subject, 'avatar_id': avatar_id,
                   'message_date': message_date, 'message_body': msg.body}
    except AccessError:
        payload = {'success': False, 'error': _('You are not allowed for send message')}
    except:
        payload = {'success': False}
    return AjaxLazyAnswer(payload)
Ejemplo n.º 2
0
def push_message(request, pk):
    # noinspection PyBroadException
    try:
        if not request.user.is_authenticated():
            raise AccessError
        recipient = get_user_model().objects.get(pk=pk)
        if recipient == request.user:
            raise AccessError
        body = request.POST.get('message') or None
        if body is None:
            raise AccessError
        msg = Message()
        msg.subject = request.POST.get('subject') or ''
        msg.ip = request.META['REMOTE_ADDR']
        msg.user_agent = request.META['HTTP_USER_AGENT']
        msg.body = body
        msg.sender = request.user
        msg.recipient = recipient
        msg.sent_at = now()
        msg.save()
        result = Message.objects.concrete_user(request.user, recipient).count()
        html = render_to_string('user/message_one.html', {'message': msg, 'user': request.user})
        payload = {'success': True, 'html': html, 'count': result, 'id': recipient.pk,
                   'total': request.user.messages_count}
    except AccessError as aerr:
        payload = {'success': False, 'error': 'You are not allowed for send message'}
    except:
        payload = {'success': False}
    return ajax_answer_lazy(payload)
Ejemplo n.º 3
0
def push_message(request, object_id):
    """
    Its Ajax posted message
    """
    try:
        if not request.user.is_authenticated():
            raise AccessError
        msg = Message()
        msg.ip = request.META["REMOTE_ADDR"]
        msg.user_agent = request.META["HTTP_USER_AGENT"]
        msg.subject = request.POST.get("message_subject") or None
        msg.body = request.POST.get("message_body") or None
        msg.sender = request.user
        msg.recipient = get_user_model().objects.get(id=object_id)
        msg.sent_at = now()
        msg.save()
        try:
            avatar_id = request.user.avatar.pk
        except:
            avatar_id = False
        message_date = msg.sent_at.strftime(settings.COMMENT_DATE_FORMAT)
        payload = {
            "success": True,
            "id": msg.pk,
            "username": msg.sender.get_name,
            "username_url": msg.sender.get_absolute_url(),
            "message_subject": msg.subject,
            "avatar_id": avatar_id,
            "message_date": message_date,
            "message_body": msg.body,
        }
    except AccessError:
        payload = {"success": False, "error": _("You are not allowed for send message")}
    except:
        payload = {"success": False}
    return AjaxLazyAnswer(payload)
Ejemplo n.º 4
0
def message_add(request):
    """
    Message add ajax
    """
    recipients = request.POST['recipients'].strip().split(',')
    recipients = filter(None, recipients)  # Remove empty values
    try:
        for u in recipients:
            user = get_user_model().objects.get(username=u)
            if user and (user != request.user):
                m = Message()
                m.recipient = user
                m.sent_at = now()
                m.sender = request.user
                m.body = request.POST['body']
                m.save()
        success = True
    except:
        success = False
    location = reverse('user_messages')
    payload = {'success': success, 'location': location}
    return AjaxLazyAnswer(payload)
Ejemplo n.º 5
0
def message_add(request):
    """
    Message add ajax
    """
    recipients = request.POST['recipients'].strip().split(',')
    recipients = filter(None, recipients)  # Remove empty values
    # noinspection PyBroadException
    try:
        for u in recipients:
            user = get_user_model().objects.get(username=u)
            if user and (user != request.user):
                m = Message()
                m.recipient = user
                m.sent_at = now()
                m.sender = request.user
                m.body = request.POST['body']
                m.save()
        success = True
    except:
        success = False
    location = reverse('user_messages')
    payload = dict(success=success, location=location)
    return ajax_answer_lazy(payload)
Ejemplo n.º 6
0
def message_add(request):
    """
    Message add ajax
    """
    recipients = request.POST['recipients'].strip().split(',')
    recipients = filter(None, recipients)  # Remove empty values
    try:
        for u in recipients:
            user = get_user_model().objects.get(username=u)
            if user and (user != request.user):
                m = Message()
                m.recipient = user
                m.sent_at = now()
                m.sender = request.user
                m.body = request.POST['body']
                m.save()
        success = True
    except:
        success = False
    location = reverse('user_messages')
    payload = {'success': success, 'location': location}
    return ajax_answer_lazy(payload)
Ejemplo n.º 7
0
def message_add(request):
    """
    Message add ajax
    """
    recipients = request.REQUEST['recipients'].strip().split(',')
    recipients = filter(None, recipients)  # Remove empty values
    try:
        for u in recipients:
            user = settings.AUTH_USER_MODEL.objects.get(username=u)
            if user and (user != request.user):
                m = Message()
                m.recipient = user
                m.sent_at = datetime.now()
                m.sender = request.user
                m.body = request.REQUEST['body']
                m.save()
        success = True
    except :
        success = False
    location = reverse('user_messages')
    payload = {'success': success, 'location':location}
    return AjaxLazyAnswer(payload)
Ejemplo n.º 8
0
def message_add(request):
    """
    Message add ajax
    """
    recipients = request.POST["recipients"].strip().split(",")
    recipients = filter(None, recipients)  # Remove empty values
    try:
        for u in recipients:
            user = get_user_model().objects.get(username=u)
            if user and (user != request.user):
                m = Message()
                m.recipient = user
                m.sent_at = now()
                m.sender = request.user
                m.body = request.POST["body"]
                m.save()
        success = True
    except:
        success = False
    location = reverse("user_messages")
    payload = {"success": success, "location": location}
    return AjaxLazyAnswer(payload)
Ejemplo n.º 9
0
def push_message(request, pk):
    # noinspection PyBroadException
    try:
        if not request.user.is_authenticated():
            raise AccessError
        recipient = get_user_model().objects.get(pk=pk)
        if recipient == request.user:
            raise AccessError
        body = request.POST.get('message') or None
        if body is None:
            raise AccessError
        msg = Message()
        msg.subject = request.POST.get('subject') or ''
        msg.ip = request.META['REMOTE_ADDR']
        msg.user_agent = request.META['HTTP_USER_AGENT']
        msg.body = body
        msg.sender = request.user
        msg.recipient = recipient
        msg.sent_at = now()
        msg.save()
        result = Message.objects.concrete_user(request.user, recipient).count()
        html = render_to_string('user/message_one.html', {
            'message': msg,
            'user': request.user
        })
        payload = {
            'success': True,
            'html': html,
            'count': result,
            'id': recipient.pk,
            'total': request.user.messages_count
        }
    except AccessError as aerr:
        payload = {
            'success': False,
            'error': 'You are not allowed for send message'
        }
    except:
        payload = {'success': False}
    return ajax_answer_lazy(payload)
Ejemplo n.º 10
0
def push_message(request, object_id):
    """
    Its Ajax posted message
    """
    try:
        if not request.user.is_authenticated():
            raise AccessError
        msg = Message()
        msg.ip = request.META['REMOTE_ADDR']
        msg.user_agent = request.META['HTTP_USER_AGENT']
        msg.subject = request.POST.get('message_subject') or None
        msg.body = request.POST.get('message_body') or None
        msg.sender = request.user
        msg.recipient = get_user_model().objects.get(id=object_id)
        msg.sent_at = now()
        msg.save()
        try:
            avatar_id = request.user.avatar.pk
        except:
            avatar_id = False
        message_date = msg.sent_at.strftime(settings.COMMENT_DATE_FORMAT)
        payload = {
            'success': True,
            'id': msg.pk,
            'username': msg.sender.get_name,
            'username_url': msg.sender.get_absolute_url(),
            'message_subject': msg.subject,
            'avatar_id': avatar_id,
            'message_date': message_date,
            'message_body': msg.body
        }
    except AccessError:
        payload = {
            'success': False,
            'error': _('You are not allowed for send message')
        }
    except:
        payload = {'success': False}
    return AjaxLazyAnswer(payload)