Exemple #1
0
def _request_follow(user_self_id, user_obj_id):
    from apps.common.functions import notification_ready_unread
    if int(user_self_id) == int(user_obj_id):
        return False

    ready = is_follower(user_self_id, user_obj_id)

    try:
        if ready:
            return False
        else:
            SocialFriendShip(user_self_id=user_self_id,
                             user_obj_id=user_obj_id,
                             type=OBJECT_TYPE._REQUEST_FOLLOWER).save()
            ready_notify = notification_ready_unread(
                recipient_id=user_obj_id,
                sender_id=user_self_id,
                type=OBJECT_TYPE._REQUEST_FOLLOWER)
            if not ready_notify:
                send_notification(recipient_id=user_obj_id,
                                  sender_id=user_self_id,
                                  object_id=user_self_id,
                                  type=OBJECT_TYPE._REQUEST_FOLLOWER)
    except Exception, ex:
        return False
Exemple #2
0
def _response_friend(user_self_id, user_obj_id, subject="Request friend"):
    if int(user_self_id) == int(user_obj_id):
        return False

    friend = is_friend(user_self_id, user_obj_id)
    try:
        if friend['is_friend'] or friend['requested']:
            return False
        if friend['ready_requested']:
            friendship = SocialFriendShip(user_self_id=user_self_id,user_obj_id=user_obj_id,type=OBJECT_TYPE._REQUEST_FRIEND)
            friendship.save()

            send_notification(recipient_id=user_obj_id, object_id=user_self_id,
                              type=OBJECT_TYPE._REQUEST_FRIEND)

    except Exception, ex:
        return False
Exemple #3
0
def _request_follow(user_self_id, user_obj_id):
    from apps.common.functions import notification_ready_unread
    if int(user_self_id) == int(user_obj_id):
        return False

    ready = is_follower(user_self_id, user_obj_id)

    try:
        if ready :
            return False
        else:
            SocialFriendShip(user_self_id=user_self_id,user_obj_id=user_obj_id,type=OBJECT_TYPE._REQUEST_FOLLOWER).save()
            ready_notify = notification_ready_unread(recipient_id=user_obj_id, sender_id=user_self_id, type=OBJECT_TYPE._REQUEST_FOLLOWER)
            if not ready_notify:
                send_notification(recipient_id=user_obj_id, sender_id=user_self_id, object_id=user_self_id,
                                  type=OBJECT_TYPE._REQUEST_FOLLOWER)
    except Exception, ex:
        return False
Exemple #4
0
def _response_friend(user_self_id, user_obj_id, subject="Request friend"):
    if int(user_self_id) == int(user_obj_id):
        return False

    friend = is_friend(user_self_id, user_obj_id)
    try:
        if friend['is_friend'] or friend['requested']:
            return False
        if friend['ready_requested']:
            friendship = SocialFriendShip(user_self_id=user_self_id,
                                          user_obj_id=user_obj_id,
                                          type=OBJECT_TYPE._REQUEST_FRIEND)
            friendship.save()

            send_notification(recipient_id=user_obj_id,
                              object_id=user_self_id,
                              type=OBJECT_TYPE._REQUEST_FRIEND)

    except Exception, ex:
        return False
Exemple #5
0
def my_event_every_day_send_notification():
    try:
        day_send_notification = date.today()+timedelta(days=1)
        obj_my_event = MyEvent.objects.filter(myeventobject__update=str(day_send_notification))
        for celery_my_event_notification in obj_my_event:
            send_notification(recipient_id=celery_my_event_notification.user_share_id,
                              sender_id=celery_my_event_notification.user_id,
                              object_id=celery_my_event_notification.event_id,
                              type=OBJECT_TYPE._CELERY_EVENT_NOTIFICATION,
                              mail=False)
        obj_self_event = MyEvent.objects.filter(myeventobject__update=str(day_send_notification))\
                                .distinct().extra(select={'user_id_obj': 'user_id'})
        for celery_self_event in obj_self_event:
            send_notification(recipient_id=celery_self_event.user_id_obj,
                              sender_id=celery_self_event.user_id_obj,
                              object_id=celery_self_event.event_id, type=OBJECT_TYPE._CELERY_EVENT_NOTIFICATION,
                              mail=False)
        return 'Success'
    except Exception, err:
        print err
        return 'Error'
