Example #1
0
def status_update(request, status_id):
    status = Facebook_Status.objects_no_filters.get(status_id=status_id)

    response = HttpResponse(content_type="application/json", status=200)
    response_data = dict()
    response_data['id'] = status.status_id

    try:
        if status.needs_refresh and waffle.flag_is_active(
                request, flag_name='status_update_request'):
            update_status_command = updatestatus.Command()
            update_status_command.graph.access_token = update_status_command.graph.get_app_access_token(
                settings.FACEBOOK_APP_ID, settings.FACEBOOK_SECRET_KEY)
            status_response_dict = update_status_command.fetch_status_object_data(
                status_id) or {}

            response_data['likes'] = getattr(
                getattr(getattr(status_response_dict, 'likes', 0), 'summary',
                        0), 'total_count', 0)
            response_data['comments'] = getattr(
                getattr(getattr(status_response_dict, 'comments', 0),
                        'summary', 0), 'total_count', 0)
            response_data['shares'] = getattr(
                getattr(status_response_dict, 'shares', 0), 'count', 0)

            try:
                update_status_command.update_status_object_in_db(
                    retrieved_status_data=status_response_dict,
                    status_object=status,
                    options={
                        'force-update': True,
                        'force-attachment-update': True
                    })
            finally:
                response.status_code = 200

    except KeyError as e:
        response.status_code = 500

    except GraphAPIError as e:
        response.status_code = 504

    except ValueError as e:
        raise e

    except Exception as e:
        print('status_update error:', e)
        raise

    finally:
        format_int_or_null = lambda x: 0 if not x else "{:,}".format(x)

        response_data['likes'] = format_int_or_null(status.like_count)
        response_data['comments'] = format_int_or_null(status.comment_count)
        response_data['shares'] = format_int_or_null(status.share_count)
        response_data['id'] = status.status_id

        response.content = json.dumps(response_data)
        return response
Example #2
0
def status_update(request, status_id):
    status = Facebook_Status.objects.get(status_id=status_id)

    response = HttpResponse(content_type="application/json")
    response_data = dict()
    response_data['id'] = status.status_id

    try:

        update_status_command = updatestatus.Command()
        update_status_command.graph.access_token = facebook.get_app_access_token(
            settings.FACEBOOK_APP_ID, settings.FACEBOOK_SECRET_KEY)
        status_response_dict = update_status_command.fetch_status_object_data(
            status_id)

        response_data['likes'] = getattr(
            getattr(getattr(status_response_dict, 'likes', None), 'summary',
                    None), 'total_count', None)
        response_data['comments'] = getattr(
            getattr(getattr(status_response_dict, 'comments', None), 'summary',
                    None), 'total_count', None)
        response_data['shares'] = getattr(
            getattr(status_response_dict, 'shares', None), 'count', None)
        try:
            status.like_count = int(response_data['likes'])
            status.comment_count = int(response_data['comments'])
            status.share_count = int(response_data['shares'])
            status.save()
            # print 'saved data to db'
        finally:
            format_int_or_null = lambda x: 0 if not x else "{:,}".format(x)

            response_data['likes'] = format_int_or_null(status.like_count)
            response_data['comments'] = format_int_or_null(
                status.comment_count)
            response_data['shares'] = format_int_or_null(status.share_count)
            response_data['id'] = status.status_id
            response.status_code = 200

    except KeyError as e:
        response.status_code = 500

    except GraphAPIError as e:
        response.status_code = 504

    except ValueError as e:
        raise e

    finally:
        # print 'response is:', response_data
        response.content = json.dumps(response_data)
        return response