def _clear_dependant_answers(tree, line_node): find_line = tree.find_line for condition in line_node.get_reversed_conditions(): dep_line_node = find_line(condition.line_id) update_model_instance(dep_line_node, raw_answer=None) _clear_dependant_answers(tree, dep_line_node)
def form_valid(self, form): response = super().form_valid(form=form) tree = self.get_tree() _clear_dependant_answers(tree, self.get_line_node(tree)) update_model_instance(self.object, is_complete=not bool(tree.next_question_to_answer)) return response
def edit_line_wizard(request, preply_id, line_id): preply = get_object_or_404(PollReply.objects.select_for_update(), pk=preply_id) user = request.user user.has_perm_to_change_or_die(preply) tree = ReplySectionTree(preply) try: line_node = tree.find_line(int(line_id)) except KeyError as e: msg = 'PollReplyLine with this id {} does not exist for PollReply {}'.format( line_id, preply, ) logger.error(msg) raise Http404(msg) from e previous_answer = None # TODO: pass instance=preply if request.method == 'POST': form = preply_forms.PollReplyFillForm(line_node=line_node, user=user, data=request.POST) if form.is_valid(): form.save() # Optimize 'next_question_to_answer' & cie PollReplyLine.populate_conditions([node for node in tree if not node.is_section]) _clear_dependant_answers(tree, line_node) if not tree.next_question_to_answer: is_complete = True url = preply.get_absolute_url() else: is_complete = False url = reverse('polls__fill_reply', args=(preply_id,)) update_model_instance(preply, is_complete=is_complete) return HttpResponseRedirect(url) else: previous = tree.get_previous_answered_question(line_node) if previous: previous_answer = _format_previous_answered_question(preply_id, previous, NodeStyle()) form = preply_forms.PollReplyFillForm(line_node=line_node, user=user) return render(request, 'creme_core/generics/blockform/edit.html', {'title': gettext('Answers of the form : {}').format(preply), 'form': form, 'help_message': previous_answer, 'cancel_url': preply.get_absolute_url(), } )
def clean(request): preply = get_object_or_404(PollReply, pk=get_from_POST_or_404(request.POST, 'id')) request.user.has_perm_to_change_or_die(preply) with atomic(): preply.lines.update(raw_answer=None, applicable=True) # Avoids statistics artifacts update_model_instance(preply, is_complete=False) if request.is_ajax(): return HttpResponse() return redirect(preply)
def save(self, *args, **kwargs): super().save(*args, **kwargs) rel_user = self.is_user if rel_user: rel_user._disable_sync_with_contact = True update_model_instance( rel_user, last_name=self.last_name, first_name=self.first_name or '', email=self.email or '', )
def save(self, *args, **kwargs): instance = self.instance conditions = self.get_cleaned_conditions() efilter = instance.efilter if conditions: role = instance.role cdata = self.cleaned_data name = cdata['name'] use_or = cdata['use_or'] ctype = instance.ctype or ContentType.objects.get_for_model( CremeEntity) if efilter is None: efilter = EntityFilter( name=name, entity_type=ctype, filter_type=self.efilter_type, use_or=use_or, ) generate_string_id_and_save( EntityFilter, [efilter], f'creme_core-credentials_{role.id}-', ) instance.efilter = efilter else: update_model_instance( efilter, entity_type=ctype, name=name, use_or=use_or, ) efilter.set_conditions( conditions, check_cycles= False, # There cannot be a cycle without sub-filter. check_privacy=False, # No sense here. ) super().save(*args, **kwargs) elif efilter: instance.efilter = None super().save(*args, **kwargs) efilter.delete() else: super().save(*args, **kwargs) return instance
def test_update_model_instance01(self): self.login() first_name = 'punpun' last_name = 'punpunyama' pupun = FakeContact.objects.create(user=self.user, first_name=first_name, last_name=last_name) first_name = first_name.title() update_model_instance(pupun, first_name=first_name) self.assertEqual(first_name, self.refresh(pupun).first_name) with self.assertNumQueries(0): update_model_instance(pupun, last_name=last_name) self.assertRaises(AttributeError, update_model_instance, pupun, first_name=first_name, unknown_field='??' )
def test_update_model_instance02(self): self.login() first_name = 'punpun' last_name = 'punpunyama' pupun = FakeContact.objects.create(user=self.user, first_name=first_name, last_name=last_name) first_name = first_name.title() last_name = last_name.title() update_model_instance(pupun, first_name=first_name, last_name=last_name) pupun = self.refresh(pupun) self.assertEqual(first_name, pupun.first_name) self.assertEqual(last_name, pupun.last_name) with self.assertNumQueries(0): update_model_instance(pupun, first_name=first_name, last_name=last_name)
def save(self, *args, **kwargs): line = self.line cdata = self.cleaned_data update_model_instance(line, conds_use_or=cdata['use_or']) conds2del = [] # TODO: select for update ?? for old_condition, condition in zip_longest(self.old_conditions, cdata['conditions']): if not condition: # Less new conditions that old conditions => delete conditions in excess conds2del.append(old_condition.id) elif not old_condition: condition.line = line condition.save() elif old_condition.update(condition): old_condition.save() if conds2del: PollFormLineCondition.objects.filter(pk__in=conds2del).delete()
def sync_with_user(sender, instance, created, **kwargs): if instance.is_team: return if getattr(instance, '_disable_sync_with_contact', False): return try: if created: kwargs = {'uuid': UUID_FIRST_CONTACT} if instance.id == 1 else {} instance._linked_contact_cache = \ get_contact_model()._create_linked_contact(instance, **kwargs) else: update_model_instance( instance.linked_contact, last_name=instance.last_name, first_name=instance.first_name, email=instance.email, ) except DatabaseError as e: logger.warning( 'Can not create linked contact for this user: %s (if it is the first user,' ' do not worry because it is normal) (%s)', instance, e)
def _copy_or_update_address(source, dest, attr_name, addr_name): change = True source_addr = getattr(source, attr_name, None) dest_addr = getattr(dest, attr_name, None) if dest_addr is None: # Should not happen setattr(dest, attr_name, copy_or_create_address(source_addr, source, addr_name)) elif source_addr is None: # Should we empty the fields of the Address ? pass else: change = update_model_instance(dest_addr, **dict(source_addr.info_fields)) return change
def update(self, **kwargs): update_model_instance(self, **kwargs)
def clean(self, preply): preply.lines.update(raw_answer=None, applicable=True) # Avoids statistics artifacts update_model_instance(preply, is_complete=False)