예제 #1
0
def post_comment_like(request, comment_id):
    response_data = {
        Protocol.RESULT: Protocol.FAIL,
        Protocol.MESSAGE: '',
    }

    try:
        user_id = int(request.session['user_id'])
        comment_id = int(comment_id)

        need_to_push = False

        comment = Comments.objects.get(id=comment_id)
        comment_likes = [user.id for user in comment.likes.only('id')]
        if user_id in comment_likes:
            response_data[Protocol.MESSAGE] = 'Warn: User has already liked'
        else:
            comment.likes.add(user_id)
            comment_likes.append(user_id)

        if comment.max_like < len(comment_likes):
            comment.max_like = len(comment_likes)
            need_to_push = True

        comment.save()
        response_data[Protocol.RESULT] = Protocol.SUCCESS

        if need_to_push and user_id != comment.author_id:
            send_push_message([comment.author.pk], push_type=PUSH_LIKE_COMMENT, thread_id=comment.thread_id,
                              comment_id=comment_id, image_url=comment.thread.image_url)

    except Exception as err:
        response_data[Protocol.MESSAGE] = str(err)

    return HttpResponse(json.dumps(response_data), content_type='application/json')
예제 #2
0
def post_comment(request, thread_id):
    response_data = {
        Protocol.RESULT: Protocol.FAIL,
        Protocol.MESSAGE: '',
    }

    try:
        thread_id = int(thread_id)
        thread = Threads.objects.get(id=thread_id)
        req_json = json.loads(request.body.decode('utf-8'))
        user_id = int(request.session['user_id'])
        user = Users.objects.get(id=user_id)
        content = req_json['content']

        comment = Comments.objects.create(author=user,
                                          thread=thread,
                                          pub_date=timezone.now(),
                                          content=content)

        response_data[Protocol.RESULT] = Protocol.SUCCESS

        if user_id != thread.author_id:
            summary = content[:17]
            if len(content) > 17:
                summary += '...'

            send_push_message([thread.author.pk], push_type=PUSH_NEW_COMMENT, thread_id=thread_id,
                              comment_id=comment.id, summary=summary, image_url=thread.image_url)

        comments = Comments.objects.filter(thread=thread)

        writers = []
        for comment in comments:
            if comment.author_id != user_id and comment.author_id != thread.author_id:
                writers.append(comment.author_id)

        if len(writers) > 0:
            summary = content[:17]
            if len(content) > 17:
                summary += '...'

            send_push_message(set(writers), push_type=PUSH_NEW_COMMENT_FRIEND, thread_id=thread_id,
                              comment_id=comment.id, summary=summary, image_url=thread.image_url)

    except Exception as err:
        response_data[Protocol.MESSAGE] = str(err)

    return HttpResponse(json.dumps(response_data), content_type='application/json')
예제 #3
0
def post_thread_like(request, thread_id):
    response_data = {
        Protocol.RESULT: Protocol.FAIL,
        Protocol.MESSAGE: ''
    }

    try:
        user_id = int(request.session['user_id'])
        user = Users.objects.get(id=user_id)

        need_to_push = False

        # update db
        thread = Threads.objects.get(id=int(thread_id))
        thread_likes = [user.id for user in thread.likes.only('id')]
        if user_id in thread_likes:
            response_data[Protocol.MESSAGE] = 'Warn: User has already liked'
        else:
            thread.likes.add(user)
            thread_likes.append(user.id)

        if thread.max_like < len(thread_likes):
            thread.max_like = len(thread_likes)
            need_to_push = True

        thread.save()
        response_data[Protocol.RESULT] = Protocol.SUCCESS

        if need_to_push and user_id != thread.author_id:
            send_push_message([thread.author.pk], push_type=PUSH_LIKE_THREAD, thread_id=thread_id,
                              image_url=thread.image_url)

    except Exception as err:
        response_data[Protocol.MESSAGE] = str(err)

    return HttpResponse(json.dumps(response_data), content_type='application/json')
예제 #4
0
def post_thread(request):
    response_data = {
        Protocol.RESULT: Protocol.FAIL,
        Protocol.MESSAGE: ''
    }

    try:
        if re.search(r'.*multipart/form-data.*', request.META['CONTENT_TYPE']):
            req_json = json.loads(request.REQUEST['json'])
            req_file = request.FILES['bg_image_file']

            file_name = fileutil.generate_file_name(req_file.name)

            # save file
            default_storage.save(settings.MEDIA_DIR + '/' + file_name, ContentFile(req_file.read()))

            # insert db
            user = Users.objects.get(id=int(request.session['user_id']))
            thread = Threads.objects.create(author=user,
                                            is_public=bool(req_json['is_public']),
                                            pub_date=timezone.now(),
                                            image_url=file_name,
                                            content=req_json['content'])

        else:
            req_json = json.loads(request.body.decode('utf-8'))
            image_url = ''

            # insert db
            user = Users.objects.get(id=int(request.session['user_id']))
            thread = Threads.objects.create(author=user,
                                            is_public=bool(req_json['is_public']),
                                            pub_date=timezone.now(),
                                            image_url=image_url,
                                            content=req_json['content'])

        thread.readers.add(user.id)

        readers = []
        for friend in user.friends.only('pk', 'friend_phones'):
            friend_phones = [phone.id for phone in friend.friend_phones.only('pk')]
            if len(friend_phones) >= 2:
                readers.append(friend.pk)

        thread.readers.add(*readers)

        response_data = {
            Protocol.RESULT: Protocol.SUCCESS,
            Protocol.MESSAGE: ''
        }

        summary = req_json['content'][:17]
        if len(req_json['content']) > 17:
            summary += '...'

        send_push_message(readers, push_type=PUSH_NEW_THREAD, thread_id=thread.pk, summary=summary,
                          image_url=thread.image_url)

    # if malformed protocol
    except Exception as err:
        response_data[Protocol.MESSAGE] = str(err)

    return HttpResponse(json.dumps(response_data), content_type='application/json')