Skip to content

Commit

Permalink
Update Django / Python support (5.0, 3.12) (#27)
Browse files Browse the repository at this point in the history
Released as v2.0
  • Loading branch information
hugorodgerbrown authored Nov 7, 2023
1 parent f5b2a27 commit dcb9599
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 24 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.8", "3.9", "3.10", "3.11"]
django: ["32", "40", "41", "42", "50", "main"]
python: ["3.8", "3.9", "3.10", "3.11", "3.12"]
django: ["32", "42", "50", "main"]
exclude:
- python: "3.8"
django: "50"
Expand All @@ -73,7 +73,7 @@ jobs:
django: "main"

env:
TOXENV: py${{ matrix.python }}-django${{ matrix.django }}
TOXENV: django${{ matrix.django }}-py${{ matrix.python }}

steps:
- name: Check out the repository
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:

- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: "v0.1.3"
rev: "v0.1.4"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## 2.0 [unreleased]
## 2.0

* Add support for Django 4.1,4.2,5.0 and Python 3.11, 3.12
* Replace flake8, isort with ruff.
* Add browser, device, os denomralised fields (thanks @mboboc)

## 1.1

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ Django app for recording daily user visits

This package supports Python 3.8 and above and Django 3.2 and above.

## Upgrading from v1 to v2

v2 added three new denormalised fields extracted from the User Agent
string - device, os, browser - to make it easier to analyse directly
in the database.

If you want to backfill historical data you will need to run the
management command `update_user_visit_user_agent_data` after the
upgrade.

---

This app consists of middleware to record user visits, and a single
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-user-visit"
version = "2.0.dev0"
version = "2.0"
description = "Django app used to track user visits."
license = "MIT"
authors = ["YunoJuno <[email protected]>"]
Expand All @@ -20,7 +20,6 @@ classifiers = [
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand Down
18 changes: 6 additions & 12 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ envlist =
fmt, lint, mypy,
django-checks,
; https://docs.djangoproject.com/en/5.0/releases/
; | 3.2 | 4.2 | 5.0 | main
; --- | --- | --- | --- | ---
; 3.8 | x | x | - | -
; 3.9 | x | x | - | -
; 3.10 | x | x | x | x
; 3.11 | - | x | x | x
; 3.12 | - | - | x | x
py38-django{32,40,41,42}
py39-django{32,40,41,42}
py310-django{32,40,41,42,50,main}
py311-django{32,40,41,42,50,main}
py312-django{32,40,41,42,50,main}
django32-py{38,39,310}
django40-py{38,39,310}
django41-py{38,39,310,311}
django42-py{38,39,310,311}
django50-py{310,311,312}
djangomain-py{311,312}

[testenv]
deps =
Expand Down
4 changes: 3 additions & 1 deletion user_visit/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class UserVisitAdmin(admin.ModelAdmin):
"timestamp",
"session_key",
"remote_addr",
"user_agent",
"device",
"os",
"browser",
"ua_string",
"context",
"created_at",
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

import argparse
from typing import Any

from django.core.management.base import BaseCommand
from django.utils.translation import gettext as _, gettext_lazy as _lazy

from user_visit.models import UserVisit


class Command(BaseCommand):
help = _lazy( # noqa: A003
"Sync browser, device and OS data missing from UserVisit"
)

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"-f",
"--force",
action="store_true",
default=False,
help=_(
"Use the --force option to update all UserVisit "
"objects (defaults to backfilling empty records only)."
),
)

def handle(self, *args: Any, **options: Any) -> None:
visits = UserVisit.objects.all()
if not options["force"]:
visits = visits.filter(ua_string="")
updated = 0
for v in visits.iterator():
user_agent = v.user_agent
v.device = user_agent.get_device()
v.os = user_agent.get_os()
v.browser = user_agent.get_browser()
v.save(update_fields=["device", "os", "browser"])
self.stdout.write(f"Updated UserVisit #{v.pk}")
updated += 1
self.stdout.write("---")
self.stdout.write(f"Updated {updated} UserVisit objects.")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.23 on 2023-11-02 10:17
# Generated by Django 4.2.7 on 2023-11-07 12:52

from django.db import migrations, models

Expand All @@ -17,11 +17,15 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name="uservisit",
name="device",
field=models.CharField(blank=True, default="", max_length=200),
field=models.CharField(
blank=True, default="", max_length=200, verbose_name="Device type"
),
),
migrations.AddField(
model_name="uservisit",
name="os",
field=models.CharField(blank=True, default="", max_length=200),
field=models.CharField(
blank=True, default="", max_length=200, verbose_name="Operating System"
),
),
]
4 changes: 3 additions & 1 deletion user_visit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class UserVisit(models.Model):
blank=True,
)
ua_string = models.TextField(
"User agent (raw)",
_lazy("User agent (raw)"),
help_text=_lazy("Client User-Agent HTTP header"),
blank=True,
)
Expand All @@ -91,11 +91,13 @@ class UserVisit(models.Model):
default="",
)
device = models.CharField(
_lazy("Device type"),
max_length=200,
blank=True,
default="",
)
os = models.CharField(
_lazy("Operating System"),
max_length=200,
blank=True,
default="",
Expand Down

0 comments on commit dcb9599

Please sign in to comment.