Skip to content

Commit

Permalink
Add new fields to UserVisit (#25)
Browse files Browse the repository at this point in the history
This is an optimization to allow filtering by os/browser/device directly in the database.

h/t @mboboc
  • Loading branch information
mboboc authored Nov 7, 2023
1 parent 38f1057 commit f5b2a27
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
27 changes: 27 additions & 0 deletions user_visit/migrations/0004_add_user_agent_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.23 on 2023-11-02 10:17

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("user_visit", "0003_uservisit_context"),
]

operations = [
migrations.AddField(
model_name="uservisit",
name="browser",
field=models.CharField(blank=True, default="", max_length=200),
),
migrations.AddField(
model_name="uservisit",
name="device",
field=models.CharField(blank=True, default="", max_length=200),
),
migrations.AddField(
model_name="uservisit",
name="os",
field=models.CharField(blank=True, default="", max_length=200),
),
]
18 changes: 18 additions & 0 deletions user_visit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def build(self, request: HttpRequest, timestamp: datetime.datetime) -> UserVisit
context=REQUEST_CONTEXT_EXTRACTOR(request),
)
uv.hash = uv.md5().hexdigest()
uv.browser = uv.user_agent.get_browser()[:200]
uv.device = uv.user_agent.get_device()[:200]
uv.os = uv.user_agent.get_os()[:200]
return uv


Expand Down Expand Up @@ -82,6 +85,21 @@ class UserVisit(models.Model):
help_text=_lazy("Client User-Agent HTTP header"),
blank=True,
)
browser = models.CharField(
max_length=200,
blank=True,
default="",
)
device = models.CharField(
max_length=200,
blank=True,
default="",
)
os = models.CharField(
max_length=200,
blank=True,
default="",
)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
hash = models.CharField( # noqa: A003
max_length=32,
Expand Down

0 comments on commit f5b2a27

Please sign in to comment.