Skip to content

Commit 5030d1d

Browse files
committed
Track users submitting events
Track when users submit bugs, news, events, comments etc in a simple table. The idea is we will then be able to use this for rate limiting, and also for tracking dosn some level of abuse.
1 parent 833bda0 commit 5030d1d

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Django 4.2.11 on 2025-11-21 08:23
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
('core', '0006_version_docsgit'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='UserSubmission',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('when', models.DateTimeField(auto_now_add=True)),
21+
('what', models.CharField(max_length=100)),
22+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
23+
],
24+
options={
25+
'indexes': [models.Index(fields=['user', 'when'], name='core_usersubm_userwhen_idx')],
26+
},
27+
),
28+
]

pgweb/core/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@ class UserProfile(models.Model):
274274
help_text="Disallow login to this account using OAuth providers like Google or Microsoft.")
275275

276276

277+
class UserSubmission(models.Model):
278+
user = models.ForeignKey(User, null=False, blank=False, on_delete=models.CASCADE)
279+
when = models.DateTimeField(null=False, blank=False, auto_now_add=True)
280+
what = models.CharField(null=False, blank=False, max_length=100)
281+
282+
class Meta:
283+
indexes = [
284+
models.Index(
285+
name='core_usersubm_userwhen_idx',
286+
fields=('user', 'when'),
287+
),
288+
]
289+
290+
277291
# Notifications sent for any moderated content.
278292
# Yes, we uglify it by storing the type of object as a string, so we don't
279293
# end up with a bazillion fields being foreign keys. Ugly, but works.

pgweb/docs/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pgweb.util.helpers import template_to_string
1515
from pgweb.util.misc import send_template_mail
1616

17-
from pgweb.core.models import Version
17+
from pgweb.core.models import Version, UserSubmission
1818
from pgweb.util.db import exec_to_dict
1919

2020
from .models import DocPage, DocPageRedirect
@@ -420,6 +420,7 @@ def commentform(request, itemid, version, filename):
420420
replyto='%s, %s' % (form.cleaned_data['email'], settings.DOCSREPORT_EMAIL),
421421
sendername='PG Doc comments form'
422422
)
423+
UserSubmission(user=request.user, what='Added comment to {}/{}'.format(version, filename)).save()
423424
return HttpResponseRedirect("done/")
424425
else:
425426
form = DocCommentForm(initial={

pgweb/misc/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pgweb.util.helpers import template_to_string
1313
from pgweb.util.misc import send_template_mail
1414

15-
from pgweb.core.models import Version
15+
from pgweb.core.models import Version, UserSubmission
1616
from pgweb.misc.models import BugIdMap
1717

1818
from .forms import SubmitBugForm
@@ -54,6 +54,8 @@ def submitbug(request):
5454
messageid=messageid,
5555
)
5656

57+
UserSubmission(user=request.user, what='Submitted bug {}'.format(bugid)).save()
58+
5759
return HttpResponseRedirect("/account/submitbug/{0}/".format(bugid))
5860
else:
5961
form = SubmitBugForm(initial={

pgweb/util/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for
4545
raise PermissionDenied("You cannot edit this item")
4646

4747
if request.method == 'POST':
48+
from pgweb.core.models import UserSubmission
49+
4850
if 'modstate' in (f.name for f in instance._meta.get_fields()) and instance.modstate == ModerationState.CREATED and request.POST.get('delete', '') == 'delete':
4951
# Don't care to validate, just delete.
5052
instance.delete()
@@ -111,6 +113,7 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for
111113
else:
112114
notify.write("{}\n".format(str(form.cleaned_data[f])))
113115
notify.write("\n")
116+
UserSubmission(user=request.user, what='Added {} {}'.format(instance._meta.verbose_name, instance.id)).save()
114117
else:
115118
subj = '{0} id {1} ({2}) has been modified'.format(instance._meta.verbose_name, instance.id, str(instance))
116119

@@ -150,6 +153,11 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for
150153
if diffrows:
151154
notify.write("\n".join(diffrows))
152155
notify.write("\n\n")
156+
if do_notify:
157+
# We only store modification events if it's a change that would've been notified about (meaning users can edit
158+
# a pending entry an unlimited number of times without storing modification events, but once it has been approved,
159+
# any further edits are logged)
160+
UserSubmission(user=request.user, what='Modified {} {}'.format(instance._meta.verbose_name, instance.id)).save()
153161

154162
if do_notify and notify.tell():
155163
send_simple_mail(

0 commit comments

Comments
 (0)