Exemple #6
0
def my_wall_save(request):
    my_wall_text = request.POST['textbox_comment']
    message_id = request.POST['message_id']
    array_notification = []
    'child node'
    if message_id != "0":
        'send notification to all user that participate in the conversation except the current user'
        message_notification = SocialMessage.objects.filter(
            Q(message_id=message_id, type=OBJECT_TYPE._MY_WALL_TYPE)
            | Q(pk=message_id, type=OBJECT_TYPE._MY_WALL_TYPE)).values(
                'object_id').distinct()
        for array_filter in message_notification:
            array_notification.append(array_filter['object_id'])
            'if sender id in notification array exclude it'
            if array_notification.count(request.user.id):
                index = array_notification.index(request.user.id)
                array_notification.pop(index)
            for wall_notification in array_notification:
                send_notification(sender_id=request.user.id,
                                  recipient_id=wall_notification,
                                  object_id=int(message_id),
                                  type=OBJECT_TYPE._MY_WALL_NOTIFICATION,
                                  mail=False)

        object_save = SocialMessage(
            content=my_wall_text,
            message_id=int(message_id),
            object_id=request.user.id,
            user=User.objects.get(pk=request.user.id),
            friend=User.objects.get(pk=request.user.id),
            type=OBJECT_TYPE._MY_WALL_TYPE)
        object_save.save()
        child_id = SocialMessage.objects.all().aggregate(Max("id"))['id__max']
        my_wall_child_row = SocialMessage.objects.filter(pk=child_id)
        get_mention_hashtag('client', OBJECT_TYPE._HASHTAG_MY_WALL_POST,
                            my_wall_text, message_id,
                            "images/products/hash_tag_no_image.jpg",
                            request.user.id)
        return render_to_response(
            'social/wall/my-wall-render-child.html', {
                "child": my_wall_child_row,
                "full_name": request.user.get_full_name(),
                "avatar": request.user.get_avatar_src_full_url()
            },
            context_instance=RequestContext(request))

    'parent node'
    if message_id == "0":
        'object_id:user_id or collection_id'
        object_id = request.POST['object_id']
        is_friend_post = request.POST['is_friend_post']
        type = request.POST['type']
        if object_id == "0":
            obj_save = SocialMessage(
                content=my_wall_text,
                object_id=request.user.id,
                user=User.objects.get(pk=request.user.id),
                friend=User.objects.get(pk=request.user.id),
                type=type)
        if object_id != "0":
            if is_friend_post == "0":
                obj_save = SocialMessage(
                    content=my_wall_text,
                    object_id=object_id,
                    user=User.objects.get(pk=request.user.id),
                    friend=User.objects.get(pk=request.user.id),
                    type=type)
            if is_friend_post == "1":
                obj_save = SocialMessage(
                    content=my_wall_text,
                    user=User.objects.get(pk=object_id),
                    friend=User.objects.get(pk=request.user.id),
                    type=type,
                    object_id=OBJECT_TYPE._MY_WALL_OBJ_FRIEND_POST)
        obj_save.save()
        parent_id = SocialMessage.objects.all().aggregate(Max("id"))['id__max']
        my_wall_parent_row = SocialMessage.objects.filter(pk=parent_id)
        get_mention_hashtag('client', OBJECT_TYPE._HASHTAG_MY_WALL_POST,
                            my_wall_text, parent_id,
                            "images/products/hash_tag_no_image.jpg",
                            request.user.id)
        return render_to_response('social/wall/my-wall-render-parent.html', {
            "parent": my_wall_parent_row,
            "full_name": request.user.get_full_name(),
        },
                                  context_instance=RequestContext(request))
