Metadata-Version: 2.1
Name: django-visit-count
Version: 1.1.0
Summary: Count visits using cache for Django models
Home-page: https://github.com/QueraTeam/django-visit-count
Download-URL: https://pypi.python.org/pypi/django-visit-count
Author: Mohammad Javad Naderi
License: MIT
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 3.2
Classifier: Framework :: Django :: 4.0
Classifier: Framework :: Django :: 4.1
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: Django (>=3.2)
Requires-Dist: django-ipware (>=2.0.0)
Provides-Extra: dev
Requires-Dist: pre-commit ; extra == 'dev'

# Django Visit Count

[![](https://img.shields.io/pypi/v/django-visit-count.svg)](https://pypi.python.org/pypi/django-visit-count/)
[![](https://img.shields.io/github/license/QueraTeam/django-visit-count.svg)](https://github.com/QueraTeam/django-visit-count/blob/master/LICENSE)
[![](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Count visits using cache for Django models.

## Installation

1. [Set-up](https://docs.djangoproject.com/en/dev/topics/cache/#setting-up-the-cache) Django's cache framework.

2. Install the Python package.

   ```
   pip install django_visit_count
   ```

## Usage

Use `VisitCountMixin`. It adds a `visit_count` field to your model.

```python
from django_visit_count.mixins import VisitCountMixin

class MyBlogPost(VisitCountMixin, models.Model):
    ...
```

Create and run migrations on your model.

```shell
$ python manage.py makemigrations my_blog_app
$ python manage.py migrate my_blog_app
```

Count visits in your view like this:

```python
def view_blog_post(request, post_id):
    post = get_object_or_404(MyBlogPost, pk=post_id)
    post.count_visit()
    ...
```

## Advanced Usage

If you need more control, you can use `is_new_visit` function.

```python
class MyBlogPost(models.Model):
    total_visits = models.PositiveIntegerField(default=0)
    ...
```

```python
from django_visit_count.utils import is_new_visit

def view_blog_post(request, post_id):
    post = get_object_or_404(MyBlogPost, pk=post_id)

    if is_new_visit(request, post):
        post.total_visits = F("total_visits") + 1
        post.save(update_fields=["total_visits"])

    ...
```

You can pass an optional keyword argument `session_duration` (integer, number of seconds)
to `count_visit` or `is_new_visit`.

## Settings

Default settings:

```python
VISIT_COUNT_DEFAULT_SESSION_DURATION = 5 * 60  # seconds
```

## Development

- Install development dependencies in your virtualenv with `pip install -e '.[dev]'`
- Install pre-commit hooks using `pre-commit install`.

## License

MIT
