Exemple #1
0
def store_article_access(request, article, access_type, galley_type='view'):

    try:
        user_agent = parse_ua_string(request.META.get('HTTP_USER_AGENT', None))
    except TypeError:
        user_agent = None

    ip = shared.get_ip_address(request)
    iso_country_code = get_iso_country_code(ip)
    country = iso_to_country_object(iso_country_code)
    counter_tracking_id = request.session.get('counter_tracking')
    identifier = counter_tracking_id if counter_tracking_id else ip

    if user_agent and not user_agent.is_bot:

        # check if the current IP has accessed this article recently.
        time_to_check = timezone.now() - timedelta(seconds=30)
        check = models.ArticleAccess.objects.filter(
            identifier=identifier,
            accessed__gte=time_to_check,
            type=access_type,
            galley_type=galley_type,
        ).count()

        if not check:

            new_access = models.ArticleAccess.objects.create(
                article=article,
                type=access_type,
                identifier=identifier,
                galley_type=galley_type,
                country=country,
            )

            return new_access

        else:
            # get the most recent access attempt and reset its accessed to now.
            with transaction.atomic():
                access = models.ArticleAccess.objects.select_for_update(
                    # Avoid concurrent writes
                    skip_locked=True, ).filter(
                        identifier=identifier,
                        accessed__gte=time_to_check,
                        type=access_type,
                        galley_type=galley_type,
                    ).order_by('-accessed')[0]

                if access:
                    access.accessed = timezone.now()
                    access.save()

                    return access

                else:
                    return None

    else:

        return None
Exemple #2
0
def store_article_access(request, article, access_type, galley_type='view'):

    user_agent = parse_ua_string(request.META.get('HTTP_USER_AGENT', None))
    counter_tracking_id = request.session.get('counter_tracking')
    identifier = counter_tracking_id if counter_tracking_id else shared.get_ip_address(
        request)

    if user_agent and not user_agent.is_bot:

        # check if the current IP has accessed this article recently.
        time_to_check = timezone.now() - timedelta(seconds=30)
        check = models.ArticleAccess.objects.filter(
            identifier=identifier,
            accessed__gte=time_to_check,
            type=access_type,
            galley_type=galley_type).count()

        if not check:

            new_access = models.ArticleAccess.objects.create(
                article=article,
                type=access_type,
                identifier=identifier,
                galley_type=galley_type)

            return new_access

        else:
            # get the most recent access attempt and reset its accessed to now.
            access = models.ArticleAccess.objects.filter(
                identifier=identifier,
                accessed__gte=time_to_check,
                type=access_type,
                galley_type=galley_type).order_by('-accessed')[0]

            if access:
                access.accessed = timezone.now()
                access.save()

                return access

            else:
                return None
    else:

        return None
Exemple #3
0
    def add_book_access(self, request, access_type='download'):
        try:
            user_agent = parse_ua_string(request.META.get('HTTP_USER_AGENT', None))
        except TypeError:
            user_agent = None

        ip = get_ip_address(request)
        iso_country_code = get_iso_country_code(ip)
        identifier = request.session.session_key

        try:
            country = core_models.Country.objects.get(
                code=iso_country_code,
            )
        except core_models.Country.DoesNotExist:
            country = None

        if user_agent and not user_agent.is_bot:

            # check if the current IP has accessed this article recently.
            time_to_check = timezone.now() - timedelta(seconds=10)
            check = BookAccess.objects.filter(
                book=self.book,
                chapter=self,
                accessed__gte=time_to_check,
                type=access_type,
                identifier=identifier,
            ).count()

            if not check:
                BookAccess.objects.create(
                    book=self.book,
                    chapter=self,
                    type=access_type,
                    country=country,
                    identifier=identifier,
                )