Exemple #7
0
def save_event(request):
    if 'check_if_update' not in request.POST:
        name_empty = []
        post_selected_array = request.POST.getlist(
            'my_event_selected_user_array[]')
        participant_quantity = int(
            request.POST['my_event_participant_quantity']) + 1
        if request.POST['name'] == "":
            check_name = False
            name_empty.append({
                "code": 0,
                "id": "id_name",
                "message": "Event name cannot be blank"
            })
        if request.POST['my_event_participant_quantity'] == '0':
            check_amount = False
            name_empty.append({
                "code":
                0,
                "id":
                "participant",
                "message":
                "Please select at least one participant"
            })
        if request.POST['my_event_participant_quantity'] != '0':
            check_amount = True
            name_empty.append({"code": 2, "id": "participant", "message": ""})
        if request.POST['date_created'] == "":
            check_date_created = False
            name_empty.append({
                "code": 0,
                "id": "id_date_created",
                "message": "Date created cannot be blank"
            })
        if request.POST['date_created'] != "":
            check_date_created = True
            name_empty.append({
                "code": 2,
                "id": "id_date_created",
                "message": ""
            })
        if request.POST['name'] != "":
            if len(MyEvent.objects.filter(name=request.POST['name'])) != 0:
                check_name = False
                name_empty.append({
                    "code": 1,
                    "id": "id_name",
                    "message": "Event existence"
                })
            if len(MyEvent.objects.filter(name=request.POST['name'])) == 0:
                check_name = True
                name_empty.append({"code": 2, "id": "id_name", "message": ""})

        if check_name and check_date_created and check_amount:
            obj_save = MyEvent(name=request.POST['name'],
                               date_created=request.POST['date_created'],
                               description=request.POST['description'],
                               participants_amount=participant_quantity,
                               user=User.objects.get(pk=request.user.id))
            obj_save.save()
            name_empty.append({"code": "save_success"})
            last_event_id = MyEvent.objects.all().aggregate(Max("id"))
            object_event_id = last_event_id['id__max']
            my_event_user_share = post_selected_array[0].split(',')
            for my_event_share in my_event_user_share:
                obj_my_event_save = MyEventObject(
                    event=MyEvent.objects.get(pk=object_event_id),
                    user_owner=request.user.id,
                    user_share_id=my_event_share,
                    is_shared=OBJECT_TYPE._MY_EVENT_NOTIFICATION)
                obj_my_event_save.save()
            my_event_user_share_new_notification = post_selected_array[
                0].split(',')
            '''
            my_event_user_share_new_notification.append(request.user.id)
            '''
            for my_event_share_new_notification in my_event_user_share_new_notification:
                send_notification(sender_id=request.user.id,
                                  recipient_id=my_event_share_new_notification,
                                  object_id=object_event_id,
                                  type=OBJECT_TYPE._MY_EVENT_NOTIFICATION,
                                  mail=True)

    if 'check_if_update' in request.POST:
        name_empty = []
        array_user_do_update = []
        social_event_activity = MyEventObject.objects.filter(
            event=request.POST['id_update'])
        post_selected_update_array = request.POST.getlist(
            'my_event_selected_user_array[]')
        my_event = MyEvent.objects.get(pk=request.POST['id_update'])
        my_event.name = request.POST['name']
        my_event.date_created = request.POST['date_created']
        my_event.description = request.POST['description']
        if request.POST['my_event_participant_quantity'] != '0':
            my_event.participants_amount = request.POST[
                'my_event_participant_quantity']
        update_fields = [
            'name', 'date_created', 'description', 'participants_amount'
        ]
        array_user_do_update.append(my_event.user_id)
        for arr_user_do_update in social_event_activity:
            array_user_do_update.append(arr_user_do_update.user_share_id)
        for check_user_do_update in array_user_do_update:
            if check_user_do_update == request.user.id:
                my_event.save(update_fields=update_fields)
                name_empty.append({"code": "update_success"})
        if my_event.user_id != request.user.id:
            name_empty.append({
                "code": 4,
                "id": "id_update_name_permission",
                "message": "Permission Denied"
            })
        if request.POST['my_event_participant_quantity'] != '0':
            social_event_activity.delete()
            Notification.objects.filter(
                object_id=request.POST['id_update'],
                type=OBJECT_TYPE._MY_EVENT_NOTIFICATION).delete()
            my_event_user_share = post_selected_update_array[0].split(',')
            for my_event_share in my_event_user_share:
                obj_my_event_save = MyEventObject(
                    event=MyEvent.objects.get(pk=request.POST['id_update']),
                    user_owner=request.user.id,
                    user_share_id=my_event_share,
                    is_shared=OBJECT_TYPE._MY_EVENT_NOTIFICATION)
                obj_my_event_save.save()
            '''
            my_event_user_share.append(request.user.id)
            '''
            for my_event_share_notification in my_event_user_share:
                Notification.objects.filter(
                    object_id=request.POST['id_update'],
                    type=OBJECT_TYPE._MY_EVENT_NOTIFICATION)
                send_notification(sender_id=request.user.id,
                                  recipient_id=my_event_share_notification,
                                  object_id=request.POST['id_update'],
                                  type=OBJECT_TYPE._MY_EVENT_NOTIFICATION,
                                  mail=True)
    return HttpResponse(json.dumps({'result': name_empty}))
