SHARE
TWEET

Untitled

a guest Dec 13th, 2016 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {'custom_answers': [OrderedDict([('user', <SimpleLazyObject: <User: 123>>), ('question', <Question: Вопрос 2>), ('text', 'TExt')]), OrderedDict([('user', <SimpleLazyObject: <User: 123>>), ('question', <Question: Вопрос 1>), ('text', 'tes')])], 'poll': <Poll: Опрос 1>, 'user': <SimpleLazyObject: <User: 123>>}
  2.    
  3. {'custom_answers': [], 'poll': <Poll: Опрос 1>, 'user': <User: 123>}
  4.    
  5. <QueryDict: {'poll': ['1'], 'custom_answers': ["{'question': 2, 'text': 'TExt'}", "{'question': 1, 'text': 'tes'}"]}>
  6.    
  7. {custom_answers': [{'text': 'TExt', 'question': 2}, {'text': 'tes', 'question': 1}], 'poll': 1}
  8.    
  9. class CustomUserAnswerSerializer(serializers.ModelSerializer):
  10.     user = serializers.PrimaryKeyRelatedField(
  11.         read_only=True,
  12.         default=CurrentUserDefault()
  13.     )
  14.  
  15.     class Meta:
  16.         model = CustomUserAnswer
  17.         fields = ('user', 'question', 'text')
  18.  
  19.  
  20. class CompletedPollSerializer(serializers.ModelSerializer):
  21.     user = serializers.PrimaryKeyRelatedField(
  22.         read_only=True,
  23.         default=CurrentUserDefault()
  24.     )
  25.     custom_answers = CustomUserAnswerSerializer(
  26.         many=True, write_only=True, required=False      
  27.     )
  28.  
  29.     class Meta:
  30.         model = CompletedPoll
  31.         fields = ('poll', 'user', 'custom_answers')
  32.  
  33.     def create(self, validated_data):
  34.         print(validated_data)
  35.    
  36. class CompletedPoll(models.Model):
  37.     user = models.ForeignKey(
  38.         settings.AUTH_USER_MODEL,
  39.         related_name='ended_polls'
  40.     )
  41.     poll = models.ForeignKey(Poll)
  42.     ended_date = models.DateTimeField(auto_now_add=True)
  43.  
  44.     class Meta:
  45.         ordering = ('-ended_date',)
  46.         unique_together = ('user', 'poll')
  47.  
  48.  
  49. class CustomUserAnswer(models.Model):
  50.     user = models.ForeignKey(
  51.         settings.AUTH_USER_MODEL,
  52.         related_name='custom_answers'
  53.     )
  54.     question = models.ForeignKey(
  55.         Question,
  56.         related_name='custom_answers'
  57.     )
  58.     text = models.CharField(
  59.         verbose_name='Текст ответа',
  60.         max_length=800
  61.     )
  62.  
  63.     class Meta:
  64.         unique_together = ('user', 'question')
  65.    
  66. class ApiPollsTestCase(APITestCase):
  67.     def setUp(self):
  68.         call_command('loaddata', 'polls/fixtures/polls')
  69.  
  70.     self.user = User.objects.create(
  71.         username='123',
  72.         password='123',
  73.         email='123@123.ru'
  74.     )
  75.  
  76.     self.client = APIClient()
  77.     self.client.credentials(
  78.         HTTP_AUTHORIZATION='Token ' + self.user.auth_token.key
  79.     )
  80.  
  81.     def test_complete_poll(self):
  82.         data = {
  83.             'poll': 1,
  84.             'custom_answers': [
  85.                 {'question': 2, 'text': 'TExt'},
  86.                 {'question': 1, 'text': 'tes'}
  87.             ]
  88.         }
  89.  
  90.         response = self.client.post(reverse('api_v1:complete-poll'), data)
  91.         print(CustomUserAnswer.objects.all(), response.data)
  92.         self.assertEqual(response.status_code, 201)
  93.         self.assertTrue(
  94.             CompletedPoll.objects.filter(
  95.                 user=self.user, poll__id=data['poll']
  96.             ).exists()
  97.         )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top