Guest User

Untitled

a guest
Feb 19th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. class IncidentEdit(UpdateView):
  2. model=Incident
  3. fields = visible_field_list
  4. sucess_url = '/status'
  5.  
  6. class IncidentForm(ModelForm):
  7. class Meta:
  8. model = Incident
  9. # Define fields you want here, it is best practice not to use '__all__'
  10. fields = [...]
  11.  
  12. def clean(self):
  13. cleaned_data = super(IncidentForm, self).clean()
  14.  
  15. field_1 = cleaned_data.get('field_1')
  16. field_2 = cleaned_data.get('field_2')
  17. field_3 = cleaned_data.get('field_3')
  18.  
  19. # Values may be None if the fields did not pass previous validations.
  20. if field_1 is not None and field_2 is not None and field_3 is not None:
  21. # If fields have values, perform validation:
  22. if not field_3 == field_1 + field_2:
  23. # Use None as the first parameter to make it a non-field error.
  24. # If you feel is related to a field, use this field's name.
  25. self.add_error(None, ValidationError('field_3 must be equal to the sum of field_1 and filed_2'))
  26.  
  27. # Required only if Django version < 1.7 :
  28. return cleaned_data
  29.  
  30.  
  31. class IncidentEdit(UpdateView):
  32. model = Incident
  33. form_class = IncidentForm
  34. fields = visible_field_list
  35. success_url = '/status'
  36.  
  37. class IncidentEdit(UpdateView):
  38.  
  39. ...
  40.  
  41. def form_valid(self, form):
  42. if form.cleaned_data['email'] in
  43. [i.email for i in Incident.objects.exclude(id=get_object().id)]:
  44. # Assume incident have email and it should be unique !!
  45. form.add_error('email', 'Incident with this email already exist')
  46. return self.form_invalid(form)
  47. return super(IncidentEdit, self).form_valid(form)
  48.  
  49. # models.py
  50. class Incident(models.Model):
  51. numboys = models.SmallIntegerField(default=0)
  52. numgirls = models.SmallIntegerField(default=0)
  53. classttl = models.SmallIntegerField(default=0)
  54.  
  55.  
  56. # views.py
  57. def retunTestPassedResp(request):
  58. return HttpResponse()
  59.  
  60. class NumValidationMixin:
  61. def form_valid(self, form):
  62. data = self.request.POST
  63. boys = data.get('numboys')
  64. girls = data.get('numgirls')
  65. ttl = data.get('classttl')
  66. if boys and girls and ttl:
  67. if int(ttl) == int(boys) + int(girls):
  68. return super().form_valid(form)
  69. # use form.errors to add the error msg as a dictonary
  70. form.errors['input invalid'] = '%s + %s not equal %s'%(boys, girls, ttl)
  71. form.errors['input invalid'] = 'losing input with boys or other'
  72. return self.form_invalid(form)
  73.  
  74. class UpdateIncident(NumValidationMixin, UpdateView):
  75. model = Incident
  76. fields = ['numboys', 'numgirls', 'classttl']
  77. success_url = reverse_lazy('test-passed')
  78.  
  79. # templates/.../Incident_form.html
  80. [...]
  81. <body>
  82. {{form}}
  83. {% if form.errors %}
  84. <p>get error</p>
  85. {{form.errors}}
  86. {% endif %}
  87. </body>
  88.  
  89. # tests.py
  90. class IncidentUpdateTest(TestCase):
  91. def setUp(self):
  92. Incident.objects.create()
  93.  
  94. def test_can_update_with_right_data(self):
  95. [...]
  96.  
  97. def test_invalid_error_with_illegal_post(self):
  98. response = self.client.post(
  99. reverse('update-incident', args=(1,)),
  100. data={'numboys': '1', 'numgirls': '1', 'classttl': '3'}
  101. )
  102. self.assertEqual(Incident.objects.first().classttl, 0)
  103. # testing response page showing error msg
  104. self.assertContains(response, 'not equal')
Add Comment
Please, Sign In to add comment