Exemple #8
0
def my_wall_save(request):
    my_wall_text = request.POST['textbox_comment']
    message_id = request.POST['message_id']
    array_notification = []
    'child node'
    if message_id != "0":
        'send notification to all user that participate in the conversation except the current user'
        message_notification = SocialMessage.objects.filter(Q(message_id=message_id, type=OBJECT_TYPE._MY_WALL_TYPE) |
                                                            Q(pk=message_id, type=OBJECT_TYPE._MY_WALL_TYPE)).values('object_id').distinct()
        for array_filter in message_notification:
            array_notification.append(array_filter['object_id'])
            'if sender id in notification array exclude it'
            if array_notification.count(request.user.id):
                index = array_notification.index(request.user.id)
                array_notification.pop(index)
            for wall_notification in array_notification:
                send_notification(sender_id=request.user.id,
                                  recipient_id=wall_notification,
                                  object_id=int(message_id),
                                  type=OBJECT_TYPE._MY_WALL_NOTIFICATION, mail=False)

        object_save = SocialMessage(content=my_wall_text,
                                    message_id=int(message_id),
                                    object_id=request.user.id,
                                    user=User.objects.get(pk=request.user.id),
                                    friend=User.objects.get(pk=request.user.id),
                                    type=OBJECT_TYPE._MY_WALL_TYPE)
        object_save.save()
        child_id = SocialMessage.objects.all().aggregate(Max("id"))['id__max']
        my_wall_child_row = SocialMessage.objects.filter(pk=child_id)
        get_mention_hashtag('client', OBJECT_TYPE._HASHTAG_MY_WALL_POST, my_wall_text, message_id,
                            "images/products/hash_tag_no_image.jpg",
                            request.user.id)
        return render_to_response('social/wall/my-wall-render-child.html',
                                  {
                                      "child": my_wall_child_row,
                                      "full_name": request.user.get_full_name(),
                                      "avatar": request.user.get_avatar_src_full_url()
                                  }, context_instance=RequestContext(request))

    'parent node'
    if message_id == "0":
        'object_id:user_id or collection_id'
        object_id = request.POST['object_id']
        is_friend_post = request.POST['is_friend_post']
        type = request.POST['type']
        if object_id == "0":
            obj_save = SocialMessage(content=my_wall_text,
                                     object_id=request.user.id,
                                     user=User.objects.get(pk=request.user.id),
                                     friend=User.objects.get(pk=request.user.id),
                                     type=type)
        if object_id != "0":
            if is_friend_post == "0":
                obj_save = SocialMessage(content=my_wall_text,
                                         object_id=object_id,
                                         user=User.objects.get(pk=request.user.id),
                                         friend=User.objects.get(pk=request.user.id),
                                         type=type)
            if is_friend_post == "1":
                obj_save = SocialMessage(content=my_wall_text,
                                         user=User.objects.get(pk=object_id),
                                         friend=User.objects.get(pk=request.user.id),
                                         type=type,
                                         object_id=OBJECT_TYPE._MY_WALL_OBJ_FRIEND_POST)
        obj_save.save()
        parent_id = SocialMessage.objects.all().aggregate(Max("id"))['id__max']
        my_wall_parent_row = SocialMessage.objects.filter(pk=parent_id)
        get_mention_hashtag('client', OBJECT_TYPE._HASHTAG_MY_WALL_POST, my_wall_text, parent_id,
                            "images/products/hash_tag_no_image.jpg", request.user.id)
        return render_to_response('social/wall/my-wall-render-parent.html',
                                  {
                                      "parent": my_wall_parent_row,
                                      "full_name": request.user.get_full_name(),
                                  },
                                  context_instance=RequestContext(request))
