Skip to content

HistoricalRecords: add additional fields (ip and browser) #8490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion readthedocs/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ExtraFieldsHistoricalModel(models.Model):

Extra data includes:

- Users after they have been deleted.
- User information to retain after they have been deleted
- IP & browser
"""

extra_history_user_id = models.IntegerField(
Expand All @@ -44,6 +45,17 @@ class ExtraFieldsHistoricalModel(models.Model):
null=True,
db_index=True,
)
extra_history_ip = models.GenericIPAddressField(
_('IP address'),
blank=True,
null=True,
)
extra_history_browser = models.CharField(
_('Browser user-agent'),
max_length=250,
blank=True,
null=True,
)

class Meta:
abstract = True
Expand Down
23 changes: 23 additions & 0 deletions readthedocs/core/migrations/0008_add_extra_history_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.24 on 2021-09-14 19:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0007_historicaluser'),
]

operations = [
migrations.AddField(
model_name='historicaluser',
name='extra_history_browser',
field=models.CharField(blank=True, max_length=250, null=True, verbose_name='Browser user-agent'),
),
migrations.AddField(
model_name='historicaluser',
name='extra_history_ip',
field=models.GenericIPAddressField(blank=True, null=True, verbose_name='IP address'),
),
]
12 changes: 11 additions & 1 deletion readthedocs/core/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from django.db.models.signals import pre_delete
from django.dispatch import Signal, receiver
from rest_framework.permissions import SAFE_METHODS
from simple_history.models import HistoricalRecords
from simple_history.signals import pre_create_historical_record

from readthedocs.analytics.utils import get_client_ip
from readthedocs.builds.models import Version
from readthedocs.core.unresolver import unresolve
from readthedocs.projects.models import Project
Expand Down Expand Up @@ -122,10 +124,18 @@ def delete_projects(sender, instance, *args, **kwargs):
@receiver(pre_create_historical_record)
def add_extra_historical_fields(sender, **kwargs):
history_instance = kwargs['history_instance']
if not history_instance:
return

history_user = kwargs['history_user']
if history_instance and history_user:
if history_user:
history_instance.extra_history_user_id = history_user.id
history_instance.extra_history_user_username = history_user.username

request = getattr(HistoricalRecords.context, 'request', None)
if request:
history_instance.extra_history_ip = get_client_ip(request)
history_instance.extra_history_browser = request.headers.get('User-Agent')


signals.check_request_enabled.connect(decide_if_cors)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.2.24 on 2021-09-14 19:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('organizations', '0006_add_assets_cleaned'),
]

operations = [
migrations.AddField(
model_name='historicalorganization',
name='extra_history_browser',
field=models.CharField(blank=True, max_length=250, null=True, verbose_name='Browser user-agent'),
),
migrations.AddField(
model_name='historicalorganization',
name='extra_history_ip',
field=models.GenericIPAddressField(blank=True, null=True, verbose_name='IP address'),
),
migrations.AddField(
model_name='historicalteam',
name='extra_history_browser',
field=models.CharField(blank=True, max_length=250, null=True, verbose_name='Browser user-agent'),
),
migrations.AddField(
model_name='historicalteam',
name='extra_history_ip',
field=models.GenericIPAddressField(blank=True, null=True, verbose_name='IP address'),
),
]
23 changes: 23 additions & 0 deletions readthedocs/projects/migrations/0082_add_extra_history_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.24 on 2021-09-14 19:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('projects', '0081_add_another_header'),
]

operations = [
migrations.AddField(
model_name='historicalproject',
name='extra_history_browser',
field=models.CharField(blank=True, max_length=250, null=True, verbose_name='Browser user-agent'),
),
migrations.AddField(
model_name='historicalproject',
name='extra_history_ip',
field=models.GenericIPAddressField(blank=True, null=True, verbose_name='IP address'),
),
]