Browse Source

Fixed duplicate UserResponse added bug and added unique constraint on UserResponse

pull/160/head
Charles Shin 8 years ago
parent
commit
833a7a877e
3 changed files with 27 additions and 10 deletions
  1. +18
    -0
      edivorce/apps/core/migrations/0008_auto_20170224_0259.py
  2. +3
    -0
      edivorce/apps/core/models.py
  3. +6
    -10
      edivorce/apps/core/utils/user_response.py

+ 18
- 0
edivorce/apps/core/migrations/0008_auto_20170224_0259.py View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0007_auto_20170210_1702'),
]
operations = [
migrations.AlterUniqueTogether(
name='userresponse',
unique_together=set([('bceid_user', 'question')]),
),
]

+ 3
- 0
edivorce/apps/core/models.py View File

@ -119,6 +119,9 @@ class UserResponse(models.Model):
value = models.TextField(blank=True)
""" The question's response from the user """
class Meta:
unique_together = ("bceid_user", "question")
def __str__(self):
return '%s -> %s' % (self.bceid_user, self.question.key)


+ 6
- 10
edivorce/apps/core/utils/user_response.py View File

@ -47,15 +47,11 @@ def copy_session_to_db(request, bceid_user):
for q in questions:
if request.session.get(q.key) is not None:
# copy the response to the database
if UserResponse.objects.filter(bceid_user=bceid_user, question=q).exists():
UserResponse.objects.update(bceid_user=bceid_user,
question=q,
value=request.session.get(q.key))
else:
UserResponse.objects.create(bceid_user=bceid_user,
question=q,
value=request.session.get(q.key))
UserResponse.objects.update_or_create(
bceid_user=bceid_user,
question=q,
defaults={'value': request.session.get(q.key)},
)
# clear the response from the session
# clear the response from the session
request.session[q.key] = None

Loading…
Cancel
Save