Exemple #9
0
def save_event(request):
    if 'check_if_update' not in request.POST:
        name_empty = []
        post_selected_array = request.POST.getlist('my_event_selected_user_array[]')
        participant_quantity = int(request.POST['my_event_participant_quantity']) + 1
        if request.POST['name'] == "":
            check_name = False
            name_empty.append({
                "code": 0,
                "id": "id_name",
                "message": "Event name cannot be blank"
            })
        if request.POST['my_event_participant_quantity'] == '0':
            check_amount = False
            name_empty.append({
                "code": 0,
                "id": "participant",
                "message": "Please select at least one participant"
            })
        if request.POST['my_event_participant_quantity'] != '0':
            check_amount = True
            name_empty.append({
                "code": 2,
                "id": "participant",
                "message": ""
            })
        if request.POST['date_created'] == "":
            check_date_created = False
            name_empty.append({
                "code": 0,
                "id": "id_date_created",
                "message": "Date created cannot be blank"
            })
        if request.POST['date_created'] != "":
            check_date_created = True
            name_empty.append({
                "code": 2,
                "id": "id_date_created",
                "message": ""
            })
        if request.POST['name'] != "":
            if len(MyEvent.objects.filter(name=request.POST['name'])) != 0:
                check_name = False
                name_empty.append({
                    "code": 1,
                    "id": "id_name",
                    "message": "Event existence"
                })
            if len(MyEvent.objects.filter(name=request.POST['name'])) == 0:
                check_name = True
                name_empty.append({
                    "code": 2,
                    "id": "id_name",
                    "message": ""
                })

        if check_name and check_date_created and check_amount:
            obj_save = MyEvent(name=request.POST['name'], date_created=request.POST['date_created'],
                               description=request.POST['description'],
                               participants_amount=participant_quantity,
                               user=User.objects.get(pk=request.user.id))
            obj_save.save()
            name_empty.append({
                "code": "save_success"
            })
            last_event_id = MyEvent.objects.all().aggregate(Max("id"))
            object_event_id = last_event_id['id__max']
            my_event_user_share = post_selected_array[0].split(',')
            for my_event_share in my_event_user_share:
                obj_my_event_save = MyEventObject(event=MyEvent.objects.get(pk=object_event_id),
                                                  user_owner=request.user.id,
                                                  user_share_id=my_event_share,
                                                  is_shared=OBJECT_TYPE._MY_EVENT_NOTIFICATION
                                                  )
                obj_my_event_save.save()
            my_event_user_share_new_notification = post_selected_array[0].split(',')
            '''
            my_event_user_share_new_notification.append(request.user.id)
            '''
            for my_event_share_new_notification in my_event_user_share_new_notification:
                send_notification(sender_id=request.user.id, recipient_id=my_event_share_new_notification,
                                  object_id=object_event_id,
                                  type=OBJECT_TYPE._MY_EVENT_NOTIFICATION, mail=True)

    if 'check_if_update' in request.POST:
        name_empty = []
        array_user_do_update = []
        social_event_activity = MyEventObject.objects.filter(event=request.POST['id_update'])
        post_selected_update_array = request.POST.getlist('my_event_selected_user_array[]')
        my_event = MyEvent.objects.get(pk=request.POST['id_update'])
        my_event.name = request.POST['name']
        my_event.date_created = request.POST['date_created']
        my_event.description = request.POST['description']
        if request.POST['my_event_participant_quantity'] != '0':
            my_event.participants_amount = request.POST['my_event_participant_quantity']
        update_fields = ['name', 'date_created', 'description', 'participants_amount']
        array_user_do_update.append(my_event.user_id)
        for arr_user_do_update in social_event_activity:
            array_user_do_update.append(arr_user_do_update.user_share_id)
        for check_user_do_update in array_user_do_update:
            if check_user_do_update == request.user.id:
                my_event.save(update_fields=update_fields)
                name_empty.append({"code": "update_success"})
        if my_event.user_id != request.user.id:
            name_empty.append({
                "code": 4,
                "id": "id_update_name_permission",
                "message": "Permission Denied"
            })
        if request.POST['my_event_participant_quantity'] != '0':
            social_event_activity.delete()
            Notification.objects.filter(object_id=request.POST['id_update'],
                                        type=OBJECT_TYPE._MY_EVENT_NOTIFICATION).delete()
            my_event_user_share = post_selected_update_array[0].split(',')
            for my_event_share in my_event_user_share:
                obj_my_event_save = MyEventObject(event=MyEvent.objects.get(pk=request.POST['id_update']),
                                                  user_owner=request.user.id,
                                                  user_share_id=my_event_share,
                                                  is_shared=OBJECT_TYPE._MY_EVENT_NOTIFICATION)
                obj_my_event_save.save()
            '''
            my_event_user_share.append(request.user.id)
            '''
            for my_event_share_notification in my_event_user_share:
                Notification.objects.filter(object_id=request.POST['id_update'],
                                            type=OBJECT_TYPE._MY_EVENT_NOTIFICATION)
                send_notification(sender_id=request.user.id, recipient_id=my_event_share_notification,
                                  object_id=request.POST['id_update'],
                                  type=OBJECT_TYPE._MY_EVENT_NOTIFICATION, mail=True)
    return HttpResponse(json.dumps({'result': name_empty}))
Exemple #10
0
 def save_mention(name, user, object_id):
     for user_mention in User.objects.filter(username=name):
         send_notification(sender_id=user, recipient_id=user_mention.id,
                           object_id=object_id, type=OBJECT_TYPE._HASHTAG_MENTION, mail=False)
     return name