Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Add IP Address tracking of user's answers
Browse files Browse the repository at this point in the history
  • Loading branch information
cltrudeau committed Oct 26, 2016
1 parent f5bd433 commit 7ee8599
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* add Google reCAPTCHA capabilities
* added views for getting latest survey
* added Email field
* added IP Address tracking on the survey submit

0.6.1
=====
Expand Down
4 changes: 2 additions & 2 deletions dform/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ def show_field_key(self, obj):

@admin.register(AnswerGroup)
class AnswerGroupAdmin(admin.ModelAdmin, mixin):
list_display = ('id', 'updated', 'show_version', 'show_data',
'show_questions', 'show_answers', 'show_actions')
list_display = ('id', 'updated', 'show_version', 'show_data',
'ip_address', 'show_questions', 'show_answers', 'show_actions')

def lookup_allowed(self, key, value):
# enable cross FK lookups for this admin object
Expand Down
5 changes: 5 additions & 0 deletions dform/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SurveyForm(forms.Form):
def __init__(self, *args, **kwargs):
self.survey_version = kwargs.pop('survey_version')
self.answer_group = kwargs.pop('answer_group', None)
self.ip_address = kwargs.pop('ip_address', '')

if 'initial' in kwargs:
raise AttributeError(
Expand Down Expand Up @@ -81,6 +82,10 @@ def save(self):
self.answer_group = AnswerGroup.factory(
survey_version=self.survey_version)

if self.ip_address:
self.answer_group.ip_address = self.ip_address
self.answer_group.save()

for name, field in self.fields.items():
question = Question.objects.get(id=name[2:],
survey_versions=self.survey_version)
Expand Down
20 changes: 20 additions & 0 deletions dform/migrations/0003_auto_20161026_1250.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-10-26 12:50
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dform', '0002_survey_use_recaptcha'),
]

operations = [
migrations.AlterField(
model_name='question',
name='field_key',
field=models.CharField(choices=[('tx', 'Text'), ('mt', 'MultiText'), ('em', 'Email'), ('dr', 'Dropdown'), ('rd', 'Radio'), ('ch', 'Checkboxes'), ('rt', 'Rating'), ('in', 'Integer'), ('fl', 'Float')], max_length=2),
),
]
20 changes: 20 additions & 0 deletions dform/migrations/0004_answergroup_ip_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-10-26 12:50
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dform', '0003_auto_20161026_1250'),
]

operations = [
migrations.AddField(
model_name='answergroup',
name='ip_address',
field=models.GenericIPAddressField(default='127.0.0.1'),
),
]
3 changes: 3 additions & 0 deletions dform/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,9 @@ class AnswerGroup(TimeTrackModel):
object_id = models.PositiveIntegerField(default=0)
group_data = GenericForeignKey('content_type', 'object_id')

ip_address = models.GenericIPAddressField(default='0.0.0.0',
verbose_name='IP Address')

class Meta:
verbose_name = 'Answer Group'

Expand Down
3 changes: 3 additions & 0 deletions dform/tests/test_dform.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,9 @@ def _survey_view(self, url_base, expect_pym, use_hooks):
self.assertEqual(302, response.status_code)
self.assertIn(survey.success_redirect, response._headers['location'][1])

self.assertEqual(1, AnswerGroup.objects.count())
self.assertEqual('127.0.0.1', AnswerGroup.objects.first().ip_address)

self.assertEqual(8, Answer.objects.count())
for question, value in expected:
answer = Answer.objects.get(question=question)
Expand Down
3 changes: 2 additions & 1 deletion dform/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def _survey_view(request, survey_version_id, token, is_embedded):
survey__token=token)

if request.method == 'POST':
form = SurveyForm(request.POST, survey_version=version)
form = SurveyForm(request.POST, survey_version=version,
ip_address=request.META['REMOTE_ADDR'])
if form.is_valid():
form.save()

Expand Down

0 comments on commit 7ee8599

Please sign in to comment.