def view_edit_achievement(request, achievement_id, edit_achievement_template): userprofile = loggedin_userprofile(request) achievement = get_object_or_404(Achievement, id=int(achievement_id)) if userprofile.is_my_achievement(achievement): achievements = list(userprofile.achievements) for achievment_info in achievements: if achievment_info['id'] == int(achievement_id): achievements.remove({'title':achievement.title, 'id':int(achievement_id), 'description':achievement.description}) break if request.method == 'GET': form = AddAchievementForm({'title':achievement.title, 'description':achievement.description}) return response(request, edit_achievement_template, {'achievement':achievement, 'form':form, 'previous_achievements':achievements}) form = AddAchievementForm(post_data(request)) if form.is_valid(): achievement.update(title=form.cleaned_data.get('title'), description=form.cleaned_data.get('description'),) from users.messages import ACHIEVEMENT_UPDATED_SUCCESSFULLY messages.success(request, ACHIEVEMENT_UPDATED_SUCCESSFULLY) return HttpResponseRedirect(redirect_to=url_reverse('users.views.view_all_achievements')) return response(request, edit_achievement_template, {'achievement':achievement, 'form':form, 'previous_achievements':achievements}) raise Http404
def test_invalid_form_submission(self): data = {'title':1000*'I achieved 67% in my 8th Grade. Believe me', 'description':1000*'Thank God. Thank God. Thank God. '} form = AddAchievementForm(data) self.assertFalse(form.is_valid()) self.assertTrue(form.errors) self.assertTrue(form.errors.has_key('title')) self.assertTrue(form.errors.has_key('description'))
def view_add_achievement(request, add_achievement_template): userprofile = loggedin_userprofile(request) achievements = userprofile.achievements if request.method == 'GET': form = AddAchievementForm() return response(request, add_achievement_template, {'form':form, 'achievements':achievements}) form = AddAchievementForm(post_data(request)) if form.is_valid(): Achievement.objects.create_achievement(userprofile, title=form.cleaned_data.get('title'), description=form.cleaned_data.get('description')) from users.messages import ACHIEVEMENT_ADDED_SUCCESSFULLY messages.success(request, ACHIEVEMENT_ADDED_SUCCESSFULLY) return HttpResponseRedirect(url_reverse('users.views.view_all_achievements')) return response(request, add_achievement_template, {'form':form, 'achievements':achievements})
def test_valid_form_submission(self): data = {'title':'I achieved 67% in my 8th Grade. Believe me. Thats one of my achievements', 'description':'Thank God. Thank God. Thank God. Thank God. Thank God. Thank God. Thank God. Thank God. Thank God. Thank God. '} form = AddAchievementForm(data) self.assertTrue(form.is_valid()) self.assertFalse(form.errors)
def test_empty_form_submission(self): data = {'title':'', 'description':''} form = AddAchievementForm(data) self.assertFalse(form.is_valid()) self.assertTrue(form.errors)